@eroc/core
Advanced tools
Comparing version 0.15.0 to 1.0.0
@@ -1,2 +0,2 @@ | ||
/* @eroc/core v0.15.0 2019-08-20T22:53:14.240Z licensed MIT */ | ||
/* @eroc/core v1.0.0 2019-08-22T20:03:48.440Z licensed MIT */ | ||
/** | ||
@@ -153,52 +153,56 @@ * Use it as a constructor | ||
const Core = class { | ||
constructor() { | ||
this.moduleInstances = new Map(); | ||
EventEmitter3(this); | ||
} | ||
constructor() { | ||
this.moduleInstances = new Map(); | ||
EventEmitter3(this); | ||
} | ||
register() { | ||
register() { | ||
}; | ||
start(module, { name = Symbol() } = {}) { | ||
const emitter = new EventEmitter3(); | ||
// emulate emitter.on(ANY, (name, data) => { | ||
emitter.emit = (name, data) => { | ||
this.emit(name, data); | ||
this.emit(ALL, { name, data }); | ||
this.moduleInstances.forEach(({ emitter }) => { | ||
EventEmitter3.prototype.emit.call(emitter, name, data); | ||
}); | ||
}; | ||
this.moduleInstances.set(name, { | ||
module, | ||
instance: module.start(emitter), | ||
name, | ||
emitter, | ||
}); | ||
start(module, { name = Symbol() } = {}) { | ||
if (this.moduleInstances.has(name)) { | ||
throw `module with name ${name} already started`; | ||
} | ||
return name; | ||
} | ||
const emitter = new EventEmitter3(); | ||
stop(name) { | ||
const wrapper = this.moduleInstances.get(name); | ||
// emulate emitter.on(ANY, (name, data) => { | ||
emitter.emit = (name, data) => { | ||
this.emit(name, data); | ||
this.emit(ALL, { name, data }); | ||
this.moduleInstances.forEach(({ emitter }) => { | ||
EventEmitter3.prototype.emit.call(emitter, name, data); | ||
}); | ||
}; | ||
if (!wrapper) { | ||
//err('!stop', module); | ||
return false; | ||
} | ||
this.moduleInstances.set(name, { | ||
module, | ||
instance: module.start(emitter), | ||
name, | ||
emitter, | ||
}); | ||
wrapper.emitter.off(); | ||
if (wrapper.module.stop) { | ||
wrapper.module.stop(wrapper.instance); | ||
return name; | ||
} | ||
this.moduleInstances.delete(name); | ||
stop(name) { | ||
const wrapper = this.moduleInstances.get(name); | ||
return true; | ||
} | ||
if (!wrapper) { | ||
//err('!stop', module); | ||
return false; | ||
} | ||
wrapper.emitter.off(); | ||
if (wrapper.module.stop) { | ||
wrapper.module.stop(wrapper.instance); | ||
} | ||
this.moduleInstances.delete(name); | ||
return true; | ||
} | ||
}; | ||
export { ALL, Core }; |
@@ -1,211 +0,215 @@ | ||
/* @eroc/core v0.15.0 2019-08-20T22:53:14.240Z licensed MIT */ | ||
/* @eroc/core v1.0.0 2019-08-22T20:03:48.440Z licensed MIT */ | ||
var Core = (function (exports) { | ||
'use strict'; | ||
'use strict'; | ||
/** | ||
* Use it as a constructor | ||
* or as a decorator for an existing object | ||
* or as a base class for extend | ||
* cannot be used as a mixin for a constructor's prototype | ||
*/ | ||
function EventEmitter3(obj) { | ||
(obj || this)._callbacks = Object.create(null); | ||
if (obj) return Object.assign(obj, EventEmitter3.prototype); | ||
} | ||
/** | ||
* Listen on the given `eventName` with `fn` | ||
* | ||
* @param {String | Symbol} eventName | ||
* @param {Function} fn | ||
* @api public | ||
*/ | ||
/** | ||
* Use it as a constructor | ||
* or as a decorator for an existing object | ||
* or as a base class for extend | ||
* cannot be used as a mixin for a constructor's prototype | ||
*/ | ||
function EventEmitter3(obj) { | ||
(obj || this)._callbacks = Object.create(null); | ||
if (obj) return Object.assign(obj, EventEmitter3.prototype); | ||
} | ||
/** | ||
* Listen on the given `eventName` with `fn` | ||
* | ||
* @param {String | Symbol} eventName | ||
* @param {Function} fn | ||
* @api public | ||
*/ | ||
EventEmitter3.prototype.on = function (eventName, fn) { | ||
(this._callbacks[eventName] = this._callbacks[eventName] || []) | ||
.push(fn); | ||
}; | ||
EventEmitter3.prototype.on = function (eventName, fn) { | ||
(this._callbacks[eventName] = this._callbacks[eventName] || []) | ||
.push(fn); | ||
}; | ||
/** | ||
* Adds an `eventName` listener that will be invoked once then removed | ||
* | ||
* @param {String | Symbol} eventName | ||
* @param {Function} fn | ||
* @api public | ||
*/ | ||
/** | ||
* Adds an `eventName` listener that will be invoked once then removed | ||
* | ||
* @param {String | Symbol} eventName | ||
* @param {Function} fn | ||
* @api public | ||
*/ | ||
EventEmitter3.prototype.once = function (eventName, fn) { | ||
const once = (data) => { | ||
this.off(eventName, once); | ||
fn(data); | ||
}; | ||
EventEmitter3.prototype.once = function (eventName, fn) { | ||
const once = (data) => { | ||
this.off(eventName, once); | ||
fn(data); | ||
}; | ||
once.fn = fn; // makes it possible to remove with off | ||
this.on(eventName, once); | ||
}; | ||
once.fn = fn; // makes it possible to remove with off | ||
this.on(eventName, once); | ||
}; | ||
/** | ||
* Remove a callback for `eventName` or | ||
* all callbacks for `eventName` or | ||
* all callbacks for all events | ||
* | ||
* @param {String | Symbol} eventName | ||
* @param {Function} fn | ||
* @api public | ||
*/ | ||
/** | ||
* Remove a callback for `eventName` or | ||
* all callbacks for `eventName` or | ||
* all callbacks for all events | ||
* | ||
* @param {String | Symbol} eventName | ||
* @param {Function} fn | ||
* @api public | ||
*/ | ||
EventEmitter3.prototype.off = function (eventName, fn) { | ||
// all | ||
if (!eventName) { | ||
this._callbacks = Object.create(null); | ||
return; | ||
} | ||
EventEmitter3.prototype.off = function (eventName, fn) { | ||
// all | ||
if (!eventName) { | ||
this._callbacks = Object.create(null); | ||
return; | ||
} | ||
// specific event | ||
const callbacks = this._callbacks[eventName]; | ||
if (!callbacks) { | ||
return; | ||
} | ||
// specific event | ||
const callbacks = this._callbacks[eventName]; | ||
if (!callbacks) { | ||
return; | ||
} | ||
// remove all handlers | ||
if (!fn) { | ||
delete this._callbacks[eventName]; | ||
return; | ||
} | ||
// remove all handlers | ||
if (!fn) { | ||
delete this._callbacks[eventName]; | ||
return; | ||
} | ||
// remove specific handler | ||
const index = callbacks.findIndex(function (cb) { | ||
return (cb === fn || cb.fn === fn) | ||
}); | ||
if (index > -1) { | ||
// Remove event specific arrays for the eventName type that no | ||
// one is subscribed for, to avoid memory leak. | ||
if (callbacks.length === 1) { | ||
delete this._callbacks[eventName]; | ||
} else { | ||
callbacks.splice(index, 1); | ||
} | ||
} | ||
}; | ||
// remove specific handler | ||
const index = callbacks.findIndex(function (cb) { | ||
return (cb === fn || cb.fn === fn) | ||
}); | ||
if (index > -1) { | ||
// Remove event specific arrays for the eventName type that no | ||
// one is subscribed for, to avoid memory leak. | ||
if (callbacks.length === 1) { | ||
delete this._callbacks[eventName]; | ||
} else { | ||
callbacks.splice(index, 1); | ||
} | ||
} | ||
}; | ||
/** | ||
* Emit `eventName` with data | ||
* | ||
* @param {String | Symbol} eventName | ||
* @param {any} data | ||
*/ | ||
/** | ||
* Emit `eventName` with data | ||
* | ||
* @param {String | Symbol} eventName | ||
* @param {any} data | ||
*/ | ||
EventEmitter3.prototype.emit = function (eventName, data) { | ||
const callbacks = this._callbacks[eventName]; | ||
if (!callbacks) { | ||
return; | ||
} | ||
const frozenCallbacks = Array.from(callbacks); | ||
frozenCallbacks.forEach(callback => { | ||
callback(data); | ||
}); | ||
}; | ||
EventEmitter3.prototype.emit = function (eventName, data) { | ||
const callbacks = this._callbacks[eventName]; | ||
if (!callbacks) { | ||
return; | ||
} | ||
const frozenCallbacks = Array.from(callbacks); | ||
frozenCallbacks.forEach(callback => { | ||
callback(data); | ||
}); | ||
}; | ||
/** | ||
* Return array of callbacks for `eventName` | ||
* | ||
* @param {String | Symbol} eventName | ||
* @return {Array} listeners | ||
* @api public | ||
*/ | ||
/** | ||
* Return array of callbacks for `eventName` | ||
* | ||
* @param {String | Symbol} eventName | ||
* @return {Array} listeners | ||
* @api public | ||
*/ | ||
EventEmitter3.prototype.listeners = function (eventName) { | ||
return this._callbacks[eventName] || []; | ||
}; | ||
EventEmitter3.prototype.listeners = function (eventName) { | ||
return this._callbacks[eventName] || []; | ||
}; | ||
/** | ||
* True if this emitter has `eventName` handlers | ||
* | ||
* @param {String | Symbol} eventName | ||
* @return {Boolean} | ||
* @api public | ||
*/ | ||
/** | ||
* True if this emitter has `eventName` handlers | ||
* | ||
* @param {String | Symbol} eventName | ||
* @return {Boolean} | ||
* @api public | ||
*/ | ||
EventEmitter3.prototype.hasListeners = function (eventName) { | ||
return Boolean(this.listeners(eventName).length); | ||
}; | ||
EventEmitter3.prototype.hasListeners = function (eventName) { | ||
return Boolean(this.listeners(eventName).length); | ||
}; | ||
/** | ||
* Returns an array of event names for which the emitter has registered listeners | ||
* | ||
* @return {Array <String || Symbol>} | ||
* @api public | ||
*/ | ||
EventEmitter3.prototype.eventNames = function () { | ||
return Reflect.ownKeys(this._callbacks); | ||
}; | ||
/** | ||
* Returns an array of event names for which the emitter has registered listeners | ||
* | ||
* @return {Array <String || Symbol>} | ||
* @api public | ||
*/ | ||
EventEmitter3.prototype.eventNames = function () { | ||
return Reflect.ownKeys(this._callbacks); | ||
}; | ||
/** | ||
* Returns an array of event anmes of type string | ||
* for which the emitter has registered listeners | ||
* | ||
* @return {Array <String>} | ||
* @api public | ||
*/ | ||
EventEmitter3.prototype.eventNamesStrings = function () { | ||
return Object.keys(this._callbacks); | ||
}; | ||
/** | ||
* Returns an array of event anmes of type string | ||
* for which the emitter has registered listeners | ||
* | ||
* @return {Array <String>} | ||
* @api public | ||
*/ | ||
EventEmitter3.prototype.eventNamesStrings = function () { | ||
return Object.keys(this._callbacks); | ||
}; | ||
const ALL = Symbol(); | ||
const ALL = Symbol(); | ||
const Core = class { | ||
constructor() { | ||
this.moduleInstances = new Map(); | ||
EventEmitter3(this); | ||
} | ||
const Core = class { | ||
constructor() { | ||
this.moduleInstances = new Map(); | ||
EventEmitter3(this); | ||
} | ||
register() { | ||
register() { | ||
}; | ||
}; | ||
start(module, { name = Symbol() } = {}) { | ||
const emitter = new EventEmitter3(); | ||
start(module, { name = Symbol() } = {}) { | ||
if (this.moduleInstances.has(name)) { | ||
throw `module with name ${name} already started`; | ||
} | ||
// emulate emitter.on(ANY, (name, data) => { | ||
emitter.emit = (name, data) => { | ||
this.emit(name, data); | ||
this.emit(ALL, { name, data }); | ||
this.moduleInstances.forEach(({ emitter }) => { | ||
EventEmitter3.prototype.emit.call(emitter, name, data); | ||
}); | ||
}; | ||
const emitter = new EventEmitter3(); | ||
this.moduleInstances.set(name, { | ||
module, | ||
instance: module.start(emitter), | ||
name, | ||
emitter, | ||
}); | ||
// emulate emitter.on(ANY, (name, data) => { | ||
emitter.emit = (name, data) => { | ||
this.emit(name, data); | ||
this.emit(ALL, { name, data }); | ||
this.moduleInstances.forEach(({ emitter }) => { | ||
EventEmitter3.prototype.emit.call(emitter, name, data); | ||
}); | ||
}; | ||
return name; | ||
} | ||
this.moduleInstances.set(name, { | ||
module, | ||
instance: module.start(emitter), | ||
name, | ||
emitter, | ||
}); | ||
stop(name) { | ||
const wrapper = this.moduleInstances.get(name); | ||
return name; | ||
} | ||
if (!wrapper) { | ||
//err('!stop', module); | ||
return false; | ||
} | ||
stop(name) { | ||
const wrapper = this.moduleInstances.get(name); | ||
wrapper.emitter.off(); | ||
if (wrapper.module.stop) { | ||
wrapper.module.stop(wrapper.instance); | ||
} | ||
if (!wrapper) { | ||
//err('!stop', module); | ||
return false; | ||
} | ||
this.moduleInstances.delete(name); | ||
wrapper.emitter.off(); | ||
if (wrapper.module.stop) { | ||
wrapper.module.stop(wrapper.instance); | ||
} | ||
return true; | ||
} | ||
}; | ||
this.moduleInstances.delete(name); | ||
exports.ALL = ALL; | ||
exports.Core = Core; | ||
return true; | ||
} | ||
}; | ||
return exports; | ||
exports.ALL = ALL; | ||
exports.Core = Core; | ||
return exports; | ||
}({})); |
235
dist/core.js
@@ -1,53 +0,220 @@ | ||
/* @eroc/core v0.15.0 2019-08-20T20:44:23.782Z licensed MIT */ | ||
import EventEmitter from 'event-e3'; | ||
/* @eroc/core v0.14.0 2019-07-22T22:25:10.964Z licensed MIT */ | ||
/** | ||
* Handles error messages | ||
* | ||
* @method err | ||
* @param {string} error the type of the error | ||
* @param {function} message the complementary message to the error | ||
*/ | ||
const err = function (error, message) { | ||
console.error(`${messages[error]}: "${message}"`); | ||
}; | ||
const Core = class { | ||
const messages = { | ||
'!start': `Could not start the given module, it's either already started and is not facory`, | ||
'!stop': `Could not stop the given module, it's either already stopped or is not registered`, | ||
'!!module': `Can't register an already registered module`, | ||
'!!listen': `There's already an listen handler to the notification` | ||
}; | ||
/** | ||
* All notifications from sandbox | ||
* | ||
* @private | ||
*/ | ||
const notifications = {}; | ||
/** | ||
* The constructor of Sandbox | ||
* | ||
* @class Sandbox | ||
* @constructor | ||
*/ | ||
const Sandbox = class { | ||
constructor(module) { | ||
this.module = module; | ||
} | ||
/** | ||
* Notifies other modules from an specific notification | ||
* | ||
* @method notify | ||
* @param {object} notification the object with notifications configs | ||
*/ | ||
notify (notification) { | ||
Object.values(notifications).forEach(listener => { | ||
const listening = listener[notification.type]; | ||
if (listening) { | ||
listening.callback.call(listening.context, notification.data); | ||
} | ||
}); | ||
} | ||
/** | ||
* Makes a module listen to an specific notification | ||
* | ||
* @method listen | ||
* @param {string | array} notification the notification that the module will be listening to | ||
*/ | ||
listen(notification) { | ||
if (!Array.isArray(notification)) { | ||
return this.addNotification.apply(this, arguments); | ||
} | ||
const args = Array.from(arguments); | ||
notification.forEach(aNotification => { | ||
args[0] = aNotification; | ||
this.addNotification.apply(this, args); | ||
}); | ||
} | ||
/** | ||
* Adds the module listener to the notifications configuration | ||
* | ||
* @method addNotification | ||
* @param {string} notification the name of the notification | ||
* @param {function} callback the callback that will be triggered when the notification is called | ||
* @param {object} context the value of "this" | ||
* @param {boolean} replace if the notification already exists, it forces to rewrite it | ||
*/ | ||
addNotification(notification, callback, context, replace) { | ||
let addNotification = false; | ||
if (!notifications[this.module] || !notifications[this.module][notification]) { | ||
addNotification = true; | ||
} else if (replace) { | ||
addNotification = true; | ||
} else { | ||
err('!!listen', notification); | ||
} | ||
if (addNotification) { | ||
notifications[this.module] = notifications[this.module] || {}; | ||
notifications[this.module][notification] = { | ||
callback: callback, | ||
context: context || undefined | ||
}; | ||
} | ||
} | ||
}; | ||
/** | ||
* Clear all notifications from an specific module | ||
* | ||
* @method clearNotifications | ||
* @param {string} module the name of the module | ||
*/ | ||
Sandbox.clearNotifications = function(module) { | ||
delete notifications[module]; | ||
}; | ||
/** | ||
* The constructor of Core | ||
* | ||
* @class CoreClass | ||
* @constructor | ||
*/ | ||
const CoreClass = class { | ||
constructor() { | ||
this.modules = {}; | ||
this.moduleInstances = {}; | ||
} | ||
register() { | ||
/** | ||
* Registers a new module | ||
* | ||
* @method register | ||
* @param {string} module the name of the new module | ||
* @param {function} constructor the constructor of the new module | ||
*/ | ||
register (module, constructor, factory = false) { | ||
if (this.modules[module]) { | ||
err('!!module', module); | ||
return false; | ||
} | ||
this.modules[module] = { | ||
constructor, | ||
factory | ||
}; | ||
}; | ||
start(module, { name = Symbol() } = {}) { | ||
const emitter = new EventEmitter(); | ||
/** | ||
* Starts a registered module, if no module is passed, it starts all modules | ||
* | ||
* @method start | ||
* @param {string} moduleName | ||
* @param {string|undefined} alias | ||
*/ | ||
start(moduleName, alias = moduleName) { | ||
if (!moduleName) { | ||
Object.keys(this.modules).forEach(moduleName => { | ||
if (!this.moduleInstances[moduleName]) { | ||
this.start(moduleName); | ||
} | ||
}); | ||
return; | ||
} | ||
// emulate emitter.on(ANY, (name, data) => { | ||
emitter.emit = (name, data) => { | ||
EventEmitter.prototype.call(emitter, name); | ||
Object.values(this.moduleInstances).forEach(({ emitter }) => { | ||
emitter.emit(name, data); | ||
const moduleWrapper = this.modules[moduleName]; | ||
if (!moduleWrapper) { | ||
console.error(`Could not start ${moduleName}, it must be registered first`); | ||
return false; | ||
} | ||
if (this.moduleInstances[alias] && !moduleWrapper.factory) { | ||
err('!start', moduleName); | ||
return false; | ||
} | ||
const instance = new moduleWrapper.constructor(new Sandbox(alias)); | ||
this.moduleInstances[alias] = instance; | ||
if (instance.init) { | ||
return instance.init(); | ||
} | ||
return true; | ||
} | ||
/** | ||
* Stops a registered module | ||
* | ||
* @method start | ||
* @param {string} module the name of the module | ||
*/ | ||
stop (moduleName) { | ||
if (!moduleName) { | ||
Object.keys(this.moduleInstances).forEach(alias => { | ||
this.stop(alias); | ||
}); | ||
}; | ||
return; | ||
} | ||
this.moduleInstances[name] = { | ||
module, | ||
instance: module.start(emitter), | ||
name, | ||
emitter, | ||
}; | ||
const instance = this.moduleInstances[moduleName]; | ||
return name; | ||
if (!instance) { | ||
//err('!stop', module); | ||
return false; | ||
} | ||
stop(name) { | ||
const wrapper = this.moduleInstances[name]; | ||
let stopReturn; | ||
if (instance.destroy) { | ||
stopReturn = instance.destroy(); | ||
} | ||
if (!wrapper) { | ||
//err('!stop', module); | ||
return false; | ||
} | ||
delete this.moduleInstances[moduleName]; | ||
wrapper.emitter.off(); | ||
if (wrapper.module.stop) { | ||
wrapper.module.stop(wrapper.instance); | ||
} | ||
Sandbox.clearNotifications(moduleName); | ||
delete this.moduleInstances[name]; | ||
return stopReturn; | ||
} | ||
return true; | ||
} | ||
}; | ||
export { Core }; | ||
const Core = new CoreClass(); | ||
export { Core, CoreClass }; |
@@ -1,214 +0,218 @@ | ||
/* @eroc/core v0.15.0 2019-08-20T22:53:14.240Z licensed MIT */ | ||
/* @eroc/core v1.0.0 2019-08-22T20:03:48.440Z licensed MIT */ | ||
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : | ||
typeof define === 'function' && define.amd ? define('Core', ['exports'], factory) : | ||
(global = global || self, factory(global.Core = {})); | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : | ||
typeof define === 'function' && define.amd ? define('Core', ['exports'], factory) : | ||
(global = global || self, factory(global.Core = {})); | ||
}(this, function (exports) { 'use strict'; | ||
/** | ||
* Use it as a constructor | ||
* or as a decorator for an existing object | ||
* or as a base class for extend | ||
* cannot be used as a mixin for a constructor's prototype | ||
*/ | ||
function EventEmitter3(obj) { | ||
(obj || this)._callbacks = Object.create(null); | ||
if (obj) return Object.assign(obj, EventEmitter3.prototype); | ||
} | ||
/** | ||
* Listen on the given `eventName` with `fn` | ||
* | ||
* @param {String | Symbol} eventName | ||
* @param {Function} fn | ||
* @api public | ||
*/ | ||
/** | ||
* Use it as a constructor | ||
* or as a decorator for an existing object | ||
* or as a base class for extend | ||
* cannot be used as a mixin for a constructor's prototype | ||
*/ | ||
function EventEmitter3(obj) { | ||
(obj || this)._callbacks = Object.create(null); | ||
if (obj) return Object.assign(obj, EventEmitter3.prototype); | ||
} | ||
/** | ||
* Listen on the given `eventName` with `fn` | ||
* | ||
* @param {String | Symbol} eventName | ||
* @param {Function} fn | ||
* @api public | ||
*/ | ||
EventEmitter3.prototype.on = function (eventName, fn) { | ||
(this._callbacks[eventName] = this._callbacks[eventName] || []) | ||
.push(fn); | ||
}; | ||
EventEmitter3.prototype.on = function (eventName, fn) { | ||
(this._callbacks[eventName] = this._callbacks[eventName] || []) | ||
.push(fn); | ||
}; | ||
/** | ||
* Adds an `eventName` listener that will be invoked once then removed | ||
* | ||
* @param {String | Symbol} eventName | ||
* @param {Function} fn | ||
* @api public | ||
*/ | ||
/** | ||
* Adds an `eventName` listener that will be invoked once then removed | ||
* | ||
* @param {String | Symbol} eventName | ||
* @param {Function} fn | ||
* @api public | ||
*/ | ||
EventEmitter3.prototype.once = function (eventName, fn) { | ||
const once = (data) => { | ||
this.off(eventName, once); | ||
fn(data); | ||
}; | ||
EventEmitter3.prototype.once = function (eventName, fn) { | ||
const once = (data) => { | ||
this.off(eventName, once); | ||
fn(data); | ||
}; | ||
once.fn = fn; // makes it possible to remove with off | ||
this.on(eventName, once); | ||
}; | ||
once.fn = fn; // makes it possible to remove with off | ||
this.on(eventName, once); | ||
}; | ||
/** | ||
* Remove a callback for `eventName` or | ||
* all callbacks for `eventName` or | ||
* all callbacks for all events | ||
* | ||
* @param {String | Symbol} eventName | ||
* @param {Function} fn | ||
* @api public | ||
*/ | ||
/** | ||
* Remove a callback for `eventName` or | ||
* all callbacks for `eventName` or | ||
* all callbacks for all events | ||
* | ||
* @param {String | Symbol} eventName | ||
* @param {Function} fn | ||
* @api public | ||
*/ | ||
EventEmitter3.prototype.off = function (eventName, fn) { | ||
// all | ||
if (!eventName) { | ||
this._callbacks = Object.create(null); | ||
return; | ||
} | ||
EventEmitter3.prototype.off = function (eventName, fn) { | ||
// all | ||
if (!eventName) { | ||
this._callbacks = Object.create(null); | ||
return; | ||
} | ||
// specific event | ||
const callbacks = this._callbacks[eventName]; | ||
if (!callbacks) { | ||
return; | ||
} | ||
// specific event | ||
const callbacks = this._callbacks[eventName]; | ||
if (!callbacks) { | ||
return; | ||
} | ||
// remove all handlers | ||
if (!fn) { | ||
delete this._callbacks[eventName]; | ||
return; | ||
} | ||
// remove all handlers | ||
if (!fn) { | ||
delete this._callbacks[eventName]; | ||
return; | ||
} | ||
// remove specific handler | ||
const index = callbacks.findIndex(function (cb) { | ||
return (cb === fn || cb.fn === fn) | ||
}); | ||
if (index > -1) { | ||
// Remove event specific arrays for the eventName type that no | ||
// one is subscribed for, to avoid memory leak. | ||
if (callbacks.length === 1) { | ||
delete this._callbacks[eventName]; | ||
} else { | ||
callbacks.splice(index, 1); | ||
} | ||
} | ||
}; | ||
// remove specific handler | ||
const index = callbacks.findIndex(function (cb) { | ||
return (cb === fn || cb.fn === fn) | ||
}); | ||
if (index > -1) { | ||
// Remove event specific arrays for the eventName type that no | ||
// one is subscribed for, to avoid memory leak. | ||
if (callbacks.length === 1) { | ||
delete this._callbacks[eventName]; | ||
} else { | ||
callbacks.splice(index, 1); | ||
} | ||
} | ||
}; | ||
/** | ||
* Emit `eventName` with data | ||
* | ||
* @param {String | Symbol} eventName | ||
* @param {any} data | ||
*/ | ||
/** | ||
* Emit `eventName` with data | ||
* | ||
* @param {String | Symbol} eventName | ||
* @param {any} data | ||
*/ | ||
EventEmitter3.prototype.emit = function (eventName, data) { | ||
const callbacks = this._callbacks[eventName]; | ||
if (!callbacks) { | ||
return; | ||
} | ||
const frozenCallbacks = Array.from(callbacks); | ||
frozenCallbacks.forEach(callback => { | ||
callback(data); | ||
}); | ||
}; | ||
EventEmitter3.prototype.emit = function (eventName, data) { | ||
const callbacks = this._callbacks[eventName]; | ||
if (!callbacks) { | ||
return; | ||
} | ||
const frozenCallbacks = Array.from(callbacks); | ||
frozenCallbacks.forEach(callback => { | ||
callback(data); | ||
}); | ||
}; | ||
/** | ||
* Return array of callbacks for `eventName` | ||
* | ||
* @param {String | Symbol} eventName | ||
* @return {Array} listeners | ||
* @api public | ||
*/ | ||
/** | ||
* Return array of callbacks for `eventName` | ||
* | ||
* @param {String | Symbol} eventName | ||
* @return {Array} listeners | ||
* @api public | ||
*/ | ||
EventEmitter3.prototype.listeners = function (eventName) { | ||
return this._callbacks[eventName] || []; | ||
}; | ||
EventEmitter3.prototype.listeners = function (eventName) { | ||
return this._callbacks[eventName] || []; | ||
}; | ||
/** | ||
* True if this emitter has `eventName` handlers | ||
* | ||
* @param {String | Symbol} eventName | ||
* @return {Boolean} | ||
* @api public | ||
*/ | ||
/** | ||
* True if this emitter has `eventName` handlers | ||
* | ||
* @param {String | Symbol} eventName | ||
* @return {Boolean} | ||
* @api public | ||
*/ | ||
EventEmitter3.prototype.hasListeners = function (eventName) { | ||
return Boolean(this.listeners(eventName).length); | ||
}; | ||
EventEmitter3.prototype.hasListeners = function (eventName) { | ||
return Boolean(this.listeners(eventName).length); | ||
}; | ||
/** | ||
* Returns an array of event names for which the emitter has registered listeners | ||
* | ||
* @return {Array <String || Symbol>} | ||
* @api public | ||
*/ | ||
EventEmitter3.prototype.eventNames = function () { | ||
return Reflect.ownKeys(this._callbacks); | ||
}; | ||
/** | ||
* Returns an array of event names for which the emitter has registered listeners | ||
* | ||
* @return {Array <String || Symbol>} | ||
* @api public | ||
*/ | ||
EventEmitter3.prototype.eventNames = function () { | ||
return Reflect.ownKeys(this._callbacks); | ||
}; | ||
/** | ||
* Returns an array of event anmes of type string | ||
* for which the emitter has registered listeners | ||
* | ||
* @return {Array <String>} | ||
* @api public | ||
*/ | ||
EventEmitter3.prototype.eventNamesStrings = function () { | ||
return Object.keys(this._callbacks); | ||
}; | ||
/** | ||
* Returns an array of event anmes of type string | ||
* for which the emitter has registered listeners | ||
* | ||
* @return {Array <String>} | ||
* @api public | ||
*/ | ||
EventEmitter3.prototype.eventNamesStrings = function () { | ||
return Object.keys(this._callbacks); | ||
}; | ||
const ALL = Symbol(); | ||
const ALL = Symbol(); | ||
const Core = class { | ||
constructor() { | ||
this.moduleInstances = new Map(); | ||
EventEmitter3(this); | ||
} | ||
const Core = class { | ||
constructor() { | ||
this.moduleInstances = new Map(); | ||
EventEmitter3(this); | ||
} | ||
register() { | ||
register() { | ||
}; | ||
}; | ||
start(module, { name = Symbol() } = {}) { | ||
const emitter = new EventEmitter3(); | ||
start(module, { name = Symbol() } = {}) { | ||
if (this.moduleInstances.has(name)) { | ||
throw `module with name ${name} already started`; | ||
} | ||
// emulate emitter.on(ANY, (name, data) => { | ||
emitter.emit = (name, data) => { | ||
this.emit(name, data); | ||
this.emit(ALL, { name, data }); | ||
this.moduleInstances.forEach(({ emitter }) => { | ||
EventEmitter3.prototype.emit.call(emitter, name, data); | ||
}); | ||
}; | ||
const emitter = new EventEmitter3(); | ||
this.moduleInstances.set(name, { | ||
module, | ||
instance: module.start(emitter), | ||
name, | ||
emitter, | ||
}); | ||
// emulate emitter.on(ANY, (name, data) => { | ||
emitter.emit = (name, data) => { | ||
this.emit(name, data); | ||
this.emit(ALL, { name, data }); | ||
this.moduleInstances.forEach(({ emitter }) => { | ||
EventEmitter3.prototype.emit.call(emitter, name, data); | ||
}); | ||
}; | ||
return name; | ||
} | ||
this.moduleInstances.set(name, { | ||
module, | ||
instance: module.start(emitter), | ||
name, | ||
emitter, | ||
}); | ||
stop(name) { | ||
const wrapper = this.moduleInstances.get(name); | ||
return name; | ||
} | ||
if (!wrapper) { | ||
//err('!stop', module); | ||
return false; | ||
} | ||
stop(name) { | ||
const wrapper = this.moduleInstances.get(name); | ||
wrapper.emitter.off(); | ||
if (wrapper.module.stop) { | ||
wrapper.module.stop(wrapper.instance); | ||
} | ||
if (!wrapper) { | ||
//err('!stop', module); | ||
return false; | ||
} | ||
this.moduleInstances.delete(name); | ||
wrapper.emitter.off(); | ||
if (wrapper.module.stop) { | ||
wrapper.module.stop(wrapper.instance); | ||
} | ||
return true; | ||
} | ||
}; | ||
this.moduleInstances.delete(name); | ||
exports.ALL = ALL; | ||
exports.Core = Core; | ||
return true; | ||
} | ||
}; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
exports.ALL = ALL; | ||
exports.Core = Core; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
})); |
{ | ||
"name": "@eroc/core", | ||
"version": "0.15.0", | ||
"version": "1.0.0", | ||
"main": "dist/core.umd.js", | ||
@@ -5,0 +5,0 @@ "module": "dist/core.es.js", |
@@ -186,3 +186,3 @@ # core [![Build Status](https://travis-ci.org/mauriciosoares/core.js.svg?branch=master)](https://travis-ci.org/mauriciosoares/core.js) [![Coverage Status](https://img.shields.io/coveralls/mauriciosoares/core.js.svg)](https://coveralls.io/r/mauriciosoares/core.js) [![Code Climate](https://codeclimate.com/github/mauriciosoares/core.js/badges/gpa.svg)](https://codeclimate.com/github/mauriciosoares/core.js) | ||
## Release History | ||
## Changelog | ||
@@ -189,0 +189,0 @@ * 0.15.0 major architecture change |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
32585
771
0
0
8