From f8d4ebd13e91742b5ba648aefd90cc4dc9589d3c Mon Sep 17 00:00:00 2001 From: Sergey Sova Date: Fri, 8 Oct 2021 03:03:26 +0300 Subject: [PATCH] refactor(combine-events): to ts --- .gitignore | 2 +- src/babel-plugin-factories.json | 3 +- src/combine-events/index.d.ts | 32 ---------- src/combine-events/index.js | 62 ------------------- src/combine-events/index.ts | 102 ++++++++++++++++++++++++++++++++ tsconfig.build.json | 4 +- 6 files changed, 107 insertions(+), 98 deletions(-) delete mode 100644 src/combine-events/index.d.ts delete mode 100644 src/combine-events/index.js create mode 100644 src/combine-events/index.ts diff --git a/.gitignore b/.gitignore index 27aeab20..2f190c00 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/src/babel-plugin-factories.json b/src/babel-plugin-factories.json index 22f9bc3b..c4dd0db1 100644 --- a/src/babel-plugin-factories.json +++ b/src/babel-plugin-factories.json @@ -1,6 +1,7 @@ { - "factories": ["patronum", "patronum/delay"], + "factories": ["patronum", "patronum/combine-events", "patronum/delay"], "mapping": { + "combineEvents": "combine-events", "delay": "delay" } } diff --git a/src/combine-events/index.d.ts b/src/combine-events/index.d.ts deleted file mode 100644 index 4307131c..00000000 --- a/src/combine-events/index.d.ts +++ /dev/null @@ -1,32 +0,0 @@ -import { Unit, Store, Event, Effect } from 'effector'; - -type Tuple = [T] | T[]; -type Shape = Record | Tuple; - -type Events = { - [Key in keyof Result]: Event; -}; - -type ReturnTarget = Target extends Store - ? S extends Result - ? Store - : Store - : Target extends Event - ? P extends Result - ? Event

- : Event - : Target extends Effect - ? P extends Result - ? Effect - : Effect - : Unit; - -export function combineEvents

(config: { - events: Events

; - reset?: Unit; -}): Event

; - -export function combineEvents< - P extends Shape, - T extends Unit

>, ->(config: { events: Events

; target: T; reset?: Unit }): ReturnTarget; diff --git a/src/combine-events/index.js b/src/combine-events/index.js deleted file mode 100644 index 38a74786..00000000 --- a/src/combine-events/index.js +++ /dev/null @@ -1,62 +0,0 @@ -const { - createStore, - createEvent, - guard, - merge, - sample, - withRegion, - is, -} = require('effector'); - -const throwError = (message) => { - throw new Error(message); -}; - -function combineEvents({ events, reset, target = createEvent({ named: 'target' }) }) { - if (!is.unit(target)) throwError('target should be a unit'); - if (reset && !is.unit(reset)) throwError('reset should be a unit'); - - withRegion(target, () => { - const isArray = Array.isArray(events); - const keys = Object.keys(events); - const defaultShape = isArray ? [...keys].fill() : {}; - - const $counter = createStore(keys.length, { named: 'counter' }); - const $results = createStore(defaultShape, { named: 'results' }); - - $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 = isArray ? [...shape] : { ...shape }; - newShape[key] = payload; - return newShape; - }); - } - - guard({ - source: sample($results, merge(Object.values(events))), - filter: $counter.map((value) => value === 0), - target, - }); - }); - - return target; -} - -module.exports = { combineEvents }; diff --git a/src/combine-events/index.ts b/src/combine-events/index.ts new file mode 100644 index 00000000..01d061e0 --- /dev/null +++ b/src/combine-events/index.ts @@ -0,0 +1,102 @@ +import { + createEvent, + createStore, + Effect, + Event, + guard, + is, + merge, + sample, + Store, + Unit, + withRegion, +} from 'effector'; + +type Tuple = [T] | T[]; +type Shape = Record | Tuple; + +type Events = { + [Key in keyof Result]: Event; +}; + +type ReturnTarget = Target extends Store + ? S extends Result + ? Store + : Store + : Target extends Event + ? P extends Result + ? Event

+ : Event + : Target extends Effect + ? P extends Result + ? Effect + : Effect + : Unit; + +export function combineEvents

(config: { + events: Events

; + reset?: Unit; +}): Event

; + +export function combineEvents< + P extends Shape, + T extends Unit

>, +>(config: { events: Events

; target: T; reset?: Unit }): ReturnTarget; + +export function combineEvents

({ + events, + reset, + target = createEvent(), +}: { + events: Events

; + reset?: Unit; + target?: Unit; +}) { + 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); +} diff --git a/tsconfig.build.json b/tsconfig.build.json index 20077fd2..e3696f29 100644 --- a/tsconfig.build.json +++ b/tsconfig.build.json @@ -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": [