@eroc/core
Advanced tools
Comparing version 2.0.0 to 2.1.0
@@ -1,2 +0,66 @@ | ||
/* @eroc/core v2.0.0 2019-11-16T15:46:54.617Z licensed MIT */ | ||
/* @eroc/core v2.1.0 2020-01-23T22:07:50.616Z licensed MIT */ | ||
const startEventRecorder = (core) => { | ||
const events = []; | ||
const listener = event => { | ||
events.push(event); | ||
}; | ||
core.on(ALL, listener); | ||
return { | ||
events, | ||
listener, | ||
}; | ||
}; | ||
const stopEventRecorder = (core, eventRecorder) => { | ||
if (!eventRecorder) { | ||
return; | ||
} | ||
const { | ||
events, | ||
listener, | ||
} = eventRecorder; | ||
core.off(ALL, listener); | ||
}; | ||
const replayEvents = (core, events, options = {}) => { | ||
if (!events.length) { | ||
return; | ||
} | ||
const { sameSpeed, pauseEmits = true } = options; | ||
if (pauseEmits) { | ||
core.paused = true; | ||
} | ||
if (sameSpeed) { | ||
replayEventsSameSpeed(core, events); | ||
} else { | ||
replayEventsInstantly(core, events); | ||
} | ||
}; | ||
const replayEventsInstantly = (core, events) => { | ||
events.forEach(event => { | ||
core.moduleEmitDirect(event.name, event.data); | ||
}); | ||
core.paused = false; | ||
}; | ||
const replayEventsSameSpeed = (core, events) => { | ||
const { length } = events; | ||
let i = 0; | ||
const playNext = () => { | ||
const event = events[i]; | ||
core.moduleEmitDirect(event.name, event.data); | ||
i += 1; | ||
if (i < length) { | ||
const timeDifference = events[i].time - event.time; | ||
setTimeout(playNext, timeDifference); | ||
return; | ||
} | ||
core.paused = false; | ||
}; | ||
playNext(); | ||
}; | ||
/** | ||
@@ -247,2 +311,2 @@ * Use it as a constructor | ||
export { ALL, Core, ERROR }; | ||
export { ALL, Core, ERROR, replayEvents, startEventRecorder, stopEventRecorder }; |
@@ -1,5 +0,69 @@ | ||
/* @eroc/core v2.0.0 2019-11-16T15:46:54.617Z licensed MIT */ | ||
/* @eroc/core v2.1.0 2020-01-23T22:07:50.616Z licensed MIT */ | ||
var Core = (function (exports) { | ||
'use strict'; | ||
const startEventRecorder = (core) => { | ||
const events = []; | ||
const listener = event => { | ||
events.push(event); | ||
}; | ||
core.on(ALL, listener); | ||
return { | ||
events, | ||
listener, | ||
}; | ||
}; | ||
const stopEventRecorder = (core, eventRecorder) => { | ||
if (!eventRecorder) { | ||
return; | ||
} | ||
const { | ||
events, | ||
listener, | ||
} = eventRecorder; | ||
core.off(ALL, listener); | ||
}; | ||
const replayEvents = (core, events, options = {}) => { | ||
if (!events.length) { | ||
return; | ||
} | ||
const { sameSpeed, pauseEmits = true } = options; | ||
if (pauseEmits) { | ||
core.paused = true; | ||
} | ||
if (sameSpeed) { | ||
replayEventsSameSpeed(core, events); | ||
} else { | ||
replayEventsInstantly(core, events); | ||
} | ||
}; | ||
const replayEventsInstantly = (core, events) => { | ||
events.forEach(event => { | ||
core.moduleEmitDirect(event.name, event.data); | ||
}); | ||
core.paused = false; | ||
}; | ||
const replayEventsSameSpeed = (core, events) => { | ||
const { length } = events; | ||
let i = 0; | ||
const playNext = () => { | ||
const event = events[i]; | ||
core.moduleEmitDirect(event.name, event.data); | ||
i += 1; | ||
if (i < length) { | ||
const timeDifference = events[i].time - event.time; | ||
setTimeout(playNext, timeDifference); | ||
return; | ||
} | ||
core.paused = false; | ||
}; | ||
playNext(); | ||
}; | ||
/** | ||
@@ -253,2 +317,5 @@ * Use it as a constructor | ||
exports.ERROR = ERROR; | ||
exports.replayEvents = replayEvents; | ||
exports.startEventRecorder = startEventRecorder; | ||
exports.stopEventRecorder = stopEventRecorder; | ||
@@ -255,0 +322,0 @@ 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,2 +0,2 @@ | ||
/* @eroc/core v2.0.0 2019-11-16T15:46:54.617Z licensed MIT */ | ||
/* @eroc/core v2.1.0 2020-01-23T22:07:50.616Z licensed MIT */ | ||
(function (global, factory) { | ||
@@ -8,2 +8,66 @@ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : | ||
const startEventRecorder = (core) => { | ||
const events = []; | ||
const listener = event => { | ||
events.push(event); | ||
}; | ||
core.on(ALL, listener); | ||
return { | ||
events, | ||
listener, | ||
}; | ||
}; | ||
const stopEventRecorder = (core, eventRecorder) => { | ||
if (!eventRecorder) { | ||
return; | ||
} | ||
const { | ||
events, | ||
listener, | ||
} = eventRecorder; | ||
core.off(ALL, listener); | ||
}; | ||
const replayEvents = (core, events, options = {}) => { | ||
if (!events.length) { | ||
return; | ||
} | ||
const { sameSpeed, pauseEmits = true } = options; | ||
if (pauseEmits) { | ||
core.paused = true; | ||
} | ||
if (sameSpeed) { | ||
replayEventsSameSpeed(core, events); | ||
} else { | ||
replayEventsInstantly(core, events); | ||
} | ||
}; | ||
const replayEventsInstantly = (core, events) => { | ||
events.forEach(event => { | ||
core.moduleEmitDirect(event.name, event.data); | ||
}); | ||
core.paused = false; | ||
}; | ||
const replayEventsSameSpeed = (core, events) => { | ||
const { length } = events; | ||
let i = 0; | ||
const playNext = () => { | ||
const event = events[i]; | ||
core.moduleEmitDirect(event.name, event.data); | ||
i += 1; | ||
if (i < length) { | ||
const timeDifference = events[i].time - event.time; | ||
setTimeout(playNext, timeDifference); | ||
return; | ||
} | ||
core.paused = false; | ||
}; | ||
playNext(); | ||
}; | ||
/** | ||
@@ -257,2 +321,5 @@ * Use it as a constructor | ||
exports.ERROR = ERROR; | ||
exports.replayEvents = replayEvents; | ||
exports.startEventRecorder = startEventRecorder; | ||
exports.stopEventRecorder = stopEventRecorder; | ||
@@ -259,0 +326,0 @@ Object.defineProperty(exports, '__esModule', { value: true }); |
{ | ||
"name": "@eroc/core", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"main": "dist/core.umd.js", | ||
@@ -5,0 +5,0 @@ "module": "dist/core.es.js", |
@@ -213,2 +213,6 @@ # 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) | ||
### 2.1.0 | ||
* eventPlayer, eventRecorder optionals are importable directly from the core | ||
### 2.0.0 | ||
@@ -215,0 +219,0 @@ |
export { Core, ALL, ERROR }; | ||
export { startEventRecorder, stopEventRecorder } from "./eventRecorder.js"; | ||
export { replayEvents } from "./eventPlayer.js"; | ||
import EventEmitter from "../node_modules/event-e3/event-e3.js"; | ||
@@ -3,0 +5,0 @@ |
@@ -14,10 +14,17 @@ export { replayEvents }; | ||
if (!sameSpeed) { | ||
events.forEach(event => { | ||
core.moduleEmitDirect(event.name, event.data); | ||
}); | ||
core.paused = false; | ||
return; | ||
if (sameSpeed) { | ||
replayEventsSameSpeed(core, events); | ||
} else { | ||
replayEventsInstantly(core, events); | ||
} | ||
}; | ||
const replayEventsInstantly = (core, events) => { | ||
events.forEach(event => { | ||
core.moduleEmitDirect(event.name, event.data); | ||
}); | ||
core.paused = false; | ||
}; | ||
const replayEventsSameSpeed = (core, events) => { | ||
const { length } = events; | ||
@@ -24,0 +31,0 @@ let i = 0; |
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
46468
1168
262