UNPKG

dexie-batch

Version:

Fetch DB entries in batches to improve performance while respecting IPC size constraints

4 lines (3 loc) 2.65 kB
/*! dexie-batch v0.4.3 | github.com/raphinesse/dexie-batch | MIT License */ !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t(require("dexie")):"function"==typeof define&&define.amd?define(["dexie"],t):(e="undefined"!=typeof globalThis?globalThis:e||self).DexieBatch=t(e.Dexie)}(this,(function(e){"use strict";function t(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}function n(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}var r=t(e).default.Promise;function i(e,t){var n=e.clone();return function(){var e=n.clone().limit(t).toArray();return n.offset(t),e}}function o(e,t){if(arguments.length<2)throw new Error('Arguments "collection" and "callback" are mandatory');if(!a(e))throw new Error('"collection" must be of type Collection');if("function"!=typeof t)throw new TypeError('"callback" must be a function')}function a(e){return!!e&&["clone","offset","limit","toArray"].every((function(t){return"function"==typeof e[t]}))}return function(){function e(t){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),function(e){var t=e&&e.batchSize;if(!(t&&Number.isInteger(t)&&t>0))throw new Error('Mandatory option "batchSize" must be a positive integer');if("limit"in e&&!(Number.isInteger(e.limit)&&e.limit>=0))throw new Error('Option "limit" must be a non-negative integer')}(t),this.opts=t}var t,a,l;return t=e,(a=[{key:"isParallel",value:function(){return Boolean(this.opts.limit)}},{key:"each",value:function(e,t){var n=this;return o.apply(void 0,arguments),this.eachBatch(e,(function(e,i){var o=i*n.opts.batchSize;return r.all(e.map((function(e,n){return t(e,o+n)})))}))}},{key:"eachBatch",value:function(e,t){o.apply(void 0,arguments);var n=this.isParallel()?"eachBatchParallel":"eachBatchSerial";return this[n](e,t)}},{key:"eachBatchParallel",value:function(e,t){o.apply(void 0,arguments);var n=this.opts,a=n.batchSize,l=n.limit;if(!l)throw new Error('Option "limit" must be set for parallel operation');var u=i(e,a),c=Math.ceil(l/a),f=Array.from({length:c},(function(e,n){return u().then((function(e){return t(e,n)}))}));return r.all(f).then((function(e){return e.length}))}},{key:"eachBatchSerial",value:function(e,t){o.apply(void 0,arguments);var n=[],a=i(e,this.opts.batchSize),l=function e(r){if(0!==r.length)return n.push(t(r,n.length)),a().then(e)};return a().then(l).then((function(){return r.all(n)})).then((function(){return n.length}))}}])&&n(t.prototype,a),l&&n(t,l),e}()})); //# sourceMappingURL=dexie-batch.min.js.map