Usually applications should be split into smaller logical parts called modules. A problem appears when each module should be configured in multiple places. And when there is lot of different modules, configuring each of them separately became a challenge.
This library is responsible for managing all app modules. You only initialize everything once and then just register and unregister new and old modules.
In each module lets create init file
someCustomModuleA init file:
//modules/someCustomModuleA/index.js
export default {
properties: {
customPropertyA: "customValueAA",
customPropertyB: "customValueAB"
},
events: {
customEventA: () => "customResponseFromEventAA",
customEventB: () => "customResponseFromEventAB"
}
}
As you can see this is just an object with properties : properties
and events
someCustomModuleB init file:
//modules/someCustomModuleB/index.js
import { Extension } from "extensioner";
export default class SomeCustomModuleB extends Extension{
constructor(someInitOptions) {
super();
this.setProperty("customPropertyA", "customValueBA");
this.setProperty("customPropertyC", "customValueBC");
this.onCustomEventA = this.onCustomEventA.bind(this);
this.setEventListener("customEventA", this.onCustomEventA);
this.onCustomEventC = this.onCustomEventC.bind(this);
this.setEventListener("customEventC", this.onCustomEventC);
}
onCustomEventA (initValue) {
return "customResponseFromEventBA";
}
onCustomEventC (initValue) {
return "customResponseFromEventBC";
}
}
In above init file we created class which extends from Extension
.
someCustomModuleC init file:
//modules/someCustomModuleC/index.js
export default function someCustomModuleC(someInitOptions) {
return {
properties: {
customPropertyB: "customValueBC",
customPropertyC: "customValueCC"
},
events: {
customEventB: () => "customResponseFromEventCB",
customEventC: () => "customResponseFromEventCC"
}
}
}
In above init file we created function which will return extension object
Lets create manager instance and register modules
//index.js
import { Manager } from "extensioner";
import someCustomModuleA from "./modules/someCustomModuleA/index.js";
import SomeCustomModuleB from "./modules/someCustomModuleB/index.js";
import someCustomModuleC from "./modules/someCustomModuleC/index.js";
const manager = new Manager();
manager
.registerExtension("someCustomModuleNameA", someCustomModuleA)
.registerExtension("someCustomModuleNameB", new SomeCustomModuleB())
.registerExtension("someCustomModuleNameC", someCustomModuleC());
Lets prepare event and call it.
const event = manager.createEvent("customEventA");
const responses = event();
/**
* responses is equal:
* [
* "customResponseFromEventAA",
* "customResponseFromEventBA"
* ]
*/
Lets prepare event with async event resolver
import { asyncListCompose } from "extensioner";
const event = manager.createEvent("customEventB", asyncListCompose);
event()
.then(responses => {
/**
* responses is equal:
* [
* "customResponseFromEventAB",
* "customResponseFromEventCB"
* ]
*/
});
Lets get property values from all modules
manager.getExtensionsWithProperty("customPropertyA").forEach(extension => {
extension.getProperty("customPropertyA")
})
Creates new manager
Each module should have one root class with all settings and this class should extend Extension
Allow to register/add any new extension and return same manager instance. Second argument can be instance of Extension class
OR
Allow to register/add any new extension and return same manager instance.
Second argument is simple object with properties: properties
and events
When onlyActive
flag is true it will return all active extensions joints for previously registered extensions
When flag is false then it will return all extensions.
When onlyActive
flag is true it will return array of active extension joints with specific property
When flag is false then it will return all extensions with specific property
When onlyActive
flag is true it will return array of active extension joints with specific event listener
When flag is false then it will return all extensions with specific event listener
Return true if extension with ext ensionInstanceName exist and is active. Otherwise false
Returns true if extension with extensionInstanceName exists.
Disable extension with undefinedextensionInstanceName name then it will return true. If extension doesn't exist it will return false
Enable extension with extensionInstanceName name then it will return true. If extension doesn't exist it will return false
Return extension joint by its name or null if doesn't exist.
Return object where keys are extension joints names, and values are extensions properties values
Create event function. Next step is to call this function to trigger event. Response from compose function
const event = manager.createEvent("onRenderHeader", (extensions, eventName, value) => { extensions.map(extension => extension.getEventListener(eventName)(value)) });
event()
.then(response => {
//do something with responses
})
composeFunction is function which compose responses from corresponding event handler.
function composeFunction(extensions: <Extension>Array, eventName: String, value: any): any
This function will get :
- extensions - list of extensions which listening for event
- eventName - event name which is resolving
- value - init value from called event
By default is used syncListCompose
function which will call sync all event handlers, returns response which will be fulfilled with array of responses.
Available functions :
- syncListCompose - will call all event handlers synchronously and return list of responses
- syncMapCompose - will call all event handlers synchronously and return object where keys are extensions names and values are responses
- asyncListCompose - will call all event handlers asynchronously and return promise which will resolve to array of responses
- asyncMapCompose - will call all event handlers asynchronously and return promise which will resolve to object where keys are extensions names and values are responses
This is object containing reference to Manager and to Extension, It also have name. It is created only in manager when extension is registered
Return true when extension is enabled
Enable extension
Disable extension
Returns extension
Returns extension name
Returns manager
Create new extension. ExtensionData is simple object with properties: properties
and events
.
Add new property to module
Check if module has property.
Return property value.
Add new event listener
Check if module has event Listener.
Return event handler.
MIT