forked from orthecreedence/hustle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhustle.min.js
1 lines (1 loc) · 10.3 KB
/
hustle.min.js
1
!function(e,r){"use strict";var t=e.indexedDB||e.webkitIndexedDB||e.mozIndexedDB||e.oIndexedDB||e.msIndexedDB,n=e.IDBKeyRange||e.webkitIDBKeyRange;if(!t)throw"IndexedDB required";var o=function(e,r){var t=function(){var t=e.apply(this,arguments);return t.name=this.name=r,this.stack=t.stack,this.message=t.message,this};return t.prototype=Object.create(e.prototype,{constructor:{value:t}}),t},u=o(e.Error,"HustleError"),s=o(u,"HustleDBClosed"),i=o(u,"HustleDBOpened"),c=o(u,"HustleBadTube"),a=o(u,"HustleBadID"),d=o(o(e.Error,"HustleNotice"),"HustleNotFound"),l=function(e){e||(e={}),e.tubes||(e.tubes=[]);var r=e.db_version?e.db_version:1,o=e.maintenance_delay?e.maintenance_delay:1e3,u=e.db_name?e.db_name:"hustle",l={ids:"_ids",reserved:"_reserved",delayed:"_delayed",buried:"_buried"};e.tubes.indexOf("default")<0&&e.tubes.push("default");var f,v=null,b=function(){if(!v)throw new s("Closed database being operated on. Did you call Hustle.open()?");return!0},p=function(e,r,t){t||(t={});var n=null,o=e.target.result,u=t.keypath?t.keypath:"id",s=!!t.autoincrement&&t.autoincrement;if(n=o.objectStoreNames.contains(r)?e.currentTarget.transaction.objectStore(r):o.createObjectStore(r,{keyPath:u,autoIncrement:s}),t.indexes)for(var i=Object.keys(t.indexes),c=0;c<i.length;c++)!function(e,r){var t=r.index?r.index:e,o=!!r.unique;try{n.createIndex(e,t,{unique:o})}catch(e){}}(i[c],t.indexes[i[c]]);return n},m=function(){return!!v&&(v.close(),v=null,!0)},h=function(e){b(),e||(e={});var r=null,t=v.transaction([l.ids],"readwrite");t.oncomplete=function(t){r?e.success&&e.success(r,t):e.error&&e.error("bad id")},t.onerror=function(r){e.error&&e.error(r)};var n=t.objectStore(l.ids),o=n.get("id");o.onsuccess=function(e){var t=o.result;t?(r=++t.value,n.put(t)):(r=1,n.put({id:"id",value:1}))}},y=function(e,r,t,n){n||(n={});var o=v.transaction([r,t],"readwrite");o.oncomplete=function(e){n.success&&n.success(e)},o.onerror=function(e){n.error&&n.error(e)};var u,s=function(e,r){n.transform&&(e=n.transform(e)),o.objectStore(t).add(e).onsuccess=r},i=o.objectStore(r);if(r==l.buried){var c=i.index("id");u=c.get(e)}else u=i.get(e);u.onsuccess=function(t){var o=u.result;if(o){var c=o.id;r==l.buried&&(c=o._id),s(o,function(e){i.delete(c).onerror=n.error})}else n.error&&n.error(new d("item "+e+" wasn't found"))}},w=function(e,r){for(var t={data:e},n=[{name:"priority",type:"int",default:1024},{name:"delay",type:"int",default:0},{name:"ttr",type:"int",default:0}],o=0;o<n.length;o++){var u=n[o];if(r[u.name])switch(t[u.name]=r[u.name],u.type){case"int":t[u.name]=parseInt(t[u.name]);break;case"float":t[u.name]=parseFloat(t[u.name])}u.default&&void 0===t[u.name]&&(t[u.name]=u.default)}return t.age=0,t.reserves=0,t.releases=0,t.timeouts=0,t.buries=0,t.kicks=0,t.created=(new Date).getTime(),t},g=function(r,t){if(b(),t||(t={}),!r)return t.error&&t.error(new HustleBadId("bad id given")),!1;var n=null,o=[l.reserved,l.delayed,l.buried].concat(e.tubes),u=v.transaction(o,"readonly");u.oncomplete=function(e){n||!t.not_found_error?t.success&&t.success(n,e):t.error&&t.error(new d("item "+r+" not found"))},u.onerror=function(e){t.error&&t.error(e)},o.forEach(function(e){(e==l.buried?u.objectStore(e).index("id").get(r):u.objectStore(e).get(r)).onsuccess=function(r){var t=r&&r.target&&r.target.result;if(n||!t)return!1;(n=t).age=Math.round(((new Date).getTime()-n.expire)/1e3),e==l.reserved?(n.state="reserved",n.ttr>0&&(n.time_left=Math.round((n.expire-(new Date).getTime())/1e3))):e==l.buried?n.state="buried":(n.state="ready",n.tube||(n.tube=e))}})},x=function(r){b(),r||(r={});var t=r.tube?r.tube:"default";if(e.tubes.indexOf(t)<0)throw new c("tube "+t+" doesn't exist");var n=null,o=v.transaction([l.reserved,t],"readwrite");o.oncomplete=function(e){r.success&&r.success(n,e)},o.onerror=function(e){r.error&&r.error(e)};var u=function(e,r){(n=e).reserves++,n.tube=t,n.ttr>0&&(n.expire=(new Date).getTime()+1e3*n.ttr),o.objectStore(l.reserved).add(n).onsuccess=r},s=o.objectStore(t);s.index("priority").openCursor().onsuccess=function(e){var t=e.target.result;t&&u(t.value,function(e){s.delete(t.value.id).onerror=r.error})}},_=function(e){e||(e={});var r=[],t=v.transaction(l.delayed,"readonly");t.oncomplete=function(e){r.forEach(function(e){y(e.id,l.delayed,e.tube,{error:function(e){console.error("Hustle: delayed move: ",e)}})})},t.onerror=function(r){console.error("Hustle: delayed move: ",r),e.error&&e.error(r)};var o=t.objectStore(l.delayed).index("activate"),u=(new Date).getTime(),s=n.upperBound(u);o.openCursor(s).onsuccess=function(e){var t=e.target.result;t&&(r.push(t.value),t.continue())}},k=function(e){e||(e={});var r=[],t=v.transaction(l.reserved,"readonly");t.oncomplete=function(e){r.forEach(function(e){y(e.id,l.reserved,e.tube,{transform:function(e){return delete e.expire,e.timeouts++,e},error:function(t){t instanceof d&&r.erase(e),console.error("Hustle: ttr move: ",t)}})})},t.onerror=function(r){console.error("Hustle: ttr move: ",r),e.error&&e.error(r)};var o=t.objectStore(l.reserved).index("expire"),u=(new Date).getTime(),s=n.upperBound(u);o.openCursor(s).onsuccess=function(e){var t=e.target.result;t&&(r.push(t.value),t.continue())}};f=function(){var e=function(){if(!v)return!1;_(),k(),setTimeout(e,o)};setTimeout(e,o)};var D={peek:g,put:function(r,t){if(b(),t||(t={}),!r)return!1;var n=t.tube?t.tube:"default";if(e.tubes.indexOf(n)<0)throw new c("tube "+n+" doesn't exist");var o=w(r,t);o.delay&&o.delay>0&&(o.tube=n,o.activate=(new Date).getTime()+1e3*o.delay,n=l.delayed,delete o.delayed),h({success:function(e){o.id=e;var r=v.transaction([n],"readwrite");r.oncomplete=function(e){t.success&&t.success(o,e)},r.onerror=function(e){t.error&&t.error(e)},r.objectStore(n).add(o).onsuccess=function(e){o.id=e.target.result}},error:function(e){t.error&&t.error(new a("error generating id"))}})},reserve:x,delete:function(e,r){b(),r||(r={}),g(e,{success:function(e){if(e){var t=e.tube,n=e.id;"reserved"==e.state?t=l.reserved:"buried"==e.state&&(t=l.buried,n=e._id);var o=v.transaction(t,"readwrite");o.oncomplete=function(t){r.success&&r.success(e,t)},o.onerror=function(e){r.error&&r.error(e)},o.objectStore(t).delete(n)}else r.success&&r.success(null)},error:r.error})},release:function(e,r){b(),r||(r={}),g(e,{not_found_error:!0,success:function(t){if("reserved"==t.state){var n=t.tube;if(r.delay){var o=parseInt(r.delay);o&&(t.activate=(new Date).getTime()+1e3*o,n=l.delayed)}y(e,l.reserved,n,{transform:function(e){if(e.releases++,r.priority){var t=parseInt(r.priority);t&&(e.priority=t)}return delete e.tube,e},success:r.success,error:r.error})}else r.error&&r.error(new d("item "+e+" isn't reserved"))},error:r.error})},bury:function(e,r){b(),r||(r={}),g(e,{not_found_error:!0,success:function(t){if("buried"!=t.state){var n=t.tube;"reserved"==t.state&&(n=l.reserved),y(e,n,l.buried,{transform:function(e){if(e.buries++,e.tube=t.tube,r.priority){var n=parseInt(r.priority);n&&(e.priority=n)}return e},success:r.success,error:r.error})}else r.success&&r.success()},error:r.error})},kick:function(r,t){b(),t||(t={});var n=0,o=[l.buried].concat(e.tubes),u=v.transaction(o,"readwrite");u.oncomplete=function(e){t.success&&t.success(n,e)},u.onerror=function(e){t.error&&t.error(e)};var s=function(e,r){e.kicks++;var t=e.tube;delete e._id,delete e.tube,u.objectStore(t).add(e).onsuccess=r},i=u.objectStore(l.buried);i.openCursor().onsuccess=function(e){var o=e.target.result;o&&(s(o.value,function(e){i.delete(o.key).onerror=t.error}),++n<r&&o.continue())}},kick_job:function(e,r){b(),r||(r={}),g(e,{not_found_error:!0,success:function(t){"buried"==t.state?y(e,l.buried,t.tube,{transform:function(e){return e.kicks++,delete e._id,delete e.tube,e},success:r.success,error:r.error}):r.error&&r.error(new d("item "+e+" isn't buried"))},error:r.error})},touch:function(e,r){b(),r||(r={}),g(e,{not_found_error:!0,success:function(t){if("reserved"!=t.state)return console.log("item.state: ",t.state),void(r.error&&r.error(new d("item "+e+" isn't reserved")));if(t.ttr<=0)r.success&&r.success();else{var n=v.transaction(l.reserved,"readwrite");n.oncomplete=function(e){r.success&&r.success(e)},n.onerror=function(e){r.error&&r.error(e)};var o=n.objectStore(l.reserved),u=o.get(e);u.onsuccess=function(e){var r=u.result;r.expire=(new Date).getTime()+1e3*r.ttr,o.put(r)}}},error:r.error})},count_ready:function(r,t){if(b(),t||(t={}),e.tubes.indexOf(r)<0)throw new c("tube "+r+" doesn't exist");var n=null,o=v.transaction(r,"readonly");o.oncomplete=function(e){t.success&&t.success(n,e)},o.onerror=function(e){t.error&&t.error(e)};var u=o.objectStore(r).count();u.onsuccess=function(e){n=u.result}},Consumer:function(e,r){r||(r={});var t=r.tube?r.tube:"default",n=r.delay?r.delay:100,o=null,u=function(n){if(n||(n={}),o&&v)return!(r.enable_fn&&!r.enable_fn())&&void x({tube:t,success:function(r){r&&(e(r),setTimeout(u))}.bind(this)})},o=null,s=function(){return!o&&(o=setInterval(u,n),!0)};return s(),this.start=s,this.stop=function(){return!!o&&(clearInterval(o),o=null,!0)},this}};return this.open=function(n){if(n||(n={}),v)throw new i("db is already open");var o=5e3+r,s=t.open(u,o);s.onerror=function(e){n.error&&n.error(e)},s.onsuccess=function(e){v=s.result,n.success&&n.success(e),f()},s.onupgradeneeded=function(r){var t=e.tubes;p(r,l.ids),p(r,l.reserved,{indexes:{expire:{index:"expire",unique:!1}}}),p(r,l.delayed,{indexes:{activate:{index:"activate",unique:!1}}}),p(r,l.buried,{indexes:{id:{unique:!1}}});for(var n=0;n<t.length;n++)[l.reserved,l.buried].indexOf(t[n])>=0||p(r,t[n],{indexes:{priority:{index:["priority","id"],unique:!1}}})}},this.close=m,this.is_open=function(){return!!v},this.wipe=function(){return m(),t.deleteDatabase(u),!0},this.Error=Error,this.Queue=D,this.promisify=function(){var e=this,r=function(r,t){return function(){var n=Array.prototype.slice.call(arguments,0);return n[t]||(n[t]={}),new Promise(function(o,u){n[t].success=o,n[t].error=u,r.apply(e,n)})}};return this.open=r(this.open,0),this.Queue.peek=r(this.Queue.peek,1),this.Queue.put=r(this.Queue.put,1),this.Queue.reserve=r(this.Queue.reserve,0),this.Queue.delete=r(this.Queue.delete,1),this.Queue.release=r(this.Queue.release,1),this.Queue.bury=r(this.Queue.bury,1),this.Queue.kick=r(this.Queue.kick,1),this.Queue.kick_job=r(this.Queue.kick_job,1),this.Queue.touch=r(this.Queue.touch,1),this.Queue.count_ready=r(this.Queue.count_ready,1),this}.bind(this),this.debug={get_db:function(){return v}},this};l.Error={DBClosed:s,DBOpened:i,BadTube:c,BadID:a,NotFound:d},e.Hustle=l}(self);