Socket
Socket
Sign inDemoInstall

storage-based-queue

Package Overview
Dependencies
Maintainers
1
Versions
23
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

storage-based-queue - npm Package Compare versions

Comparing version 0.0.5-beta6 to 0.0.5-beta7

src/config.data.js

2

dist/queue.min.js

@@ -1,1 +0,1 @@

!function t(e,n,r){function i(a,u){if(!n[a]){if(!e[a]){var c="function"==typeof require&&require;if(!u&&c)return c(a,!0);if(o)return o(a,!0);var s=new Error("Cannot find module '"+a+"'");throw s.code="MODULE_NOT_FOUND",s}var l=n[a]={exports:{}};e[a][0].call(l.exports,function(t){var n=e[a][1][t];return i(n||t)},l,l.exports,t,e,n,r)}return n[a].exports}for(var o="function"==typeof require&&require,a=0;a<r.length;a++)i(r[a]);return i}({1:[function(t,e,n){"use strict";Object.defineProperty(n,"__esModule",{value:!0}),n.default={storage:"localstorage",prefix:"sq_jobs",timeout:1e3,limit:-1,principle:"fifo"}},{}],2:[function(t,e,n){"use strict";Object.defineProperty(n,"__esModule",{value:!0});var r=function(){function t(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}return function(e,n,r){return n&&t(e.prototype,n),r&&t(e,r),e}}(),i=function(t){return t&&t.__esModule?t:{default:t}}(t("./config.data")),o=function(){function t(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t),this.config=i.default,this.merge(e)}return r(t,[{key:"set",value:function(t,e){this.config[t]=e}},{key:"get",value:function(t){return this.config[t]}},{key:"has",value:function(t){return Object.prototype.hasOwnProperty.call(this.config,t)}},{key:"merge",value:function(t){this.config=Object.assign({},this.config,t)}},{key:"remove",value:function(t){return delete this.config[t]}},{key:"all",value:function(){return this.config}}]),t}();n.default=o},{"./config.data":1}],3:[function(t,e,n){"use strict";Object.defineProperty(n,"__esModule",{value:!0});var r=function(){function t(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}return function(e,n,r){return n&&t(e.prototype,n),r&&t(e,r),e}}(),i=function(){function t(){!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t),this._container={}}return r(t,[{key:"has",value:function(t){return Object.prototype.hasOwnProperty.call(this._container,t)}},{key:"get",value:function(t){return this._container[t]}},{key:"all",value:function(){return this._container}},{key:"bind",value:function(t,e){this._container[t]=e}},{key:"remove",value:function(t){Object.prototype.hasOwnProperty.call(this._container,t)&&delete this._container[t]}},{key:"removeAll",value:function(){this._container={}}}]),t}();n.default=i},{}],4:[function(t,e,n){"use strict";Object.defineProperty(n,"__esModule",{value:!0});var r=function(){function t(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}return function(e,n,r){return n&&t(e.prototype,n),r&&t(e,r),e}}(),i=function(){function t(){!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t),this.store={},this.verifierPattern=/^[a-z0-9\-\_]+\:before$|after$|retry$|\*$/,this.wildcards=["*","error"],this.emptyFunc=function(){},this.store.before={},this.store.after={},this.store.retry={},this.store.wildcard={},this.store.error=this.emptyFunc,this.store["*"]=this.emptyFunc}return r(t,[{key:"on",value:function(t,e){if("function"!=typeof e)throw new Error("Event should be an function");this.isValid(t)&&this.add(t,e)}},{key:"emit",value:function(t,e){if(this.wildcards.indexOf(t)>-1)this.wildcard.apply(this,[t].concat(Array.prototype.slice.call(arguments)));else{var n=this.getType(t),r=this.getName(t);if(this.store[n]){(this.store[n][r]||this.emptyFunc).call(null,e)}}this.wildcard("*",t,e)}},{key:"wildcard",value:function(t,e,n){this.store.wildcard[t]&&this.store.wildcard[t].call(null,e,n)}},{key:"add",value:function(t,e){if(this.wildcards.indexOf(t)>-1)this.store.wildcard[t]=e;else{var n=this.getType(t),r=this.getName(t);this.store[n][r]=e}}},{key:"has",value:function(t){try{var e=t.split(":");return e.length>1?!!this.store[e[1]][e[0]]:!!this.store.wildcard[e[0]]}catch(t){return!1}}},{key:"getName",value:function(t){return t.match(/(.*)\:.*/)[1]}},{key:"getType",value:function(t){return t.match(/^[a-z0-9\-\_]+\:(.*)/)[1]}},{key:"isValid",value:function(t){return this.verifierPattern.test(t)||this.wildcards.indexOf(t)>-1}}]),t}();n.default=i},{}],5:[function(t,e,n){"use strict";Object.defineProperty(n,"__esModule",{value:!0});var r=function(t){return t&&t.__esModule?t:{default:t}}(t("./queue"));window.Queue=r.default,n.default=r.default},{"./queue":6}],6:[function(t,e,n){"use strict";function r(t){return t&&t.__esModule?t:{default:t}}Object.defineProperty(n,"__esModule",{value:!0});var i=function(){return function(t,e){if(Array.isArray(t))return t;if(Symbol.iterator in Object(t))return function(t,e){var n=[],r=!0,i=!1,o=void 0;try{for(var a,u=t[Symbol.iterator]();!(r=(a=u.next()).done)&&(n.push(a.value),!e||n.length!==e);r=!0);}catch(t){i=!0,o=t}finally{try{!r&&u.return&&u.return()}finally{if(i)throw o}}return n}(t,e);throw new TypeError("Invalid attempt to destructure non-iterable instance")}}(),o=r(t("./container")),a=r(t("./storage-capsule")),u=r(t("./config")),c=r(t("./event")),s=t("./utils"),l=r(t("./storage/localstorage")),f=function(){function t(t){(function(t){this.currentChannel,this.currentTimeout,this.stopped=!0,this.running=!1,this.channels={},this.config=new u.default(t),this.storage=new a.default(this.config,new l.default(this.config)),this.event=new c.default,this.container=new o.default,this.timeout=this.config.get("timeout")}).call(this,t)}function e(){return r.call(this).all().filter(s.excludeSpecificTasks.bind(["freezed"]))}function n(t,e){"tag"in t&&(this.event.emit(t.tag+":"+e,t),this.event.emit(t.tag+":*",t))}function r(){return this.storage.channel(this.currentChannel)}function f(t){return r.call(this).save(t)}function f(t){return r.call(this).save(function(t){t.priority=t.priority||0,isNaN(t.priority)&&(t.priority=0);return t}(t))}function h(){var t=this.config.get("timeout");return this.currentTimeout=setTimeout(function(){var t,e=r.call(this).fetch().shift();if(void 0===e)return console.log("-> "+this.currentChannel+" channel is empty..."),void y.call(this);this.container.has(e.handler)||console.warn(e.handler+"-> job not found");var i=this.container.get(e.handler),o=new i.handler;(function(t){return r.call(this).update(t._id,{locked:!0})}).call(this,e),d.call(this,"before",o,e.args),n.call(this,e,"before");var a=Object.values(i.deps||{});(t=o.handle).call.apply(t,[o,e.args].concat(function(t){if(Array.isArray(t)){for(var e=0,n=Array(t.length);e<t.length;e++)n[e]=t[e];return n}return Array.from(t)}(a))).then(function(t,e){var i=this;return function(o){o?function(t,e){p.call(this,t._id)}.call(i,t,e):function(t,e){n.call(this,t,"retry");var i=function(t,e){"retry"in e||(e.retry=1);"tried"in t||(t.tried=0,t.retry=e.retry);++t.tried,t.tried>=e.retry&&(t.freezed=!0);return t}.call(this,t,e);return i.locked=!1,r.call(this).update(t._id,i)}.call(i,t,e),d.call(this,"after",e,t.args),n.call(this,t,"after"),i.next()}}.call(this,e,o).bind(this)).catch(function(t,e){var n=this;return function(e){p.call(n,t._id),n.event.emit("error",t),n.next()}}.call(this,e,o).bind(this))}.bind(this),t)}function p(t){return r.call(this).delete(t)}function d(t,e,n){(0,s.hasMethod)(e,t)&&("before"==t&&(0,s.isFunction)(e.before)?e.before.call(e,n):"after"==t&&(0,s.isFunction)(e.after)&&e.after.call(e,n))}function y(){this.stop(),this.currentTimeout&&(this.currentTimeout=clearTimeout(this.currentTimeout))}return t.FIFO="fifo",t.LIFO="lifo",t.prototype.add=function(t){var e=f.call(this,t);return e&&this.stopped&&!0===this.running&&this.start(),e},t.prototype.next=function(){if(this.stopped)return console.log("[stopped]-> next"),function(){this.running=!1}.call(this),y.call(this);console.log("[next]->"),this.start()},t.prototype.start=function(){return this.stopped=!1,function(){if(!t.isRegistered){var e=t.jobs||[],n=!0,r=!1,o=void 0;try{for(var a,u=e[Symbol.iterator]();!(n=(a=u.next()).done);n=!0){var c=a.value,s=c.handler.toString().match(/function\s([a-zA-Z_]+).*?/),l=i(s,2),f=(l[0],l[1]);f&&this.container.bind(f,c)}}catch(t){r=!0,o=t}finally{try{!n&&u.return&&u.return()}finally{if(r)throw o}}t.isRegistered=!0}}.call(this),this.running=h.call(this)>0,console.log("[started]->",this.running),this.running},t.prototype.stop=function(){console.log("[stopping]->"),this.stopped=!0},t.prototype.forceStop=function(){console.log("[forceStopped]->"),y.call(this)},t.prototype.create=function(t){return t in this.channels||(this.currentChannel=t,this.channels[t]=(0,s.clone)(this)),this.channels[t]},t.prototype.channel=function(t){if(!this.channels[t])throw new Error('Channel of "'+t+'" not found');return this.channels[t]},t.prototype.isEmpty=function(){return this.count()<1},t.prototype.count=function(){return e.call(this).length},t.prototype.countByTag=function(t){return e.call(this).filter(function(e){return e.tag===t}).length},t.prototype.clear=function(){this.currentChannel&&this.storage.clear(this.currentChannel)},t.prototype.clearByTag=function(t){var e=this;r.call(this).all().filter(s.utilClearByTag.bind(t)).forEach(function(t){return r.call(e).delete(t._id)})},t.prototype.has=function(t){return e.call(this).findIndex(function(e){return e._id===t})>-1},t.prototype.hasByTag=function(t){return e.call(this).findIndex(function(e){return e.tag===t})>-1},t.prototype.setTimeout=function(t){this.timeout=t,this.config.set("timeout",t)},t.prototype.setLimit=function(t){this.config.set("limit",t)},t.prototype.setPrefix=function(t){this.config.set("prefix",t)},t.prototype.setPrinciple=function(t){this.config.set("principle",t)},t.prototype.on=function(t,e){var n;(n=this.event).on.apply(n,arguments)},t.prototype.error=function(t){this.event.on("error",t)},t.register=function(e){if(!(e instanceof Array))throw new Error("Queue jobs should be objects within an array");t.isRegistered=!1,t.jobs=e},t}();n.default=f},{"./config":2,"./container":3,"./event":4,"./storage-capsule":7,"./storage/localstorage":8,"./utils":9}],7:[function(t,e,n){"use strict";function r(t){return t&&t.__esModule?t:{default:t}}Object.defineProperty(n,"__esModule",{value:!0});var i=function(){function t(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}return function(e,n,r){return n&&t(e.prototype,n),r&&t(e,r),e}}(),o=r(t("group-by")),a=(r(t("./storage/localstorage")),r(t("./config")),t("./utils")),u=function(){function t(e,n){!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t),this.storage=n,this.config=e}return i(t,[{key:"channel",value:function(t){return this.storageChannel=t,this}},{key:"fetch",value:function(){var t=this,e=this.all().filter(a.excludeSpecificTasks),n=(0,o.default)(e,"priority");return Object.keys(n).map(function(t){return parseInt(t)}).sort(function(t,e){return e-t}).reduce(function(e,r){return"lifo"===t.config.get("principle")?e.concat(n[r].sort(a.lifo)):e.concat(n[r].sort(a.fifo))},[])}},{key:"save",value:function(t){try{var e=this.storage.get(this.storageChannel);return this.isExceeded()?(console.warn("Task limit exceeded: The '"+this.storageChannel+"' channel limit is "+this.config.get("limit")),!1):(t=this.prepareTask(t),e.push(t),this.storage.set(this.storageChannel,JSON.stringify(e)),t._id)}catch(t){return!1}}},{key:"update",value:function(t,e){try{var n=this.all(),r=n.findIndex(function(e){return e._id==t});return!(r<0)&&(n[r]=Object.assign({},n[r],e),this.storage.set(this.storageChannel,JSON.stringify(n)),!0)}catch(t){return!1}}},{key:"delete",value:function(t){try{var e=this.all(),n=e.findIndex(function(e){return e._id===t});return!(n<0)&&(delete e[n],this.storage.set(this.storageChannel,JSON.stringify(e.filter(function(t){return t}))),!0)}catch(t){return!1}}},{key:"all",value:function(){return this.storage.get(this.storageChannel)}},{key:"generateId",value:function(){return(65536*(1+Math.random())).toString(16)}},{key:"prepareTask",value:function(t){return t.createdAt=Date.now(),t._id=this.generateId(),t}},{key:"isExceeded",value:function(){var t=this.config.get("limit"),e=this.all().filter(a.excludeSpecificTasks);return!(-1===t||t>e.length)}},{key:"clear",value:function(t){this.storage.clear(t)}}]),t}();n.default=u},{"./config":2,"./storage/localstorage":8,"./utils":9,"group-by":11}],8:[function(t,e,n){"use strict";Object.defineProperty(n,"__esModule",{value:!0});var r=function(){function t(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}return function(e,n,r){return n&&t(e.prototype,n),r&&t(e,r),e}}(),i=function(){function t(e){!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t),this.storage=localStorage,this.config=e}return r(t,[{key:"get",value:function(t){try{var e=this.storageName(t);return this.has(e)?JSON.parse(this.storage.getItem(e)):[]}catch(t){return[]}}},{key:"set",value:function(t,e){this.storage.setItem(this.storageName(t),e)}},{key:"has",value:function(t){return t in this.storage}},{key:"clear",value:function(t){this.storage.removeItem(this.storageName(t))}},{key:"clearAll",value:function(){this.storage.clear()}},{key:"storageName",value:function(t){return this.getPrefix()+"_"+t}},{key:"getPrefix",value:function(){return this.config.get("prefix")}}]),t}();n.default=i},{}],9:[function(t,e,n){"use strict";function r(t,e){return Object.prototype.hasOwnProperty.call(t,e)}function i(t){var e=Array.isArray(this)?this:["freezed","locked"],n=[],i=!0,o=!1,a=void 0;try{for(var u,c=e[Symbol.iterator]();!(i=(u=c.next()).done);i=!0){var s=u.value;n.push(!1===r(t,s)||!1===t[s])}}catch(t){o=!0,a=t}finally{try{!i&&c.return&&c.return()}finally{if(o)throw a}}return!(n.indexOf(!1)>-1)}Object.defineProperty(n,"__esModule",{value:!0}),n.clone=function(t){var e=Object.create(Object.getPrototypeOf(t),Object.getOwnPropertyNames(t).reduce(function(e,n){return e[n]=Object.getOwnPropertyDescriptor(t,n),e},{}));return Object.isExtensible(t)||Object.preventExtensions(e),Object.isSealed(t)&&Object.seal(e),Object.isFrozen(t)&&Object.freeze(e),e},n.hasProperty=r,n.hasMethod=function(t,e){return t instanceof Object&&e in t},n.isFunction=function(t){return t instanceof Function},n.excludeSpecificTasks=i,n.utilClearByTag=function(t){return!!i.call(["locked"],t)&&t.tag===this},n.fifo=function(t,e){return t.createdAt-e.createdAt},n.lifo=function(t,e){return e.createdAt-t.createdAt}},{}],10:[function(t,e,n){var r=/\b(Array|Date|Object|Math|JSON)\b/g;e.exports=function(t,e){var n=function(t){for(var e=[],n=0;n<t.length;n++)~e.indexOf(t[n])||e.push(t[n]);return e}(function(t){return t.replace(/\.\w+|\w+ *\(|"[^"]*"|'[^']*'|\/([^/]+)\//g,"").replace(r,"").match(/[a-zA-Z_]\w*/g)||[]}(t));return e&&"string"==typeof e&&(e=function(t){return function(e){return t+e}}(e)),e?function(t,e,n){return t.replace(/\.\w+|\w+ *\(|"[^"]*"|'[^']*'|\/([^/]+)\/|[a-zA-Z_]\w*/g,function(t){return"("==t[t.length-1]?n(t):~e.indexOf(t)?n(t):t})}(t,n,e):n}},{}],11:[function(t,e,n){var r=t("to-function");e.exports=function(t,e){var n,i={};e=r(e);for(var o=0;o<t.length;++o)i[n=e(t[o],o)]=i[n]||[],i[n].push(t[o]);return i}},{"to-function":12}],12:[function(t,e,n){function r(t){switch({}.toString.call(t)){case"[object Object]":return function(t){var e={};for(var n in t)e[n]="string"==typeof t[n]?i(t[n]):r(t[n]);return function(t){if("object"!=typeof t)return!1;for(var n in e){if(!(n in t))return!1;if(!e[n](t[n]))return!1}return!0}}(t);case"[object Function]":return t;case"[object String]":return function(t){return/^ *\W+/.test(t)?new Function("_","return _ "+t):new Function("_","return "+function(t){var e=o(t);if(!e.length)return"_."+t;var n,r,i;for(r=0;r<e.length;r++)i=e[r],t=function(t,e,n){return e.replace(new RegExp("(\\.)?"+t,"g"),function(t,e){return e?t:n})}(i,t,n="('function' == typeof "+(n="_."+i)+" ? "+n+"() : "+n+")");return t}(t))}(t);case"[object RegExp]":return function(t){return function(e){return t.test(e)}}(t);default:return i(t)}}function i(t){return function(e){return t===e}}var o;try{o=t("props")}catch(e){o=t("component-props")}e.exports=r},{"component-props":10,props:10}]},{},[5]);
!function t(e,n,r){function i(a,u){if(!n[a]){if(!e[a]){var c="function"==typeof require&&require;if(!u&&c)return c(a,!0);if(o)return o(a,!0);var s=new Error("Cannot find module '"+a+"'");throw s.code="MODULE_NOT_FOUND",s}var l=n[a]={exports:{}};e[a][0].call(l.exports,function(t){var n=e[a][1][t];return i(n||t)},l,l.exports,t,e,n,r)}return n[a].exports}for(var o="function"==typeof require&&require,a=0;a<r.length;a++)i(r[a]);return i}({1:[function(t,e,n){var r=/\b(Array|Date|Object|Math|JSON)\b/g;e.exports=function(t,e){var n=function(t){for(var e=[],n=0;n<t.length;n++)~e.indexOf(t[n])||e.push(t[n]);return e}(function(t){return t.replace(/\.\w+|\w+ *\(|"[^"]*"|'[^']*'|\/([^/]+)\//g,"").replace(r,"").match(/[a-zA-Z_]\w*/g)||[]}(t));return e&&"string"==typeof e&&(e=function(t){return function(e){return t+e}}(e)),e?function(t,e,n){return t.replace(/\.\w+|\w+ *\(|"[^"]*"|'[^']*'|\/([^/]+)\/|[a-zA-Z_]\w*/g,function(t){return"("==t[t.length-1]?n(t):~e.indexOf(t)?n(t):t})}(t,n,e):n}},{}],2:[function(t,e,n){var r=t("to-function");e.exports=function(t,e){var n,i={};e=r(e);for(var o=0;o<t.length;++o)i[n=e(t[o],o)]=i[n]||[],i[n].push(t[o]);return i}},{"to-function":3}],3:[function(t,e,n){function r(t){switch({}.toString.call(t)){case"[object Object]":return function(t){var e={};for(var n in t)e[n]="string"==typeof t[n]?i(t[n]):r(t[n]);return function(t){if("object"!=typeof t)return!1;for(var n in e){if(!(n in t))return!1;if(!e[n](t[n]))return!1}return!0}}(t);case"[object Function]":return t;case"[object String]":return function(t){return/^ *\W+/.test(t)?new Function("_","return _ "+t):new Function("_","return "+function(t){var e=o(t);if(!e.length)return"_."+t;var n,r,i;for(r=0;r<e.length;r++)i=e[r],t=function(t,e,n){return e.replace(new RegExp("(\\.)?"+t,"g"),function(t,e){return e?t:n})}(i,t,n="('function' == typeof "+(n="_."+i)+" ? "+n+"() : "+n+")");return t}(t))}(t);case"[object RegExp]":return function(t){return function(e){return t.test(e)}}(t);default:return i(t)}}function i(t){return function(e){return t===e}}var o;try{o=t("props")}catch(e){o=t("component-props")}e.exports=r},{"component-props":1,props:1}],4:[function(t,e,n){"use strict";Object.defineProperty(n,"__esModule",{value:!0}),n.default={storage:"localstorage",prefix:"sq_jobs",timeout:1e3,limit:-1,principle:"fifo"}},{}],5:[function(t,e,n){"use strict";Object.defineProperty(n,"__esModule",{value:!0});var r=function(){function t(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}return function(e,n,r){return n&&t(e.prototype,n),r&&t(e,r),e}}(),i=function(t){return t&&t.__esModule?t:{default:t}}(t("./config.data")),o=function(){function t(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t),this.config=i.default,this.merge(e)}return r(t,[{key:"set",value:function(t,e){this.config[t]=e}},{key:"get",value:function(t){return this.config[t]}},{key:"has",value:function(t){return Object.prototype.hasOwnProperty.call(this.config,t)}},{key:"merge",value:function(t){console.log(this.config,t),this.config=Object.assign({},this.config,t)}},{key:"remove",value:function(t){return delete this.config[t]}},{key:"all",value:function(){return this.config}}]),t}();n.default=o},{"./config.data":4}],6:[function(t,e,n){"use strict";Object.defineProperty(n,"__esModule",{value:!0});var r=function(){function t(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}return function(e,n,r){return n&&t(e.prototype,n),r&&t(e,r),e}}(),i=function(){function t(){!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t),this._container={}}return r(t,[{key:"has",value:function(t){return Object.prototype.hasOwnProperty.call(this._container,t)}},{key:"get",value:function(t){return this._container[t]}},{key:"all",value:function(){return this._container}},{key:"bind",value:function(t,e){this._container[t]=e}},{key:"remove",value:function(t){Object.prototype.hasOwnProperty.call(this._container,t)&&delete this._container[t]}},{key:"removeAll",value:function(){this._container={}}}]),t}();n.default=i},{}],7:[function(t,e,n){"use strict";Object.defineProperty(n,"__esModule",{value:!0});var r=function(){function t(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}return function(e,n,r){return n&&t(e.prototype,n),r&&t(e,r),e}}(),i=function(){function t(){!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t),this.store={},this.verifierPattern=/^[a-z0-9\-\_]+\:before$|after$|retry$|\*$/,this.wildcards=["*","error"],this.emptyFunc=function(){},this.store.before={},this.store.after={},this.store.retry={},this.store.wildcard={},this.store.error=this.emptyFunc,this.store["*"]=this.emptyFunc}return r(t,[{key:"on",value:function(t,e){if("function"!=typeof e)throw new Error("Event should be an function");this.isValid(t)&&this.add(t,e)}},{key:"emit",value:function(t,e){if(this.wildcards.indexOf(t)>-1)this.wildcard.apply(this,[t].concat(Array.prototype.slice.call(arguments)));else{var n=this.getType(t),r=this.getName(t);if(this.store[n]){(this.store[n][r]||this.emptyFunc).call(null,e)}}this.wildcard("*",t,e)}},{key:"wildcard",value:function(t,e,n){this.store.wildcard[t]&&this.store.wildcard[t].call(null,e,n)}},{key:"add",value:function(t,e){if(this.wildcards.indexOf(t)>-1)this.store.wildcard[t]=e;else{var n=this.getType(t),r=this.getName(t);this.store[n][r]=e}}},{key:"has",value:function(t){try{var e=t.split(":");return e.length>1?!!this.store[e[1]][e[0]]:!!this.store.wildcard[e[0]]}catch(t){return!1}}},{key:"getName",value:function(t){return t.match(/(.*)\:.*/)[1]}},{key:"getType",value:function(t){return t.match(/^[a-z0-9\-\_]+\:(.*)/)[1]}},{key:"isValid",value:function(t){return this.verifierPattern.test(t)||this.wildcards.indexOf(t)>-1}}]),t}();n.default=i},{}],8:[function(t,e,n){"use strict";Object.defineProperty(n,"__esModule",{value:!0});var r=function(t){return t&&t.__esModule?t:{default:t}}(t("./queue"));window.Queue=r.default,n.default=r.default},{"./queue":9}],9:[function(t,e,n){"use strict";function r(t){return t&&t.__esModule?t:{default:t}}Object.defineProperty(n,"__esModule",{value:!0});var i=function(){return function(t,e){if(Array.isArray(t))return t;if(Symbol.iterator in Object(t))return function(t,e){var n=[],r=!0,i=!1,o=void 0;try{for(var a,u=t[Symbol.iterator]();!(r=(a=u.next()).done)&&(n.push(a.value),!e||n.length!==e);r=!0);}catch(t){i=!0,o=t}finally{try{!r&&u.return&&u.return()}finally{if(i)throw o}}return n}(t,e);throw new TypeError("Invalid attempt to destructure non-iterable instance")}}(),o=r(t("./container")),a=r(t("./storage-capsule")),u=r(t("./config")),c=r(t("./event")),s=t("./utils"),l=r(t("./storage/localstorage")),f=function(){function t(t){(function(t){this.currentChannel,this.currentTimeout,this.stopped=!0,this.running=!1,this.channels={},this.config=new u.default(t),this.storage=new a.default(this.config,new l.default(this.config)),this.event=new c.default,this.container=new o.default,this.timeout=this.config.get("timeout")}).call(this,t)}function e(){return r.call(this).all().filter(s.excludeSpecificTasks.bind(["freezed"]))}function n(t,e){"tag"in t&&(this.event.emit(t.tag+":"+e,t),this.event.emit(t.tag+":*",t))}function r(){return this.storage.channel(this.currentChannel)}function f(t){return r.call(this).save(t)}function f(t){return r.call(this).save(function(t){t.priority=t.priority||0,isNaN(t.priority)&&(t.priority=0);return t}(t))}function h(){var t=this.config.get("timeout");return this.currentTimeout=setTimeout(function(){var t,e=r.call(this).fetch().shift();if(void 0===e)return console.log("-> "+this.currentChannel+" channel is empty..."),void y.call(this);this.container.has(e.handler)||console.warn(e.handler+"-> job not found");var i=this.container.get(e.handler),o=new i.handler;(function(t){return r.call(this).update(t._id,{locked:!0})}).call(this,e),p.call(this,"before",o,e.args),n.call(this,e,"before");var a=Object.values(i.deps||{});(t=o.handle).call.apply(t,[o,e.args].concat(function(t){if(Array.isArray(t)){for(var e=0,n=Array(t.length);e<t.length;e++)n[e]=t[e];return n}return Array.from(t)}(a))).then(function(t,e){var i=this;return function(o){o?function(t,e){d.call(this,t._id)}.call(i,t,e):function(t,e){n.call(this,t,"retry");var i=function(t,e){"retry"in e||(e.retry=1);"tried"in t||(t.tried=0,t.retry=e.retry);++t.tried,t.tried>=e.retry&&(t.freezed=!0);return t}.call(this,t,e);return i.locked=!1,r.call(this).update(t._id,i)}.call(i,t,e),p.call(this,"after",e,t.args),n.call(this,t,"after"),i.next()}}.call(this,e,o).bind(this)).catch(function(t,e){var n=this;return function(e){d.call(n,t._id),n.event.emit("error",t),n.next()}}.call(this,e,o).bind(this))}.bind(this),t)}function d(t){return r.call(this).delete(t)}function p(t,e,n){(0,s.hasMethod)(e,t)&&("before"==t&&(0,s.isFunction)(e.before)?e.before.call(e,n):"after"==t&&(0,s.isFunction)(e.after)&&e.after.call(e,n))}function y(){this.stop(),this.currentTimeout&&(this.currentTimeout=clearTimeout(this.currentTimeout))}return t.FIFO="fifo",t.LIFO="lifo",t.prototype.add=function(t){var e=f.call(this,t);return e&&this.stopped&&!0===this.running&&this.start(),e},t.prototype.next=function(){if(this.stopped)return console.log("[stopped]-> next"),function(){this.running=!1}.call(this),y.call(this);console.log("[next]->"),this.start()},t.prototype.start=function(){return this.stopped=!1,function(){if(!t.isRegistered){var e=t.jobs||[],n=!0,r=!1,o=void 0;try{for(var a,u=e[Symbol.iterator]();!(n=(a=u.next()).done);n=!0){var c=a.value,s=c.handler.toString().match(/function\s([a-zA-Z_]+).*?/),l=i(s,2),f=(l[0],l[1]);f&&this.container.bind(f,c)}}catch(t){r=!0,o=t}finally{try{!n&&u.return&&u.return()}finally{if(r)throw o}}t.isRegistered=!0}}.call(this),this.running=h.call(this)>0,console.log("[started]->",this.running),this.running},t.prototype.stop=function(){console.log("[stopping]->"),this.stopped=!0},t.prototype.forceStop=function(){console.log("[forceStopped]->"),y.call(this)},t.prototype.create=function(t){return t in this.channels||(this.currentChannel=t,this.channels[t]=(0,s.clone)(this)),this.channels[t]},t.prototype.channel=function(t){if(!this.channels[t])throw new Error('Channel of "'+t+'" not found');return this.channels[t]},t.prototype.isEmpty=function(){return this.count()<1},t.prototype.count=function(){return e.call(this).length},t.prototype.countByTag=function(t){return e.call(this).filter(function(e){return e.tag===t}).length},t.prototype.clear=function(){this.currentChannel&&this.storage.clear(this.currentChannel)},t.prototype.clearByTag=function(t){var e=this;r.call(this).all().filter(s.utilClearByTag.bind(t)).forEach(function(t){return r.call(e).delete(t._id)})},t.prototype.has=function(t){return e.call(this).findIndex(function(e){return e._id===t})>-1},t.prototype.hasByTag=function(t){return e.call(this).findIndex(function(e){return e.tag===t})>-1},t.prototype.setTimeout=function(t){this.timeout=t,this.config.set("timeout",t)},t.prototype.setLimit=function(t){this.config.set("limit",t)},t.prototype.setPrefix=function(t){this.config.set("prefix",t)},t.prototype.setPrinciple=function(t){this.config.set("principle",t)},t.prototype.on=function(t,e){var n;(n=this.event).on.apply(n,arguments)},t.prototype.error=function(t){this.event.on("error",t)},t.register=function(e){if(!(e instanceof Array))throw new Error("Queue jobs should be objects within an array");t.isRegistered=!1,t.jobs=e},t}();n.default=f},{"./config":5,"./container":6,"./event":7,"./storage-capsule":10,"./storage/localstorage":11,"./utils":12}],10:[function(t,e,n){"use strict";function r(t){return t&&t.__esModule?t:{default:t}}Object.defineProperty(n,"__esModule",{value:!0});var i=function(){function t(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}return function(e,n,r){return n&&t(e.prototype,n),r&&t(e,r),e}}(),o=r(t("group-by")),a=(r(t("./storage/localstorage")),r(t("./config")),t("./utils")),u=function(){function t(e,n){!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t),this.storage=n,this.config=e}return i(t,[{key:"channel",value:function(t){return this.storageChannel=t,this}},{key:"fetch",value:function(){var t=this.all().filter(a.excludeSpecificTasks),e=(0,o.default)(t,"priority");return Object.keys(e).map(function(t){return parseInt(t)}).sort(function(t,e){return e-t}).reduce(this.reduceTasks(e),[])}},{key:"save",value:function(t){try{var e=this.storage.get(this.storageChannel);return this.isExceeded()?(console.warn("Task limit exceeded: The '"+this.storageChannel+"' channel limit is "+this.config.get("limit")),!1):(t=this.prepareTask(t),e.push(t),this.storage.set(this.storageChannel,JSON.stringify(e)),t._id)}catch(t){return!1}}},{key:"update",value:function(t,e){try{var n=this.all(),r=n.findIndex(function(e){return e._id==t});return!(r<0)&&(n[r]=Object.assign({},n[r],e),this.storage.set(this.storageChannel,JSON.stringify(n)),!0)}catch(t){return!1}}},{key:"delete",value:function(t){try{var e=this.all(),n=e.findIndex(function(e){return e._id===t});return!(n<0)&&(delete e[n],this.storage.set(this.storageChannel,JSON.stringify(e.filter(function(t){return t}))),!0)}catch(t){return!1}}},{key:"all",value:function(){return this.storage.get(this.storageChannel)}},{key:"generateId",value:function(){return(65536*(1+Math.random())).toString(16)}},{key:"prepareTask",value:function(t){return t.createdAt=Date.now(),t._id=this.generateId(),t}},{key:"reduceTasks",value:function(t){var e=this;return function(n,r){return"lifo"===e.config.get("principle")?n.concat(t[r].sort(a.lifo)):n.concat(t[r].sort(a.fifo))}.bind(this)}},{key:"isExceeded",value:function(){var t=this.config.get("limit"),e=this.all().filter(a.excludeSpecificTasks);return!(-1===t||t>e.length)}},{key:"clear",value:function(t){this.storage.clear(t)}}]),t}();n.default=u},{"./config":5,"./storage/localstorage":11,"./utils":12,"group-by":2}],11:[function(t,e,n){"use strict";Object.defineProperty(n,"__esModule",{value:!0});var r=function(){function t(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}return function(e,n,r){return n&&t(e.prototype,n),r&&t(e,r),e}}(),i=function(){function t(e){!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t),this.storage=localStorage,this.config=e}return r(t,[{key:"get",value:function(t){try{var e=this.storageName(t);return this.has(e)?JSON.parse(this.storage.getItem(e)):[]}catch(t){return[]}}},{key:"set",value:function(t,e){this.storage.setItem(this.storageName(t),e)}},{key:"has",value:function(t){return t in this.storage}},{key:"clear",value:function(t){this.storage.removeItem(this.storageName(t))}},{key:"clearAll",value:function(){this.storage.clear()}},{key:"storageName",value:function(t){return this.getPrefix()+"_"+t}},{key:"getPrefix",value:function(){return this.config.get("prefix")}}]),t}();n.default=i},{}],12:[function(t,e,n){"use strict";function r(t,e){return Object.prototype.hasOwnProperty.call(t,e)}function i(t){var e=Array.isArray(this)?this:["freezed","locked"],n=[],i=!0,o=!1,a=void 0;try{for(var u,c=e[Symbol.iterator]();!(i=(u=c.next()).done);i=!0){var s=u.value;n.push(!1===r(t,s)||!1===t[s])}}catch(t){o=!0,a=t}finally{try{!i&&c.return&&c.return()}finally{if(o)throw a}}return!(n.indexOf(!1)>-1)}Object.defineProperty(n,"__esModule",{value:!0}),n.clone=function(t){var e=Object.create(Object.getPrototypeOf(t),Object.getOwnPropertyNames(t).reduce(function(e,n){return e[n]=Object.getOwnPropertyDescriptor(t,n),e},{}));return Object.isExtensible(t)||Object.preventExtensions(e),Object.isSealed(t)&&Object.seal(e),Object.isFrozen(t)&&Object.freeze(e),e},n.hasProperty=r,n.hasMethod=function(t,e){return t instanceof Object&&e in t},n.isFunction=function(t){return t instanceof Function},n.excludeSpecificTasks=i,n.utilClearByTag=function(t){return!!i.call(["locked"],t)&&t.tag===this},n.fifo=function(t,e){return t.createdAt-e.createdAt},n.lifo=function(t,e){return e.createdAt-t.createdAt}},{}]},{},[8]);

@@ -21,3 +21,3 @@ var gulp = require("gulp");

browserify({
entries: ["./dev/index.js"],
entries: ["./src/index.js"],
debug: true

@@ -54,3 +54,3 @@ })

execSync(
"./node_modules/.bin/babel --plugins transform-flow-strip-types dev/" +
"./node_modules/.bin/babel --plugins transform-flow-strip-types src/" +
file +

@@ -64,5 +64,5 @@ " > lib/" +

gulp.task("watch", function() {
gulp.watch("dev/**/*.js", ["es6", "stripTypes"]);
gulp.watch("src/**/*.js", ["es6", "stripTypes"]);
});
gulp.task("default", ["es6", "watch"]);
import groupBy from 'group-by';
import LocalStorage from './storage/localstorage';
import groupBy from "group-by";
import LocalStorage from "./storage/localstorage";
import Config from './config';
import { excludeSpecificTasks, lifo, fifo } from './utils';
import Config from "./config";
import { excludeSpecificTasks, lifo, fifo } from "./utils";

@@ -28,14 +28,7 @@ export default class StorageCapsule {

const all = this.all().filter(excludeSpecificTasks);
const tasks = groupBy(all, 'priority');
return Object.
keys(tasks).
const tasks = groupBy(all, "priority");
return Object.keys(tasks).
map(key => parseInt(key)).
sort((a, b) => b - a).
reduce((result, key) => {
if (this.config.get('principle') === 'lifo') {
return result.concat(tasks[key].sort(lifo));
} else {
return result.concat(tasks[key].sort(fifo));
}
}, []);
reduce(this.reduceTasks(tasks), []);
}

@@ -51,3 +44,7 @@

if (this.isExceeded()) {
console.warn(`Task limit exceeded: The '${this.storageChannel}' channel limit is ${this.config.get('limit')}`);
console.warn(
`Task limit exceeded: The '${
this.storageChannel
}' channel limit is ${this.config.get("limit")}`);
return false;

@@ -100,3 +97,6 @@ }

this.storage.set(this.storageChannel, JSON.stringify(data.filter(d => d)));
this.storage.set(
this.storageChannel,
JSON.stringify(data.filter(d => d)));
return true;

@@ -122,4 +122,16 @@ } catch (e) {

reduceTasks(tasks) {
const reduceFunc = (result, key) => {
if (this.config.get("principle") === "lifo") {
return result.concat(tasks[key].sort(lifo));
} else {
return result.concat(tasks[key].sort(fifo));
}
};
return reduceFunc.bind(this);
}
isExceeded() {
const limit = this.config.get('limit');
const limit = this.config.get("limit");
const tasks = this.all().filter(excludeSpecificTasks);

@@ -126,0 +138,0 @@ return !(limit === -1 || limit > tasks.length);

{
"name": "storage-based-queue",
"version": "0.0.5-beta6",
"version": "0.0.5-beta7",
"description": "Simply queue manager",

@@ -5,0 +5,0 @@ "main": "lib/queue.js",

@@ -0,1 +1,2 @@

[![npm version](https://badge.fury.io/js/storage-based-queue.svg)](https://badge.fury.io/js/storage-based-queue)
[![Build Status](https://travis-ci.org/atayahmet/storage-based-queue.svg?branch=v0.0.5-beta5)](https://travis-ci.org/atayahmet/storage-based-queue)

@@ -21,2 +22,11 @@ [![Dependency Status](https://img.shields.io/david/atayahmet/storage-based-queue.svg?style=flat-square)](https://david-dm.org/atayahmet/storage-based-queue)

### Channels
You need to create at least one channel. One channel can be created as many channels as desired. Channels run independently of each other. The areas where each channel will store tasks are also separate. The area where tasks are stored is named with the channel name and prefix.
The important thing to remember here is that each newly created channel is actually a new copy of the Queue class. So a new instance is formed, but the dependencies of the channels are still alive as singletons.
Example; You created two channels. Their names are channelA and channelB. If you make a setting in the channelA instance, this change will also be reflected in channelB and all other channels.
### Workers
Worker classes should return `boolean` `(true / false)` data with the Promise class as the return value. The return `Promise / resolve (true)` must be true if a task is successfully **completed** and you want to pass the next task. A possible exception should also be **tried** again: `Promise / resolve (false)`. If we do not want the task to be retried and we want to pass the next task: `Promise / reject ('any value')`

@@ -23,0 +33,0 @@

@@ -15,4 +15,4 @@ import Config from "../lib/config";

defaultTimeout = 1000;
queue = new Queue(config);
config = new Config;
queue = new Queue;
storage = new LocalStorage(config);

@@ -240,2 +240,10 @@ });

});
it('should be set principle of config', () => {
queue.setPrinciple(Queue.FIFO);
expect(config.get('principle')).toEqual(Queue.FIFO);
queue.setPrinciple(Queue.LIFO);
expect(queue.config.get('principle')).toEqual(Queue.LIFO);
});
});
import StorageCapsule from "../lib/storage-capsule";
import LocalStorage from "../lib/storage/localstorage";
import Config from "../lib/config";
import Queue from "../lib/queue";

@@ -9,3 +10,3 @@ describe('Storage capsule class tests', () => {

const lStorage = new LocalStorage(config);
let storage, exmpTask;
let storage, exmpTask, defaultTimeout;

@@ -15,2 +16,3 @@ beforeEach(() => {

storage = new StorageCapsule(config, lStorage);
defaultTimeout = 1000;
lStorage.clear('test-1');

@@ -124,2 +126,31 @@ lStorage.clear('test-2');

it('should be sort tasks by fifo/lifo, -> fetch()', () => {
storage.channel('test-1');
let flag = false;
runs(() => {
setTimeout(() => {
const newTask1 = Object.assign({}, exmpTask, {tag: 'test-1'});
storage.save(newTask1);
}, (defaultTimeout + 100));
setTimeout(() => {
const newTask2 = Object.assign({}, exmpTask, {tag: 'test-2'});
storage.save(newTask2);
flag = true;
}, (defaultTimeout + 200));
});
waitsFor(() => {
return flag;
}, "", (defaultTimeout + 300));
runs(() => {
config.set('principle', Queue.FIFO);
expect(storage.fetch()[0].tag).toEqual('test-1');
config.set('principle', Queue.LIFO);
expect(storage.fetch()[0].tag).toEqual('test-2');
});
});
it('should be get all tasks, -> all()', () => {

@@ -126,0 +157,0 @@ storage.channel('test-1');

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc