Created
July 18, 2018 13:10
-
-
Save jrop/c8fa4eaa11f1c07c65c0f07cc42a0786 to your computer and use it in GitHub Desktop.
Pusher.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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