beyond
Version:
The Full Stack Universal Typescript Framework
70 lines (58 loc) • 2.35 kB
JavaScript
/**
* Child wrapper
* Responsible for registering and deregistering the events of the child
*/
module.exports = class {
get dp() {
return this.
}
get child() {
return this.
}
// The invalidation events to which the child is subscribed
// A child has been processed, but its state has not been changed (event 'change' is not emitted)
/**
* Child wrapper constructor
*
* @param name {string} The name under which the child was registered in the dp
* @param dp {object} The parent dynamic processor
* @param child {object} The child dynamic processor
* @param subscriptions {object} The invalidation events of the dp emitted by the child
*/
constructor(name, dp, child, subscriptions) {
if (typeof name !== 'string') throw new Error('Invalid child name specification');
if (!child) {
throw new Error(`Child property "${name}" is undefined`);
}
if (typeof child.on !== 'function' || typeof child.initialise !== 'function') {
throw new Error(`Child property "${name}" is not a dynamic processor`);
}
if (!child.dp) {
throw new Error(`Child "${name}" must have the property .dp set`);
}
this.
this.
child.on('change', this.
child.on('processed', this.
subscriptions = subscriptions ? subscriptions : [];
this.
if (typeof event !== 'string') throw new Error('Invalid event type, it should be string');
return !['initialised', 'change'].includes(event);
});
subscriptions.forEach(event => child.on(event, this.
}
/**
* Destroy the child wrapper
*/
destroy() {
const child = this.
child.off('change', this.
child.off('processed', this.
this.
}
}