Skip to content

Commit

Permalink
refactor(functional): move pipe function, rename old pipe to flow
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulHax committed Nov 30, 2023
1 parent f5ae4e9 commit f58d5f2
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 51 deletions.
11 changes: 3 additions & 8 deletions src/core/remote/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
import { Maybe } from '@/src/types';
import { Deferred, defer } from '@/src/utils';
import { debug } from '@/src/utils/loggers';
import pipe from '@/src/utils/pipe';
import { flow } from '@/src/utils/functional';
import { nanoid } from 'nanoid';
import { Socket, io } from 'socket.io-client';
import { z } from 'zod';
Expand Down Expand Up @@ -160,13 +160,8 @@ export default class RpcClient {
this.socket.on(STREAM_RESULT_EVENT, this.onStreamResultEvent);
}

protected serialize = (obj: any) => {
return pipe(...this.serializers)(obj);
};

protected deserialize = (obj: any) => {
return pipe(...this.deserializers)(obj);
};
protected serialize = flow(...this.serializers);
protected deserialize = flow(...this.deserializers);

async connect(uri: string) {
await this.disconnect();
Expand Down
3 changes: 2 additions & 1 deletion src/io/import/processors/restoreStateFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import {
fileToDataSource,
} from '@/src/io/import/dataSource';
import { MANIFEST, isStateFile } from '@/src/io/state-file';
import { ensureError, partition, pipe } from '@/src/utils';
import { ensureError, partition } from '@/src/utils';
import { pipe } from '@/src/utils/functional';
import Pipeline, { PipelineContext } from '@/src/core/pipeline';
import { Awaitable } from '@vueuse/core';
import doneWithDataSource from '@/src/io/import/processors/doneWithDataSource';
Expand Down
42 changes: 42 additions & 0 deletions src/utils/functional.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
type FlowFunction<T> = (o: T) => T;

export function flow<T>(...fns: Array<FlowFunction<T>>) {
return (input: T) => fns.reduce((result, fn) => fn(result), input);
}

// Pipe code from
// https://dev.to/ecyrbe/how-to-use-advanced-typescript-to-define-a-pipe-function-381h
// Changed second parameter to rest/spread argument.
type AnyFunc = (...arg: any) => any;

type LastFnReturnType<F extends Array<AnyFunc>, Else = never> = F extends [
...any[],
(...arg: any) => infer R
]
? R
: Else;

type PipeArgs<F extends AnyFunc[], Acc extends AnyFunc[] = []> = F extends [
(...args: infer A) => infer B
]
? [...Acc, (...args: A) => B]
: F extends [(...args: infer A) => any, ...infer Tail]
? Tail extends [(arg: infer B) => any, ...any[]]
? PipeArgs<Tail, [...Acc, (...args: A) => B]>
: Acc
: Acc;

// Example:
// const myNumber = pipe(
// "1",
// (a: string) => Number(a),
// (c: number) => c + 1,
// (d: number) => `${d}`,
// (e: string) => Number(e)
// );
export function pipe<F extends AnyFunc[]>(
arg: Parameters<F[0]>[0],
...fns: PipeArgs<F> extends F ? F : PipeArgs<F>
): LastFnReturnType<F, ReturnType<F[0]>> {
return (fns.slice(1) as AnyFunc[]).reduce((acc, fn) => fn(acc), fns[0](arg));
}
37 changes: 0 additions & 37 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,40 +326,3 @@ export function normalizeForStore<T, K extends keyof T>(objects: T[], key: K) {

return { order, byKey };
}

// Pipe code from
// https://dev.to/ecyrbe/how-to-use-advanced-typescript-to-define-a-pipe-function-381h
type AnyFunc = (...arg: any) => any;

type LastFnReturnType<F extends Array<AnyFunc>, Else = never> = F extends [
...any[],
(...arg: any) => infer R
]
? R
: Else;

type PipeArgs<F extends AnyFunc[], Acc extends AnyFunc[] = []> = F extends [
(...args: infer A) => infer B
]
? [...Acc, (...args: A) => B]
: F extends [(...args: infer A) => any, ...infer Tail]
? Tail extends [(arg: infer B) => any, ...any[]]
? PipeArgs<Tail, [...Acc, (...args: A) => B]>
: Acc
: Acc;

// Example:
// const valid = pipe(
// "1",
// (a: string) => Number(a),
// (c: number) => c + 1,
// (d: number) => `${d}`,
// (e: string) => Number(e)
// );
export function pipe<FirstFn extends AnyFunc, F extends AnyFunc[]>(
arg: Parameters<FirstFn>[0],
firstFn: FirstFn,
...fns: PipeArgs<F> extends F ? F : PipeArgs<F>
): LastFnReturnType<F, ReturnType<FirstFn>> {
return (fns as AnyFunc[]).reduce((acc, fn) => fn(acc), firstFn(arg));
}
5 changes: 0 additions & 5 deletions src/utils/pipe.ts

This file was deleted.

0 comments on commit f58d5f2

Please sign in to comment.