CoreContextModule

Base class, acting as a pluggable module, for extending CoreContext functionality. It enables clean separation of concerns while maintaining full access to context capabilities. Modules are assigned to context (CoreContext), can provide services to features (Object3DFeature), and manage their own lifecycle through the useCtx pattern.

Examples

//TODO short examples

Usage

How to implement

Let's create a couple of modules as an example.
Pay attention to useCtx - a built-in lifecycle event method.

MyModule.js
class MyModule extends KVY.CoreContextModule {
    someValue = 228;

    // Called when assigned to ctx.
    // Returns a cleanup function that is called when removed. 
    // Similar to `useEffect()` in React.
    useCtx(ctx) {
        console.log('MyModule: assigned to ctx');

        // Access to ctx and its components
        ctx.three.renderer;

        // Cleanup
        return () => {
            console.log("MyModule: removed from ctx");
        }
    }

    someMethod() {
        ...
    }

    invokeCustomEvent() {
        // CoreContextModule inherits from EventEmitter from eventemitter3
        this.emit("eventType", 1488, "zhma", 52, "etc", { whatever: null })
    }
}
OneMoreModule.js
class OneMoreModule extends KVY.CoreContextModule {
    useCtx(ctx) {
        console.log("OneMoreModule: assigned to ctx");
        return () => console.log('OneMoreModule: removed from ctx');
    }
}

Assign to context

Modules are assigned in CoreContext during initialization as a dictionary.

const ctx = KVY.CoreContext(THREE, {
    myModuleKey: new MyModule(),
    oneMore: new OneMoreModule(),
}); 
// >>> "MyModule: assigned to ctx"
// >>> "OneMoreModule: assigned to ctx"

This can also be done later if needed via the ctx.assignModules method (also as a dictionary) or by using ctx.assignModule with a specified key.

ctx.assignModules({
    myModuleKey: new MyModule(),
    oneMore: new OneMoreModule(),
}); 
// >>> "MyModule: assigned to ctx"
// >>> "OneMoreModule: assigned to ctx"

ctx.assignModule("someKey", new OneMoreModule())
// >>> "OneMoreModule: assigned to ctx"

Combining methods is allowed

const ctx = KVY.CoreContext(THREE, {
    myModuleKey: new MyModule(),
}); 
// >>> "MyModule: assigned to ctx"

ctx.assignModules({
    oneMore: new OneMoreModule(),
}); 
// >>> "OneMoreModule: assigned to ctx"

You can also do it this way:

const ctx = KVY.CoreContext(THREE, { myModuleA: new ModuleA() })
	.assignModules({
		moduleB: new ModuleA(),
		moduleC: new ModuleD(),
	})
	.assignModule("moduleD", new ModuleD());
If you try to assign a module to a key that is already in use, the assignment will not happen, and a warning will be logged to the console.

Access to modules

Wherever the context ctx is available, its ctx.modules is also accessible as a dictionary.

ctx.modules.myModuleKey; // -> MyModule
ctx.modules.oneMore; // -> OneMoreModule
class MyFeature extends KVY.Object3DFeature {
    useCtx(ctx) {
        ctx.modules.myModuleKey // -> MyModule
    }
}

Remove modules

You can remove a module from the context using the removeModule method by the key to which it was assigned.

const ctx = KVY.CoreContext(THREE, {
    myModuleKey: new MyModule(),
    oneMore: new OneMoreModule(),
}); 
// >>> "MyModule: assigned to ctx"
// >>> "OneMoreModule: assigned to ctx"

ctx.removeModule("oneMore");
// >>> "OneMoreModule: removed from ctx"

ctx.modules; // -> { myModuleKey: MyModule }
ctx.modules.myModuleKey; // -> MyModule
ctx.modules.oneMore; // -> undefined

When a module is removed, the cleanup function defined in the useCtx method is called.
Also, if the destroy() method is called on ctx, all modules will be removed.

const ctx = KVY.CoreContext(THREE, {
    myModuleKey: new MyModule(),
    oneMore: new OneMoreModule(),
}); 
// >>> "MyModule: assigned to ctx"
// >>> "OneMoreModule: assigned to ctx"

ctx.destroy();
// >>> "MyModule: removed from ctx"
// >>> "OneMoreModule: removed from ctx"

API

Base abstract class.
Extends EventEmitter.

Properties

.isCoreContextModule: Boolean

(readonly) Flag to mark that it is an instance of CoreContextModule.

ctx: CoreContext

(readonly) Getter for the instance of CoreContext this is assigned to. Throws exception if try to access before assign.

hasCtx: Boolean

(readonly) Flag to check if this instance has been assigned to some CoreContext.

Overridable Lifecycle Methods

useCtx( ctx: CoreContext ): Function

Overridable Lifecycle Method. Called when the module is assigned to a CoreContext. The returned cleanup (optional) function is called when the module is removed from the context. Also cleanup is called if context is destroyed. Calling the method manually is prohibited.

  • ctx - An instance of CoreContext to which the module was assigned.

Source

src/core/CoreContextModule.ts

three-kvy-core ...