|
| 1 | +interface Plugins<TConfig> {} |
| 2 | + |
| 3 | +type PluginNames = keyof Plugins<any>; |
| 4 | + |
| 5 | +interface MiddlewarePlugin<TKind extends PluginNames, TPreviousConfig> { |
| 6 | + readonly _group: TKind; |
| 7 | + configure<TConfig>(cb: (conf: TPreviousConfig) => TConfig): Plugins<TConfig>[TKind]; |
| 8 | + // combine<TConfig>(cb: Plugins<(conf: TPreviousConfig) => TConfig>[TKind]): Plugins<TConfig>[TKind]; |
| 9 | + // The above "works", but the below _should_ result in less to check and is simpler (and is needed to trigger the OOM bug) |
| 10 | + combine<TConfig>(cb: MiddlewarePlugin<TKind, (conf: TPreviousConfig) => TConfig>): Plugins<TConfig>[TKind]; |
| 11 | +} |
| 12 | + |
| 13 | +const GroupOneName = "GroupOne"; |
| 14 | +type GroupOneName = typeof GroupOneName; |
| 15 | +type GroupOne<T> = PluginA<T> | PluginB<T>; |
| 16 | +interface Plugins<TConfig> { |
| 17 | + [GroupOneName]: GroupOne<TConfig>; |
| 18 | +} |
| 19 | +class PluginA<TConf> implements MiddlewarePlugin<GroupOneName, TConf> { |
| 20 | + readonly _kind = "PluginA"; |
| 21 | + readonly _group = GroupOneName; |
| 22 | + constructor(public value: TConf) {} |
| 23 | + configure<T>(cb: (conf: TConf) => T): GroupOne<T> { |
| 24 | + return new PluginA(cb(this.value)); |
| 25 | + } |
| 26 | + combine<T>(plug: GroupOne<(conf: TConf) => T>): GroupOne<T> { |
| 27 | + return plug.configure(f => f(this.value)); |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +class PluginB<TConf> implements MiddlewarePlugin<GroupOneName, TConf> { |
| 32 | + readonly _kind = "PluginB"; |
| 33 | + readonly _group = GroupOneName; |
| 34 | + constructor(public value: TConf) {} |
| 35 | + configure<T>(cb: (conf: TConf) => T): GroupOne<T> { |
| 36 | + return new PluginB(cb(this.value)); |
| 37 | + } |
| 38 | + combine<T>(plug: GroupOne<(conf: TConf) => T>): GroupOne<T> { |
| 39 | + return plug.configure(f => f(this.value)); |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +// One plugin group isn't quite enough to run OOM - we need a few more |
| 44 | + |
| 45 | +const GroupTwoName = "GroupTwo"; |
| 46 | +type GroupTwoName = typeof GroupTwoName; |
| 47 | +type GroupTwo<T> = PluginC<T> | PluginD<T>; |
| 48 | +interface Plugins<TConfig> { |
| 49 | + [GroupTwoName]: GroupTwo<TConfig>; |
| 50 | +} |
| 51 | +class PluginC<TConf> implements MiddlewarePlugin<GroupTwoName, TConf> { |
| 52 | + readonly _kind = "PluginC"; |
| 53 | + readonly _group = GroupTwoName; |
| 54 | + constructor(public value: TConf) {} |
| 55 | + configure<T>(cb: (conf: TConf) => T): GroupTwo<T> { |
| 56 | + return new PluginC(cb(this.value)); |
| 57 | + } |
| 58 | + combine<T>(plug: GroupTwo<(conf: TConf) => T>): GroupTwo<T> { |
| 59 | + return plug.configure(f => f(this.value)); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +class PluginD<TConf> implements MiddlewarePlugin<GroupTwoName, TConf> { |
| 64 | + readonly _kind = "PluginD"; |
| 65 | + readonly _group = GroupTwoName; |
| 66 | + constructor(public value: TConf) {} |
| 67 | + configure<T>(cb: (conf: TConf) => T): GroupTwo<T> { |
| 68 | + return new PluginD(cb(this.value)); |
| 69 | + } |
| 70 | + combine<T>(plug: GroupTwo<(conf: TConf) => T>): GroupTwo<T> { |
| 71 | + return plug.configure(f => f(this.value)); |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +const GroupThreeName = "GroupThree"; |
| 76 | +type GroupThreeName = typeof GroupThreeName; |
| 77 | +type GroupThree<T> = PluginE<T> | PluginF<T>; |
| 78 | +interface Plugins<TConfig> { |
| 79 | + [GroupThreeName]: GroupThree<TConfig>; |
| 80 | +} |
| 81 | +class PluginE<TConf> implements MiddlewarePlugin<GroupThreeName, TConf> { |
| 82 | + readonly _kind = "PluginC"; |
| 83 | + readonly _group = GroupThreeName; |
| 84 | + constructor(public value: TConf) {} |
| 85 | + configure<T>(cb: (conf: TConf) => T): GroupThree<T> { |
| 86 | + return new PluginE(cb(this.value)); |
| 87 | + } |
| 88 | + combine<T>(plug: GroupThree<(conf: TConf) => T>): GroupThree<T> { |
| 89 | + return plug.configure(f => f(this.value)); |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +class PluginF<TConf> implements MiddlewarePlugin<GroupThreeName, TConf> { |
| 94 | + readonly _kind = "PluginD"; |
| 95 | + readonly _group = GroupThreeName; |
| 96 | + constructor(public value: TConf) {} |
| 97 | + configure<T>(cb: (conf: TConf) => T): GroupThree<T> { |
| 98 | + return new PluginF(cb(this.value)); |
| 99 | + } |
| 100 | + combine<T>(plug: GroupThree<(conf: TConf) => T>): GroupThree<T> { |
| 101 | + return plug.configure(f => f(this.value)); |
| 102 | + } |
| 103 | +} |
0 commit comments