-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
y-indexeddb.js
188 lines (177 loc) · 5.34 KB
/
y-indexeddb.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
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
185
186
187
188
import * as Y from 'yjs'
import * as idb from 'lib0/indexeddb.js'
import * as mutex from 'lib0/mutex.js'
import { Observable } from 'lib0/observable.js'
const customStoreName = 'custom'
const updatesStoreName = 'updates'
export const PREFERRED_TRIM_SIZE = 500
/**
* @param {IndexeddbPersistence} idbPersistence
*/
export const fetchUpdates = idbPersistence => {
const [updatesStore] = idb.transact(/** @type {IDBDatabase} */ (idbPersistence.db), [updatesStoreName]) // , 'readonly')
return idb.getAll(updatesStore, idb.createIDBKeyRangeLowerBound(idbPersistence._dbref, false)).then(updates =>
idbPersistence._mux(() =>
idbPersistence.doc.transact(() =>
updates.forEach(val => Y.applyUpdate(idbPersistence.doc, val))
)
)
)
.then(() => idb.getLastKey(updatesStore).then(lastKey => { idbPersistence._dbref = lastKey + 1 }))
.then(() => idb.count(updatesStore).then(cnt => { idbPersistence._dbsize = cnt }))
.then(() => updatesStore)
}
/**
* @param {IndexeddbPersistence} idbPersistence
* @param {boolean} forceStore
*/
export const storeState = (idbPersistence, forceStore = true) =>
fetchUpdates(idbPersistence)
.then(updatesStore => {
if (forceStore || idbPersistence._dbsize >= PREFERRED_TRIM_SIZE) {
idb.addAutoKey(updatesStore, Y.encodeStateAsUpdate(idbPersistence.doc))
.then(() => idb.del(updatesStore, idb.createIDBKeyRangeUpperBound(idbPersistence._dbref, true)))
.then(() => idb.count(updatesStore).then(cnt => { idbPersistence._dbsize = cnt }))
}
})
/**
* @param {string} name
*/
export const clearDocument = name => idb.deleteDB(name)
/**
* @extends Observable<string>
*/
export class IndexeddbPersistence extends Observable {
/**
* @param {string} name
* @param {Y.Doc} doc
*/
constructor (name, doc) {
super()
this.doc = doc
this.name = name
this._mux = mutex.createMutex()
this._dbref = 0
this._dbsize = 0
/**
* @type {IDBDatabase|null}
*/
this.db = null
this.synced = false
const initDB = (/** @type {IDBDatabase} */ db) =>
idb.createStores(db, [
['updates', { autoIncrement: true }],
['custom']
])
this._db = idb.openDB(name, initDB)
this.getDB = async () => {
this.db = await this._db
try {
this.db.transaction('updates').abort()
return this.db
} catch (e) {
// browser sometimes fails, we do a re-open
// console.log('idb create transaction failed, close and reopen...', e, this.db)
}
this.db.close()
// reopen
this._db = idb.openDB(name, initDB)
this.db = await this._db
return this.db
}
/**
* @type {Promise<IndexeddbPersistence>}
*/
this.whenSynced = this.getDB().then(db => {
const currState = Y.encodeStateAsUpdate(doc)
return fetchUpdates(this).then(updatesStore => idb.addAutoKey(updatesStore, currState)).then(() => {
this.emit('synced', [this])
this.synced = true
return this
})
})
/**
* Timeout in ms untill data is merged and persisted in idb.
*/
this._storeTimeout = 1000
/**
* @type {any}
*/
this._storeTimeoutId = null
/**
* @param {Uint8Array} update
*/
this._storeUpdate = update =>
this._mux(() => {
this.getDB().then(db => {
const [updatesStore] = idb.transact(/** @type {IDBDatabase} */ db, [updatesStoreName])
idb.addAutoKey(updatesStore, update)
if (++this._dbsize >= PREFERRED_TRIM_SIZE) {
// debounce store call
if (this._storeTimeoutId !== null) {
clearTimeout(this._storeTimeoutId)
}
this._storeTimeoutId = setTimeout(() => {
storeState(this, false)
this._storeTimeoutId = null
}, this._storeTimeout)
}
})
})
doc.on('update', this._storeUpdate)
this.destroy = this.destroy.bind(this)
doc.on('destroy', this.destroy)
}
destroy () {
if (this._storeTimeoutId) {
clearTimeout(this._storeTimeoutId)
this._storeTimeoutId = null
}
this.doc.off('update', this._storeUpdate)
this.doc.off('destroy', this.destroy)
return this._db.then(db => {
db.close()
})
}
/**
* Destroys this instance and removes all data from indexeddb.
*
* @return {Promise<void>}
*/
clearData () {
return this.destroy().then(() => {
idb.deleteDB(this.name)
})
}
/**
* @param {String | number | ArrayBuffer | Date} key
* @return {Promise<String | number | ArrayBuffer | Date | any>}
*/
get (key) {
return this.getDB().then(db => {
const [custom] = idb.transact(db, [customStoreName], 'readonly')
return idb.get(custom, key)
})
}
/**
* @param {String | number | ArrayBuffer | Date} key
* @param {String | number | ArrayBuffer | Date} value
* @return {Promise<String | number | ArrayBuffer | Date>}
*/
set (key, value) {
return this.getDB().then(db => {
const [custom] = idb.transact(db, [customStoreName])
return idb.put(custom, value, key)
})
}
/**
* @param {String | number | ArrayBuffer | Date} key
* @return {Promise<undefined>}
*/
del (key) {
return this.getDB().then(db => {
const [custom] = idb.transact(db, [customStoreName])
return idb.del(custom, key)
})
}
}