Comparing version 4.2.1 to 5.0.0
@@ -7,2 +7,25 @@ # Change Log | ||
## [5.0.0] - 2017-09-05 | ||
- Added | ||
- method`on` | ||
- events: | ||
- beforeSet | ||
- set | ||
- create | ||
- update | ||
- beforeRemove | ||
- remove | ||
- beforeBulkSet | ||
- bulkSet | ||
- beforeBulkRemove | ||
- bulkRemove | ||
- expired | ||
- options: | ||
- `autoRemovePeriod`, now InCache can remove automatically expired records without calling any methods | ||
- `nullIfNotFound` | ||
- Deprecated methods: | ||
- `onCreated` | ||
- `onUpdated` | ||
- `onRemoved` | ||
## [4.2.1] - 2017-09-01 | ||
@@ -9,0 +32,0 @@ - Fixed wrong warning message |
@@ -1,2 +0,2 @@ | ||
// [AIV] InCache Build version: 4.2.1 | ||
// [AIV] InCache Build version: 5.0.0 | ||
var incache = | ||
@@ -282,3 +282,4 @@ /******/ (function(modules) { // webpackBootstrap | ||
var helper = __webpack_require__(4); | ||
var fs = __webpack_require__(5); | ||
var Flak = __webpack_require__(5); | ||
var fs = __webpack_require__(9); | ||
@@ -297,5 +298,8 @@ var InCache = function () { | ||
* @param [opts.share=true] {boolean} if true use global object as storage | ||
* @param [opts.autoRemovePeriod=0] {number} period in seconds to remove expired records. When set, the records will be removed only on check, when 0 it won't run | ||
* @param [opts.nullIfNotFound=true] {boolean} calling `get` if the key is not found returns `null`. If false returns `undefined` | ||
* @param [opts.global] {Object} **deprecated:** global record configuration | ||
* @param [opts.global.silent=false] {boolean} **deprecated:** if true no event will be triggered, use `silent` instead | ||
* @param [opts.global.life=0] {number} **deprecated:** max age in seconds. If 0 not expire, use `maxAge` instead | ||
* @fires InCache#expired | ||
* @constructor | ||
@@ -310,2 +314,6 @@ */ | ||
this._emitter = new Flak(); | ||
this._timerExpiryCheck = null; | ||
/** | ||
@@ -316,3 +324,2 @@ * Global key | ||
*/ | ||
this.GLOBAL_KEY = '___InCache___storage___global___key___'; | ||
@@ -323,3 +330,3 @@ | ||
* @ignore | ||
* @type {{storeName: string, save: boolean, filePath: string, maxAge: number, expires: null, silent: boolean, global: {silent: boolean, life: number}}} | ||
* @type {{storeName: string, save: boolean, filePath: string, maxAge: number, expires: null, silent: boolean, share: boolean, global: {silent: boolean, life: number}}} | ||
*/ | ||
@@ -334,2 +341,4 @@ this.DEFAULT_CONFIG = { | ||
share: true, | ||
autoRemovePeriod: 0, | ||
nullIfNotFound: true, | ||
global: { | ||
@@ -394,2 +403,4 @@ silent: false, | ||
value: function setConfig() { | ||
var _this2 = this; | ||
var opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
@@ -405,2 +416,4 @@ | ||
this._opts = opts; | ||
/** | ||
@@ -431,2 +444,16 @@ * Root object | ||
if (helper.isServer()) this._read(); | ||
if (this._timerExpiryCheck) { | ||
clearInterval(this._timerExpiryCheck); | ||
this._timerExpiryCheck = null; | ||
} | ||
if (opts.autoRemovePeriod) { | ||
this._timerExpiryCheck = setInterval(function () { | ||
var expired = _this2.removeExpired(); | ||
if (expired.length) { | ||
_this2._emitter.fire('expired', expired); | ||
} | ||
}, opts.autoRemovePeriod * 1000); | ||
} | ||
} | ||
@@ -464,3 +491,7 @@ | ||
* @param [opts.life=0] {number} **deprecated:** max age in seconds. If 0 not expire. (overwrites global configuration) | ||
* @returns {InCache~record} | ||
* @returns {InCache~record|*} | ||
* @fires InCache#beforeSet | ||
* @fires InCache#create | ||
* @fires InCache#update | ||
* @fires InCache#set | ||
* @example | ||
@@ -477,2 +508,7 @@ * inCache.set('my key', 'my value'); | ||
if (!opts.silent && this._emitter.fireTheFirst('beforeSet', key, value) === false) { | ||
return; | ||
} | ||
var record = { | ||
@@ -501,6 +537,12 @@ isNew: true, | ||
record.updatedOn = new Date(); | ||
if (!opts.silent) this._onUpdated.call(this, key, record); | ||
if (!opts.silent) { | ||
this._onUpdated.call(this, key, record); | ||
this._emitter.fire('update', key, record); | ||
} | ||
} else { | ||
record.createdOn = new Date(); | ||
if (!opts.silent) this._onCreated.call(this, key, record); | ||
if (!opts.silent) { | ||
this._onCreated.call(this, key, record); | ||
this._emitter.fire('create', key, record); | ||
} | ||
} | ||
@@ -510,2 +552,6 @@ | ||
if (!opts.silent) { | ||
this._emitter.fire('set', key, record); | ||
} | ||
return record; | ||
@@ -529,9 +575,9 @@ } | ||
if (this.has(key)) { | ||
if (this.expired(key)) { | ||
if (!this._opts.autoRemovePeriod && this.expired(key)) { | ||
this.remove(key, true); | ||
return null; | ||
return this._opts.nullIfNotFound ? null : undefined; | ||
} | ||
return onlyValue ? this._storage[key].value : this._storage[key]; | ||
} else { | ||
return null; | ||
return this._opts.nullIfNotFound ? null : undefined; | ||
} | ||
@@ -544,2 +590,4 @@ } | ||
* @param [silent=false] {boolean} if true no event will be triggered | ||
* @fires InCache#beforeRemove | ||
* @fires InCache#remove | ||
* @example | ||
@@ -554,4 +602,10 @@ * inCache.remove('my key'); | ||
if (!silent && this._emitter.fireTheFirst('beforeRemove', key) === false) { | ||
return; | ||
} | ||
delete this._storage[key]; | ||
if (!silent) this._onRemoved.call(this, key); | ||
if (!silent) { | ||
this._onRemoved.call(this, key); | ||
this._emitter.fire('remove', key); | ||
} | ||
} | ||
@@ -598,2 +652,3 @@ | ||
* Remove expired records | ||
* @returns {Array} expired keys | ||
* @example | ||
@@ -612,7 +667,10 @@ * inCache.set('my key 1', 'my value'); | ||
value: function removeExpired() { | ||
var expired = []; | ||
for (var key in this._storage) { | ||
if (this._storage.hasOwnProperty(key) && this.expired(key)) { | ||
this.remove(key, true); | ||
expired.push(key); | ||
} | ||
} | ||
return expired; | ||
} | ||
@@ -715,2 +773,5 @@ | ||
* @param records {array} array of object, e.g. [{key: foo1, value: bar1},{key: foo2, value: bar2}] | ||
* @param [silent=false] {boolean} if true no event will be triggered | ||
* @fires InCache#beforeBulkSet | ||
* @fires InCache#bulkSet | ||
* @example | ||
@@ -728,4 +789,10 @@ * inCache.bulkSet([ | ||
value: function bulkSet(records) { | ||
var silent = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; | ||
if (!helper.is(records, 'array')) throw new Error('records must be an array of object, e.g. {key: foo, value: bar}'); | ||
if (!silent && this._emitter.fireTheFirst('beforeBulkSet', records) === false) { | ||
return; | ||
} | ||
for (var i = 0; i < records.length; i++) { | ||
@@ -735,2 +802,6 @@ if (helper.is(records[i].key, 'undefined') || helper.is(records[i].value, 'undefined')) throw new Error('key and value properties are required'); | ||
} | ||
if (!silent) { | ||
this._emitter.fire('bulkSet', records); | ||
} | ||
} | ||
@@ -741,2 +812,5 @@ | ||
* @param keys {array} an array of keys | ||
* @param [silent=false] {boolean} if true no event will be triggered | ||
* @fires InCache#beforeBulkRemove | ||
* @fires InCache#bulkRemove | ||
* @example | ||
@@ -748,8 +822,16 @@ * inCache.bulkRemove(['key1', 'key2', 'key3']); | ||
key: 'bulkRemove', | ||
value: function bulkRemove(keys) { | ||
value: function bulkRemove(keys, silent) { | ||
if (!helper.is(keys, 'array')) throw new Error('keys must be an array of keys'); | ||
if (!silent && this._emitter.fireTheFirst('beforeBulkRemove', keys) === false) { | ||
return; | ||
} | ||
for (var i = 0; i < keys.length; i++) { | ||
this.remove(keys[i], true); | ||
} | ||
if (!silent) { | ||
this._emitter.fire('bulkRemove', keys); | ||
} | ||
} | ||
@@ -788,3 +870,3 @@ | ||
if (this._storage.hasOwnProperty(key)) { | ||
if (this.expired(key)) { | ||
if (!this._opts.autoRemovePeriod && this.expired(key)) { | ||
this.remove(key, true); | ||
@@ -866,4 +948,100 @@ } else { | ||
/** | ||
* Triggered when a record has been deleted | ||
* Adds listener to instance | ||
* @param eventName {string} event name | ||
* @param callback {Function} callback | ||
*/ | ||
}, { | ||
key: 'on', | ||
value: function on(eventName, callback) { | ||
this._emitter.on.call(this._emitter, eventName, callback); | ||
} | ||
/** | ||
* Triggered before set | ||
* @event InCache#beforeSet | ||
* @param key {string} key | ||
* @param value {string} value | ||
* @since 5.0.0 | ||
*/ | ||
/** | ||
* Triggered after set | ||
* @event InCache#set | ||
* @param key {string} key | ||
* @param record {InCache~record} record object | ||
* @since 5.0.0 | ||
*/ | ||
/** | ||
* Triggered after create the record | ||
* @event InCache#create | ||
* @param key {string} key of record | ||
* @param record {InCache~record} record object | ||
* @since 5.0.0 | ||
*/ | ||
/** | ||
* Triggered after update the record | ||
* @event InCache#update | ||
* @param key {string} key of record | ||
* @param record {InCache~record} record object | ||
* @since 5.0.0 | ||
*/ | ||
/** | ||
* Triggered before remove the record | ||
* @event InCache#beforeRemove | ||
* @param key {string} key of record to be removed | ||
* @since 5.0.0 | ||
*/ | ||
/** | ||
* Triggered after record has been removed | ||
* @event InCache#remove | ||
* @param key {string} key of record | ||
* @since 5.0.0 | ||
*/ | ||
/** | ||
* Triggered before bulk set | ||
* @event InCache#beforeBulkSet | ||
* @param records {array} array of objects | ||
* @since 5.0.0 | ||
*/ | ||
/** | ||
* Triggered after bulk set | ||
* @event InCache#bulkSet | ||
* @param records {array} array of objects | ||
* @since 5.0.0 | ||
*/ | ||
/** | ||
* Triggered before remove the records | ||
* @event InCache#beforeBulkRemove | ||
* @param keys {array} array of keys to be removed | ||
* @since 5.0.0 | ||
*/ | ||
/** | ||
* Triggered after records have been removed | ||
* @event InCache#bulkRemove | ||
* @param keys {array} array of keys removed | ||
* @since 5.0.0 | ||
*/ | ||
/** | ||
* Triggered when records are expired and `opts.autoRemovePeriod` is set | ||
* @event InCache#expired | ||
* @param keys {array} array of keys expired | ||
* @since 5.0.0 | ||
*/ | ||
/***************************** DEPRECATED ********************************/ | ||
/** | ||
* Triggered when a record has been deleted. **Deprecated:** use `on('remove', callback)` instead | ||
* @param callback {InCache~removedCallback} callback function | ||
* @deprecated | ||
* @example | ||
@@ -885,7 +1063,9 @@ * inCache.onRemoved((key)=>{ | ||
* @param key {string} key of record removed | ||
* @deprecated | ||
*/ | ||
/** | ||
* Triggered when a record has been created | ||
* Triggered when a record has been created. **Deprecated:** use `on('create', callback)` instead | ||
* @param callback {InCache~createdCallback} callback function | ||
* @deprecated | ||
* @example | ||
@@ -908,7 +1088,9 @@ * inCache.onCreated((key, record)=>{ | ||
* @param record {InCache~record} record object | ||
* @deprecated | ||
*/ | ||
/** | ||
* Triggered when a record has been updated | ||
* Triggered when a record has been updated. **Deprecated:** use `on('update', callback)` instead | ||
* @param callback {InCache~updatedCallback} callback function | ||
* @deprecated | ||
* @example | ||
@@ -931,2 +1113,3 @@ * inCache.onUpdated((key, record)=>{ | ||
* @param record {InCache~record} record object | ||
* @deprecated | ||
*/ | ||
@@ -1073,3 +1256,567 @@ | ||
module.exports = __webpack_require__(6); | ||
/***/ }), | ||
/* 6 */ | ||
/***/ (function(module, exports, __webpack_require__) { | ||
"use strict"; | ||
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; }; }(); | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
var helper = __webpack_require__(7); | ||
var error = __webpack_require__(8); | ||
var Flak = function () { | ||
//TODO add support to cross-domain through postMessage, see: https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage | ||
/** | ||
* Constructor | ||
* @param [opts] {Object} options | ||
* @param [opts.maxListeners=10] {number} Max number listeners per event | ||
* @param [opts.asyncDelay=10] {number} Delay in ms for async method `fireAsync` | ||
* @example | ||
* const emitter = new Flak(); | ||
*/ | ||
function Flak() { | ||
var opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
_classCallCheck(this, Flak); | ||
/** | ||
* Class options | ||
* @type {{maxListeners: number, asyncDelay: number}} | ||
* @ignore | ||
*/ | ||
this.defaultClassOpts = { | ||
maxListeners: 10, | ||
asyncDelay: 10 // ms | ||
}; | ||
/** | ||
* Event options | ||
* @type {{maxCalls: number, prepend: boolean}} | ||
* @ignore | ||
*/ | ||
this.defaultListenerOpts = { | ||
maxCalls: 0, | ||
prepend: false | ||
}; | ||
this.opts = helper.defaults(opts, this.defaultClassOpts); | ||
this.events = {}; | ||
} | ||
/** | ||
* Create event and add listener | ||
* @param eventName {string} event name | ||
* @param listener {Function} listener function | ||
* @param [opts] {Object} option object | ||
* @param [opts.maxCalls=0] {number} Max calls for event created, disabled if is `0` | ||
* @param [opts.prepend=false] {boolean} Adds the listener function to the beginning of the listeners array for the event named `eventName` | ||
* @private | ||
* @ignore | ||
*/ | ||
_createClass(Flak, [{ | ||
key: '_createEvent', | ||
value: function _createEvent(eventName, listener, opts) { | ||
if (!this.events[eventName]) this.events[eventName] = []; | ||
if (this.opts.maxListeners) { | ||
var maxListeners = this.opts.maxListeners; | ||
var listenersCount = this.events[eventName].length; | ||
if (listenersCount >= maxListeners) throw new Error(error[3] + maxListeners); | ||
} | ||
listener.opts = helper.defaults(opts, this.defaultListenerOpts); | ||
listener.info = { | ||
calls: 0 | ||
}; | ||
if (opts.prepend) this.events[eventName].unshift(listener);else this.events[eventName].push(listener); | ||
this._created.call(this, eventName, listener, opts); | ||
} | ||
/** | ||
* Call event | ||
* @param eventName {string} event name | ||
* @param eventListener {Function} event listener | ||
* @param args args {*} ...arguments | ||
* @private | ||
* @ignore | ||
*/ | ||
}, { | ||
key: '_callEvent', | ||
value: function _callEvent(eventName, eventListener, args) { | ||
if (eventListener.opts.maxCalls && eventListener.info.calls++ >= eventListener.opts.maxCalls) { | ||
this.off(eventName, eventListener); | ||
return; | ||
} | ||
this._catchAll.call(this, args); | ||
return eventListener.apply(this, args); | ||
} | ||
/** | ||
* Callback on create | ||
* @private | ||
* @ignore | ||
*/ | ||
}, { | ||
key: '_created', | ||
value: function _created() {} | ||
/** | ||
* Callback on remove | ||
* @private | ||
* @ignore | ||
*/ | ||
}, { | ||
key: '_removed', | ||
value: function _removed() {} | ||
/** | ||
* Callback catch all | ||
* @private | ||
* @ignore | ||
*/ | ||
}, { | ||
key: '_catchAll', | ||
value: function _catchAll() {} | ||
/** | ||
* Adds event listener for eventName | ||
* @param eventName {string} event name | ||
* @param listener {(Function|Function[])} listener function | ||
* @param [opts] {Object} option object | ||
* @param [opts.maxCalls=0] {number} Max calls for event created, disabled if is `0` | ||
* @param [opts.prepend=false] {boolean} Adds the listener function to the beginning of the listeners array for the event named `eventName` | ||
* @returns {Flak} | ||
* @example | ||
* emitter.on('myEvent', (param)=>{ | ||
* console.log(param); | ||
* }); | ||
*/ | ||
}, { | ||
key: 'on', | ||
value: function on(eventName, listener) { | ||
var opts = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
if (!helper.is(eventName, 'string')) throw new Error(error[0]); | ||
eventName = eventName.trim(); | ||
if (!eventName.length) throw new Error(error[4]); | ||
if (helper.is(listener, 'array')) { | ||
for (var i in listener) { | ||
if (listener.hasOwnProperty(i)) { | ||
if (!helper.is(listener[i], 'function')) throw new Error(error[1]); | ||
this._createEvent(eventName, listener[i], opts); | ||
} | ||
} | ||
} else { | ||
if (!helper.is(listener, 'function')) throw new Error(error[1]); | ||
this._createEvent(eventName, listener, opts); | ||
} | ||
return this; | ||
} | ||
/** | ||
* Adds a one time listener function for the event named eventName. | ||
* This is a wrapper method of `on` that set to `opts.maxCalls = 1` | ||
* @param eventName {string} event name | ||
* @param listener {(Function|Function[])} listener function | ||
* @returns {Flak} | ||
* @example | ||
* emitter.once('myEvent', (param)=>{ | ||
* console.log(param); | ||
* }); | ||
*/ | ||
}, { | ||
key: 'once', | ||
value: function once(eventName, listener) { | ||
return this.on(eventName, listener, { | ||
maxCalls: 1 | ||
}); | ||
} | ||
/** | ||
* Calls each of the listeners registered for the event | ||
* @param eventName {string} event name | ||
* @param [args] {*} ...arguments | ||
* @returns {Flak} | ||
* @example | ||
* emitter.fire('myEvent', param1, param2, ...); | ||
*/ | ||
}, { | ||
key: 'fire', | ||
value: function fire(eventName) { | ||
if (this.exists(eventName)) { | ||
for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
args[_key - 1] = arguments[_key]; | ||
} | ||
for (var j = 0; j < this.events[eventName].length; j++) { | ||
this._callEvent(eventName, this.events[eventName][j], args); | ||
} | ||
}return this; | ||
} | ||
/** | ||
* Calls the first of the listeners registered for the event and return it | ||
* @param eventName {string} event name | ||
* @param [args] {*} ...arguments | ||
* @returns {*} | ||
* @since 0.3.0 | ||
* @example | ||
* emitter.on('myEvent', (param1, param2)=>{ | ||
* return param1 + '-' + param2; | ||
* }); | ||
* console.log('foo-bar' === emitter.fireTheFirst('myEvent', 'foo', 'bar')) //=> true; | ||
*/ | ||
}, { | ||
key: 'fireTheFirst', | ||
value: function fireTheFirst(eventName) { | ||
for (var _len2 = arguments.length, args = Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) { | ||
args[_key2 - 1] = arguments[_key2]; | ||
} | ||
if (this.exists(eventName)) return this._callEvent(eventName, this.events[eventName][0], args); | ||
} | ||
/** | ||
* Calls each of the listeners registered for the event, this method is async | ||
* @param eventName {string} event name | ||
* @param args {*} ...arguments | ||
* @example | ||
* emitter.fireAsync('myEvent', param1, param2, ...); | ||
*/ | ||
}, { | ||
key: 'fireAsync', | ||
value: function fireAsync(eventName) { | ||
var _this = this; | ||
for (var _len3 = arguments.length, args = Array(_len3 > 1 ? _len3 - 1 : 0), _key3 = 1; _key3 < _len3; _key3++) { | ||
args[_key3 - 1] = arguments[_key3]; | ||
} | ||
args.unshift(eventName); | ||
setTimeout(function () { | ||
_this.fire.apply(_this, args); | ||
}, this.opts.asyncDelay); | ||
} | ||
/** | ||
* Remove event/listener | ||
* @param eventName {string} event name | ||
* @param [listener] {Function} listener function, if is set remove listener only for this event | ||
* @returns {Flak} | ||
* @example | ||
* emitter.off('myEvent'); // remove event | ||
* emitter.off('myEvent', listener); // remove specific listener | ||
*/ | ||
}, { | ||
key: 'off', | ||
value: function off(eventName, listener) { | ||
if (!helper.is(eventName, 'string')) throw new Error(error[0]); | ||
if (this.events[eventName]) if (typeof listener === 'function') { | ||
for (var i = 0; i < this.events[eventName].length; i++) { | ||
if (this.events[eventName][i] === listener) { | ||
this.events[eventName].splice(i, 1); | ||
this._removed.call(this, eventName, listener); | ||
} | ||
} | ||
} else { | ||
delete this.events[eventName]; | ||
this._removed.call(this, eventName); | ||
} | ||
return this; | ||
} | ||
/** | ||
* Adds the listener function to the beginning of the listeners array for the event named eventName. | ||
* This is a wrapper method of `on` that set to `opts.prepend = true` | ||
* @param eventName {string} event name | ||
* @param listener {(Function|Function[])} listener function | ||
* @returns {Flak} | ||
*/ | ||
}, { | ||
key: 'prependListener', | ||
value: function prependListener(eventName, listener) { | ||
return this.on(eventName, listener, { | ||
prepend: true | ||
}); | ||
} | ||
/** | ||
* Adds a one time listener function to the beginning of the listeners array for the event named eventName. | ||
* This is a wrapper method of `on` that set to `opts.maxCalls = 1` and `opts.prepend = true` | ||
* @param eventName {string} event name | ||
* @param listener {(Function|Function[])} listener function | ||
* @returns {Flak} | ||
*/ | ||
}, { | ||
key: 'prependOnceListener', | ||
value: function prependOnceListener(eventName, listener) { | ||
return this.on(eventName, listener, { | ||
maxCalls: 1, | ||
prepend: true | ||
}); | ||
} | ||
/** | ||
* Remove all events | ||
* @returns {Flak} | ||
* @example | ||
* emitter.clear(); | ||
*/ | ||
}, { | ||
key: 'clear', | ||
value: function clear() { | ||
this.events = []; | ||
return this; | ||
} | ||
/** | ||
* Get listeners count | ||
* @param eventName {string} event name | ||
* @returns {number} | ||
* @example | ||
* emitter.on('event', listener1); | ||
* emitter.on('event', listener2); | ||
* emitter.on('event1', listener3); | ||
* | ||
* emitter.getListenersCount('event'); // 2 | ||
*/ | ||
}, { | ||
key: 'getListenersCount', | ||
value: function getListenersCount(eventName) { | ||
return this.getListeners(eventName).length; | ||
} | ||
/** | ||
* Get listeners list of event | ||
* @param eventName {string} event name | ||
* @returns {Array} | ||
*/ | ||
}, { | ||
key: 'getListeners', | ||
value: function getListeners(eventName) { | ||
if (!helper.is(eventName, 'string')) throw new Error(error[0]); | ||
if (!this.exists(eventName)) throw new Error(error[5]); | ||
return this.events[eventName]; | ||
} | ||
/** | ||
* Get events list | ||
* @returns {Object} | ||
*/ | ||
}, { | ||
key: 'getEvents', | ||
value: function getEvents() { | ||
return this.events; | ||
} | ||
/** | ||
* Check if event exists | ||
* @param eventName {string} event name | ||
* @returns {boolean} | ||
*/ | ||
}, { | ||
key: 'exists', | ||
value: function exists(eventName) { | ||
if (!helper.is(eventName, 'string')) throw new Error(error[0]); | ||
return !helper.is(this.events[eventName], 'undefined'); | ||
} | ||
/** | ||
* Get max number of listeners per event | ||
* @returns {number} | ||
*/ | ||
}, { | ||
key: 'getMaxListeners', | ||
value: function getMaxListeners() { | ||
return this.opts.maxListeners; | ||
} | ||
/** | ||
* Set max number of listeners per event | ||
* @param value {int} number max listeners | ||
* @returns {Flak} | ||
*/ | ||
}, { | ||
key: 'setMaxListeners', | ||
value: function setMaxListeners(value) { | ||
if (!helper.is(value, 'number')) throw new Error(error[2]); | ||
this.opts.maxListeners = value; | ||
return this; | ||
} | ||
/** | ||
* Triggered when an event is fired | ||
* @param callback {Function} callback function | ||
* @returns {Flak} | ||
* @since 0.2.0 | ||
* @example | ||
* emitter.onCatchAll(args=>{ | ||
* // args is an array of params | ||
* console.log(args); | ||
* }); | ||
* | ||
* emitter.on('myEvent', param=>{ | ||
* console.log(param); | ||
* }); | ||
* | ||
* emitter.fire('myEvent'); | ||
*/ | ||
}, { | ||
key: 'onCatchAll', | ||
value: function onCatchAll(callback) { | ||
this._catchAll = callback; | ||
return this; | ||
} | ||
/** | ||
* Triggered when an event is created | ||
* @param callback {Function} callback function | ||
* @returns {Flak} | ||
* @example | ||
* emitter.onCreated(obj=>{ | ||
* console.log(obj); //-> eventName, listener, opts | ||
* }); | ||
* | ||
* emitter.on('myEvent', (param)=>{ | ||
* console.log(param); | ||
* }); | ||
*/ | ||
}, { | ||
key: 'onCreated', | ||
value: function onCreated(callback) { | ||
this._created = callback; | ||
return this; | ||
} | ||
/** | ||
* Triggered when an event is removed | ||
* @param callback {Function} callback function | ||
* @returns {Flak} | ||
* @example | ||
* emitter.onRemoved(obj=>{ | ||
* console.log(obj); //-> eventName, (listener) | ||
* }); | ||
* | ||
* emitter.off('myEvent'); | ||
*/ | ||
}, { | ||
key: 'onRemoved', | ||
value: function onRemoved(callback) { | ||
this._removed = callback; | ||
return this; | ||
} | ||
}]); | ||
return Flak; | ||
}(); | ||
module.exports = Flak; | ||
module.exports._error = error; | ||
/***/ }), | ||
/* 7 */ | ||
/***/ (function(module, exports, __webpack_require__) { | ||
"use strict"; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
var helper = {}; | ||
/** | ||
* Get object type | ||
* @param object {*} | ||
* @param type {string} | ||
* @returns {boolean} | ||
*/ | ||
helper.is = function (object, type) { | ||
var objectToString = Object.prototype.toString.call(object); | ||
return objectToString.toLowerCase() === '[object ' + type + ']'.toLowerCase(); | ||
}; | ||
/** | ||
* Set default value | ||
* @param opts {Object} options | ||
* @param defaultOpts {Object} default options | ||
* @returns {*} | ||
*/ | ||
helper.defaults = function (opts, defaultOpts) { | ||
for (var i in defaultOpts) { | ||
if (defaultOpts.hasOwnProperty(i)) if (!opts.hasOwnProperty(i)) { | ||
opts[i] = defaultOpts[i]; | ||
} else { | ||
if (_typeof(opts[i]) === 'object') { | ||
helper.defaults(opts[i], defaultOpts[i]); | ||
} | ||
} | ||
} | ||
return opts; | ||
}; | ||
module.exports = helper; | ||
/***/ }), | ||
/* 8 */ | ||
/***/ (function(module, exports, __webpack_require__) { | ||
"use strict"; | ||
module.exports = ['event name is required and must be a string', 'listener is required and must be a function or an array of function', 'value must be a number', 'increase maxListeners per event: ', 'event name not valid', 'event not found']; | ||
/***/ }), | ||
/* 9 */ | ||
/***/ (function(module, exports, __webpack_require__) { | ||
"use strict"; | ||
/***/ }) | ||
/******/ ]); |
@@ -1,2 +0,2 @@ | ||
// [AIV] InCache Build version: 4.2.1 | ||
var incache=function(e){function t(n){if(r[n])return r[n].exports;var i=r[n]={i:n,l:!1,exports:{}};return e[n].call(i.exports,i,i.exports,t),i.l=!0,i.exports}var r={};return t.m=e,t.c=r,t.d=function(e,r,n){t.o(e,r)||Object.defineProperty(e,r,{configurable:!1,enumerable:!0,get:n})},t.n=function(e){var r=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(r,"a",r),r},t.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},t.p="",t(t.s=1)}([function(e,t,r){"use strict";function n(){throw new Error("setTimeout has not been defined")}function i(){throw new Error("clearTimeout has not been defined")}function o(e){if(f===setTimeout)return setTimeout(e,0);if((f===n||!f)&&setTimeout)return f=setTimeout,setTimeout(e,0);try{return f(e,0)}catch(t){try{return f.call(null,e,0)}catch(t){return f.call(this,e,0)}}}function a(e){if(h===clearTimeout)return clearTimeout(e);if((h===i||!h)&&clearTimeout)return h=clearTimeout,clearTimeout(e);try{return h(e)}catch(t){try{return h.call(null,e)}catch(t){return h.call(this,e)}}}function s(){p&&v&&(p=!1,v.length?y=v.concat(y):m=-1,y.length&&u())}function u(){if(!p){var e=o(s);p=!0;for(var t=y.length;t;){for(v=y,y=[];++m<t;)v&&v[m].run();m=-1,t=y.length}v=null,p=!1,a(e)}}function l(e,t){this.fun=e,this.array=t}function c(){}var f,h,d=e.exports={};!function(){try{f="function"==typeof setTimeout?setTimeout:n}catch(e){f=n}try{h="function"==typeof clearTimeout?clearTimeout:i}catch(e){h=i}}();var v,y=[],p=!1,m=-1;d.nextTick=function(e){var t=new Array(arguments.length-1);if(arguments.length>1)for(var r=1;r<arguments.length;r++)t[r-1]=arguments[r];y.push(new l(e,t)),1!==y.length||p||o(u)},l.prototype.run=function(){this.fun.apply(null,this.array)},d.title="browser",d.browser=!0,d.env={},d.argv=[],d.version="",d.versions={},d.on=c,d.addListener=c,d.once=c,d.off=c,d.removeListener=c,d.removeAllListeners=c,d.emit=c,d.prependListener=c,d.prependOnceListener=c,d.listeners=function(e){return[]},d.binding=function(e){throw new Error("process.binding is not supported")},d.cwd=function(){return"/"},d.chdir=function(e){throw new Error("process.chdir is not supported")},d.umask=function(){return 0}},function(e,t,r){"use strict";e.exports=r(2)},function(e,t,r){"use strict";(function(t,n){function i(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var o=function(){function e(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}return function(t,r,n){return r&&e(t.prototype,r),n&&e(t,n),t}}(),a=r(4),s=r(5),u=function(){function e(){var r=this,n=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};i(this,e),this.GLOBAL_KEY="___InCache___storage___global___key___",this.DEFAULT_CONFIG={storeName:"",save:!0,filePath:".incache",maxAge:0,expires:null,silent:!1,share:!0,global:{silent:!1,life:0}},this._onRemoved=function(){},this._onCreated=function(){},this._onUpdated=function(){},a.isServer()&&(t.stdin.resume(),t.on("exit",function(){r._write()}),t.on("SIGINT",function(){r._write()})),this.setConfig(n)}return o(e,[{key:"_write",value:function(){var e=this._memory,t=e.config,r=e.data;if(t.save){var n=JSON.stringify(r);s.writeFileSync(t.filePath,n)}}},{key:"_read",value:function(){var e=this._memory.config;if(e.save&&s.existsSync(e.filePath)){var t=s.readFileSync(e.filePath);try{this._storage=this._memory.data=JSON.parse(t)}catch(e){this._storage=this._memory.data={}}}}},{key:"setConfig",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};e.global&&(a.deprecated(e.global.life,"global.life is deprecated use maxAge instead"),a.deprecated(e.global.silent,"global.silent is deprecated use silent instead")),a.defaults(e,this.DEFAULT_CONFIG),this._root=e.share?a.isServer()?n:window:{},e.storeName&&(this.GLOBAL_KEY+=e.storeName),this._root[this.GLOBAL_KEY]||(this._root[this.GLOBAL_KEY]={metadata:{lastSave:null},data:{},config:this.DEFAULT_CONFIG}),this._root[this.GLOBAL_KEY].config=e,this._memory=this._root[this.GLOBAL_KEY],this._storage=this._memory.data,a.isServer()&&this._read()}},{key:"getConfig",value:function(){return this._memory.config}},{key:"set",value:function(e,t){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{},n={isNew:!0,createdOn:null,updatedOn:null,expiresOn:null,value:t};return r=a.defaults(r,this.DEFAULT_CONFIG),r.expires&&(a.is(r.expires,"date")||a.is(r.expires,"string"))?n.expiresOn=new Date(r.expires):r.maxAge&&a.is(r.maxAge,"number")?n.expiresOn=a.addMSToNow(r.maxAge):r.life&&a.is(r.life,"number")&&(a.deprecated(r.life,"life is deprecated use maxAge instead"),n.expiresOn=a.addSecondsToNow(r.life)),this.has(e)?(n.isNew=!1,n.createdOn=this._storage[e].createdOn,n.updatedOn=new Date,r.silent||this._onUpdated.call(this,e,n)):(n.createdOn=new Date,r.silent||this._onCreated.call(this,e,n)),this._storage[e]=n,n}},{key:"get",value:function(e){var t=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];return this.has(e)?this.expired(e)?(this.remove(e,!0),null):t?this._storage[e].value:this._storage[e]:null}},{key:"remove",value:function(e){var t=arguments.length>1&&void 0!==arguments[1]&&arguments[1];delete this._storage[e],t||this._onRemoved.call(this,e)}},{key:"removeFrom",value:function(e,t){if(!this.has(e))return null;if(a.is(t,"undefined"))throw new Error("where cannot be undefined");var r=this.get(e);if(!a.is(r,"array"))throw new Error("value must be an array");var n=r.length;for(var i in r)if(r.hasOwnProperty(i)){var o=[];for(var s in t)t.hasOwnProperty(s)&&(a.is(t,"object")?o.push(void 0!==r[i][s]&&r[i][s]===t[s]):o.push(r[i]===t));o.length&&-1===o.indexOf(!1)&&r.splice(i,1)}n!==r.length&&this.set(e,r)}},{key:"removeExpired",value:function(){for(var e in this._storage)this._storage.hasOwnProperty(e)&&this.expired(e)&&this.remove(e,!0)}},{key:"addTo",value:function(e,t){if(!this.has(e))return null;var r=this.get(e);if(!a.is(r,"array"))throw new Error("object must be an array");return r.push(t),this.set(e,r)}},{key:"prependTo",value:function(e,t){if(!this.has(e))return null;var r=this.get(e);if(!a.is(r,"array"))throw new Error("object must be an array");return r.unshift(t),this.set(e,r)}},{key:"updateIn",value:function(e,t,r){if(!this.has(e))return null;if(a.is(t,"undefined"))throw new Error("value cannot be undefined");if(a.is(r,"undefined"))throw new Error("where cannot be undefined");var n=this.get(e);if(!a.is(n,"array"))throw new Error("value must be an array");var i=!1;for(var o in n)if(n.hasOwnProperty(o)){var s=[];for(var u in r)r.hasOwnProperty(u)&&(a.is(r,"object")?s.push(void 0!==n[o][u]&&n[o][u]===r[u]):s.push(n[o]===r));s.length&&-1===s.indexOf(!1)&&(i=!0,n[o]=t)}i&&this.set(e,n)}},{key:"bulkSet",value:function(e){if(!a.is(e,"array"))throw new Error("records must be an array of object, e.g. {key: foo, value: bar}");for(var t=0;t<e.length;t++){if(a.is(e[t].key,"undefined")||a.is(e[t].value,"undefined"))throw new Error("key and value properties are required");this.set(e[t].key,e[t].value,{silent:!0,fromBulk:!0})}}},{key:"bulkRemove",value:function(e){if(!a.is(e,"array"))throw new Error("keys must be an array of keys");for(var t=0;t<e.length;t++)this.remove(e[t],!0)}},{key:"clean",value:function(e){if(!a.is(e,"string"))throw new Error("key must be a string");for(var t in this._storage)this._storage.hasOwnProperty(t)&&-1!==t.indexOf(e)&&delete this._storage[t]}},{key:"all",value:function(){var e=[];for(var t in this._storage)this._storage.hasOwnProperty(t)&&(this.expired(t)?this.remove(t,!0):e.push({key:t,value:this._storage[t].value}));return e}},{key:"expired",value:function(e){if(this._storage[e]&&this._storage[e].expiresOn){return new Date>new Date(this._storage[e].expiresOn)}return!1}},{key:"clear",value:function(){this._storage=this._memory.data={}}},{key:"has",value:function(e){return this._storage.hasOwnProperty(e)}},{key:"destroy",value:function(){for(var e=arguments.length,t=Array(e),r=0;r<e;r++)t[r]=arguments[r];this.remove.apply(this,t)}},{key:"onRemoved",value:function(e){this._onRemoved=e}},{key:"onCreated",value:function(e){this._onCreated=e}},{key:"onUpdated",value:function(e){this._onUpdated=e}}]),e}();e.exports=u}).call(t,r(0),r(3))},function(e,t,r){"use strict";var n,i="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e};n=function(){return this}();try{n=n||Function("return this")()||(0,eval)("this")}catch(e){"object"===("undefined"==typeof window?"undefined":i(window))&&(n=window)}e.exports=n},function(e,t,r){"use strict";(function(t){var r="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},n={};n.is=function(e,t){return Object.prototype.toString.call(e).toLowerCase()==="[object "+t+"]".toLowerCase()},n.defaults=function(e,t){for(var i in t)t.hasOwnProperty(i)&&(e.hasOwnProperty(i)?"object"===r(e[i])&&n.defaults(e[i],t[i]):e[i]=t[i]);return e},n.addSecondsToNow=function(e){var t=new Date;return new Date(t.setSeconds(t.getSeconds()+e))},n.addMSToNow=function(e){var t=new Date;return new Date(t.setMilliseconds(t.getMilliseconds()+e))},n.isServer=function(){return"object"===(void 0===t?"undefined":r(t))&&void 0!==t.pid},n.deprecated=function(e,t){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:"warn";return void 0!==e&&(console[r](t||e),!0)},e.exports=n}).call(t,r(0))},function(e,t,r){"use strict"}]); | ||
// [AIV] InCache Build version: 5.0.0 | ||
var incache=function(e){function t(n){if(r[n])return r[n].exports;var i=r[n]={i:n,l:!1,exports:{}};return e[n].call(i.exports,i,i.exports,t),i.l=!0,i.exports}var r={};return t.m=e,t.c=r,t.d=function(e,r,n){t.o(e,r)||Object.defineProperty(e,r,{configurable:!1,enumerable:!0,get:n})},t.n=function(e){var r=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(r,"a",r),r},t.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},t.p="",t(t.s=1)}([function(e,t,r){"use strict";function n(){throw new Error("setTimeout has not been defined")}function i(){throw new Error("clearTimeout has not been defined")}function o(e){if(h===setTimeout)return setTimeout(e,0);if((h===n||!h)&&setTimeout)return h=setTimeout,setTimeout(e,0);try{return h(e,0)}catch(t){try{return h.call(null,e,0)}catch(t){return h.call(this,e,0)}}}function s(e){if(c===clearTimeout)return clearTimeout(e);if((c===i||!c)&&clearTimeout)return c=clearTimeout,clearTimeout(e);try{return c(e)}catch(t){try{return c.call(null,e)}catch(t){return c.call(this,e)}}}function a(){p&&d&&(p=!1,d.length?y=d.concat(y):m=-1,y.length&&u())}function u(){if(!p){var e=o(a);p=!0;for(var t=y.length;t;){for(d=y,y=[];++m<t;)d&&d[m].run();m=-1,t=y.length}d=null,p=!1,s(e)}}function l(e,t){this.fun=e,this.array=t}function f(){}var h,c,v=e.exports={};!function(){try{h="function"==typeof setTimeout?setTimeout:n}catch(e){h=n}try{c="function"==typeof clearTimeout?clearTimeout:i}catch(e){c=i}}();var d,y=[],p=!1,m=-1;v.nextTick=function(e){var t=new Array(arguments.length-1);if(arguments.length>1)for(var r=1;r<arguments.length;r++)t[r-1]=arguments[r];y.push(new l(e,t)),1!==y.length||p||o(u)},l.prototype.run=function(){this.fun.apply(null,this.array)},v.title="browser",v.browser=!0,v.env={},v.argv=[],v.version="",v.versions={},v.on=f,v.addListener=f,v.once=f,v.off=f,v.removeListener=f,v.removeAllListeners=f,v.emit=f,v.prependListener=f,v.prependOnceListener=f,v.listeners=function(e){return[]},v.binding=function(e){throw new Error("process.binding is not supported")},v.cwd=function(){return"/"},v.chdir=function(e){throw new Error("process.chdir is not supported")},v.umask=function(){return 0}},function(e,t,r){"use strict";e.exports=r(2)},function(e,t,r){"use strict";(function(t,n){function i(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var o=function(){function e(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}return function(t,r,n){return r&&e(t.prototype,r),n&&e(t,n),t}}(),s=r(4),a=r(5),u=r(9),l=function(){function e(){var r=this,n=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};i(this,e),this._emitter=new a,this._timerExpiryCheck=null,this.GLOBAL_KEY="___InCache___storage___global___key___",this.DEFAULT_CONFIG={storeName:"",save:!0,filePath:".incache",maxAge:0,expires:null,silent:!1,share:!0,autoRemovePeriod:0,nullIfNotFound:!0,global:{silent:!1,life:0}},this._onRemoved=function(){},this._onCreated=function(){},this._onUpdated=function(){},s.isServer()&&(t.stdin.resume(),t.on("exit",function(){r._write()}),t.on("SIGINT",function(){r._write()})),this.setConfig(n)}return o(e,[{key:"_write",value:function(){var e=this._memory,t=e.config,r=e.data;if(t.save){var n=JSON.stringify(r);u.writeFileSync(t.filePath,n)}}},{key:"_read",value:function(){var e=this._memory.config;if(e.save&&u.existsSync(e.filePath)){var t=u.readFileSync(e.filePath);try{this._storage=this._memory.data=JSON.parse(t)}catch(e){this._storage=this._memory.data={}}}}},{key:"setConfig",value:function(){var e=this,t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};t.global&&(s.deprecated(t.global.life,"global.life is deprecated use maxAge instead"),s.deprecated(t.global.silent,"global.silent is deprecated use silent instead")),s.defaults(t,this.DEFAULT_CONFIG),this._opts=t,this._root=t.share?s.isServer()?n:window:{},t.storeName&&(this.GLOBAL_KEY+=t.storeName),this._root[this.GLOBAL_KEY]||(this._root[this.GLOBAL_KEY]={metadata:{lastSave:null},data:{},config:this.DEFAULT_CONFIG}),this._root[this.GLOBAL_KEY].config=t,this._memory=this._root[this.GLOBAL_KEY],this._storage=this._memory.data,s.isServer()&&this._read(),this._timerExpiryCheck&&(clearInterval(this._timerExpiryCheck),this._timerExpiryCheck=null),t.autoRemovePeriod&&(this._timerExpiryCheck=setInterval(function(){var t=e.removeExpired();t.length&&e._emitter.fire("expired",t)},1e3*t.autoRemovePeriod))}},{key:"getConfig",value:function(){return this._memory.config}},{key:"set",value:function(e,t){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};if(r.silent||!1!==this._emitter.fireTheFirst("beforeSet",e,t)){var n={isNew:!0,createdOn:null,updatedOn:null,expiresOn:null,value:t};return r=s.defaults(r,this.DEFAULT_CONFIG),r.expires&&(s.is(r.expires,"date")||s.is(r.expires,"string"))?n.expiresOn=new Date(r.expires):r.maxAge&&s.is(r.maxAge,"number")?n.expiresOn=s.addMSToNow(r.maxAge):r.life&&s.is(r.life,"number")&&(s.deprecated(r.life,"life is deprecated use maxAge instead"),n.expiresOn=s.addSecondsToNow(r.life)),this.has(e)?(n.isNew=!1,n.createdOn=this._storage[e].createdOn,n.updatedOn=new Date,r.silent||(this._onUpdated.call(this,e,n),this._emitter.fire("update",e,n))):(n.createdOn=new Date,r.silent||(this._onCreated.call(this,e,n),this._emitter.fire("create",e,n))),this._storage[e]=n,r.silent||this._emitter.fire("set",e,n),n}}},{key:"get",value:function(e){var t=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];return this.has(e)?!this._opts.autoRemovePeriod&&this.expired(e)?(this.remove(e,!0),this._opts.nullIfNotFound?null:void 0):t?this._storage[e].value:this._storage[e]:this._opts.nullIfNotFound?null:void 0}},{key:"remove",value:function(e){var t=arguments.length>1&&void 0!==arguments[1]&&arguments[1];(t||!1!==this._emitter.fireTheFirst("beforeRemove",e))&&(delete this._storage[e],t||(this._onRemoved.call(this,e),this._emitter.fire("remove",e)))}},{key:"removeFrom",value:function(e,t){if(!this.has(e))return null;if(s.is(t,"undefined"))throw new Error("where cannot be undefined");var r=this.get(e);if(!s.is(r,"array"))throw new Error("value must be an array");var n=r.length;for(var i in r)if(r.hasOwnProperty(i)){var o=[];for(var a in t)t.hasOwnProperty(a)&&(s.is(t,"object")?o.push(void 0!==r[i][a]&&r[i][a]===t[a]):o.push(r[i]===t));o.length&&-1===o.indexOf(!1)&&r.splice(i,1)}n!==r.length&&this.set(e,r)}},{key:"removeExpired",value:function(){var e=[];for(var t in this._storage)this._storage.hasOwnProperty(t)&&this.expired(t)&&(this.remove(t,!0),e.push(t));return e}},{key:"addTo",value:function(e,t){if(!this.has(e))return null;var r=this.get(e);if(!s.is(r,"array"))throw new Error("object must be an array");return r.push(t),this.set(e,r)}},{key:"prependTo",value:function(e,t){if(!this.has(e))return null;var r=this.get(e);if(!s.is(r,"array"))throw new Error("object must be an array");return r.unshift(t),this.set(e,r)}},{key:"updateIn",value:function(e,t,r){if(!this.has(e))return null;if(s.is(t,"undefined"))throw new Error("value cannot be undefined");if(s.is(r,"undefined"))throw new Error("where cannot be undefined");var n=this.get(e);if(!s.is(n,"array"))throw new Error("value must be an array");var i=!1;for(var o in n)if(n.hasOwnProperty(o)){var a=[];for(var u in r)r.hasOwnProperty(u)&&(s.is(r,"object")?a.push(void 0!==n[o][u]&&n[o][u]===r[u]):a.push(n[o]===r));a.length&&-1===a.indexOf(!1)&&(i=!0,n[o]=t)}i&&this.set(e,n)}},{key:"bulkSet",value:function(e){var t=arguments.length>1&&void 0!==arguments[1]&&arguments[1];if(!s.is(e,"array"))throw new Error("records must be an array of object, e.g. {key: foo, value: bar}");if(t||!1!==this._emitter.fireTheFirst("beforeBulkSet",e)){for(var r=0;r<e.length;r++){if(s.is(e[r].key,"undefined")||s.is(e[r].value,"undefined"))throw new Error("key and value properties are required");this.set(e[r].key,e[r].value,{silent:!0,fromBulk:!0})}t||this._emitter.fire("bulkSet",e)}}},{key:"bulkRemove",value:function(e,t){if(!s.is(e,"array"))throw new Error("keys must be an array of keys");if(t||!1!==this._emitter.fireTheFirst("beforeBulkRemove",e)){for(var r=0;r<e.length;r++)this.remove(e[r],!0);t||this._emitter.fire("bulkRemove",e)}}},{key:"clean",value:function(e){if(!s.is(e,"string"))throw new Error("key must be a string");for(var t in this._storage)this._storage.hasOwnProperty(t)&&-1!==t.indexOf(e)&&delete this._storage[t]}},{key:"all",value:function(){var e=[];for(var t in this._storage)this._storage.hasOwnProperty(t)&&(!this._opts.autoRemovePeriod&&this.expired(t)?this.remove(t,!0):e.push({key:t,value:this._storage[t].value}));return e}},{key:"expired",value:function(e){if(this._storage[e]&&this._storage[e].expiresOn){return new Date>new Date(this._storage[e].expiresOn)}return!1}},{key:"clear",value:function(){this._storage=this._memory.data={}}},{key:"has",value:function(e){return this._storage.hasOwnProperty(e)}},{key:"destroy",value:function(){for(var e=arguments.length,t=Array(e),r=0;r<e;r++)t[r]=arguments[r];this.remove.apply(this,t)}},{key:"on",value:function(e,t){this._emitter.on.call(this._emitter,e,t)}},{key:"onRemoved",value:function(e){this._onRemoved=e}},{key:"onCreated",value:function(e){this._onCreated=e}},{key:"onUpdated",value:function(e){this._onUpdated=e}}]),e}();e.exports=l}).call(t,r(0),r(3))},function(e,t,r){"use strict";var n,i="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e};n=function(){return this}();try{n=n||Function("return this")()||(0,eval)("this")}catch(e){"object"===("undefined"==typeof window?"undefined":i(window))&&(n=window)}e.exports=n},function(e,t,r){"use strict";(function(t){var r="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},n={};n.is=function(e,t){return Object.prototype.toString.call(e).toLowerCase()==="[object "+t+"]".toLowerCase()},n.defaults=function(e,t){for(var i in t)t.hasOwnProperty(i)&&(e.hasOwnProperty(i)?"object"===r(e[i])&&n.defaults(e[i],t[i]):e[i]=t[i]);return e},n.addSecondsToNow=function(e){var t=new Date;return new Date(t.setSeconds(t.getSeconds()+e))},n.addMSToNow=function(e){var t=new Date;return new Date(t.setMilliseconds(t.getMilliseconds()+e))},n.isServer=function(){return"object"===(void 0===t?"undefined":r(t))&&void 0!==t.pid},n.deprecated=function(e,t){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:"warn";return void 0!==e&&(console[r](t||e),!0)},e.exports=n}).call(t,r(0))},function(e,t,r){"use strict";e.exports=r(6)},function(e,t,r){"use strict";function n(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var i=function(){function e(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}return function(t,r,n){return r&&e(t.prototype,r),n&&e(t,n),t}}(),o=r(7),s=r(8),a=function(){function e(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};n(this,e),this.defaultClassOpts={maxListeners:10,asyncDelay:10},this.defaultListenerOpts={maxCalls:0,prepend:!1},this.opts=o.defaults(t,this.defaultClassOpts),this.events={}}return i(e,[{key:"_createEvent",value:function(e,t,r){if(this.events[e]||(this.events[e]=[]),this.opts.maxListeners){var n=this.opts.maxListeners;if(this.events[e].length>=n)throw new Error(s[3]+n)}t.opts=o.defaults(r,this.defaultListenerOpts),t.info={calls:0},r.prepend?this.events[e].unshift(t):this.events[e].push(t),this._created.call(this,e,t,r)}},{key:"_callEvent",value:function(e,t,r){return t.opts.maxCalls&&t.info.calls++>=t.opts.maxCalls?void this.off(e,t):(this._catchAll.call(this,r),t.apply(this,r))}},{key:"_created",value:function(){}},{key:"_removed",value:function(){}},{key:"_catchAll",value:function(){}},{key:"on",value:function(e,t){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};if(!o.is(e,"string"))throw new Error(s[0]);if(e=e.trim(),!e.length)throw new Error(s[4]);if(o.is(t,"array")){for(var n in t)if(t.hasOwnProperty(n)){if(!o.is(t[n],"function"))throw new Error(s[1]);this._createEvent(e,t[n],r)}}else{if(!o.is(t,"function"))throw new Error(s[1]);this._createEvent(e,t,r)}return this}},{key:"once",value:function(e,t){return this.on(e,t,{maxCalls:1})}},{key:"fire",value:function(e){if(this.exists(e)){for(var t=arguments.length,r=Array(t>1?t-1:0),n=1;n<t;n++)r[n-1]=arguments[n];for(var i=0;i<this.events[e].length;i++)this._callEvent(e,this.events[e][i],r)}return this}},{key:"fireTheFirst",value:function(e){for(var t=arguments.length,r=Array(t>1?t-1:0),n=1;n<t;n++)r[n-1]=arguments[n];if(this.exists(e))return this._callEvent(e,this.events[e][0],r)}},{key:"fireAsync",value:function(e){for(var t=this,r=arguments.length,n=Array(r>1?r-1:0),i=1;i<r;i++)n[i-1]=arguments[i];n.unshift(e),setTimeout(function(){t.fire.apply(t,n)},this.opts.asyncDelay)}},{key:"off",value:function(e,t){if(!o.is(e,"string"))throw new Error(s[0]);if(this.events[e])if("function"==typeof t)for(var r=0;r<this.events[e].length;r++)this.events[e][r]===t&&(this.events[e].splice(r,1),this._removed.call(this,e,t));else delete this.events[e],this._removed.call(this,e);return this}},{key:"prependListener",value:function(e,t){return this.on(e,t,{prepend:!0})}},{key:"prependOnceListener",value:function(e,t){return this.on(e,t,{maxCalls:1,prepend:!0})}},{key:"clear",value:function(){return this.events=[],this}},{key:"getListenersCount",value:function(e){return this.getListeners(e).length}},{key:"getListeners",value:function(e){if(!o.is(e,"string"))throw new Error(s[0]);if(!this.exists(e))throw new Error(s[5]);return this.events[e]}},{key:"getEvents",value:function(){return this.events}},{key:"exists",value:function(e){if(!o.is(e,"string"))throw new Error(s[0]);return!o.is(this.events[e],"undefined")}},{key:"getMaxListeners",value:function(){return this.opts.maxListeners}},{key:"setMaxListeners",value:function(e){if(!o.is(e,"number"))throw new Error(s[2]);return this.opts.maxListeners=e,this}},{key:"onCatchAll",value:function(e){return this._catchAll=e,this}},{key:"onCreated",value:function(e){return this._created=e,this}},{key:"onRemoved",value:function(e){return this._removed=e,this}}]),e}();e.exports=a,e.exports._error=s},function(e,t,r){"use strict";var n="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},i={};i.is=function(e,t){return Object.prototype.toString.call(e).toLowerCase()==="[object "+t+"]".toLowerCase()},i.defaults=function(e,t){for(var r in t)t.hasOwnProperty(r)&&(e.hasOwnProperty(r)?"object"===n(e[r])&&i.defaults(e[r],t[r]):e[r]=t[r]);return e},e.exports=i},function(e,t,r){"use strict";e.exports=["event name is required and must be a string","listener is required and must be a function or an array of function","value must be a number","increase maxListeners per event: ","event name not valid","event not found"]},function(e,t,r){"use strict"}]); |
{ | ||
"name": "incache", | ||
"version": "4.2.1", | ||
"version": "5.0.0", | ||
"description": "Simple key/value in-memory storage or on disk to persist some data", | ||
@@ -53,3 +53,5 @@ "main": "index.js", | ||
}, | ||
"dependencies": {} | ||
"dependencies": { | ||
"flak": "^0.3.0" | ||
} | ||
} |
@@ -47,7 +47,7 @@ <div align="center"> | ||
### Events | ||
### Some events | ||
```javascript | ||
// Triggered when a record has been deleted | ||
incache.onRemoved(key => { | ||
incache.on('remove', key => { | ||
console.log(key); | ||
@@ -57,3 +57,3 @@ }); | ||
// Triggered when a record has been created | ||
incache.onCreated((key, record) => { | ||
incache.on('create', (key, record) => { | ||
console.log(key, record); | ||
@@ -63,3 +63,3 @@ }); | ||
//Triggered when a record has been updated | ||
incache.onUpdated((key, record) => { | ||
incache.on('update', (key, record) => { | ||
console.log(key, record); | ||
@@ -70,3 +70,3 @@ }); | ||
### API | ||
See <a href="https://github.com/fabioricali/incache/blob/master/api.md">full documentation</a> | ||
Please see the **<a href="https://github.com/fabioricali/incache/blob/master/api.md">full documentation</a>** for more details. | ||
@@ -73,0 +73,0 @@ ### Browser |
const helper = require('./helper'); | ||
const Flak = require('flak'); | ||
const fs = require('fs'); | ||
@@ -16,5 +17,8 @@ | ||
* @param [opts.share=true] {boolean} if true use global object as storage | ||
* @param [opts.autoRemovePeriod=0] {number} period in seconds to remove expired records. When set, the records will be removed only on check, when 0 it won't run | ||
* @param [opts.nullIfNotFound=true] {boolean} calling `get` if the key is not found returns `null`. If false returns `undefined` | ||
* @param [opts.global] {Object} **deprecated:** global record configuration | ||
* @param [opts.global.silent=false] {boolean} **deprecated:** if true no event will be triggered, use `silent` instead | ||
* @param [opts.global.life=0] {number} **deprecated:** max age in seconds. If 0 not expire, use `maxAge` instead | ||
* @fires InCache#expired | ||
* @constructor | ||
@@ -24,2 +28,6 @@ */ | ||
this._emitter = new Flak(); | ||
this._timerExpiryCheck = null; | ||
/** | ||
@@ -30,3 +38,2 @@ * Global key | ||
*/ | ||
this.GLOBAL_KEY = '___InCache___storage___global___key___'; | ||
@@ -37,3 +44,3 @@ | ||
* @ignore | ||
* @type {{storeName: string, save: boolean, filePath: string, maxAge: number, expires: null, silent: boolean, global: {silent: boolean, life: number}}} | ||
* @type {{storeName: string, save: boolean, filePath: string, maxAge: number, expires: null, silent: boolean, share: boolean, global: {silent: boolean, life: number}}} | ||
*/ | ||
@@ -48,2 +55,4 @@ this.DEFAULT_CONFIG = { | ||
share: true, | ||
autoRemovePeriod: 0, | ||
nullIfNotFound: true, | ||
global: { | ||
@@ -110,2 +119,4 @@ silent: false, | ||
this._opts = opts; | ||
/** | ||
@@ -142,2 +153,16 @@ * Root object | ||
this._read(); | ||
if (this._timerExpiryCheck) { | ||
clearInterval(this._timerExpiryCheck); | ||
this._timerExpiryCheck = null; | ||
} | ||
if (opts.autoRemovePeriod) { | ||
this._timerExpiryCheck = setInterval(() => { | ||
let expired = this.removeExpired(); | ||
if(expired.length){ | ||
this._emitter.fire('expired', expired); | ||
} | ||
}, opts.autoRemovePeriod * 1000); | ||
} | ||
} | ||
@@ -172,3 +197,7 @@ | ||
* @param [opts.life=0] {number} **deprecated:** max age in seconds. If 0 not expire. (overwrites global configuration) | ||
* @returns {InCache~record} | ||
* @returns {InCache~record|*} | ||
* @fires InCache#beforeSet | ||
* @fires InCache#create | ||
* @fires InCache#update | ||
* @fires InCache#set | ||
* @example | ||
@@ -180,2 +209,7 @@ * inCache.set('my key', 'my value'); | ||
set(key, value, opts = {}) { | ||
if (!opts.silent && this._emitter.fireTheFirst('beforeSet', key, value) === false) { | ||
return; | ||
} | ||
let record = { | ||
@@ -204,8 +238,12 @@ isNew: true, | ||
record.updatedOn = new Date(); | ||
if (!opts.silent) | ||
if (!opts.silent) { | ||
this._onUpdated.call(this, key, record); | ||
this._emitter.fire('update', key, record); | ||
} | ||
} else { | ||
record.createdOn = new Date(); | ||
if (!opts.silent) | ||
if (!opts.silent) { | ||
this._onCreated.call(this, key, record); | ||
this._emitter.fire('create', key, record); | ||
} | ||
} | ||
@@ -215,2 +253,6 @@ | ||
if (!opts.silent) { | ||
this._emitter.fire('set', key, record); | ||
} | ||
return record; | ||
@@ -229,9 +271,9 @@ } | ||
if (this.has(key)) { | ||
if (this.expired(key)) { | ||
if (!this._opts.autoRemovePeriod && this.expired(key)) { | ||
this.remove(key, true); | ||
return null; | ||
return (this._opts.nullIfNotFound ? null : undefined); | ||
} | ||
return onlyValue ? this._storage[key].value : this._storage[key]; | ||
} else { | ||
return null; | ||
return (this._opts.nullIfNotFound ? null : undefined); | ||
} | ||
@@ -244,2 +286,4 @@ } | ||
* @param [silent=false] {boolean} if true no event will be triggered | ||
* @fires InCache#beforeRemove | ||
* @fires InCache#remove | ||
* @example | ||
@@ -249,5 +293,10 @@ * inCache.remove('my key'); | ||
remove(key, silent = false) { | ||
if (!silent && this._emitter.fireTheFirst('beforeRemove', key) === false) { | ||
return; | ||
} | ||
delete this._storage[key]; | ||
if (!silent) | ||
if (!silent) { | ||
this._onRemoved.call(this, key); | ||
this._emitter.fire('remove', key); | ||
} | ||
} | ||
@@ -298,2 +347,3 @@ | ||
* Remove expired records | ||
* @returns {Array} expired keys | ||
* @example | ||
@@ -309,7 +359,10 @@ * inCache.set('my key 1', 'my value'); | ||
removeExpired() { | ||
const expired = []; | ||
for (let key in this._storage) { | ||
if (this._storage.hasOwnProperty(key) && this.expired(key)) { | ||
this.remove(key, true); | ||
expired.push(key); | ||
} | ||
} | ||
return expired; | ||
} | ||
@@ -413,2 +466,5 @@ | ||
* @param records {array} array of object, e.g. [{key: foo1, value: bar1},{key: foo2, value: bar2}] | ||
* @param [silent=false] {boolean} if true no event will be triggered | ||
* @fires InCache#beforeBulkSet | ||
* @fires InCache#bulkSet | ||
* @example | ||
@@ -422,6 +478,10 @@ * inCache.bulkSet([ | ||
*/ | ||
bulkSet(records) { | ||
bulkSet(records, silent = false) { | ||
if (!helper.is(records, 'array')) | ||
throw new Error('records must be an array of object, e.g. {key: foo, value: bar}'); | ||
if (!silent && this._emitter.fireTheFirst('beforeBulkSet', records) === false) { | ||
return; | ||
} | ||
for (let i = 0; i < records.length; i++) { | ||
@@ -432,2 +492,6 @@ if (helper.is(records[i].key, 'undefined') || helper.is(records[i].value, 'undefined')) | ||
} | ||
if (!silent) { | ||
this._emitter.fire('bulkSet', records); | ||
} | ||
} | ||
@@ -438,12 +502,23 @@ | ||
* @param keys {array} an array of keys | ||
* @param [silent=false] {boolean} if true no event will be triggered | ||
* @fires InCache#beforeBulkRemove | ||
* @fires InCache#bulkRemove | ||
* @example | ||
* inCache.bulkRemove(['key1', 'key2', 'key3']); | ||
*/ | ||
bulkRemove(keys) { | ||
bulkRemove(keys, silent) { | ||
if (!helper.is(keys, 'array')) | ||
throw new Error('keys must be an array of keys'); | ||
if (!silent && this._emitter.fireTheFirst('beforeBulkRemove', keys) === false) { | ||
return; | ||
} | ||
for (let i = 0; i < keys.length; i++) { | ||
this.remove(keys[i], true); | ||
} | ||
if (!silent) { | ||
this._emitter.fire('bulkRemove', keys); | ||
} | ||
} | ||
@@ -478,3 +553,3 @@ | ||
if (this._storage.hasOwnProperty(key)) { | ||
if (this.expired(key)) { | ||
if (!this._opts.autoRemovePeriod && this.expired(key)) { | ||
this.remove(key, true); | ||
@@ -540,4 +615,97 @@ } else { | ||
/** | ||
* Triggered when a record has been deleted | ||
* Adds listener to instance | ||
* @param eventName {string} event name | ||
* @param callback {Function} callback | ||
*/ | ||
on(eventName, callback) { | ||
this._emitter.on.call(this._emitter, eventName, callback); | ||
} | ||
/** | ||
* Triggered before set | ||
* @event InCache#beforeSet | ||
* @param key {string} key | ||
* @param value {string} value | ||
* @since 5.0.0 | ||
*/ | ||
/** | ||
* Triggered after set | ||
* @event InCache#set | ||
* @param key {string} key | ||
* @param record {InCache~record} record object | ||
* @since 5.0.0 | ||
*/ | ||
/** | ||
* Triggered after create the record | ||
* @event InCache#create | ||
* @param key {string} key of record | ||
* @param record {InCache~record} record object | ||
* @since 5.0.0 | ||
*/ | ||
/** | ||
* Triggered after update the record | ||
* @event InCache#update | ||
* @param key {string} key of record | ||
* @param record {InCache~record} record object | ||
* @since 5.0.0 | ||
*/ | ||
/** | ||
* Triggered before remove the record | ||
* @event InCache#beforeRemove | ||
* @param key {string} key of record to be removed | ||
* @since 5.0.0 | ||
*/ | ||
/** | ||
* Triggered after record has been removed | ||
* @event InCache#remove | ||
* @param key {string} key of record | ||
* @since 5.0.0 | ||
*/ | ||
/** | ||
* Triggered before bulk set | ||
* @event InCache#beforeBulkSet | ||
* @param records {array} array of objects | ||
* @since 5.0.0 | ||
*/ | ||
/** | ||
* Triggered after bulk set | ||
* @event InCache#bulkSet | ||
* @param records {array} array of objects | ||
* @since 5.0.0 | ||
*/ | ||
/** | ||
* Triggered before remove the records | ||
* @event InCache#beforeBulkRemove | ||
* @param keys {array} array of keys to be removed | ||
* @since 5.0.0 | ||
*/ | ||
/** | ||
* Triggered after records have been removed | ||
* @event InCache#bulkRemove | ||
* @param keys {array} array of keys removed | ||
* @since 5.0.0 | ||
*/ | ||
/** | ||
* Triggered when records are expired and `opts.autoRemovePeriod` is set | ||
* @event InCache#expired | ||
* @param keys {array} array of keys expired | ||
* @since 5.0.0 | ||
*/ | ||
/***************************** DEPRECATED ********************************/ | ||
/** | ||
* Triggered when a record has been deleted. **Deprecated:** use `on('remove', callback)` instead | ||
* @param callback {InCache~removedCallback} callback function | ||
* @deprecated | ||
* @example | ||
@@ -556,7 +724,9 @@ * inCache.onRemoved((key)=>{ | ||
* @param key {string} key of record removed | ||
* @deprecated | ||
*/ | ||
/** | ||
* Triggered when a record has been created | ||
* Triggered when a record has been created. **Deprecated:** use `on('create', callback)` instead | ||
* @param callback {InCache~createdCallback} callback function | ||
* @deprecated | ||
* @example | ||
@@ -576,7 +746,9 @@ * inCache.onCreated((key, record)=>{ | ||
* @param record {InCache~record} record object | ||
* @deprecated | ||
*/ | ||
/** | ||
* Triggered when a record has been updated | ||
* Triggered when a record has been updated. **Deprecated:** use `on('update', callback)` instead | ||
* @param callback {InCache~updatedCallback} callback function | ||
* @deprecated | ||
* @example | ||
@@ -596,2 +768,3 @@ * inCache.onUpdated((key, record)=>{ | ||
* @param record {InCache~record} record object | ||
* @deprecated | ||
*/ | ||
@@ -598,0 +771,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
104628
2234
1
+ Addedflak@^0.3.0
+ Addedflak@0.3.1(transitive)