Skip to content

Instantly share code, notes, and snippets.

@bpander
Created August 29, 2019 03:30
Show Gist options
  • Save bpander/20c093d0519cf475188c7c9a4a7a5b5a to your computer and use it in GitHub Desktop.
Save bpander/20c093d0519cf475188c7c9a4a7a5b5a to your computer and use it in GitHub Desktop.
import 'redux';
type Duck<T> = {
reducer: (state: T) => T;
};
type GetBranch<T> = <R>(rootState: R) => T;
type DuckCreator<T> = (getBranch: GetBranch<T>) => Duck<T>;
interface DuckCreatorMap {
[key: string]: DuckCreator<any>;
}
const combineDucks = <T extends DuckCreatorMap>(duckMap: T): DuckCreator<T> => {
return {} as any;
};
interface Thing { id: string; name: string }
interface ThingState { things: Record<string, Thing> }
const getThingById = (thingId: string) => (state: ThingState) => state.things[thingId];
const thing = (prefix: string) => (getBranch: GetBranch<ThingState>) => {
const reducer = (state: ThingState) => state;
const loadThing = (thingId: string) => async (dispatch, getState) => {
};
return { reducer, loadThing };
};
const identity = <T>(t: T) => t;
const root = combineDucks({
thing1: thing('THING_1'),
thing2: thing('THING_2'),
nested: combineDucks({
thing3: thing('THING_3'),
}),
contexts: context('CTX', {
thing4: thing('THING_4'),
}),
})(identity);
// root.nested.thing.
// root.contexts.get('lkj').loadThing('')
// root.thing1.
// root.thing1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment