(Target: any, MixinClass: Partial<any> | ((t: Partial<any>) => Partial<any>))
| 862 | } |
| 863 | |
| 864 | private _applyMixin(Target: any, MixinClass: Partial<any> | ((t: Partial<any>) => Partial<any>)) { |
| 865 | if (!('prototype' in Target)) throw new Error('Target must be a class.'); |
| 866 | if (typeof MixinClass === 'function') MixinClass = MixinClass(Target.prototype); |
| 867 | this.ctx.effect(() => { |
| 868 | let oldValue = null; |
| 869 | for (const val of Object.getOwnPropertyNames(MixinClass)) { |
| 870 | if (Target.prototype[val]) { |
| 871 | logger.warn(`${Target.name}.prototype[${val}] already exists.`); |
| 872 | oldValue = Target.prototype[val]; |
| 873 | } |
| 874 | Target.prototype[val] = MixinClass[val]; |
| 875 | } |
| 876 | return () => { |
| 877 | for (const val of Object.getOwnPropertyNames(MixinClass)) { |
| 878 | if (Target.prototype[val] !== MixinClass[val]) { |
| 879 | logger.warn(`Failed to unload mixin ${Target.name}.prototype[${val}]: not the same as the original value.`); |
| 880 | } else { |
| 881 | delete Target.prototype[val]; |
| 882 | if (oldValue) Target.prototype[val] = oldValue; |
| 883 | } |
| 884 | } |
| 885 | }; |
| 886 | }); |
| 887 | } |
| 888 | |
| 889 | public applyMixin<T extends keyof KnownHandlers>(name: T, MixinClass: any) { |
| 890 | this.withHandlerClass(name, (HandlerClass) => { |
no test coverage detected