diff --git a/src/midjourney.message.ts b/src/midjourney.message.ts index 4200ec7..bfe89d1 100644 --- a/src/midjourney.message.ts +++ b/src/midjourney.message.ts @@ -1,7 +1,9 @@ import axios from "axios"; import { MJMessage } from "./interfaces"; +import { CreateQueue } from "./queue"; export class MidjourneyMessage { + private magApiQueue = CreateQueue(1); constructor( public ChannelId: string, protected SalaiToken: string, @@ -19,7 +21,7 @@ export class MidjourneyMessage { loading?: (uri: string) => void, options?: string ) { - const data = await this.RetrieveMessages(this.Limit); + const data = await this.safeRetrieveMessages(this.Limit); for (let i = 0; i < data.length; i++) { const item = data[i]; if ( @@ -35,7 +37,6 @@ export class MidjourneyMessage { this.log("no attachment"); break; } - const imageUrl = item.attachments[0].url; if (!imageUrl.endsWith(".png")) { loading && loading(imageUrl); @@ -66,6 +67,7 @@ export class MidjourneyMessage { await this.Wait(1000 * 2); } } + async WaitOptionMessage( content: string, options: string, @@ -85,6 +87,23 @@ export class MidjourneyMessage { return new Promise((resolve) => setTimeout(resolve, ms)); } + // limit the number of concurrent interactions + protected async safeRetrieveMessages(limit = 50) { + const item: any = await new Promise((resolve, reject) => { + this.magApiQueue.push( + { + task: this.RetrieveMessages.bind(this, limit), + callback: (data) => { + resolve(data); + }, + }, + (err) => { + reject(err); + } + ); + }); + return item; + } async RetrieveMessages(limit = 50) { const headers = { authorization: this.SalaiToken }; const response = await axios.get( diff --git a/src/midjourney.ts b/src/midjourney.ts index ebc5af5..c3c33dc 100644 --- a/src/midjourney.ts +++ b/src/midjourney.ts @@ -2,10 +2,8 @@ import axios from "axios"; import { MidjourneyMessage } from "./midjourney.message"; import { CreateQueue, QueueTask } from "./queue"; import { random, sleep } from "./utls"; -import { QueueObject, tryEach } from "async"; - export class Midjourney extends MidjourneyMessage { - ApiQueue: QueueObject>; + private ApiQueue = CreateQueue(1); constructor( public ServerId: string, public ChannelId: string, @@ -13,8 +11,6 @@ export class Midjourney extends MidjourneyMessage { public debug = false ) { super(ChannelId, SalaiToken, debug); - this.log("Midjourney constructor"); - this.ApiQueue = CreateQueue(1); } async Imagine(prompt: string, loading?: (uri: string) => void) { @@ -52,7 +48,7 @@ export class Midjourney extends MidjourneyMessage { protected async interactions( payload: any, - callback: (result: number) => void + callback?: (result: number) => void ) { const headers = { authorization: this.SalaiToken }; const t0 = performance.now(); diff --git a/src/queue.ts b/src/queue.ts index dca20c7..ec1485f 100644 --- a/src/queue.ts +++ b/src/queue.ts @@ -4,21 +4,6 @@ export interface QueueTask { task: TaskFunction; callback?: (result: T) => void; } -// const queue = async.queue(async ({ task, callback }: QueueTask) => { -// const result = await task(); -// callback(result); -// }, 10); - -// const task1: TaskFunction = async () => { -// // do some async work -// return 42; -// }; -// queue.push({ -// task: task1, -// callback: (result: number) => { -// console.log(result); // output: 42 -// }, -// }); export function CreateQueue(concurrency: number) { return async.queue(async ({ task, callback }: QueueTask) => { const result = await task();