Skip to content

Commit

Permalink
add magApiQueue
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric committed Apr 29, 2023
1 parent e3e51f6 commit 58f0235
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 23 deletions.
23 changes: 21 additions & 2 deletions src/midjourney.message.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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 (
Expand All @@ -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);
Expand Down Expand Up @@ -66,6 +67,7 @@ export class MidjourneyMessage {
await this.Wait(1000 * 2);
}
}

async WaitOptionMessage(
content: string,
options: string,
Expand All @@ -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(
Expand Down
8 changes: 2 additions & 6 deletions src/midjourney.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,15 @@ 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<QueueTask<any>>;
private ApiQueue = CreateQueue(1);
constructor(
public ServerId: string,
public ChannelId: string,
protected SalaiToken: string,
public debug = false
) {
super(ChannelId, SalaiToken, debug);
this.log("Midjourney constructor");
this.ApiQueue = CreateQueue(1);
}

async Imagine(prompt: string, loading?: (uri: string) => void) {
Expand Down Expand Up @@ -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();
Expand Down
15 changes: 0 additions & 15 deletions src/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,6 @@ export interface QueueTask<T> {
task: TaskFunction<T>;
callback?: (result: T) => void;
}
// const queue = async.queue(async ({ task, callback }: QueueTask<any>) => {
// const result = await task();
// callback(result);
// }, 10);

// const task1: TaskFunction<number> = async () => {
// // do some async work
// return 42;
// };
// queue.push({
// task: task1,
// callback: (result: number) => {
// console.log(result); // output: 42
// },
// });
export function CreateQueue<T>(concurrency: number) {
return async.queue(async ({ task, callback }: QueueTask<any>) => {
const result = await task();
Expand Down

0 comments on commit 58f0235

Please sign in to comment.