Skip to content

Commit

Permalink
refactor(combine-events): to ts
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeysova committed Oct 8, 2021
1 parent 6815790 commit f8d4ebd
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 98 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ typings/
.nuxt

# rollup.config.js default build output
node_modules/dist/
dist/

# Uncomment the public line if your project uses Gatsby
# https://nextjs.org/blog/next-9-1#public-directory-support
Expand Down
3 changes: 2 additions & 1 deletion src/babel-plugin-factories.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"factories": ["patronum", "patronum/delay"],
"factories": ["patronum", "patronum/combine-events", "patronum/delay"],
"mapping": {
"combineEvents": "combine-events",
"delay": "delay"
}
}
32 changes: 0 additions & 32 deletions src/combine-events/index.d.ts

This file was deleted.

62 changes: 0 additions & 62 deletions src/combine-events/index.js

This file was deleted.

102 changes: 102 additions & 0 deletions src/combine-events/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import {
createEvent,
createStore,
Effect,
Event,
guard,
is,
merge,
sample,
Store,
Unit,
withRegion,
} from 'effector';

type Tuple<T = unknown> = [T] | T[];
type Shape = Record<string, unknown> | Tuple;

type Events<Result> = {
[Key in keyof Result]: Event<Result[Key]>;
};

type ReturnTarget<Result, Target> = Target extends Store<infer S>
? S extends Result
? Store<S>
: Store<Result>
: Target extends Event<infer P>
? P extends Result
? Event<P>
: Event<Result>
: Target extends Effect<infer P, infer D, infer F>
? P extends Result
? Effect<P, D, F>
: Effect<Result, D, F>
: Unit<Result>;

export function combineEvents<P extends Shape>(config: {
events: Events<P>;
reset?: Unit<any>;
}): Event<P>;

export function combineEvents<
P extends Shape,
T extends Unit<P extends Tuple ? P : Partial<P>>,
>(config: { events: Events<P>; target: T; reset?: Unit<any> }): ReturnTarget<P, T>;

export function combineEvents<P>({
events,
reset,
target = createEvent(),
}: {
events: Events<P>;
reset?: Unit<any>;
target?: Unit<any>;
}) {
if (!is.unit(target)) throwError('target should be a unit');
if (reset && !is.unit(reset)) throwError('reset should be a unit');

withRegion(target, () => {
const keys = Object.keys(events);
const defaultShape = Array.isArray(events) ? [...keys].fill('') : {};

const $counter = createStore(keys.length);
const $results = createStore(defaultShape);

$counter.reset(sample(target));
$results.reset(target);

if (reset) {
$counter.reset(sample(reset));
$results.reset(reset);
}

for (const key of keys) {
const $isDone = createStore(false)
.on(events[key], () => true)
.reset(target);

if (reset) {
$isDone.reset(reset);
}

$counter.on($isDone, (value) => value - 1);
$results.on(events[key], (shape, payload) => {
const newShape = Array.isArray(shape) ? [...shape] : { ...shape };
newShape[key] = payload;
return newShape;
});
}

guard({
source: sample($results, merge(Object.values(events))),
filter: $counter.map((value) => value === 0),
target,
});
});

return target;
}

function throwError(message: string) {
throw new Error(message);
}
4 changes: 2 additions & 2 deletions tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
"extends": "./tsconfig.json",
"compilerOptions": {
"emitDeclarationOnly": true,
"declarationDir": "./node_modules/dist",
"declarationDir": "./dist",
"declaration": true,
"rootDir": "./src",
"outDir": "./node_modules/dist",
"outDir": "./dist",
"allowJs": false
},
"exclude": [
Expand Down

0 comments on commit f8d4ebd

Please sign in to comment.