@eroc/core
Advanced tools
Comparing version 3.3.0 to 4.0.0
@@ -1,2 +0,2 @@ | ||
/* @eroc/core v3.3.0 2021-08-16T18:31:42.516Z licensed MIT */ | ||
/* @eroc/core v4.0.0 2021-08-17T18:13:54.055Z licensed MIT */ | ||
const startEventRecorder = (core) => { | ||
@@ -69,10 +69,8 @@ const events = []; | ||
// listen for all events | ||
core.on(ALL, ({ name, data, time }) => { | ||
const timeString = new Date(time).toISOString(); | ||
logger.debug(`${timeString} event ${String(name)} data`, data); | ||
core.on(ALL, ({ name, data/*, time*/ }) => { | ||
logger.debug(`event ${String(name)} data`, data); | ||
}); | ||
core.on(ERROR, ({ time, phase, error }) => { | ||
const timeString = new Date(time).toISOString(); | ||
core.on(ERROR, ({ phase, error/*, time*/ }) => { | ||
console.error(`Error during phase ${phase} at ${timeString}`, error); | ||
@@ -307,149 +305,149 @@ }); | ||
const Core = class { | ||
constructor() { | ||
this.paused = false; | ||
this.moduleInstances = new Map(); | ||
this.boundModuleEmit = this.moduleEmit.bind(this); | ||
EventEmitter3(this); | ||
} | ||
const createCore = function () { | ||
const core = {}; | ||
getState(name) { | ||
if (!this.moduleInstances.has(name)) { | ||
return Promise.reject(`module with name ${name} does not exist`); | ||
} | ||
core.paused = false; | ||
core.moduleInstances = new Map(); | ||
EventEmitter3(core); | ||
return Object.assign(core, { | ||
getState(name) { | ||
if (!core.moduleInstances.has(name)) { | ||
return Promise.reject(`module with name ${name} does not exist`); | ||
} | ||
const wrapper = this.moduleInstances.get(name); | ||
if (!wrapper.module.getState) { | ||
return Promise.resolve({}); | ||
} | ||
return Promise.resolve().then(() => { | ||
return wrapper.module.getState(wrapper.instance); | ||
}); | ||
} | ||
const wrapper = core.moduleInstances.get(name); | ||
if (!wrapper.module.getState) { | ||
return Promise.resolve({}); | ||
} | ||
return Promise.resolve().then(() => { | ||
return wrapper.module.getState(wrapper.instance); | ||
}); | ||
}, | ||
/* returns a promise with an object | ||
as keys the names of the module instances | ||
as value the state received */ | ||
getAllStates() { | ||
const promises = []; | ||
const names = []; // freeze names in case something is added ore removed while the promise is being resolved | ||
this.moduleInstances.forEach((wrapper, name) => { | ||
promises.push(this.getState(name)); | ||
names.push(name); | ||
}); | ||
/* returns a promise with an object | ||
as keys the names of the module instances | ||
as value the state received */ | ||
getAllStates() { | ||
const promises = []; | ||
const names = []; // freeze names in case something is added ore removed while the promise is being resolved | ||
core.moduleInstances.forEach((wrapper, name) => { | ||
promises.push(core.getState(name)); | ||
names.push(name); | ||
}); | ||
return Promise.all(promises).then((results) => { | ||
// Promise.all preserves order | ||
const resultsAsObject = {}; | ||
results.forEach((result, i) => { | ||
resultsAsObject[names[i]] = deepCopyAdded(result); | ||
return Promise.all(promises).then((results) => { | ||
// Promise.all preserves order | ||
const resultsAsObject = {}; | ||
results.forEach((result, i) => { | ||
resultsAsObject[names[i]] = deepCopyAdded(result); | ||
}); | ||
return resultsAsObject; | ||
}); | ||
return resultsAsObject; | ||
}); | ||
} | ||
}, | ||
restoreState (name, state) { | ||
if (!this.moduleInstances.has(name)) { | ||
return Promise.reject(`module with name ${name} does not exist`); | ||
} | ||
restoreState (name, state) { | ||
if (!core.moduleInstances.has(name)) { | ||
return Promise.reject(`module with name ${name} does not exist`); | ||
} | ||
const wrapper = this.moduleInstances.get(name); | ||
if (!wrapper.module.restoreState) { | ||
return Promise.resolve(); | ||
} | ||
return Promise.resolve().then(() => { | ||
const stateCopy = deepCopyAdded(state); // avoid mutations | ||
return wrapper.module.restoreState(wrapper.instance, stateCopy); | ||
}); | ||
const wrapper = core.moduleInstances.get(name); | ||
if (!wrapper.module.restoreState) { | ||
return Promise.resolve(); | ||
} | ||
return Promise.resolve().then(() => { | ||
const stateCopy = deepCopyAdded(state); // avoid mutations | ||
return wrapper.module.restoreState(wrapper.instance, stateCopy); | ||
}); | ||
} | ||
}, | ||
restoreAllStates(states) { | ||
return Promise.all(Object.entries(states).map(([name, state]) => { | ||
return this.restoreState(name, state); | ||
})); | ||
} | ||
restoreAllStates(states) { | ||
return Promise.all(Object.entries(states).map(([name, state]) => { | ||
return core.restoreState(name, state); | ||
})); | ||
}, | ||
start(module, { name = Symbol(), data = undefined } = {}) { | ||
if (this.moduleInstances.has(name)) { | ||
return Promise.reject(`module with name ${name} already started`); | ||
} | ||
start(module, { name = Symbol(), data = undefined } = {}) { | ||
if (core.moduleInstances.has(name)) { | ||
return Promise.reject(`module with name ${name} already started`); | ||
} | ||
if (!module.start) { | ||
return Promise.reject(`module must have start defined`); | ||
} | ||
if (!module.start) { | ||
return Promise.reject(`module must have start defined`); | ||
} | ||
const emitter = new EventEmitter3(); | ||
const emitter = new EventEmitter3(); | ||
emitter.emit = this.boundModuleEmit; | ||
emitter.emit = core.moduleEmit; | ||
return Promise.resolve().then(() => { | ||
return module.start(emitter, data); | ||
}).then(instance => { | ||
this.moduleInstances.set(name, { | ||
module, | ||
instance, | ||
name, | ||
emitter, | ||
return Promise.resolve().then(() => { | ||
return module.start(emitter, data); | ||
}).then(instance => { | ||
core.moduleInstances.set(name, { | ||
module, | ||
instance, | ||
name, | ||
emitter, | ||
}); | ||
}).then(() => { | ||
return name; | ||
}).catch(errorModuleStart => { | ||
core.emit(ERROR, { | ||
time: Date.now(), | ||
phase: `module.start`, | ||
error: errorModuleStart, | ||
}); | ||
}); | ||
}).then(() => { | ||
return name; | ||
}).catch(errorModuleStart => { | ||
this.emit(ERROR, { | ||
time: Date.now(), | ||
phase: `module.start`, | ||
error: errorModuleStart, | ||
}); | ||
}); | ||
} | ||
}, | ||
stop(name) { | ||
const wrapper = this.moduleInstances.get(name); | ||
stop(name) { | ||
const wrapper = core.moduleInstances.get(name); | ||
if (!wrapper) { | ||
return Promise.resolve(false); | ||
} | ||
wrapper.emitter.off(); | ||
return Promise.resolve().then(() => { | ||
this.moduleInstances.delete(name); | ||
if (wrapper.module.stop) { | ||
wrapper.module.stop(wrapper.instance); | ||
if (!wrapper) { | ||
return Promise.resolve(false); | ||
} | ||
}).catch(errorModuleStop => { | ||
this.emit(ERROR, { | ||
time: Date.now(), | ||
phase: `module.stop`, | ||
error: errorModuleStop, | ||
}); | ||
}).then(() => { | ||
return true; | ||
}); | ||
} | ||
moduleEmit(name, data) { | ||
if (this.paused) { | ||
return; | ||
} | ||
this.moduleEmitDirect(name, data); | ||
} | ||
wrapper.emitter.off(); | ||
moduleEmitDirect(name, data) { | ||
this.emit(name, data); | ||
this.emit(ALL, { name, data, time: Date.now() }); | ||
this.moduleInstances.forEach(({ emitter }) => { | ||
try { | ||
EventEmitter3.prototype.emit.call(emitter, name, data); | ||
} catch (error) { | ||
this.emit(ERROR, { | ||
return Promise.resolve().then(() => { | ||
core.moduleInstances.delete(name); | ||
if (wrapper.module.stop) { | ||
wrapper.module.stop(wrapper.instance); | ||
} | ||
}).catch(errorModuleStop => { | ||
core.emit(ERROR, { | ||
time: Date.now(), | ||
phase: `module runtime (emitter.on)`, | ||
error, | ||
phase: `module.stop`, | ||
error: errorModuleStop, | ||
}); | ||
}).then(() => { | ||
return true; | ||
}); | ||
}, | ||
moduleEmit(name, data) { | ||
if (core.paused) { | ||
return; | ||
} | ||
}); | ||
} | ||
core.moduleEmitDirect(name, data); | ||
}, | ||
moduleEmitDirect(name, data) { | ||
core.emit(name, data); | ||
core.emit(ALL, { name, data, time: Date.now() }); | ||
core.moduleInstances.forEach(({ emitter }) => { | ||
try { | ||
EventEmitter3.prototype.emit.call(emitter, name, data); | ||
} catch (error) { | ||
core.emit(ERROR, { | ||
time: Date.now(), | ||
phase: `module runtime (emitter.on)`, | ||
error, | ||
}); | ||
} | ||
}); | ||
}, | ||
}); | ||
}; | ||
export { ALL, Core, ERROR, replayEvents, startEventRecorder, stopEventRecorder, useDefaultLogging }; | ||
export { ALL, ERROR, createCore, replayEvents, startEventRecorder, stopEventRecorder, useDefaultLogging }; |
@@ -1,2 +0,2 @@ | ||
/* @eroc/core v3.3.0 2021-08-16T18:31:42.516Z licensed MIT */ | ||
/* @eroc/core v4.0.0 2021-08-17T18:13:54.055Z licensed MIT */ | ||
var Core = (function (exports) { | ||
@@ -72,10 +72,8 @@ 'use strict'; | ||
// listen for all events | ||
core.on(ALL, ({ name, data, time }) => { | ||
const timeString = new Date(time).toISOString(); | ||
logger.debug(`${timeString} event ${String(name)} data`, data); | ||
core.on(ALL, ({ name, data/*, time*/ }) => { | ||
logger.debug(`event ${String(name)} data`, data); | ||
}); | ||
core.on(ERROR, ({ time, phase, error }) => { | ||
const timeString = new Date(time).toISOString(); | ||
core.on(ERROR, ({ phase, error/*, time*/ }) => { | ||
console.error(`Error during phase ${phase} at ${timeString}`, error); | ||
@@ -310,152 +308,152 @@ }); | ||
const Core = class { | ||
constructor() { | ||
this.paused = false; | ||
this.moduleInstances = new Map(); | ||
this.boundModuleEmit = this.moduleEmit.bind(this); | ||
EventEmitter3(this); | ||
} | ||
const createCore = function () { | ||
const core = {}; | ||
getState(name) { | ||
if (!this.moduleInstances.has(name)) { | ||
return Promise.reject(`module with name ${name} does not exist`); | ||
} | ||
core.paused = false; | ||
core.moduleInstances = new Map(); | ||
EventEmitter3(core); | ||
return Object.assign(core, { | ||
getState(name) { | ||
if (!core.moduleInstances.has(name)) { | ||
return Promise.reject(`module with name ${name} does not exist`); | ||
} | ||
const wrapper = this.moduleInstances.get(name); | ||
if (!wrapper.module.getState) { | ||
return Promise.resolve({}); | ||
} | ||
return Promise.resolve().then(() => { | ||
return wrapper.module.getState(wrapper.instance); | ||
}); | ||
} | ||
const wrapper = core.moduleInstances.get(name); | ||
if (!wrapper.module.getState) { | ||
return Promise.resolve({}); | ||
} | ||
return Promise.resolve().then(() => { | ||
return wrapper.module.getState(wrapper.instance); | ||
}); | ||
}, | ||
/* returns a promise with an object | ||
as keys the names of the module instances | ||
as value the state received */ | ||
getAllStates() { | ||
const promises = []; | ||
const names = []; // freeze names in case something is added ore removed while the promise is being resolved | ||
this.moduleInstances.forEach((wrapper, name) => { | ||
promises.push(this.getState(name)); | ||
names.push(name); | ||
}); | ||
/* returns a promise with an object | ||
as keys the names of the module instances | ||
as value the state received */ | ||
getAllStates() { | ||
const promises = []; | ||
const names = []; // freeze names in case something is added ore removed while the promise is being resolved | ||
core.moduleInstances.forEach((wrapper, name) => { | ||
promises.push(core.getState(name)); | ||
names.push(name); | ||
}); | ||
return Promise.all(promises).then((results) => { | ||
// Promise.all preserves order | ||
const resultsAsObject = {}; | ||
results.forEach((result, i) => { | ||
resultsAsObject[names[i]] = deepCopyAdded(result); | ||
return Promise.all(promises).then((results) => { | ||
// Promise.all preserves order | ||
const resultsAsObject = {}; | ||
results.forEach((result, i) => { | ||
resultsAsObject[names[i]] = deepCopyAdded(result); | ||
}); | ||
return resultsAsObject; | ||
}); | ||
return resultsAsObject; | ||
}); | ||
} | ||
}, | ||
restoreState (name, state) { | ||
if (!this.moduleInstances.has(name)) { | ||
return Promise.reject(`module with name ${name} does not exist`); | ||
} | ||
restoreState (name, state) { | ||
if (!core.moduleInstances.has(name)) { | ||
return Promise.reject(`module with name ${name} does not exist`); | ||
} | ||
const wrapper = this.moduleInstances.get(name); | ||
if (!wrapper.module.restoreState) { | ||
return Promise.resolve(); | ||
} | ||
return Promise.resolve().then(() => { | ||
const stateCopy = deepCopyAdded(state); // avoid mutations | ||
return wrapper.module.restoreState(wrapper.instance, stateCopy); | ||
}); | ||
const wrapper = core.moduleInstances.get(name); | ||
if (!wrapper.module.restoreState) { | ||
return Promise.resolve(); | ||
} | ||
return Promise.resolve().then(() => { | ||
const stateCopy = deepCopyAdded(state); // avoid mutations | ||
return wrapper.module.restoreState(wrapper.instance, stateCopy); | ||
}); | ||
} | ||
}, | ||
restoreAllStates(states) { | ||
return Promise.all(Object.entries(states).map(([name, state]) => { | ||
return this.restoreState(name, state); | ||
})); | ||
} | ||
restoreAllStates(states) { | ||
return Promise.all(Object.entries(states).map(([name, state]) => { | ||
return core.restoreState(name, state); | ||
})); | ||
}, | ||
start(module, { name = Symbol(), data = undefined } = {}) { | ||
if (this.moduleInstances.has(name)) { | ||
return Promise.reject(`module with name ${name} already started`); | ||
} | ||
start(module, { name = Symbol(), data = undefined } = {}) { | ||
if (core.moduleInstances.has(name)) { | ||
return Promise.reject(`module with name ${name} already started`); | ||
} | ||
if (!module.start) { | ||
return Promise.reject(`module must have start defined`); | ||
} | ||
if (!module.start) { | ||
return Promise.reject(`module must have start defined`); | ||
} | ||
const emitter = new EventEmitter3(); | ||
const emitter = new EventEmitter3(); | ||
emitter.emit = this.boundModuleEmit; | ||
emitter.emit = core.moduleEmit; | ||
return Promise.resolve().then(() => { | ||
return module.start(emitter, data); | ||
}).then(instance => { | ||
this.moduleInstances.set(name, { | ||
module, | ||
instance, | ||
name, | ||
emitter, | ||
return Promise.resolve().then(() => { | ||
return module.start(emitter, data); | ||
}).then(instance => { | ||
core.moduleInstances.set(name, { | ||
module, | ||
instance, | ||
name, | ||
emitter, | ||
}); | ||
}).then(() => { | ||
return name; | ||
}).catch(errorModuleStart => { | ||
core.emit(ERROR, { | ||
time: Date.now(), | ||
phase: `module.start`, | ||
error: errorModuleStart, | ||
}); | ||
}); | ||
}).then(() => { | ||
return name; | ||
}).catch(errorModuleStart => { | ||
this.emit(ERROR, { | ||
time: Date.now(), | ||
phase: `module.start`, | ||
error: errorModuleStart, | ||
}); | ||
}); | ||
} | ||
}, | ||
stop(name) { | ||
const wrapper = this.moduleInstances.get(name); | ||
stop(name) { | ||
const wrapper = core.moduleInstances.get(name); | ||
if (!wrapper) { | ||
return Promise.resolve(false); | ||
} | ||
wrapper.emitter.off(); | ||
return Promise.resolve().then(() => { | ||
this.moduleInstances.delete(name); | ||
if (wrapper.module.stop) { | ||
wrapper.module.stop(wrapper.instance); | ||
if (!wrapper) { | ||
return Promise.resolve(false); | ||
} | ||
}).catch(errorModuleStop => { | ||
this.emit(ERROR, { | ||
time: Date.now(), | ||
phase: `module.stop`, | ||
error: errorModuleStop, | ||
}); | ||
}).then(() => { | ||
return true; | ||
}); | ||
} | ||
moduleEmit(name, data) { | ||
if (this.paused) { | ||
return; | ||
} | ||
this.moduleEmitDirect(name, data); | ||
} | ||
wrapper.emitter.off(); | ||
moduleEmitDirect(name, data) { | ||
this.emit(name, data); | ||
this.emit(ALL, { name, data, time: Date.now() }); | ||
this.moduleInstances.forEach(({ emitter }) => { | ||
try { | ||
EventEmitter3.prototype.emit.call(emitter, name, data); | ||
} catch (error) { | ||
this.emit(ERROR, { | ||
return Promise.resolve().then(() => { | ||
core.moduleInstances.delete(name); | ||
if (wrapper.module.stop) { | ||
wrapper.module.stop(wrapper.instance); | ||
} | ||
}).catch(errorModuleStop => { | ||
core.emit(ERROR, { | ||
time: Date.now(), | ||
phase: `module runtime (emitter.on)`, | ||
error, | ||
phase: `module.stop`, | ||
error: errorModuleStop, | ||
}); | ||
}).then(() => { | ||
return true; | ||
}); | ||
}, | ||
moduleEmit(name, data) { | ||
if (core.paused) { | ||
return; | ||
} | ||
}); | ||
} | ||
core.moduleEmitDirect(name, data); | ||
}, | ||
moduleEmitDirect(name, data) { | ||
core.emit(name, data); | ||
core.emit(ALL, { name, data, time: Date.now() }); | ||
core.moduleInstances.forEach(({ emitter }) => { | ||
try { | ||
EventEmitter3.prototype.emit.call(emitter, name, data); | ||
} catch (error) { | ||
core.emit(ERROR, { | ||
time: Date.now(), | ||
phase: `module runtime (emitter.on)`, | ||
error, | ||
}); | ||
} | ||
}); | ||
}, | ||
}); | ||
}; | ||
exports.ALL = ALL; | ||
exports.Core = Core; | ||
exports.ERROR = ERROR; | ||
exports.createCore = createCore; | ||
exports.replayEvents = replayEvents; | ||
@@ -462,0 +460,0 @@ exports.startEventRecorder = startEventRecorder; |
{ | ||
"name": "@eroc/core", | ||
"version": "3.3.0", | ||
"version": "4.0.0", | ||
"description": "Lightweight framework for scalable applications", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -36,11 +36,11 @@ # 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) | ||
`import { Core, ALL, ERROR } from "./node_modules/@eroc/core/dist/core.es.js";` | ||
`import { createCore, ALL, ERROR } from "./node_modules/@eroc/core/dist/core.es.js";` | ||
With node, rollup, webpack or parcel | ||
`import { Core, ALL, ERROR } from "@eroc/core";` | ||
`import { createCore, ALL, ERROR } from "@eroc/core";` | ||
With old NodeJs or Browserify | ||
`const { Core, ALL, ERROR } = require("@eroc/core/dist/core.umd.cjs");` | ||
`const { createCore, ALL, ERROR } = require("@eroc/core/dist/core.umd.cjs");` | ||
@@ -89,6 +89,6 @@ | ||
```js | ||
import { Core } from "@eroc/core"; | ||
import { createCore } from "@eroc/core"; | ||
import * as exampleModule from "./exampleModule.js"; | ||
const core = new Core(); | ||
const core = createCore(); | ||
core.start(exampleModule); | ||
@@ -169,5 +169,5 @@ ``` | ||
### Core | ||
### createCore | ||
#### `new Core()` | ||
#### `createCore()` | ||
@@ -228,6 +228,6 @@ Returns a new instance of core. | ||
```js | ||
import { Core, useDefaultLogging } from "@eroc/core"; | ||
import { createCore, useDefaultLogging } from "@eroc/core"; | ||
const core = new Core(); | ||
const core = createCore(); | ||
@@ -251,6 +251,6 @@ // listen for all events | ||
```js | ||
import { Core, useDefaultLogging } from "@eroc/core"; | ||
import { createCore, useDefaultLogging } from "@eroc/core"; | ||
const core = new Core(); | ||
const core = createCore(); | ||
let eventRecording = startEventRecorder(core); | ||
@@ -270,6 +270,6 @@ stopEventRecorder(core, eventRecording); | ||
```js | ||
import { Core, replayEvents } from "@eroc/core"; | ||
import { createCore, replayEvents } from "@eroc/core"; | ||
const core = new Core(); | ||
const core = createCore(); | ||
// ... initialize modules | ||
@@ -318,2 +318,7 @@ const events = // get events | ||
### 4.0.0 | ||
* Core class is replaced with createCore function | ||
* The default logger no longer displays the time (you can create your own logger) | ||
### 3.3.0 | ||
@@ -320,0 +325,0 @@ |
246
src/core.js
@@ -1,2 +0,2 @@ | ||
export { Core, ALL, ERROR }; | ||
export { createCore, ALL, ERROR }; | ||
export { startEventRecorder, stopEventRecorder } from "./eventRecorder.js"; | ||
@@ -12,147 +12,147 @@ export { replayEvents } from "./eventPlayer.js"; | ||
const Core = class { | ||
constructor() { | ||
this.paused = false; | ||
this.moduleInstances = new Map(); | ||
this.boundModuleEmit = this.moduleEmit.bind(this); | ||
EventEmitter(this); | ||
} | ||
const createCore = function () { | ||
const core = {}; | ||
getState(name) { | ||
if (!this.moduleInstances.has(name)) { | ||
return Promise.reject(`module with name ${name} does not exist`); | ||
} | ||
core.paused = false; | ||
core.moduleInstances = new Map(); | ||
EventEmitter(core); | ||
return Object.assign(core, { | ||
getState(name) { | ||
if (!core.moduleInstances.has(name)) { | ||
return Promise.reject(`module with name ${name} does not exist`); | ||
} | ||
const wrapper = this.moduleInstances.get(name); | ||
if (!wrapper.module.getState) { | ||
return Promise.resolve({}); | ||
} | ||
return Promise.resolve().then(() => { | ||
return wrapper.module.getState(wrapper.instance); | ||
}); | ||
} | ||
const wrapper = core.moduleInstances.get(name); | ||
if (!wrapper.module.getState) { | ||
return Promise.resolve({}); | ||
} | ||
return Promise.resolve().then(() => { | ||
return wrapper.module.getState(wrapper.instance); | ||
}); | ||
}, | ||
/* returns a promise with an object | ||
as keys the names of the module instances | ||
as value the state received */ | ||
getAllStates() { | ||
const promises = []; | ||
const names = []; // freeze names in case something is added ore removed while the promise is being resolved | ||
this.moduleInstances.forEach((wrapper, name) => { | ||
promises.push(this.getState(name)); | ||
names.push(name); | ||
}); | ||
/* returns a promise with an object | ||
as keys the names of the module instances | ||
as value the state received */ | ||
getAllStates() { | ||
const promises = []; | ||
const names = []; // freeze names in case something is added ore removed while the promise is being resolved | ||
core.moduleInstances.forEach((wrapper, name) => { | ||
promises.push(core.getState(name)); | ||
names.push(name); | ||
}); | ||
return Promise.all(promises).then((results) => { | ||
// Promise.all preserves order | ||
const resultsAsObject = {}; | ||
results.forEach((result, i) => { | ||
resultsAsObject[names[i]] = deepCopyAdded(result); | ||
return Promise.all(promises).then((results) => { | ||
// Promise.all preserves order | ||
const resultsAsObject = {}; | ||
results.forEach((result, i) => { | ||
resultsAsObject[names[i]] = deepCopyAdded(result); | ||
}); | ||
return resultsAsObject; | ||
}); | ||
return resultsAsObject; | ||
}); | ||
} | ||
}, | ||
restoreState (name, state) { | ||
if (!this.moduleInstances.has(name)) { | ||
return Promise.reject(`module with name ${name} does not exist`); | ||
} | ||
restoreState (name, state) { | ||
if (!core.moduleInstances.has(name)) { | ||
return Promise.reject(`module with name ${name} does not exist`); | ||
} | ||
const wrapper = this.moduleInstances.get(name); | ||
if (!wrapper.module.restoreState) { | ||
return Promise.resolve(); | ||
} | ||
return Promise.resolve().then(() => { | ||
const stateCopy = deepCopyAdded(state); // avoid mutations | ||
return wrapper.module.restoreState(wrapper.instance, stateCopy); | ||
}); | ||
const wrapper = core.moduleInstances.get(name); | ||
if (!wrapper.module.restoreState) { | ||
return Promise.resolve(); | ||
} | ||
return Promise.resolve().then(() => { | ||
const stateCopy = deepCopyAdded(state); // avoid mutations | ||
return wrapper.module.restoreState(wrapper.instance, stateCopy); | ||
}); | ||
} | ||
}, | ||
restoreAllStates(states) { | ||
return Promise.all(Object.entries(states).map(([name, state]) => { | ||
return this.restoreState(name, state); | ||
})); | ||
} | ||
restoreAllStates(states) { | ||
return Promise.all(Object.entries(states).map(([name, state]) => { | ||
return core.restoreState(name, state); | ||
})); | ||
}, | ||
start(module, { name = Symbol(), data = undefined } = {}) { | ||
if (this.moduleInstances.has(name)) { | ||
return Promise.reject(`module with name ${name} already started`); | ||
} | ||
start(module, { name = Symbol(), data = undefined } = {}) { | ||
if (core.moduleInstances.has(name)) { | ||
return Promise.reject(`module with name ${name} already started`); | ||
} | ||
if (!module.start) { | ||
return Promise.reject(`module must have start defined`); | ||
} | ||
if (!module.start) { | ||
return Promise.reject(`module must have start defined`); | ||
} | ||
const emitter = new EventEmitter(); | ||
const emitter = new EventEmitter(); | ||
emitter.emit = this.boundModuleEmit; | ||
emitter.emit = core.moduleEmit; | ||
return Promise.resolve().then(() => { | ||
return module.start(emitter, data); | ||
}).then(instance => { | ||
this.moduleInstances.set(name, { | ||
module, | ||
instance, | ||
name, | ||
emitter, | ||
return Promise.resolve().then(() => { | ||
return module.start(emitter, data); | ||
}).then(instance => { | ||
core.moduleInstances.set(name, { | ||
module, | ||
instance, | ||
name, | ||
emitter, | ||
}); | ||
}).then(() => { | ||
return name; | ||
}).catch(errorModuleStart => { | ||
core.emit(ERROR, { | ||
time: Date.now(), | ||
phase: `module.start`, | ||
error: errorModuleStart, | ||
}); | ||
}); | ||
}).then(() => { | ||
return name; | ||
}).catch(errorModuleStart => { | ||
this.emit(ERROR, { | ||
time: Date.now(), | ||
phase: `module.start`, | ||
error: errorModuleStart, | ||
}); | ||
}); | ||
} | ||
}, | ||
stop(name) { | ||
const wrapper = this.moduleInstances.get(name); | ||
stop(name) { | ||
const wrapper = core.moduleInstances.get(name); | ||
if (!wrapper) { | ||
return Promise.resolve(false); | ||
} | ||
wrapper.emitter.off(); | ||
return Promise.resolve().then(() => { | ||
this.moduleInstances.delete(name); | ||
if (wrapper.module.stop) { | ||
wrapper.module.stop(wrapper.instance); | ||
if (!wrapper) { | ||
return Promise.resolve(false); | ||
} | ||
}).catch(errorModuleStop => { | ||
this.emit(ERROR, { | ||
time: Date.now(), | ||
phase: `module.stop`, | ||
error: errorModuleStop, | ||
}); | ||
}).then(() => { | ||
return true; | ||
}); | ||
} | ||
moduleEmit(name, data) { | ||
if (this.paused) { | ||
return; | ||
} | ||
this.moduleEmitDirect(name, data); | ||
} | ||
wrapper.emitter.off(); | ||
moduleEmitDirect(name, data) { | ||
this.emit(name, data); | ||
this.emit(ALL, { name, data, time: Date.now() }); | ||
this.moduleInstances.forEach(({ emitter }) => { | ||
try { | ||
EventEmitter.prototype.emit.call(emitter, name, data); | ||
} catch (error) { | ||
this.emit(ERROR, { | ||
return Promise.resolve().then(() => { | ||
core.moduleInstances.delete(name); | ||
if (wrapper.module.stop) { | ||
wrapper.module.stop(wrapper.instance); | ||
} | ||
}).catch(errorModuleStop => { | ||
core.emit(ERROR, { | ||
time: Date.now(), | ||
phase: `module runtime (emitter.on)`, | ||
error, | ||
phase: `module.stop`, | ||
error: errorModuleStop, | ||
}); | ||
}).then(() => { | ||
return true; | ||
}); | ||
}, | ||
moduleEmit(name, data) { | ||
if (core.paused) { | ||
return; | ||
} | ||
}); | ||
} | ||
core.moduleEmitDirect(name, data); | ||
}, | ||
moduleEmitDirect(name, data) { | ||
core.emit(name, data); | ||
core.emit(ALL, { name, data, time: Date.now() }); | ||
core.moduleInstances.forEach(({ emitter }) => { | ||
try { | ||
EventEmitter.prototype.emit.call(emitter, name, data); | ||
} catch (error) { | ||
core.emit(ERROR, { | ||
time: Date.now(), | ||
phase: `module runtime (emitter.on)`, | ||
error, | ||
}); | ||
} | ||
}); | ||
}, | ||
}); | ||
}; |
@@ -7,12 +7,10 @@ export { useDefaultLogging }; | ||
// listen for all events | ||
core.on(ALL, ({ name, data, time }) => { | ||
const timeString = new Date(time).toISOString(); | ||
logger.debug(`${timeString} event ${String(name)} data`, data); | ||
core.on(ALL, ({ name, data/*, time*/ }) => { | ||
logger.debug(`event ${String(name)} data`, data); | ||
}); | ||
core.on(ERROR, ({ time, phase, error }) => { | ||
const timeString = new Date(time).toISOString(); | ||
core.on(ERROR, ({ phase, error/*, time*/ }) => { | ||
console.error(`Error during phase ${phase} at ${timeString}`, error); | ||
}); | ||
}; |
Sorry, the diff of this file is not supported yet
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
62239
374
1406