Abstract
Experimental
The type of target this extension will extend.
Static methods for working with extensions of the subclass type, e.g.
MyExt.for(someTarget)
.
Instance and static members you can override to customize extension creation, deletion, target and return types.
Static
__del__Experimental
This method is called by delete() if it finds an existing extension for the target. You can override it in a subclass to do any necessary cleanup on the extension. (If you need to retrieve the extension, you can call .get() on the target.)
Static
__new__Experimental
This method is called by for() to create the extension instance for a target. You can override this method to customize instance creation behavior, e.g. to execute the constructor within a job, or create a promise for an extension instance to be asynchronously initiaized, etc.
If you will be creating something other than an instance of the subclass,
you must also redeclare the type of the __type__
property. For example:
class AsyncExt extends Ext {
declare readonly __type__: Job<this>
// simulate slow initialization
*setup() { yield *sleep(100); return this; }
static __new__<Class extends typeof AsyncExt>(
tgt: Ext.Target<Class>, map: Ext.Map<Class>
) {
const ext = new this(tgt);
map.set(tgt, root.start(ext.setup()) as Ext.Type<Class>);
}
}
Now, AsyncExt.for(someTarget)
will create and cache a Job yielding an
extension whose setup()
has finished. (And subclasses of AsyncExt
will share the same behavior, while being subtyped appropriately.)
(Note: your __new__
method must store what it created in the supplied
map for the given target, and the value it sets must conform to the
declared __type__
, which is not checked for you by TypeScript!)
Readonly
Experimental
__type__A "virtual" property you can override in subclasses to change the static
interface's return type. For example, if a subclass does declare readonly __type__: Promise<this>
, and overrides __new__
() to return a promise, then the
static APIs for the subclass (like .for()
) will return promises instead
of instances. (See the __new__
method for more
details.)
Note: this property is not actually set by any code, so you can't do anything other than declare it. It's just a hack to work around TypeScript's limited type parameterization for static generics.
Readonly
Experimental
ofThe target the extension was created for (set automatically by the base
class constructor). You can narrow the target type either by extending
Ext<SomeType>
directly, or by using declare readonly of: SomeType
in
your subclass.
Experimental
Use .for() instead!
Never directly call the constructor of an Ext subclass except from the
__new__()
method - otherwise you run the risk of having
multiple instances for the same target. (And it may accept invalid
parameter values if you've redefined the type of the of
property in a sub-subclass.)
A base class for more complex extension types, providing static accessor and management APIs. (e.g.
MyExt.for(aTarget)
,MyExt.delete(aTarget)
, etc.)To create an extension class, just subclass Ext with an appropriate target type, e.g.:
You can then use
MyExt.for()
,.delete()
,.has()
, etc. on instances ofMyTargetType
, to manage theMyExt
instances attached to them.