You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Oct 12, 2022. It is now read-only.
Repository navigation
This repository was archived by the owner on Oct 12, 2022. It is now read-only.
I took some time (too much) to refine the Mixin pattern I've been using, with the intention of offering to write a new version of http://www.typescriptlang.org/Handbook#mixins if you guys like it. I think it's a good use of user-defined type guard functions.
I'll give a short summary, then some items of differences between the new pattern and the Handbook's, and then the example from the Handbook rewritten in terms of the new pattern. I have no opinion about naming conventions, so please do suggest alternatives.
For the summary I'll use the example as a reference. The new pattern produces a namespace ActivatableMixin with three visible members:
-An interface, ActivatableInterface
-A function usedBy, which tests if an object's class uses the mixin. This is the main improvement over the Handbook pattern.
-A function mixInto, such that ActivatableMixin.mixInto(C) does everything that the Handbook's applyMixins(C, [Activatable]) does, and additionally stores the class C, so we can later use (x instanceof C) in ActivatableMixin.usedBy.
In addition to the usedBy function, I'm suggesting two small alterations to prevent some surprising behavior of the current Handbook pattern that bit me.
Added an option to exclude "constructor" from the mixed-in property names. In the code below (see makeMixinApplicator) it defaults to true. When it's excluded, you have smartObject.constructor === SmartObject instead of smartObject.constructor === <constructor of last-supplied mixin>, which I think is less surprising, at least for someone like me who didn't know the JS prototype system well when first learning TS.
The implementation of the mixin is hidden. This is so that a new TS user doesn't mistakenly think they can use instanceof to check if an object implements a mixin. It's an easy mistake to make since smartObject instanceof Activatable compiles fine.
namespaceDisposableMixin{varclassesUsingThis : Function[]=[];exportinterfaceDisposableInterface{isDisposed: boolean;dispose() : void;}classDisposableImplementationimplementsDisposableInterface{isDisposed:boolean;dispose():void{this.isDisposed=true;}}exportvarmixInto=makeMixinApplicator(DisposableImplementation,classesUsingThis);exportvarusedBy=makeMixinInstanceChecker<DisposableInterface>(classesUsingThis);}namespaceActivatableMixin{varclassesUsingThis : Function[]=[];exportinterfaceActivatableInterface{isActive: booleanactivate() : voiddeactivate() : void}classActivatableImplementationimplementsActivatableInterface{isActive: boolean;activate(){this.isActive=true;}deactivate(){this.isActive=false;}}exportvarmixInto=makeMixinApplicator(ActivatableImplementation,classesUsingThis);exportvarisActivatable=makeMixinInstanceChecker<ActivatableInterface>(classesUsingThis);}importDisposableInterface=DisposableMixin.DisposableInterface;importActivatableInterface=ActivatableMixin.ActivatableInterface;classSmartObjectimplementsDisposableInterface,ActivatableInterface{constructor(){setInterval(()=>console.log("activated: "+this.isActive+" | disposed: "+this.isDisposed),500);}interact(){this.activate();}// DisposableisDisposed: boolean=false;dispose: ()=>void;// ActivatableisActive: boolean=false;activate: ()=>void;deactivate: ()=>void;}DisposableMixin.mixInto(SmartObject);ActivatableMixin.mixInto(SmartObject);varsmartObj=newSmartObject();setTimeout(()=>smartObj.interact(),1000);////////////////////////////////////////// In your runtime library somewhere////////////////////////////////////////functionmakeMixinApplicator(mixinImplementation:any,classesUsingMixin:any[],exclude_constructor=true) : (c:any)=>void{return(c:any)=>{classesUsingMixin.push(c);Object.getOwnPropertyNames(mixinImplementation.prototype).forEach(name=>{if(!exclude_constructor||name!=="constructor"){(<any>c.prototype)[name]=(<any>mixinImplementation.prototype)[name];}})};}functionmakeMixinInstanceChecker<T>(classesUsingMixin:Function[]) : ((x:any)=>x is T){return(x:any) : x is T=>{for(leti=0;i<classesUsingMixin.length;i++){if(xinstanceofclassesUsingMixin[i])returntrue;}returnfalse;}}
Activity
DanielRosenwasser commentedon Aug 13, 2015
This could probably use union types, and potentially local types.
DustinWehr commentedon Sep 29, 2015
I took some time (too much) to refine the Mixin pattern I've been using, with the intention of offering to write a new version of http://www.typescriptlang.org/Handbook#mixins if you guys like it. I think it's a good use of user-defined type guard functions.
I'll give a short summary, then some items of differences between the new pattern and the Handbook's, and then the example from the Handbook rewritten in terms of the new pattern. I have no opinion about naming conventions, so please do suggest alternatives.
For the summary I'll use the example as a reference. The new pattern produces a namespace ActivatableMixin with three visible members:
-An interface,
ActivatableInterface-A function
usedBy, which tests if an object's class uses the mixin. This is the main improvement over the Handbook pattern.-A function
mixInto, such thatActivatableMixin.mixInto(C)does everything that the Handbook'sapplyMixins(C, [Activatable])does, and additionally stores the classC, so we can later use(x instanceof C)inActivatableMixin.usedBy.In addition to the usedBy function, I'm suggesting two small alterations to prevent some surprising behavior of the current Handbook pattern that bit me.
smartObject.constructor === SmartObjectinstead ofsmartObject.constructor === <constructor of last-supplied mixin>, which I think is less surprising, at least for someone like me who didn't know the JS prototype system well when first learning TS.smartObject instanceof Activatablecompiles fine.