Skip to content

Commit

Permalink
refactor(interceptors): update to new thi.ng/paths API
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Mar 24, 2020
1 parent 182b36b commit 4fce623
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
28 changes: 14 additions & 14 deletions packages/interceptors/src/event-bus.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Atom, IAtom } from "@thi.ng/atom";
import { illegalArgs } from "@thi.ng/errors";
import { setInUnsafe, updateInUnsafe } from "@thi.ng/paths";
import {
implementsFunction,
isArray,
isFunction,
isPromise
isPromise,
} from "@thi.ng/checks";
import { illegalArgs } from "@thi.ng/errors";
import { setIn, updateIn } from "@thi.ng/paths";
import {
EffectDef,
EffectPriority,
Expand All @@ -29,7 +29,7 @@ import {
InterceptorContext,
InterceptorFn,
LOGGER,
SideEffect
SideEffect,
} from "./api";
import type { IDeref, IObjectOf } from "@thi.ng/api";

Expand Down Expand Up @@ -209,13 +209,13 @@ export class StatelessEventBus implements IDispatch {
LOGGER.warn(`skipping invalid async effect: ${id}`);
}
},
-999
-999,
],

[FX_DELAY]: [
([x, body]) =>
new Promise((res) => setTimeout(() => res(body), x)),
1000
1000,
],

[FX_FETCH]: [
Expand All @@ -226,8 +226,8 @@ export class StatelessEventBus implements IDispatch {
}
return resp;
}),
1000
]
1000,
],
});
}

Expand Down Expand Up @@ -714,21 +714,21 @@ export class EventBus extends StatelessEventBus
// handlers
this.addHandlers({
[EV_SET_VALUE]: (state, [_, [path, val]]) => ({
[FX_STATE]: setIn(state, path, val)
[FX_STATE]: setInUnsafe(state, path, val),
}),
[EV_UPDATE_VALUE]: (state, [_, [path, fn, ...args]]) => ({
[FX_STATE]: updateIn(state, path, fn, ...args)
[FX_STATE]: updateInUnsafe(state, path, fn, ...args),
}),
[EV_TOGGLE_VALUE]: (state, [_, path]) => ({
[FX_STATE]: updateIn(state, path, (x) => !x)
[FX_STATE]: updateInUnsafe(state, path, (x) => !x),
}),
[EV_UNDO]: undoHandler("undo"),
[EV_REDO]: undoHandler("redo")
[EV_REDO]: undoHandler("redo"),
});

// effects
this.addEffects({
[FX_STATE]: [(state) => this.state.reset(state), -1000]
[FX_STATE]: [(state) => this.state.reset(state), -1000],
});
}

Expand Down Expand Up @@ -788,7 +788,7 @@ const undoHandler = (action: string): InterceptorFn => (
? ok !== undefined
? ev[1]
: ev[2]
: undefined
: undefined,
};
} else {
LOGGER.warn("no history in context");
Expand Down
16 changes: 8 additions & 8 deletions packages/interceptors/src/interceptors.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { getIn, setter, updater } from "@thi.ng/paths";
import { defSetterUnsafe, defUpdaterUnsafe, getInUnsafe } from "@thi.ng/paths";
import {
Event,
FX_CANCEL,
FX_DISPATCH,
FX_DISPATCH_NOW,
FX_STATE,
InterceptorFn,
InterceptorPredicate
InterceptorPredicate,
} from "./api";
import type { Fn, FnO, Path } from "@thi.ng/api";

Expand Down Expand Up @@ -34,7 +34,7 @@ export const forwardSideFx = (fxID: string): InterceptorFn => (
* @param event -
*/
export const dispatch = (event: Event): InterceptorFn => () => ({
[FX_DISPATCH]: event
[FX_DISPATCH]: event,
});

/**
Expand All @@ -44,7 +44,7 @@ export const dispatch = (event: Event): InterceptorFn => () => ({
* @param event -
*/
export const dispatchNow = (event: Event): InterceptorFn => () => ({
[FX_DISPATCH_NOW]: event
[FX_DISPATCH_NOW]: event,
});

/**
Expand Down Expand Up @@ -127,15 +127,15 @@ export const ensurePred = (
!pred(state, e, bus, ctx)
? {
[FX_CANCEL]: true,
...(err ? err(state, e, bus, ctx) : null)
...(err ? err(state, e, bus, ctx) : null),
}
: undefined;

const eventPathState = (
state: any,
path: Fn<Event, Path> | undefined,
e: Event
) => getIn(state, path ? path(e) : e[1]);
) => getInUnsafe(state, path ? path(e) : e[1]);

/**
* Specialization of {@link ensurePred} to ensure a state value is less than
Expand Down Expand Up @@ -245,7 +245,7 @@ export const ensureParamRange = (
* @param tx -
*/
export const valueSetter = <T>(path: Path, tx?: Fn<T, T>): InterceptorFn => {
const $ = setter(path);
const $ = defSetterUnsafe(path);
return (state, [_, val]) => ({ [FX_STATE]: $(state, tx ? tx(val) : val) });
};

Expand Down Expand Up @@ -273,6 +273,6 @@ export const valueSetter = <T>(path: Path, tx?: Fn<T, T>): InterceptorFn => {
* @param fn -
*/
export const valueUpdater = <T>(path: Path, fn: FnO<T, T>): InterceptorFn => {
const $ = updater(path, fn);
const $ = defUpdaterUnsafe(path, fn);
return (state, [_, ...args]) => ({ [FX_STATE]: $(state, ...args) });
};

0 comments on commit 4fce623

Please sign in to comment.