Skip to content

Commit

Permalink
Merge pull request #10 from erictik:queue
Browse files Browse the repository at this point in the history
fix queue
  • Loading branch information
zcpua authored Apr 29, 2023
2 parents c2bfdd6 + 1f19272 commit e8d7d1a
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 146 deletions.
25 changes: 25 additions & 0 deletions example/imagine.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require("dotenv").config();
const { Midjourney } = require("../libs");
/**
*
* a simple example of how to use the imagine command
* ```
* npx tsx example/imagine.ts
* ```
*/
async function main() {
const client = new Midjourney(
process.env.SERVER_ID,
process.env.CHANNEL_ID,
process.env.SALAI_TOKEN,
true
);
const msg = await client.Imagine("A little pink elephant", (uri) => {
console.log("loading", uri);
});
console.log({ msg });
}
main().catch((err) => {
console.error(err);
process.exit(1);
});
60 changes: 46 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
},
"dependencies": {
"axios": "^1.3.6",
"p-limit": "^4.0.0",
"p-queue": "^6.6.2",
"throat": "^6.0.2",
"tslib": "^2.5.0"
}
}
10 changes: 5 additions & 5 deletions src/queue.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import PLimit from "p-limit";
import PQueue from "p-queue";

class ConcurrentQueue {
private limit: any;
private queue: (() => Promise<any>)[] = [];
private limit: any;

constructor(concurrency: number) {
this.limit = PLimit(concurrency);
this.limit = new PQueue({ concurrency });
}

public getWaiting(): number {
return this.queue.length;
}

public async addTask<T>(task: () => Promise<T>): Promise<T> {
return await this.limit(async () => {
return await this.limit.add(async () => {
const result = await task();
return result;
});
Expand All @@ -26,7 +27,6 @@ class ConcurrentQueue {
);
}
}

export function CreateQueue<T>(concurrency: number) {
return new ConcurrentQueue(5);
}
Expand Down
14 changes: 3 additions & 11 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
{
"compilerOptions": {
/* Visit https://aka.ms/tsconfig to read more about this file */

/* Projects */
// "incremental": true, /* Save .tsbuildinfo files to allow for incremental compilation of projects. */
// "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */
// "tsBuildInfoFile": "./.tsbuildinfo", /* Specify the path to .tsbuildinfo incremental compilation file. */
// "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects. */
// "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */

/* Language and Environment */
"target": "es2016" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
"lib": [
"es2021",
"esnext.asynciterable"
], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
] /* Specify a set of bundled library declaration files that describe the target runtime environment. */,
// "jsx": "preserve", /* Specify what JSX code is generated. */
// "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */
// "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
Expand All @@ -26,7 +24,6 @@
// "noLib": true, /* Disable including any library files, including the default lib.d.ts. */
// "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
// "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */

/* Modules */
"module": "commonjs" /* Specify what module code is generated. */,
// "rootDir": "./", /* Specify the root folder within your source files. */
Expand All @@ -47,12 +44,10 @@
// "resolveJsonModule": true, /* Enable importing .json files. */
// "allowArbitraryExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */

/* JavaScript Support */
// "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */
// "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */
// "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */

/* Emit */
"declaration": true /* Generate .d.ts files from TypeScript and JavaScript files in your project. */,
// "declarationMap": true, /* Create sourcemaps for d.ts files. */
Expand All @@ -63,7 +58,7 @@
"outDir": "./libs" /* Specify an output folder for all emitted files. */,
// "removeComments": true, /* Disable emitting comments. */
// "noEmit": true, /* Disable emitting files from a compilation. */
"importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
"importHelpers": true /* Allow importing helper functions from tslib once per project, instead of including them per-file. */,
// "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types. */
// "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
// "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */
Expand All @@ -77,15 +72,13 @@
// "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */
"declarationDir": "./libs" /* Specify the output directory for generated declaration files. */,
// "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */

/* Interop Constraints */
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
// "verbatimModuleSyntax": true, /* Do not transform or elide any imports or exports not marked as type-only, ensuring they are written in the output file's format based on the 'module' setting. */
"allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
"allowSyntheticDefaultImports": true /* Allow 'import x from y' when a module doesn't have a default export. */,
"esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */,
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
"forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,

/* Type Checking */
"strict": true /* Enable all strict type-checking options. */,
// "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */
Expand All @@ -106,7 +99,6 @@
// "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type. */
// "allowUnusedLabels": true, /* Disable error reporting for unused labels. */
// "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */

/* Completeness */
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
"skipLibCheck": true /* Skip type checking all .d.ts files. */
Expand Down
Loading

0 comments on commit e8d7d1a

Please sign in to comment.