-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathepvStorePersistentIdbKeyVal.js
More file actions
184 lines (178 loc) · 5.85 KB
/
Copy pathepvStorePersistentIdbKeyVal.js
File metadata and controls
184 lines (178 loc) · 5.85 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
const log = require("./log").sub("storeIdbKeyVal")
// p-pipe 1.2.0 car n'utilise pas de async ni de spread
const pPipe = function (input) {
const args = Array.isArray(input) ? input : arguments
if (args.length === 0) {
return Promise.reject(new Error("Expected at least one argument"))
}
return [].slice.call(args, 1).reduce((a, b) => {
return function () {
return Promise.resolve(a.apply(null, arguments)).then(b)
}
}, args[0])
}
const ctxAssign = (variable, fn) => (ctx) => {
if (!fn) return { [variable]: ctx }
if (!ctx) ctx = {}
return fn(ctx).then((res) => {
if (variable) {
ctx[variable] = res
}
return ctx
})
}
const compact = require("lodash/compact")
const mapValues = require("lodash/mapValues")
const groupBy = require("lodash/groupBy")
const padStart = require("lodash/padStart")
const { createStore, get, set, del, clear, keys } = require("idb-keyval")
const epvStore = require("./epvStore")
const { patch: patchData } = require("./kkvHelpers")
const create = require("lodash/create")
const { observable } = require("kobs")
const dbCall = (db, method, arg1, arg2) => {
// const callName = method.name
// console.log("starting", callName)
// console.time(callName)
return method.apply(null, compact([arg1, arg2, db])).then((res) => {
// console.timeEnd(callName)
// console.log("done", callName, res)
return res
})
}
const spy = (fn) => (v) => {
fn(v)
return v
}
const patchKey = (storeName, patchCount) =>
storeName + "/" + padStart(patchCount, 5, "0")
const maxPatches = 200
const createDb = (arg) => {
if (typeof arg === "string") throw new Error("invalid arg")
const { dbName, storeName } = arg
return createStore(dbName, storeName)
}
const ensureInitStore = ({ keys, db }) => {
if (keys.indexOf("activeStore") >= 0) return Promise.resolve(keys)
const activeStoreName = new Date().toISOString()
return dbCall(db, set, patchKey(activeStoreName, 0), {}).then(() =>
dbCall(db, set, "activeStore", activeStoreName).then(() => [
"activeStore",
patchKey(activeStoreName, 0),
])
)
}
const loadStoresMetaData = ({ db, keys }) =>
dbCall(db, get, "activeStore").then((activeStoreName) => {
const keysByStore = groupBy(keys, (k) => k.split("/")[0])
delete keysByStore.activeStore
const allStores = mapValues(keysByStore, (keys, name) => {
const patchesToLoad = keys.sort()
return {
name,
patchesCount: patchesToLoad.length,
patchesToLoad,
}
})
return {
all: allStores,
active: allStores[activeStoreName],
}
})
const loadActiveStoreData = ({ db, stores }) => {
const data = new Map()
const activeStore = stores.active
return Promise.all(
activeStore.patchesToLoad.map((k) => dbCall(db, get, k))
).then((patches) => {
patches.forEach((patch) => {
patchData(data, patch)
})
return data
})
}
const writePatch = (db, store, patch) => {
const key = patchKey(store.name, store.patchesCount)
store.patchesCount++
// return Promise.reject(new Error("fakePersistenceError")) // for test only
return dbCall(db, set, key, patch)
}
const removeOldStores = (db, stores) =>
dbCall(db, keys).then((keys) => {
const keysByStore = groupBy(keys, (k) => k.split("/")[0])
delete keysByStore.activeStore
const activeStoreName = stores.active.name
const initializingStoreName =
stores.initializing && stores.initializing.name
const oldStores = Object.keys(keysByStore).filter(
(s) => s !== activeStoreName && s !== initializingStoreName
)
return Promise.all(
oldStores.map((store) => {
const keys = keysByStore[store]
return Promise.all(keys.map((k) => dbCall(db, del, k)))
})
)
})
const createNewStore = (db, data, stores) => {
const storeName = new Date().toISOString()
const newStore = {
name: storeName,
patchesCount: 1,
}
stores.initializing = newStore
stores.all[storeName] = newStore
return dbCall(db, set, patchKey(storeName, 0), data)
.then(() => dbCall(db, set, "activeStore", storeName))
.then(() => {
stores.active = newStore
stores.initializing = null
return removeOldStores(db, stores)
})
}
//db is idbStore
// store is a virtual store with state and patches
// expect to be called wtih dbName
module.exports = pPipe([
createDb,
ctxAssign("db"),
ctxAssign("keys", ({ db }) => dbCall(db, keys)),
ctxAssign("keys", ensureInitStore),
ctxAssign("stores", loadStoresMetaData),
ctxAssign("data", loadActiveStoreData),
spy(({ data, stores }) =>
log("entities loaded from store", {
count: data.length,
storeName: stores.active.name,
})
),
({ db, data, stores }) => {
const persisting = observable(0, "persisting")
const persistenceError = observable(false, "persistenceError")
const memoryStore = epvStore(data)
const patchAndSave = (patch) => {
// call memory store patch
memoryStore.patch(patch)
// and then persist it
persisting(persisting() + 1)
writePatch(db, stores.active, patch)
.then(() => persisting(persisting() - 1))
.catch(persistenceError)
// pour les patchs suivant l'initialisation d'un nouveau store, tant que l'opération est encore en cours
if (stores.initializing) {
log.debug("new store still initializing when writing patch")
writePatch(db, stores.initializing, patch).catch(persistenceError)
}
if (!stores.initializing && stores.active.patchesCount > maxPatches) {
createNewStore(db, memoryStore.backup(), stores)
}
}
return create(memoryStore, {
patch: patchAndSave,
persisting,
clearAllData: () => dbCall(db, clear),
setData: data => createNewStore(db, data, stores),//attention, le memoryStore n'est pas modifié : c'est uniquement pour le restore et il faut recharger l'appli après
persistenceError,
})
},
])