-
Notifications
You must be signed in to change notification settings - Fork 1
/
dexie-batch.js
107 lines (87 loc) · 3.06 KB
/
dexie-batch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
const { Promise } = require('dexie')
module.exports = class DexieBatch {
constructor(opts) {
assertValidOptions(opts)
this.opts = opts
}
isParallel() {
return Boolean(this.opts.limit)
}
each(collection, callback) {
assertValidMethodArgs(...arguments)
return this.eachBatch(collection, (batch, batchIdx) => {
const baseIdx = batchIdx * this.opts.batchSize
return Promise.all(batch.map((item, i) => callback(item, baseIdx + i)))
})
}
eachBatch(collection, callback) {
assertValidMethodArgs(...arguments)
const delegate = this.isParallel() ? 'eachBatchParallel' : 'eachBatchSerial'
return this[delegate](collection, callback)
}
eachBatchParallel(collection, callback) {
assertValidMethodArgs(...arguments)
const { batchSize, limit } = this.opts
if (!limit) {
throw new Error('Option "limit" must be set for parallel operation')
}
const nextBatch = batchIterator(collection, batchSize)
const numBatches = Math.ceil(limit / batchSize)
const batchPromises = Array.from({ length: numBatches }, (_, idx) =>
nextBatch().then(batch => callback(batch, idx))
)
return Promise.all(batchPromises).then(batches => batches.length)
}
eachBatchSerial(collection, callback) {
assertValidMethodArgs(...arguments)
const userPromises = []
const nextBatch = batchIterator(collection, this.opts.batchSize)
const nextUnlessEmpty = batch => {
if (batch.length === 0) return
userPromises.push(callback(batch, userPromises.length))
return nextBatch().then(nextUnlessEmpty)
}
return nextBatch()
.then(nextUnlessEmpty)
.then(() => Promise.all(userPromises))
.then(() => userPromises.length)
}
}
// Does not conform to JS iterator requirements
function batchIterator(collection, batchSize) {
const it = collection.clone()
return () => {
const batchPromise = it.clone().limit(batchSize).toArray()
it.offset(batchSize)
return batchPromise
}
}
function assertValidOptions(opts) {
const batchSize = opts && opts.batchSize
if (!(batchSize && Number.isInteger(batchSize) && batchSize > 0)) {
throw new Error('Mandatory option "batchSize" must be a positive integer')
}
if ('limit' in opts && !(Number.isInteger(opts.limit) && opts.limit >= 0)) {
throw new Error('Option "limit" must be a non-negative integer')
}
}
function assertValidMethodArgs(collection, callback) {
if (arguments.length < 2) {
throw new Error('Arguments "collection" and "callback" are mandatory')
}
if (!isCollectionInstance(collection)) {
throw new Error('"collection" must be of type Collection')
}
if (!(typeof callback === 'function')) {
throw new TypeError('"callback" must be a function')
}
}
// We would need the Dexie instance that created the collection to get the
// Collection constructor and do some proper type checking.
// So for now we resort to duck typing
function isCollectionInstance(obj) {
if (!obj) return false
return ['clone', 'offset', 'limit', 'toArray'].every(
name => typeof obj[name] === 'function'
)
}