UNPKG

secure-event-emitter

Version:

SecureEventEmitter is a tiny javascript package that uses restrict rules and mechanisms to build safer and protected event-driven architecture. It's similar to nodejs EventEmitter, but dictates stricter rules to prevent misuse.

84 lines (59 loc) 3.15 kB
"use strict"; 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 _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _classPrivateFieldInitSpec(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); } function _checkPrivateRedeclaration(obj, privateCollection) { if (privateCollection.has(obj)) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } function _classPrivateFieldGet(receiver, privateMap) { var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); return _classApplyDescriptorGet(receiver, descriptor); } function _classExtractFieldDescriptor(receiver, privateMap, action) { if (!privateMap.has(receiver)) { throw new TypeError("attempted to " + action + " private field on non-instance"); } return privateMap.get(receiver); } function _classApplyDescriptorGet(receiver, descriptor) { if (descriptor.get) { return descriptor.get.call(receiver); } return descriptor.value; } var _require = require('./utils'), isSymbol = _require.isSymbol, isDescribedSymbol = _require.isDescribedSymbol; var _setMeta = /*#__PURE__*/new WeakMap(); var Payload = /*#__PURE__*/_createClass(function Payload(origin) { var _this = this; _classCallCheck(this, Payload); _classPrivateFieldInitSpec(this, _setMeta, { writable: true, value: function value() { var meta = {}; meta.date = Date.now(); meta._index = getIndex(); // ... // ... _this.meta = meta; } }); if (!isSymbol(origin)) { throw new TypeError('Payload origin must be a Symbol type'); } if (!isDescribedSymbol(origin)) { throw new TypeError('origin symbol description is required. Try Symbol(description)'); } this.origin = origin; for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { args[_key - 1] = arguments[_key]; } this.args = args; _classPrivateFieldGet(this, _setMeta).call(this); if ((this instanceof Payload ? this.constructor : void 0) === Payload) { Object.freeze(this); } }); Payload.isPayload = function (object) { return object instanceof Payload; }; Payload.validator = function (payload) { if (!Payload.isPayload(payload)) { return 'Emitter payload can be an only Payload type'; } }; var getIndex = function () { var current = 0; return function () { return ++current; }; }(); module.exports = { Payload: Payload };