Comparing version 0.0.13 to 0.1.0
export interface TaskCallback<T> { | ||
(value: T): Promise<T>; | ||
(value: T): Promise<void>; | ||
} | ||
@@ -7,2 +7,2 @@ export interface Task<T, R = unknown> { | ||
} | ||
export declare const generatorify: <T, R = unknown>(task: Task<T, R>) => AsyncIterable<T>; | ||
export declare const generatorify: <T, R>(task: Task<T, R>) => AsyncIterable<T>; |
@@ -1,26 +0,13 @@ | ||
import { defer } from "./defer.js"; | ||
import { createPoll } from "./deferredPoll.js"; | ||
export const generatorify = (task)=>{ | ||
const dPoll = [ | ||
defer() | ||
]; | ||
Promise.resolve(task((value)=>{ | ||
const next = defer(); | ||
const prev = dPoll.push(next) - 2; | ||
const prevDeferred = dPoll[prev]; | ||
prevDeferred.resolve({ | ||
const poll = createPoll(); | ||
Promise.resolve(task(async (value)=>{ | ||
await poll.push({ | ||
value, | ||
done: false | ||
}); | ||
return prevDeferred.promise.then((v)=>v.value); | ||
})).then(async (value)=>{ | ||
dPoll[dPoll.length - 1].resolve({ | ||
}).promise; | ||
})).then(async (value)=>poll.done({ | ||
value, | ||
done: true | ||
}); | ||
await Promise.all(dPoll.map((d)=>d.promise)); | ||
return { | ||
value, | ||
done: true | ||
}; | ||
}); | ||
})); | ||
return { | ||
@@ -30,5 +17,3 @@ [Symbol.asyncIterator] () { | ||
next () { | ||
const current = dPoll[0]; | ||
current.promise.then(()=>dPoll.shift()); | ||
return current.promise; | ||
return poll.pull().promise; | ||
} | ||
@@ -35,0 +20,0 @@ }; |
export * from './defer'; | ||
export * from './generatorify'; | ||
export * from './deferredPoll'; | ||
export * from './combine'; |
export * from "./defer.js"; | ||
export * from "./generatorify.js"; | ||
export * from "./deferredPoll.js"; | ||
export * from "./combine.js"; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "asygen", | ||
"description": "0-Deps, simple and fast async generator library for browser and NodeJS", | ||
"version": "0.0.13", | ||
"version": "0.1.0", | ||
"type": "module", | ||
"types": "build/index.d.ts", | ||
"main": "build/index.cjs", | ||
"module": "build/index.js", | ||
"types": "build/index.d.ts", | ||
"exports": { | ||
"require": "./build/index.cjs", | ||
"import": "./build/index.js" | ||
}, | ||
"files": [ | ||
"build", | ||
"src/index.ts", | ||
"src/combine.ts", | ||
"src/defer.ts", | ||
"src/generatorify.ts" | ||
"src/deferredPoll.ts", | ||
"src/generatorify.ts", | ||
"src/index.ts" | ||
], | ||
"sideEffects": false, | ||
"scripts": { | ||
@@ -53,3 +58,3 @@ "build": "rm -rf build && inop src build -p -i __tests__ && tsc --declaration --emitDeclarationOnly", | ||
"devDependencies": { | ||
"@swc/core": "^1.3.46", | ||
"@swc/core": "^1.3.50", | ||
"@swc/jest": "^0.2.24", | ||
@@ -59,6 +64,6 @@ "@types/jest": "^29.5.0", | ||
"fast-glob": "^3.2.12", | ||
"inop": "^0.1.1", | ||
"inop": "^0.2.2", | ||
"jest": "^29.5.0", | ||
"typescript": "^5.0.4" | ||
} | ||
} | ||
} |
@@ -47,2 +47,23 @@ # Asygen | ||
#### Combine generators | ||
```typescript | ||
import { combine } from 'asygen'; | ||
const sleep = (timeout: number) => new Promise(resolve => setTimeout(resolve, timeout)); | ||
async function* generate(timeout: number, count: number) { | ||
for (let index = 0; index < count; index++) { | ||
yield index; | ||
await sleep(timeout); | ||
} | ||
} | ||
for await (const data of combine(generate(100, 5), generate(500, 2))) { | ||
// handle data | ||
} | ||
// First: 0 1 2 3 4 - | ||
// Second: 0 . . . . 1 | ||
// Combined: 0 0 1 2 3 4 1 | ||
``` | ||
## License | ||
@@ -49,0 +70,0 @@ |
@@ -1,5 +0,5 @@ | ||
import { defer, Deferred } from './defer'; | ||
import { createPoll } from './deferredPoll'; | ||
export interface TaskCallback<T> { | ||
(value: T): Promise<T>; | ||
(value: T): Promise<void>; | ||
} | ||
@@ -11,16 +11,8 @@ | ||
export const generatorify = <T, R = unknown>(task: Task<T, R>): AsyncIterable<T> => { | ||
const dPoll: Deferred<IteratorResult<T, R>>[] = [defer<IteratorYieldResult<T>>()]; | ||
export const generatorify = <T, R>(task: Task<T, R>): AsyncIterable<T> => { | ||
const poll = createPoll<IteratorResult<T, R>>(); | ||
Promise.resolve(task(value => { | ||
const next = defer<IteratorYieldResult<T>>(); | ||
const prev = dPoll.push(next) - 2; | ||
const prevDeferred = dPoll[prev] as Deferred<IteratorYieldResult<T>>; | ||
prevDeferred.resolve({ value, done: false }); | ||
return prevDeferred.promise.then(v => v.value); | ||
})).then(async (value) => { | ||
dPoll[dPoll.length - 1].resolve({ value, done: true }); | ||
await Promise.all(dPoll.map(d => d.promise)); | ||
return { value, done: true }; | ||
}); | ||
Promise.resolve(task(async value => { | ||
await poll.push({ value, done: false }).promise; | ||
})).then(async (value) => poll.done({ value, done: true })); | ||
@@ -31,5 +23,3 @@ return { | ||
next() { | ||
const current = dPoll[0]; | ||
current.promise.then(() => dPoll.shift()); | ||
return current.promise; | ||
return poll.pull().promise; | ||
}, | ||
@@ -36,0 +26,0 @@ }; |
export * from './defer'; | ||
export * from './generatorify'; | ||
export * from './deferredPoll'; | ||
export * from './combine'; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
32584
33
494
85