Comparing version 0.101.0 to 0.102.0
import { Event, Tuple } from 'effector'; | ||
export function combineEvents<Shape extends Tuple>( | ||
shape: Shape, | ||
): Event< | ||
{ [K in keyof Shape]: Shape[K] extends Event<infer U> ? U : Shape[K] } | ||
export function combineEvents<Shape extends Tuple>(_: { | ||
events: Shape; | ||
}): Event< | ||
{ [Key in keyof Shape]: Shape[Key] extends Event<infer U> ? U : Shape[Key] } | ||
>; | ||
export function combineEvents<Shape>( | ||
shape: Shape, | ||
): Event< | ||
{ [K in keyof Shape]: Shape[K] extends Event<infer U> ? U : Shape[K] } | ||
export function combineEvents<Shape>(_: { | ||
events: Shape; | ||
}): Event< | ||
{ [Key in keyof Shape]: Shape[Key] extends Event<infer U> ? U : Shape[Key] } | ||
>; |
@@ -9,4 +9,13 @@ const { | ||
} = require('effector'); | ||
const { readConfig } = require('../library'); | ||
function combineEvents(events) { | ||
function combineEvents(argument) { | ||
const { events, loc, name = 'unknown' } = readConfig(argument, [ | ||
'events', | ||
'sid', | ||
'loc', | ||
'name', | ||
]); | ||
const target = createEvent(); | ||
@@ -17,12 +26,20 @@ | ||
const keys = Object.keys(events); | ||
const counter = createStore(keys.length).reset(sample(target)); | ||
const defaultShape = isArray ? [...keys].fill() : {}; | ||
const results = createStore(defaultShape).reset(target); | ||
const $counter = createStore(keys.length, { name: `${name}Counter`, loc }); | ||
const $results = createStore(defaultShape, { | ||
name: `${name}Results`, | ||
loc, | ||
}); | ||
$counter.reset(sample(target)); | ||
$results.reset(sample(target)); | ||
for (const key of keys) { | ||
const done = createStore(false) | ||
const $isDone = createStore(false) | ||
.on(events[key], () => true) | ||
.reset(target); | ||
counter.on(done, (value) => value - 1); | ||
results.on(events[key], (shape, payload) => { | ||
$counter.on($isDone, (value) => value - 1); | ||
$results.on(events[key], (shape, payload) => { | ||
const newShape = isArray ? [...shape] : { ...shape }; | ||
@@ -35,4 +52,4 @@ newShape[key] = payload; | ||
guard({ | ||
source: sample(results, merge(Object.values(events))), | ||
filter: counter.map((value) => value === 0), | ||
source: sample($results, merge(Object.values(events))), | ||
filter: $counter.map((value) => value === 0), | ||
target, | ||
@@ -39,0 +56,0 @@ }); |
@@ -7,3 +7,3 @@ # Patronum/CombineEvents | ||
## `combineEvents(object)` | ||
## `combineEvents({ events: Record<key, Event<T>> })` | ||
@@ -14,4 +14,6 @@ ### Formulae | ||
const target = combineEvents({ | ||
key1: event1, | ||
key2: event2, | ||
events: { | ||
key1: event1, | ||
key2: event2, | ||
}, | ||
}); | ||
@@ -30,5 +32,7 @@ ``` | ||
const target = combineEvents({ | ||
a: first, | ||
second, | ||
another: third, | ||
events: { | ||
a: first, | ||
second, | ||
another: third, | ||
}, | ||
}); | ||
@@ -47,3 +51,3 @@ | ||
## `combineEvents(array)` | ||
## `combineEvents({ events: Array<Event<T>> })` | ||
@@ -53,3 +57,3 @@ ### Formulae | ||
```ts | ||
const target = combineEvents([event1, event2]); | ||
const target = combineEvents({ events: [event1, event2] }); | ||
``` | ||
@@ -66,3 +70,3 @@ | ||
const target = combineEvents([first, second, third]); | ||
const target = combineEvents({ events: [first, second, third] }); | ||
@@ -69,0 +73,0 @@ target.watch((list) => { |
@@ -1,3 +0,3 @@ | ||
// @ts-nocheck | ||
const { is, guard, createEvent } = require('effector'); | ||
const { readConfig } = require('../library'); | ||
@@ -9,8 +9,10 @@ /** | ||
*/ | ||
function condition({ | ||
source = createEvent(), | ||
if: test, | ||
then: thenBranch = createEvent(), | ||
else: elseBranch = createEvent(), | ||
}) { | ||
function condition(argument) { | ||
const { | ||
source = createEvent(), | ||
if: test, | ||
then: thenBranch = createEvent(), | ||
else: elseBranch = createEvent(), | ||
} = readConfig(argument, ['source', 'if', 'then', 'else']); | ||
const checker = | ||
@@ -17,0 +19,0 @@ is.unit(test) || isFunction(test) ? test : (value) => value === test; |
@@ -1,7 +0,10 @@ | ||
import { Unit, Event } from 'effector'; | ||
import { Unit, Event, Store } from 'effector'; | ||
export function delay<T>(unit: Unit<T>, time: number): Event<T>; | ||
export function delay<T>( | ||
unit: Unit<T>, | ||
options: { time: (payload: T) => number }, | ||
): Event<T>; | ||
export function delay<T>(_: { | ||
source: Unit<T>; | ||
timeout: (payload: T) => number; | ||
}): Event<T>; | ||
export function delay<T>(_: { | ||
source: Unit<T>; | ||
timeout: Store<number> | number; | ||
}): Event<T>; |
@@ -1,28 +0,62 @@ | ||
const { createEffect, forward } = require('effector'); | ||
const { createEffect, createEvent, forward, is, sample } = require('effector'); | ||
const { readConfig } = require('../library'); | ||
function delay(unit, options) { | ||
const timer = | ||
typeof options === 'object' | ||
? toFunction(options.time) | ||
: toFunction(options); | ||
function delay(argument) { | ||
const { source, timeout, loc, name = 'unknown', sid } = readConfig(argument, [ | ||
'source', | ||
'timeout', | ||
const timeout = createEffect({ | ||
handler: (data) => | ||
'loc', | ||
'sid', | ||
'name', | ||
]); | ||
if (!is.unit(source)) | ||
throw new TypeError('source must be a unit from effector'); | ||
const ms = validateTimeout(timeout); | ||
const tick = createEvent({ name: `${name}Delayed`, sid, loc }); | ||
const timerFx = createEffect({ | ||
config: { name: `${name}DelayTimer`, loc }, | ||
handler: ({ payload, milliseconds }) => | ||
new Promise((resolve) => { | ||
setTimeout(resolve, timer(data), data); | ||
setTimeout(resolve, milliseconds, payload); | ||
}), | ||
}); | ||
sample({ | ||
source: { milliseconds: ms }, | ||
clock: source, | ||
fn: ({ milliseconds }, payload) => ({ | ||
payload, | ||
milliseconds: | ||
typeof milliseconds === 'function' | ||
? milliseconds(payload) | ||
: milliseconds, | ||
}), | ||
target: timerFx, | ||
}); | ||
forward({ | ||
from: unit, | ||
to: timeout, | ||
from: timerFx.done.map((payload) => payload.result), | ||
to: tick, | ||
}); | ||
return timeout.done.map(({ result }) => result); | ||
return tick; | ||
} | ||
function toFunction(time) { | ||
return typeof time === 'function' ? time : () => time; | ||
module.exports = { delay }; | ||
function validateTimeout(timeout) { | ||
if ( | ||
is.store(timeout) || | ||
typeof timeout === 'function' || | ||
typeof timeout === 'number' | ||
) { | ||
return timeout; | ||
} | ||
throw new TypeError( | ||
`'timeout' argument must be a function, Store, or a number. Passed "${typeof timeout}"`, | ||
); | ||
} | ||
module.exports = { delay }; |
@@ -7,3 +7,3 @@ # Patronum/Delay | ||
## `delay(trigger, time)` | ||
## `delay({ source, timeout: number })` | ||
@@ -13,11 +13,11 @@ ### Formulae | ||
```ts | ||
event = delay(trigger, time); | ||
event = delay({ source, timeout: number }); | ||
``` | ||
- When `trigger` is triggered, wait for `time`, then trigger `event` with payload of the `trigger` | ||
- When `source` is triggered, wait for `timeout`, then trigger `event` with payload of the `source` | ||
### Arguments | ||
1. `trigger` _(`Event<T>` | `Store<T>` | `Effect<T>`)_ — Source unit, data from this unit used to trigger `event` with. | ||
2. `time` _(`number`)_ — time to wait before trigger `event` | ||
1. `source` _(`Event<T>` | `Store<T>` | `Effect<T>`)_ — Source unit, data from this unit used to trigger `event` with. | ||
1. `timeout` _(`number`)_ — time to wait before trigger `event` | ||
@@ -35,3 +35,3 @@ ### Returns | ||
const trigger = createEvent<string>(); // createStore or createEffect | ||
const delayed = delay(trigger, 300); | ||
const delayed = delay({ source: trigger, timeout: 300 }); | ||
@@ -45,3 +45,3 @@ delayed.watch((payload) => console.info('triggered', payload)); | ||
## `delay(trigger, { time: fn })` | ||
## `delay({ source, timeout: Function })` | ||
@@ -51,12 +51,11 @@ ### Formulae | ||
```ts | ||
event = delay(trigger, { time: fn }); | ||
event = delay({ source, timeout: Function }); | ||
``` | ||
- When `trigger` is triggered, call `time` with payload to get the timeout for delay, then trigger `event` with payload of the `trigger` | ||
- When `source` is triggered, call `timeout` with payload to get the timeout for delay, then trigger `event` with payload of the `source` | ||
### Arguments | ||
1. `trigger` _(`Event<T>` | `Store<T>` | `Effect<T>`)_ — Source unit, data from this unit used to trigger `event` with. | ||
2. `options` _(`{ time: (payload: T) => number }`)_ — Setup delay options | ||
- `time` _(`(payload: T) => number`)_ — Calculate delay for each `trigger` call. Receives the payload of `trigger` as argument. Should return `number` — delay in milliseconds. | ||
1. `source` _(`Event<T>` | `Store<T>` | `Effect<T>`)_ — Source unit, data from this unit used to trigger `event` with. | ||
1. `timeout` _(`(payload: T) => number`)_ — Calculate delay for each `source` call. Receives the payload of `source` as argument. Should return `number` — delay in milliseconds. | ||
@@ -72,3 +71,6 @@ ### Example | ||
const logDelayed = delay($data, { time: (string) => string.length * 100 }); | ||
const logDelayed = delay({ | ||
source: $data, | ||
timeout: (string) => string.length * 100, | ||
}); | ||
@@ -85,1 +87,38 @@ logDelayed.watch((data) => console.log('log', data)); | ||
``` | ||
## `delay({ source, timeout: Store })` | ||
### Formulae | ||
```ts | ||
event = delay({ source, timeout: $store }); | ||
``` | ||
- When `source` is triggered, read timeout from `timeout` store, then trigger `event` with payload of the `source` | ||
### Arguments | ||
1. `source` _(`Event<T>` | `Store<T>` | `Effect<T>`)_ — Source unit, data from this unit used to trigger `event` with. | ||
1. `timeout` _(`Store<number>`)_ — Store with number — delay in milliseconds. | ||
### Example | ||
```ts | ||
import { createEvent, createStore } from 'effector'; | ||
import { delay } from 'patronum/delay'; | ||
const update = createEvent<string>(); | ||
const $data = createStore(''); | ||
const $timeout = createStore(500); | ||
const logDelayed = delay({ | ||
source: $data, | ||
timeout: $timeout, | ||
}); | ||
logDelayed.watch((data) => console.log('log', data)); | ||
update('Hello'); | ||
// after 500ms | ||
// => log Hello | ||
``` |
import { Store } from 'effector'; | ||
export function every<T>( | ||
predicate: (value: T) => boolean, | ||
stores: Array<Store<T>>, | ||
): Store<boolean>; | ||
export function every<T>(_: { | ||
predicate: (value: T) => boolean; | ||
stores: Array<Store<T>>; | ||
}): Store<boolean>; | ||
export function every<T extends string>( | ||
value: T, | ||
stores: Array<Store<T>>, | ||
): Store<boolean>; | ||
export function every<T extends string>(_: { | ||
predicate: T; | ||
stores: Array<Store<T>>; | ||
}): Store<boolean>; | ||
export function every<T>(value: T, stores: Array<Store<T>>): Store<boolean>; | ||
export function every<T>(_: { | ||
predicate: T; | ||
stores: Array<Store<T>>; | ||
}): Store<boolean>; |
const { combine } = require('effector'); | ||
const { readConfig } = require('../library'); | ||
function every(predicate, list) { | ||
function every(argument) { | ||
const { predicate, stores } = readConfig(argument, ['predicate', 'stores']); | ||
const checker = isFunction(predicate) | ||
@@ -8,3 +11,3 @@ ? predicate | ||
return combine(list, (values) => values.every(checker)); | ||
return combine(stores, (values) => values.every(checker)); | ||
} | ||
@@ -11,0 +14,0 @@ |
@@ -7,6 +7,6 @@ # Patronum/Every | ||
## `every(predicate, stores)` | ||
## `every({ predicate: Function, stores })` | ||
```ts | ||
$result = every(predicate, stores); | ||
$result = every({ predicate: fn, stores }); | ||
``` | ||
@@ -31,3 +31,6 @@ | ||
const $fitsSquare = every((size) => size < 800, [$width, $height]); | ||
const $fitsSquare = every({ | ||
predicate: (size) => size < 800, | ||
stores: [$width, $height], | ||
}); | ||
@@ -37,6 +40,6 @@ console.assert(true === $fitsSquare.getState()); | ||
## `every(value, stores)` | ||
## `every({ predicate: value, stores })` | ||
```ts | ||
$result = every(value, stores); | ||
$result = every({ predicate: value, stores }); | ||
``` | ||
@@ -48,3 +51,3 @@ | ||
1. `value` _(`T`)_ — Data to compare stores values with | ||
1. `predicate` _(`T`)_ — Data to compare stores values with | ||
1. `stores` _(`Array<Store<T>>`)_ — List of stores to compare with `value` | ||
@@ -63,5 +66,8 @@ 1. type of `value` and `stores` should should be the same | ||
const $isFormCorrect = every(true, [$isPasswordCorrect, $isEmailCorrect]); | ||
const $isFormCorrect = every({ | ||
predicate: true, | ||
stores: [$isPasswordCorrect, $isEmailCorrect], | ||
}); | ||
console.assert(true === $isFormCorrect.getState()); | ||
``` |
{ | ||
"name": "patronum", | ||
"version": "0.101.0", | ||
"version": "0.102.0", | ||
"description": "☄️ Effector utility library delivering modularity and convenience", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -70,4 +70,6 @@ # ☄️ effecto patronum ✨ | ||
const trigger = createEvent<string>(); // createStore or createEffect | ||
const delayed = delay(trigger, 300); | ||
// `timeout` also supports (payload) => number and Store<number> | ||
const delayed = delay({ source: trigger, timeout: 300 }); | ||
delayed.watch((payload) => console.info('triggered', payload)); | ||
@@ -156,3 +158,3 @@ | ||
const effect = createEffect().use(() => Promise.resolve(null)); | ||
const $status = status(effect); | ||
const $status = status({ effect }); | ||
@@ -178,5 +180,8 @@ $status.watch((value) => console.log(`status: ${value}`)); | ||
spread(trigger, { | ||
first: $first, | ||
second: $second, | ||
spread({ | ||
source: trigger, | ||
targets: { | ||
first: $first, | ||
second: $second, | ||
}, | ||
}); | ||
@@ -203,5 +208,7 @@ | ||
const event = combineEvents({ | ||
event1, | ||
event2, | ||
event3, | ||
events: { | ||
event1, | ||
event2, | ||
event3, | ||
}, | ||
}); | ||
@@ -222,3 +229,6 @@ | ||
const $isFormCorrect = every(true, [$isPasswordCorrect, $isEmailCorrect]); | ||
const $isFormCorrect = every({ | ||
predicate: true, | ||
stores: [$isPasswordCorrect, $isEmailCorrect], | ||
}); | ||
// true | ||
@@ -246,6 +256,9 @@ ``` | ||
const parts = reshape($original, { | ||
length: (string) => string.length, | ||
first: (string) => string.split(' ')[0] || '', | ||
second: (string) => string.split(' ')[1] || '', | ||
const parts = reshape({ | ||
source: $original, | ||
shape: { | ||
length: (string) => string.length, | ||
first: (string) => string.split(' ')[0] || '', | ||
second: (string) => string.split(' ')[1] || '', | ||
}, | ||
}); | ||
@@ -266,5 +279,8 @@ | ||
const received = splitMap(nameReceived, { | ||
firstName: (string) => string.split(' ')[0], // string | undefined | ||
lastName: (string) => string.split(' ')[1], // string | undefined | ||
const received = splitMap({ | ||
source: nameReceived, | ||
cases: { | ||
firstName: (string) => string.split(' ')[0], // string | undefined | ||
lastName: (string) => string.split(' ')[1], // string | undefined | ||
}, | ||
}); | ||
@@ -271,0 +287,0 @@ |
import { Store } from 'effector'; | ||
export function reshape<T, S extends {}>( | ||
store: Store<T>, | ||
shape: { [K in keyof S]: (v: T) => S[K] }, | ||
): { [K in keyof S]: Store<S[K]> }; | ||
export function reshape<Type, Shape extends {}>(_: { | ||
source: Store<Type>; | ||
shape: { [ShapeKey in keyof Shape]: (value: Type) => Shape[ShapeKey] }; | ||
}): { [ResultKey in keyof Shape]: Store<Shape[ResultKey]> }; |
@@ -1,2 +0,5 @@ | ||
function reshape(store, shape) { | ||
const { readConfig } = require('../library'); | ||
function reshape(argument) { | ||
const { source, shape } = readConfig(argument, ['source', 'shape']); | ||
const result = {}; | ||
@@ -6,3 +9,3 @@ | ||
if (key in shape) { | ||
result[key] = store.map(shape[key]); | ||
result[key] = source.map(shape[key]); | ||
} | ||
@@ -9,0 +12,0 @@ } |
@@ -10,6 +10,6 @@ # Patronum/Reshape | ||
```ts | ||
shape = reshape(store, cases); | ||
result = reshape(source, shape); | ||
``` | ||
- Call each function in `cases` object with data from `store`, and create store with the same name as key in `cases` | ||
- Call each function in `shape` object with data from `source`, and create store with the same name as key in `shape` | ||
@@ -26,14 +26,17 @@ > No arguments yet | ||
const shape = reshape($original, { | ||
length: (string) => string.length, | ||
lowercase: (string) => string.toLowerCase(), | ||
uppercase: (string) => string.toUpperCase(), | ||
const result = reshape({ | ||
source: $original, | ||
shape: { | ||
length: (string) => string.length, | ||
lowercase: (string) => string.toLowerCase(), | ||
uppercase: (string) => string.toUpperCase(), | ||
}, | ||
}); | ||
// shape.length: Store<number>; | ||
// shape.lowercase: Store<string>; | ||
// shape.uppercase: Store<string>; | ||
// result.length: Store<number>; | ||
// result.lowercase: Store<string>; | ||
// result.uppercase: Store<string>; | ||
shape.length.watch((length) => console.log('String length:', length)); | ||
shape.lowercase.watch((lowercase) => console.log('lowercase:', lowercase)); | ||
result.length.watch((length) => console.log('String length:', length)); | ||
result.lowercase.watch((lowercase) => console.log('lowercase:', lowercase)); | ||
``` |
import { Store } from 'effector'; | ||
export function some<T>( | ||
predicate: (value: T) => boolean, | ||
stores: Array<Store<T>>, | ||
): Store<boolean>; | ||
export function some<T>(_: { | ||
predicate: (value: T) => boolean; | ||
stores: Array<Store<T>>; | ||
}): Store<boolean>; | ||
export function some<T extends string>( | ||
value: T, | ||
stores: Array<Store<T>>, | ||
): Store<boolean>; | ||
export function some<T extends string>(_: { | ||
predicate: T; | ||
stores: Array<Store<T>>; | ||
}): Store<boolean>; | ||
export function some<T>(value: T, stores: Array<Store<T>>): Store<boolean>; | ||
export function some<T>(_: { | ||
predicate: T; | ||
stores: Array<Store<T>>; | ||
}): Store<boolean>; |
const { combine } = require('effector'); | ||
const { readConfig } = require('../library'); | ||
function some(predicate, list) { | ||
function some(argument) { | ||
const { predicate, stores } = readConfig(argument, ['predicate', 'stores']); | ||
const checker = isFunction(predicate) | ||
@@ -8,3 +11,3 @@ ? predicate | ||
return combine(list, (values) => values.some(checker)); | ||
return combine(stores, (values) => values.some(checker)); | ||
} | ||
@@ -11,0 +14,0 @@ |
@@ -7,8 +7,9 @@ # Patronum/Some | ||
## `some(predicate, stores)` | ||
## `some({ predicate: Function, stores })` | ||
```ts | ||
$result = some(predicate, stores); | ||
$result = some({ predicate: (value) => true, stores }); | ||
``` | ||
- read it as: has some predicate at at least one store | ||
- `$result` will be `true` if each at least `predicate` on each store value from `values` returns `true`, otherwise it will be `false` | ||
@@ -31,3 +32,6 @@ | ||
const $tooBig = some((size) => size > 800, [$width, $height]); | ||
const $tooBig = some({ | ||
predicate: (size) => size > 800, | ||
stores: [$width, $height], | ||
}); | ||
@@ -37,15 +41,15 @@ console.assert(true === $tooBig.getState()); | ||
## `some(value, stores)` | ||
## `some({ predicate: value, stores })` | ||
```ts | ||
$result = some(value, stores); | ||
$result = some({ predicate: value, stores }); | ||
``` | ||
- `$result` will be `true` if at least one value in `stores` equals `values`, otherwise it will be `false` | ||
- `$result` will be `true` if at least one value in `stores` equals `value`, otherwise it will be `false` | ||
### Arguments | ||
1. `value` _(`T`)_ — Data to compare stores values with | ||
1. `predicate` _(`T`)_ — Data to compare stores values with | ||
1. `stores` _(`Array<Store<T>>`)_ — List of stores to compare with `value` | ||
1. type of `value` and `stores` should should be the same | ||
1. type of `predicate` and `stores` should should be the same | ||
@@ -62,5 +66,8 @@ ### Return | ||
const $isFormFailed = some(false, [$isPasswordCorrect, $isEmailCorrect]); | ||
const $isFormFailed = some({ | ||
predicate: false, | ||
stores: [$isPasswordCorrect, $isEmailCorrect], | ||
}); | ||
console.assert(false === $isFormFailed.getState()); | ||
``` |
@@ -1,2 +0,2 @@ | ||
import { Event } from 'effector'; | ||
import { Unit, Event } from 'effector'; | ||
@@ -6,6 +6,6 @@ export function splitMap< | ||
Cases extends Record<string, (payload: S) => any | undefined> | ||
>( | ||
source: Event<S>, | ||
cases: Cases, | ||
): { | ||
>(_: { | ||
source: Unit<S>; | ||
cases: Cases; | ||
}): { | ||
[K in keyof Cases]: Cases[K] extends (p: S) => infer R | ||
@@ -12,0 +12,0 @@ ? Event<Exclude<R, undefined>> |
const { is } = require('effector'); | ||
const { readConfig } = require('../library'); | ||
function splitMap(source, cases) { | ||
function splitMap(argument) { | ||
const { source, cases } = readConfig(argument, ['source', 'cases']); | ||
const result = {}; | ||
@@ -5,0 +7,0 @@ |
@@ -19,5 +19,8 @@ # Patronum/SplitMap | ||
const extractors = splitMap(event, { | ||
getType: (input) => input.type, | ||
getDemo: (input) => input.demo, | ||
const extractors = splitMap({ | ||
source: event, | ||
cases: { | ||
getType: (input) => input.type, | ||
getDemo: (input) => input.demo, | ||
}, | ||
}); | ||
@@ -24,0 +27,0 @@ |
import {} from 'effector'; | ||
// TODO: should be typed | ||
export function spread(source: any, cases: any): void; | ||
export function spread(cases: any): void; | ||
export function spread(_: { source: any; targets: any }): void; | ||
export function spread(_: { targets: any }): void; |
/* eslint-disable no-param-reassign */ | ||
const { createEvent, sample, guard } = require('effector'); | ||
const { readConfig } = require('../library'); | ||
/** | ||
* @examle | ||
* spread(dataObject, { first: targetA, second: targetB }) | ||
* @example | ||
* spread({ source: dataObject, targets: { first: targetA, second: targetB } }) | ||
* forward({ | ||
* to: spread({ first: targetA, second: targetB }) | ||
* to: spread({targets: { first: targetA, second: targetB } }) | ||
* }) | ||
*/ | ||
function spread(source, targetCases) { | ||
if (targetCases === undefined) { | ||
targetCases = source; | ||
source = createEvent(); | ||
} | ||
for (const key in targetCases) { | ||
if (key in targetCases) { | ||
const correctKey = guard(source, { | ||
filter: (data) => | ||
typeof data === 'object' && data !== null && key in data, | ||
function spread(argument) { | ||
const { | ||
loc, | ||
name = 'unknown', | ||
source = createEvent({ loc, name: `${name}Source` }), | ||
targets, | ||
} = readConfig(argument, ['loc', 'name', 'source', 'targets']); | ||
for (const targetKey in targets) { | ||
if (targetKey in targets) { | ||
const hasTargetKey = guard({ | ||
source, | ||
filter: (object) => | ||
typeof object === 'object' && object !== null && targetKey in object, | ||
}); | ||
sample({ | ||
source: correctKey, | ||
fn: (data) => data[key], | ||
target: targetCases[key], | ||
source: hasTargetKey, | ||
fn: (object) => object[targetKey], | ||
target: targets[targetKey], | ||
}); | ||
@@ -28,0 +33,0 @@ } |
@@ -7,3 +7,3 @@ # Patronum/Spread | ||
## `spread(source, targets)` | ||
## `spread({ source, targets })` | ||
@@ -13,3 +13,3 @@ ### Formulae | ||
```ts | ||
spread(source, { field: target, ... }) | ||
spread({ source, targets: { field: target, ... } }) | ||
``` | ||
@@ -40,5 +40,8 @@ | ||
spread(formReceived, { | ||
first: $first, | ||
last: $last, | ||
spread({ | ||
source: formReceived, | ||
targets: { | ||
first: $first, | ||
last: $last, | ||
}, | ||
}); | ||
@@ -69,5 +72,8 @@ | ||
spread($form, { | ||
first: firstNameChanged, | ||
last: lastNameChanged, | ||
spread({ | ||
source: $form, | ||
targets: { | ||
first: firstNameChanged, | ||
last: lastNameChanged, | ||
}, | ||
}); | ||
@@ -86,6 +92,6 @@ | ||
## `spread(targets)` | ||
## `spread({ targets })` | ||
```ts | ||
source = spread({ field: target, ... }) | ||
source = spread({ targets: { field: target, ... } }) | ||
``` | ||
@@ -123,4 +129,6 @@ | ||
target: spread({ | ||
first: $first, | ||
last: $last, | ||
targets: { | ||
first: $first, | ||
last: $last, | ||
}, | ||
}), | ||
@@ -149,8 +157,13 @@ }); | ||
spread(trigger, { | ||
first: $targetA, | ||
second: spread({ | ||
foo: $targetB, | ||
bar: $targetC, | ||
}), | ||
spread({ | ||
source: trigger, | ||
targets: { | ||
first: $targetA, | ||
second: spread({ | ||
targets: { | ||
foo: $targetB, | ||
bar: $targetC, | ||
}, | ||
}), | ||
}, | ||
}); | ||
@@ -157,0 +170,0 @@ |
@@ -5,5 +5,5 @@ import { Store, Effect } from 'effector'; | ||
export function status<Params, Result>( | ||
effect: Effect<P, R>, | ||
initialValue?: Status, | ||
): Store<Status>; | ||
export function status<Params, Result>(_: { | ||
effect: Effect<Params, Result>; | ||
defaultValue?: Status; | ||
}): Store<Status>; |
const { createStore } = require('effector'); | ||
const { readConfig } = require('../library'); | ||
function status(effect, initialValue = 'initial') { | ||
const $status = createStore(initialValue); | ||
function status(argument) { | ||
const { effect, defaultValue = 'initial' } = readConfig(argument, [ | ||
'effect', | ||
'defaultValue', | ||
]); | ||
const $status = createStore(defaultValue); | ||
@@ -6,0 +11,0 @@ $status |
@@ -10,6 +10,6 @@ # Patronum/Status | ||
```ts | ||
$status = status(effect, initial); | ||
$status = status({ effect, defaultValue }); | ||
``` | ||
- When `status` is run, set `initial` value to `$status` | ||
- When `status` is run, set `defaultValue` value to `$status` | ||
- When `effect` is called, set `pending` status. | ||
@@ -22,3 +22,3 @@ - When `effect` is succeeded, set `done` status. | ||
1. `effect` _(Effect<P, R>)_ — any effect, that you need to watch status | ||
2. `initial` _('initial' | 'pending' | 'done' | 'fail')_ _optional_ — when `$status` initializes, set initial value for it. By default value is `"initial"` | ||
2. `defaultValue` _('initial' | 'pending' | 'done' | 'fail')_ _optional_ — when `$status` initializes, set initial value for it. By default value is `"initial"` | ||
@@ -40,3 +40,3 @@ ## Returns | ||
const effect = createEffect().use(() => Promise.resolve(null)); | ||
const $status = status(effect); | ||
const $status = status({ effect }); | ||
@@ -58,3 +58,3 @@ $status.watch((value) => console.log(`status: ${value}`)); | ||
const effect = createEffect().use(() => Promise.resolve(null)); | ||
const $status = status(effect, 'pending'); | ||
const $status = status({ effect, defaultValue: 'pending' }); | ||
@@ -80,3 +80,3 @@ $status.watch((value) => console.log(`status: ${value}`)); | ||
const $status = status(effect); | ||
const $status = status({ effect }); | ||
$status.reset(reset); | ||
@@ -83,0 +83,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
46707
539
291