MCPcopy Create free account
hub / github.com/hydro-dev/Hydro / _applyMixin

Method _applyMixin

framework/framework/server.ts:864–887  ·  view source on GitHub ↗
(Target: any, MixinClass: Partial<any> | ((t: Partial<any>) => Partial<any>))

Source from the content-addressed store, hash-verified

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) => {

Callers 4

applyMixinMethod · 0.95
handlerMixinMethod · 0.95
httpHandlerMixinMethod · 0.95
wsHandlerMixinMethod · 0.95

Calls 1

warnMethod · 0.45

Tested by

no test coverage detected