Skip to content

Commit

Permalink
feat: perfect pipe
Browse files Browse the repository at this point in the history
  • Loading branch information
tclxshunquan-wang committed Jul 8, 2024
1 parent bef75cb commit 1717a00
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 24 deletions.
5 changes: 5 additions & 0 deletions .changeset/olive-guests-press.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hyperse/pipeline": patch
---

publish @hyperse/pipeline
4 changes: 2 additions & 2 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ export default defineConfig([
...base,
{
rules: {
'prefer-rest-params': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/ban-types': 'off',
'prefer-rest-params': 'off',
'@typescript-eslint/no-empty-object-type': 'warn',
'@typescript-eslint/no-empty-object-type': 'off',
},
},
]);
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
"@hyperse/eslint-config-hyperse": "^1.0.10",
"@hyperse/exec-program": "^1.0.6",
"@types/node": "^20.14.9",
"@types/ramda": "^0.30.0",
"@types/react": "^18.3.3",
"@types/react-dom": "^18.3.0",
"commitizen": "4.3.0",
Expand Down
51 changes: 49 additions & 2 deletions src/reactive/pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1589,6 +1589,12 @@ export function pipe<
typeof fn9
>;

/**
* Creates a function that pipes the input through a series of functions.
*
* @param functions - The functions to pipe.
* @returns A function that accepts the input and passes it through the piped functions.
*/
export function pipe(
...functions: Array<Function>
): (...args: unknown[]) => unknown {
Expand Down Expand Up @@ -1652,31 +1658,72 @@ export function pipe(
return reactiveFun;
}

export const isExitPipeValue = <T>(x: unknown): x is ExitPipeReturnValue<T> =>
exitPipeReturnValues.has(x as object);
/**
* Checks if the provided value is an instance of `ExitPipeReturnValue`.
*
* @param x - The value to check.
* @returns A boolean indicating whether the value is an instance of `ExitPipeReturnValue`.
* @template T - The type of the value.
*/
export function isExitPipeValue<T>(x: unknown): x is ExitPipeReturnValue<T> {
return exitPipeReturnValues.has(x as object);
}

/**
* Exits the pipe and returns a value.
*
* @template T - The type of the value to be returned.
* @param r - The value to be returned.
* @returns An object containing the returned value.
*/
export function exitPipe<T>(r: T) {
const exitPipeReturnValue: ExitPipeReturnValue<T> = { r };
exitPipeReturnValues.add(exitPipeReturnValue);
return exitPipeReturnValue;
}

/**
* Registers a callback function to be executed before all pipes.
*
* @param fn - The callback function to be executed before all pipes.
*/
export function beforeAll(fn: PipeCallback) {
beforeAllFns.add(fn);
}

/**
* Registers a callback function to be executed after all pipes have completed.
*
* @param fn - The callback function to be executed.
*/
export function afterAll(fn: PipeCallback) {
afterAllFns.add(fn);
}

/**
* Clears all the functions registered in the `beforeAllFns` array.
*/
export function clearBeforeAll() {
beforeAllFns.clear();
}

/**
* Clears all the registered afterAll functions.
*/
export function clearAfterAll() {
afterAllFns.clear();
}

/**
* Adds a function to the pipe context and returns a new function that applies the added function to the provided arguments.
*
* @template T - The type of the first argument.
* @template L - The type of the second argument.
* @template R - The type of the return value.
* @template C - The type of the pipe context function.
* @param {Function} fn - The function to be added to the pipe context.
* @returns {(r: T, l: L) => R} - A new function that applies the added function to the provided arguments.
*/
export function pipeContext<T, L, R, C>(
fn: (x: C) => (r: T, l: L) => R
): (r: T, l: L) => R {
Expand Down
19 changes: 0 additions & 19 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -814,7 +814,6 @@ __metadata:
"@hyperse/eslint-config-hyperse": "npm:^1.0.10"
"@hyperse/exec-program": "npm:^1.0.6"
"@types/node": "npm:^20.14.9"
"@types/ramda": "npm:^0.30.0"
"@types/react": "npm:^18.3.3"
"@types/react-dom": "npm:^18.3.0"
commitizen: "npm:4.3.0"
Expand Down Expand Up @@ -1429,15 +1428,6 @@ __metadata:
languageName: node
linkType: hard

"@types/ramda@npm:^0.30.0":
version: 0.30.0
resolution: "@types/ramda@npm:0.30.0"
dependencies:
types-ramda: "npm:^0.30.0"
checksum: 10/abe9d9c872778d2598298a0c4805fa715618fe79a4d7e203c71076bc72e28c97f04ffb108274cbfc33b0407a5aa3c2c37f609705a175d4102418e292e4e55a22
languageName: node
linkType: hard

"@types/react-dom@npm:^18.3.0":
version: 18.3.0
resolution: "@types/react-dom@npm:18.3.0"
Expand Down Expand Up @@ -8696,15 +8686,6 @@ __metadata:
languageName: node
linkType: hard

"types-ramda@npm:^0.30.0":
version: 0.30.1
resolution: "types-ramda@npm:0.30.1"
dependencies:
ts-toolbelt: "npm:^9.6.0"
checksum: 10/b865c8f97df911b82b51b3cb4afa9b52b971d344dd79d203cd118e424a03761aebfea538c643f6283f700a3b4000d07d2168e353691b5caff076542bad78e420
languageName: node
linkType: hard

"typescript-eslint@npm:^8.0.0-alpha.27":
version: 8.0.0-alpha.30
resolution: "typescript-eslint@npm:8.0.0-alpha.30"
Expand Down

0 comments on commit 1717a00

Please sign in to comment.