Uneventful
    Preparing search index...

    Class Ext<Target>Abstract Experimental

    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.:

    class MyExt extends Ext<MyTargetType> {
    // ...
    }

    You can then use MyExt.for(), .delete(), .has(), etc. on instances of MyTargetType, to manage the MyExt instances attached to them.

    Type Parameters

    • Target extends WeakKey = WeakKey

      The type of target this extension will extend.

    Index

    Extension Management

    Static methods for working with extensions of the subclass type, e.g. MyExt.for(someTarget).

    Lifecycle Hooks

    Instance and static members you can override to customize extension creation, deletion, target and return types.

    Other

    Extension Management

    Lifecycle Hooks

    • 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.)

      Type Parameters

      • Class extends typeof Ext

      Parameters

      Returns void

    • 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!)

      Type Parameters

      • Class extends typeof Ext

      Parameters

      Returns void

    __type__: unknown

    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.

    of: Target

    The 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.

    Other

    • Experimental

      Type Parameters

      • Target extends object = object

      Parameters

      Returns Ext<Target>

      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.)