Skip to content

Instantly share code, notes, and snippets.

@OliverJAsh
Created November 22, 2018 08:32
Show Gist options
  • Save OliverJAsh/0afad9771d032193a16f40c554ec5883 to your computer and use it in GitHub Desktop.
Save OliverJAsh/0afad9771d032193a16f40c554ec5883 to your computer and use it in GitHub Desktop.
// Wrap the epic to:
// - `takeUntil` the counter is removed
// - pass the local state into the counter epic
const wrappedCounterEpic = (
counterId: string
): Epic<Action, Action, State> => (action$, state$) => {
const counterRemovedAction$ = action$.filter(
checkIsCounterRemovedAction
);
const counterStateAction$ = counterEpic(
action$,
// Note: this should be converted to a `StateObservable`
// type but we've skipped this to keep the example simple.
getCounterState(counterId)(state$.value),
{}
);
const counterRemoved$ = counterRemovedAction$.filter(
counterRemovedAction =>
counterRemovedAction.id === counterId
);
return counterStateAction$.takeUntil(counterRemoved$);
};
// When an "added" action is received, start a new instance of
// the list item epic.
const rootEpic: Epic<Action, Action, State> = (
action$,
state$
) => {
const counterAddedAction$ = action$.filter(
checkIsCounterAddedAction
);
const counterStateAction$ = counterAddedAction$.mergeMap(
counterAddedAction =>
wrappedCounterEpic(counterAddedAction.id)(
action$,
state$,
{}
)
);
return counterStateAction$;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment