Skip to content

Instantly share code, notes, and snippets.

@jrop
Created July 18, 2018 13:10
Show Gist options
  • Save jrop/c8fa4eaa11f1c07c65c0f07cc42a0786 to your computer and use it in GitHub Desktop.
Save jrop/c8fa4eaa11f1c07c65c0f07cc42a0786 to your computer and use it in GitHub Desktop.
Pusher.ts
export type Receiver = (ctx: any, ...children: any[]) => any;
// type DynamicContextGenerator = (ctx: any, i: number) => any;
export class Pusher {
ctx: any;
receiver: Receiver;
children: Pusher[];
// getDynamicContext: DynamicContextGenerator = null;
constructor(ctx: any, receiver: Receiver, ...children: Pusher[]) {
this.ctx = ctx;
this.receiver = receiver;
this.children = children;
}
with(ctx) {
Object.assign(this.ctx, ctx);
return this;
}
//
// Pushes `ctx` "down" to children
//
build(ctx = this.ctx) {
ctx = Object.assign({}, ctx, this.ctx);
const children = this.children.map((c /*, i*/) =>
// this.getDynamicContext
// ? c.push(Object.assign({}, ctx, this.getDynamicContext(ctx, i)))
// :
c.build(ctx)
);
return this.receiver(ctx, ...children);
}
// setDynamicContextGenerator(fn: DynamicContextGenerator) {
// this.getDynamicContext = fn;
// return this;
// }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment