Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
158 additions
and
76 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# async-value (WIP) | ||
|
||
Async Value pipelines (WIP) | ||
|
||
## Usage | ||
|
||
## Reference | ||
|
||
- [tubo - npm]( https://www.npmjs.com/package/tubo ) | ||
- [Ramda Documentation]( https://ramdajs.com/docs/#pipe ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
{ | ||
"name": "snoval", | ||
"version": "1.0.0", | ||
"description": "Async Value pipelines (WIP)", | ||
"keywords": [ | ||
"async", | ||
"value", | ||
"functional", | ||
"pipeline" | ||
], | ||
"homepage": "https://github.com/snomiao/js/tree/main/packages/async-value#readme", | ||
"bugs": { | ||
"url": "https://github.com/snomiao/js/issues?q=is%3Aissue+is%3Aopen+async-value" | ||
}, | ||
"repository": "https://github.com/snomiao/js/tree/main/packages/async-value", | ||
"license": "GPLv3", | ||
"author": "snomiao <snomiao@gmail.com>", | ||
"type": "module", | ||
"exports": { | ||
".": { | ||
"require": "./lib/index.cjs", | ||
"import": "./lib/index.js" | ||
} | ||
}, | ||
"main": "lib/index.cjs", | ||
"module": "lib/index.js", | ||
"types": "./lib/index.d.ts", | ||
"files": [ | ||
"lib" | ||
], | ||
"scripts": { | ||
"build": "snobuild --lib", | ||
"prepack": "npm run build", | ||
"test": "mocha --require tsx ./src/**/*.test.ts" | ||
}, | ||
"devDependencies": { | ||
"@types/mocha": "^9.1.1", | ||
"mocha": "^10.0.0", | ||
"snobuild": "^2.1.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import type { Conder, Iterater, Looper, PromiseLiker, Reducer } from "./types"; | ||
export async function asyncMap<V, R>(a: PromiseLiker<V[]>, f: Iterater<V, R>) { | ||
const r = []; | ||
let i = 0; | ||
a = await a; | ||
for await (const v of a) r.push(await f(v, i++, a)); | ||
return r; | ||
} | ||
export async function asyncReduce<V, R>(a: PromiseLiker<V[]>, f: Reducer<V, R>, r?: R) { | ||
a = await a; | ||
if (r === undefined) r = a[0] as unknown as R; | ||
let i = 0; | ||
for await (const v of a) r = await f(r, v, i++, a); | ||
return r; | ||
} | ||
export async function asyncForEach<V, R>(a: PromiseLiker<V[]>, f: Iterater<V, R>) { | ||
let i = 0; | ||
a = await a; | ||
for await (const v of a) await f(v, i++, a); | ||
} | ||
export async function asyncWhile<V, R>(cond: Conder<V>, f: Looper<V>) { | ||
let i = 0; | ||
let v = null; | ||
while ((v = await cond())) await f(v, i++); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { strictEqual } from "assert"; | ||
import { asyncMap } from "./asyncFn"; | ||
import asyncValue from "./index"; | ||
it("number pipe valueOf", async () => { | ||
strictEqual( | ||
await asyncValue(12) | ||
.pipe((v) => v * 12) | ||
.valueOf(), | ||
144, | ||
); | ||
}); | ||
|
||
it("number pipe value", async () => { | ||
strictEqual(await asyncValue(12).pipe((v) => v * 12).value, 144); | ||
}); | ||
|
||
it("number multi pipe value", async () => { | ||
strictEqual( | ||
await asyncValue(1) | ||
.pipe((n) => n / 2) | ||
.pipe((n) => n.toString()) | ||
.pipe((s) => s.split(".")) | ||
.pipe((a) => asyncMap(a, Number)) | ||
// .pipe(Array.prototype.reverse) | ||
.value, | ||
[5, 0], | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { asyncForEach, asyncMap, asyncReduce } from "./asyncFn"; | ||
import { Iterater, PromiseLiker, Reducer } from "./types"; | ||
export * from "./asyncFn"; | ||
|
||
export function asyncArray<V>(value: PromiseLiker<V[]>) { | ||
const map = <R>(fn: Iterater<V, R>) => asyncArray(asyncMap(value, fn)); | ||
const forEach = <R>(fn: Iterater<V, R>) => asyncValue(asyncForEach(value, fn)); | ||
const reduce = <R>(fn: Reducer<V, R>) => asyncReduce(value, fn); | ||
return { ...asyncValue(value), map, forEach, reduce }; | ||
} | ||
export default function asyncValue<V>(value: PromiseLiker<V>) { | ||
const pipe = <R>(f: { (v: V): PromiseLiker<R> }) => | ||
asyncValue(Promise.resolve(value).then((e) => f.call(e))); | ||
const arrayPipe = <R extends any[]>(f: { (v: V): PromiseLiker<R> }) => | ||
asyncArray(Promise.resolve(value).then(f)); | ||
const valueOf = async () => await value; | ||
return { | ||
value, | ||
valueOf, | ||
pipe, | ||
arrayPipe, | ||
}; | ||
} | ||
// flow control | ||
/** | ||
* @example const { lock, unlock } = useLockers(n); | ||
*/ | ||
export function useLockers(n = 1) { | ||
const resolves = []; | ||
const lock = () => new Promise<void>((resolve) => (n-- ? resolve() : resolves.push(resolve))); | ||
const unlock = () => (n++, resolves.shift()?.()); | ||
return { lock, unlock }; | ||
} | ||
export function useLimiter(n = 1) { | ||
const { lock, unlock } = useLockers(n); | ||
const wait = <T>(fn: () => Promise<T>): Promise<T> => lock().then(fn).finally(unlock()); | ||
return { wait }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export type PromiseLiker<T> = PromiseLike<T> | T; | ||
export type Iterater<V, R> = { (v: V, i: number, a: V[]): PromiseLiker<R> }; | ||
export type Reducer<V, R> = { (r: R, v: V, i: number, a: V[]): PromiseLiker<R> }; | ||
export type Looper<V> = { (v: V, i: number): PromiseLiker<void> }; | ||
export type Conder<R> = { (): PromiseLiker<R> }; |