Skip to content

Commit

Permalink
feat(interceptors): update forwardSideFx(), refactor iceps as arrow fns
Browse files Browse the repository at this point in the history
- assign `true` to target side fx if no event body
  • Loading branch information
postspectacular committed Nov 13, 2018
1 parent 62b3c31 commit 9334f83
Showing 1 changed file with 57 additions and 47 deletions.
104 changes: 57 additions & 47 deletions packages/interceptors/src/interceptors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,37 +18,40 @@ import {
/**
* Debug interceptor to log the current event to the console.
*/
export function trace(_, e) {
console.log("event:", e);
}
export const trace: InterceptorFn =
(_, e) => console.log("event:", e);

/**
* Higher-order interceptor. Returns interceptor which unpacks payload
* from event and assigns it as is to given side effect ID.
* from event and assigns it as is to given side effect ID. Assigns
* `true` to side effect if event has no payload.
*
* @param fxID side effect ID
*/
export function forwardSideFx(fxID: string): InterceptorFn {
return (_, [__, body]) => ({ [fxID]: body });
}
export const forwardSideFx =
(fxID: string): InterceptorFn =>
(_, [__, body]) =>
({ [fxID]: body !== undefined ? body : true });

/**
* Higher-order interceptor. Returns interceptor which assigns given
* event to `FX_DISPATCH` side effect.
*
* @param event
*/
export const dispatch = (event: Event): InterceptorFn =>
() => ({ [FX_DISPATCH]: event });
export const dispatch =
(event: Event): InterceptorFn =>
() => ({ [FX_DISPATCH]: event });

/**
* Higher-order interceptor. Returns interceptor which assigns given
* event to `FX_DISPATCH_NOW` side effect.
*
* @param event
*/
export const dispatchNow = (event: Event): InterceptorFn =>
() => ({ [FX_DISPATCH_NOW]: event });
export const dispatchNow =
(event: Event): InterceptorFn =>
() => ({ [FX_DISPATCH_NOW]: event });

/**
* Higher-order interceptor. Returns interceptor which calls
Expand Down Expand Up @@ -80,9 +83,9 @@ export const dispatchNow = (event: Event): InterceptorFn =>
*
* @param id
*/
export function snapshot(id = "history"): InterceptorFn {
return (_, __, ___, ctx) => (ctx[id].record());
}
export const snapshot =
(id = "history"): InterceptorFn =>
(_, __, ___, ctx) => (ctx[id].record());

/**
* Higher-order interceptor for validation purposes. Takes a predicate
Expand Down Expand Up @@ -123,13 +126,15 @@ export function snapshot(id = "history"): InterceptorFn {
* @param pred predicate applied to given state & event
* @param err interceptor triggered on predicate failure
*/
export function ensurePred(pred: InterceptorPredicate, err?: InterceptorFn): InterceptorFn {
return (state, e, bus) => {
if (!pred(state, e, bus)) {
return { [FX_CANCEL]: true, ...(err ? err(state, e, bus) : null) };
}
};
}
export const ensurePred =
(pred: InterceptorPredicate, err?: InterceptorFn): InterceptorFn =>
(state, e, bus) =>
!pred(state, e, bus) ?
{
[FX_CANCEL]: true,
...(err ? err(state, e, bus) : null)
} :
undefined;

/**
* Specialization of `ensurePred()` to ensure a state value is less than
Expand All @@ -150,9 +155,9 @@ export function ensurePred(pred: InterceptorPredicate, err?: InterceptorFn): Int
* @param path path extractor
* @param err error interceptor
*/
export function ensureStateLessThan(max: number, path?: (e: Event) => Path, err?: InterceptorFn) {
return ensurePred((state, e) => getIn(state, path ? path(e) : e[1]) < max, err);
}
export const ensureStateLessThan =
(max: number, path?: (e: Event) => Path, err?: InterceptorFn) =>
ensurePred((state, e) => getIn(state, path ? path(e) : e[1]) < max, err);

/**
* Specialization of `ensurePred()` to ensure a state value is greater
Expand All @@ -162,9 +167,9 @@ export function ensureStateLessThan(max: number, path?: (e: Event) => Path, err?
* @param path path extractor
* @param err error interceptor
*/
export function ensureStateGreaterThan(min: number, path?: (e: Event) => Path, err?: InterceptorFn) {
return ensurePred((state, e) => getIn(state, path ? path(e) : e[1]) > min, err);
}
export const ensureStateGreaterThan =
(min: number, path?: (e: Event) => Path, err?: InterceptorFn) =>
ensurePred((state, e) => getIn(state, path ? path(e) : e[1]) > min, err);

/**
* Specialization of `ensurePred()` to ensure a state value is within
Expand All @@ -176,12 +181,12 @@ export function ensureStateGreaterThan(min: number, path?: (e: Event) => Path, e
* @param path path extractor
* @param err error interceptor
*/
export function ensureStateRange(min: number, max: number, path?: (e: Event) => Path, err?: InterceptorFn) {
return ensurePred((state, e) => {
const x = getIn(state, path ? path(e) : e[1]);
return x >= min && x <= max;
}, err);
}
export const ensureStateRange =
(min: number, max: number, path?: (e: Event) => Path, err?: InterceptorFn) =>
ensurePred((state, e) => {
const x = getIn(state, path ? path(e) : e[1]);
return x >= min && x <= max;
}, err);

/**
* Specialization of `ensurePred()` to ensure an event's payload value
Expand All @@ -198,12 +203,12 @@ export function ensureStateRange(min: number, max: number, path?: (e: Event) =>
* @param value event value extractor
* @param err error interceptor
*/
export function ensureParamRange(min: number, max: number, value?: (e: Event) => number, err?: InterceptorFn) {
return ensurePred((_, e) => {
const x = value ? value(e) : e[1];
return x >= min && x <= max;
}, err);
}
export const ensureParamRange =
(min: number, max: number, value?: (e: Event) => number, err?: InterceptorFn) =>
ensurePred((_, e) => {
const x = value ? value(e) : e[1];
return x >= min && x <= max;
}, err);

/**
* Higher-order interceptor. Returns new interceptor to set state value
Expand All @@ -224,10 +229,13 @@ export function ensureParamRange(min: number, max: number, value?: (e: Event) =>
* @param path
* @param tx
*/
export function valueSetter<T>(path: Path, tx?: (x: T) => T): InterceptorFn {
const $ = setter(path);
return (state, [_, val]) => ({ [FX_STATE]: $(state, tx ? tx(val) : val) });
}
export const valueSetter =
<T>(path: Path, tx?: (x: T) => T): InterceptorFn => {
const $ = setter(path);
return (state, [_, val]) =>
({ [FX_STATE]: $(state, tx ? tx(val) : val) });
};


/**
* Higher-order interceptor. Returns new interceptor to update state
Expand All @@ -250,7 +258,9 @@ export function valueSetter<T>(path: Path, tx?: (x: T) => T): InterceptorFn {
* @param path
* @param fn
*/
export function valueUpdater<T>(path: Path, fn: (x: T, ...args: any[]) => T): InterceptorFn {
const $ = updater(path, fn);
return (state, [_, ...args]) => ({ [FX_STATE]: $(state, ...args) });
}
export const valueUpdater =
<T>(path: Path, fn: (x: T, ...args: any[]) => T): InterceptorFn => {
const $ = updater(path, fn);
return (state, [_, ...args]) =>
({ [FX_STATE]: $(state, ...args) });
};

0 comments on commit 9334f83

Please sign in to comment.