@flowio/events
Advanced tools
Comparing version 0.1.7 to 0.1.8
@@ -1,2 +0,2 @@ | ||
'use strict'; | ||
"use strict"; | ||
@@ -6,16 +6,16 @@ Object.defineProperty(exports, "__esModule", { | ||
}); | ||
exports["default"] = void 0; | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
var _wolfy87Eventemitter = _interopRequireDefault(require("wolfy87-eventemitter")); | ||
var _wolfy87Eventemitter = require('wolfy87-eventemitter'); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } | ||
var _wolfy87Eventemitter2 = _interopRequireDefault(_wolfy87Eventemitter); | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } | ||
// Maintain a static reference to emitters by key. | ||
var emitters = {}; | ||
/** | ||
@@ -26,3 +26,5 @@ * Create an event emitter by channel. Multiple instances across different package / files can | ||
var ChannelEventEmitter = function () { | ||
var ChannelEventEmitter = | ||
/*#__PURE__*/ | ||
function () { | ||
function ChannelEventEmitter() { | ||
@@ -34,6 +36,8 @@ var channel = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'default'; | ||
var emitter = emitters[channel]; | ||
if (!emitter) { | ||
emitter = new _wolfy87Eventemitter2.default(); | ||
emitter = new _wolfy87Eventemitter["default"](); | ||
emitters[channel] = emitter; | ||
} | ||
this.emitter = emitter; | ||
@@ -44,24 +48,21 @@ this.channel = channel; | ||
_createClass(ChannelEventEmitter, [{ | ||
key: 'on', | ||
key: "on", | ||
value: function on(event) { | ||
var callback = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ChannelEventEmitter.noop; | ||
this.emitter.on(event, callback); | ||
} | ||
}, { | ||
key: 'once', | ||
key: "once", | ||
value: function once(event) { | ||
var callback = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ChannelEventEmitter.noop; | ||
this.emitter.once(event, callback); | ||
} | ||
}, { | ||
key: 'off', | ||
key: "off", | ||
value: function off(event) { | ||
var callback = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ChannelEventEmitter.noop; | ||
this.emitter.off(event, callback); | ||
} | ||
}, { | ||
key: 'trigger', | ||
key: "trigger", | ||
value: function trigger(event, data) { | ||
@@ -71,3 +72,3 @@ this.emitter.trigger(event, [data]); | ||
}, { | ||
key: 'removeAll', | ||
key: "removeAll", | ||
value: function removeAll() { | ||
@@ -77,3 +78,3 @@ this.emitter.removeAllListeners(); | ||
}], [{ | ||
key: 'noop', | ||
key: "noop", | ||
value: function noop() {} | ||
@@ -85,2 +86,2 @@ }]); | ||
exports.default = ChannelEventEmitter; | ||
exports["default"] = ChannelEventEmitter; |
@@ -1,2 +0,2 @@ | ||
'use strict'; | ||
"use strict"; | ||
@@ -6,21 +6,19 @@ Object.defineProperty(exports, "__esModule", { | ||
}); | ||
var _channelEventEmitter = require('./channel-event-emitter'); | ||
Object.defineProperty(exports, 'ChannelEventEmitter', { | ||
Object.defineProperty(exports, "ChannelEventEmitter", { | ||
enumerable: true, | ||
get: function get() { | ||
return _interopRequireDefault(_channelEventEmitter).default; | ||
return _channelEventEmitter["default"]; | ||
} | ||
}); | ||
var _triggerOnceEvents = require('./trigger-once-events'); | ||
Object.defineProperty(exports, 'TriggerOnceEvents', { | ||
Object.defineProperty(exports, "TriggerOnceEvents", { | ||
enumerable: true, | ||
get: function get() { | ||
return _interopRequireDefault(_triggerOnceEvents).default; | ||
return _triggerOnceEvents["default"]; | ||
} | ||
}); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var _channelEventEmitter = _interopRequireDefault(require("./channel-event-emitter")); | ||
var _triggerOnceEvents = _interopRequireDefault(require("./trigger-once-events")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } |
@@ -1,2 +0,2 @@ | ||
'use strict'; | ||
"use strict"; | ||
@@ -6,36 +6,33 @@ Object.defineProperty(exports, "__esModule", { | ||
}); | ||
exports["default"] = void 0; | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); /* eslint-disable no-console */ | ||
var _find = _interopRequireDefault(require("lodash/find")); | ||
var _find = require('lodash/find'); | ||
var _includes = _interopRequireDefault(require("lodash/includes")); | ||
var _find2 = _interopRequireDefault(_find); | ||
var _filter = _interopRequireDefault(require("lodash/filter")); | ||
var _includes = require('lodash/includes'); | ||
var _channelEventEmitter = _interopRequireDefault(require("./channel-event-emitter")); | ||
var _includes2 = _interopRequireDefault(_includes); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } | ||
var _filter = require('lodash/filter'); | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
var _filter2 = _interopRequireDefault(_filter); | ||
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } | ||
var _channelEventEmitter = require('./channel-event-emitter'); | ||
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } | ||
var _channelEventEmitter2 = _interopRequireDefault(_channelEventEmitter); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
var TriggerOnceEvents = function () { | ||
var TriggerOnceEvents = | ||
/*#__PURE__*/ | ||
function () { | ||
function TriggerOnceEvents() { | ||
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, | ||
_ref$channel = _ref.channel, | ||
channel = _ref$channel === undefined ? 'default' : _ref$channel, | ||
channel = _ref$channel === void 0 ? 'default' : _ref$channel, | ||
_ref$triggerOnceEvent = _ref.triggerOnceEvents, | ||
triggerOnceEvents = _ref$triggerOnceEvent === undefined ? [] : _ref$triggerOnceEvent; | ||
triggerOnceEvents = _ref$triggerOnceEvent === void 0 ? [] : _ref$triggerOnceEvent; | ||
_classCallCheck(this, TriggerOnceEvents); | ||
this.emitter = new _channelEventEmitter2.default(channel); | ||
this.emitter = new _channelEventEmitter["default"](channel); | ||
this.triggerOnceEvents = triggerOnceEvents; | ||
@@ -46,14 +43,15 @@ this.previouslyTriggered = []; | ||
_createClass(TriggerOnceEvents, [{ | ||
key: 'getPreviouslyTriggeredEvent', | ||
key: "getPreviouslyTriggeredEvent", | ||
value: function getPreviouslyTriggeredEvent(event) { | ||
return (0, _find2.default)(this.previouslyTriggered, { event: event }); | ||
return (0, _find["default"])(this.previouslyTriggered, { | ||
event: event | ||
}); | ||
} | ||
}, { | ||
key: 'on', | ||
key: "on", | ||
value: function on(event, callback) { | ||
var previous = this.getPreviouslyTriggeredEvent(event); | ||
this.emitter.on(event, callback); | ||
this.emitter.on(event, callback); // Immediately trigger if it is triggerOnce and has been triggered already | ||
// Immediately trigger if it is triggerOnce and has been triggered already | ||
if (previous && (0, _includes2.default)(this.triggerOnceEvents, event)) { | ||
if (previous && (0, _includes["default"])(this.triggerOnceEvents, event)) { | ||
try { | ||
@@ -63,4 +61,4 @@ callback(previous.data); | ||
// TODO: Rely on @flowio/logger | ||
console.log('[ERROR] Failed to execute callback function for subscribed event[' + event + '] due to error[' + error.message + '] in provided callback function', error); | ||
this.emitter.trigger('error', new Error('Failed to execute callback function for subscribed event[' + event + '] due to error[' + error.message + '] in provided callback function')); | ||
console.log("[ERROR] Failed to execute callback function for subscribed event[".concat(event, "] due to error[").concat(error.message, "] in provided callback function"), error); | ||
this.emitter.trigger('error', new Error("Failed to execute callback function for subscribed event[".concat(event, "] due to error[").concat(error.message, "] in provided callback function"))); | ||
} | ||
@@ -70,7 +68,6 @@ } | ||
}, { | ||
key: 'onError', | ||
key: "onError", | ||
value: function onError(callback) { | ||
this.emitter.on('error', callback); | ||
} | ||
/** | ||
@@ -87,9 +84,9 @@ * Trigger an event. Check if the event is configured to only be triggered once | ||
}, { | ||
key: 'trigger', | ||
key: "trigger", | ||
value: function trigger(event, data) { | ||
var previous = this.getPreviouslyTriggeredEvent(event); | ||
if (previous && (0, _includes2.default)(this.triggerOnceEvents, event)) { | ||
if (previous && (0, _includes["default"])(this.triggerOnceEvents, event)) { | ||
// TODO: Rely on @flowio/logger | ||
console.log('[WARNING] Event [' + event + '] was already triggered. It is configured to only be triggered once per page view.'); | ||
console.log("[WARNING] Event [".concat(event, "] was already triggered. It is configured to only be triggered once per page view.")); | ||
return; | ||
@@ -100,10 +97,12 @@ } | ||
this.emitter.trigger(event, data); | ||
this.previouslyTriggered.push({ event: event, data: data }); | ||
this.previouslyTriggered.push({ | ||
event: event, | ||
data: data | ||
}); | ||
} catch (error) { | ||
// TODO: Rely on @flowio/logger | ||
console.log('[ERROR] Failed to execute all callbacks for triggered event[' + event + '] due to error[' + error.message + '] in provided callback function', error); | ||
this.emitter.trigger('error', new Error('Failed to execute all callbacks for triggered event[' + event + '] due to error[' + error.message + '] in provided callback function')); | ||
console.log("[ERROR] Failed to execute all callbacks for triggered event[".concat(event, "] due to error[").concat(error.message, "] in provided callback function"), error); | ||
this.emitter.trigger('error', new Error("Failed to execute all callbacks for triggered event[".concat(event, "] due to error[").concat(error.message, "] in provided callback function"))); | ||
} | ||
} | ||
/** | ||
@@ -117,3 +116,3 @@ * Reset previously triggered events. If no eventsToReset are provided, all events previously | ||
}, { | ||
key: 'resetPreviouslyTriggered', | ||
key: "resetPreviouslyTriggered", | ||
value: function resetPreviouslyTriggered() { | ||
@@ -123,4 +122,4 @@ var eventsToReset = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; | ||
if (eventsToReset.length) { | ||
this.previouslyTriggered = (0, _filter2.default)(this.previouslyTriggered, function (previous) { | ||
return !(0, _includes2.default)(eventsToReset, previous.event); | ||
this.previouslyTriggered = (0, _filter["default"])(this.previouslyTriggered, function (previous) { | ||
return !(0, _includes["default"])(eventsToReset, previous.event); | ||
}); | ||
@@ -136,2 +135,2 @@ } else { | ||
exports.default = TriggerOnceEvents; | ||
exports["default"] = TriggerOnceEvents; |
{ | ||
"name": "@flowio/events", | ||
"version": "0.1.7", | ||
"version": "0.1.8", | ||
"description": "Event Bus (Pub/Sub)", | ||
@@ -26,11 +26,10 @@ "main": "lib/index.js", | ||
"devDependencies": { | ||
"@babel/cli": "^7.0.0", | ||
"@babel/core": "^7.0.0", | ||
"@babel/preset-env": "^7.0.0", | ||
"@babel/register": "^7.0.0", | ||
"@flowio/eslint-config-flowio": "^12.0.0", | ||
"@flowio/eslint-plugin-flowio": "^0.0.1", | ||
"babel-cli": "^6.26.0", | ||
"babel-core": "^6.26.0", | ||
"babel-eslint": "^8.2.1", | ||
"babel-jest": "^22.2.2", | ||
"babel-plugin-transform-runtime": "^6.23.0", | ||
"babel-preset-env": "^1.6.1", | ||
"babel-register": "^6.26.0", | ||
"babel-eslint": "^9.0.0", | ||
"babel-jest": "^23.4.2", | ||
"chai": "^4.1.2", | ||
@@ -54,3 +53,3 @@ "chai-spies": "^1.0.0", | ||
}, | ||
"gitHead": "78966044144faf9a9eaf2c1f39a3f08187dc3870" | ||
"gitHead": "c90761c44010efdd81d7cccf38dbe7432726c0a2" | ||
} |
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
25
189
16432