Skip to content

Commit

Permalink
feat(interceptors): add default FX_UNDO/REDO side fx
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Apr 14, 2018
1 parent ba0c876 commit a102eb7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
9 changes: 6 additions & 3 deletions packages/interceptors/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@ export const FX_STATE = "--state";
/**
* Currently unused
*/
export const FX_REDO_RESTORE = "--redo-restore";
export const FX_REDO = "--redo";

/**
* Currently unused
*/
export const FX_UNDO_STORE = "--undo-store";
export const FX_UNDO = "--undo";

/**
* Currently unused
*/
export const FX_UNDO_RESTORE = "--undo-restore";
export const FX_UNDO_STORE = "--undo-store";

export interface Event extends Array<any> {
[0]: PropertyKey;
Expand All @@ -61,5 +61,8 @@ export interface InterceptorContext {
[FX_DISPATCH]?: Event | Event[];
[FX_DISPATCH_NOW]?: Event | Event[];
[FX_DISPATCH_ASYNC]?: AsyncEffectDef | AsyncEffectDef[];
[FX_UNDO_STORE]?: string | string[];
[FX_UNDO]?: string | string[];
[FX_REDO]?: string | string[];
[id: string]: any;
}
22 changes: 21 additions & 1 deletion packages/interceptors/src/event-bus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,22 @@ export class EventBus extends StatelessEventBus implements
* Resets state atom to provided value (only a single update per
* processing frame)
*
* #### `FX_UNDO`
*
* Calls `ctx[fxarg].undo()` where `fxarg` is the value assigned to
* the `FX_UNDO` side effect by an event handler, e.g.
*
* ```
* // example event handler
* // assumes that `ctx.history` is a @thi.ng/atom/History instance or similar
* (state, e, bus, ctx) => ({ [FX_UNDO]: "history" })
* ```
*
* #### `FX_REDO`
*
* Similar to `FX_UNDO`, but calls `ctx[fxarg].redo()` where `fxarg`
* is the value assigned to the `FX_REDO` side effect by an event
* handler.
*/
addBuiltIns(): any {
super.addBuiltIns();
Expand All @@ -563,7 +579,11 @@ export class EventBus extends StatelessEventBus implements
({ [FX_STATE]: updateIn(state, path, fn, ...args) }));

// effects
this.addEffect(FX_STATE, (x) => this.state.reset(x), -1000);
this.addEffects({
[FX_STATE]: [(x) => this.state.reset(x), -1000],
[api.FX_UNDO]: [(x, _, ctx) => ctx[x].undo(), -1000],
[api.FX_REDO]: [(x, _, ctx) => ctx[x].redo(), -1000],
});
}

/**
Expand Down

0 comments on commit a102eb7

Please sign in to comment.