-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathy-indexeddb.js
184 lines (172 loc) · 5.41 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
import * as Y from 'yjs'
import * as idb from 'lib0/indexeddb'
import * as promise from 'lib0/promise'
import { Observable } from 'lib0/observable'
const customStoreName = 'custom'
const updatesStoreName = 'updates'
export const PREFERRED_TRIM_SIZE = 500
/**
* @param {IndexeddbPersistence} idbPersistence
* @param {function(IDBObjectStore):void} [beforeApplyUpdatesCallback]
* @param {function(IDBObjectStore):void} [afterApplyUpdatesCallback]
*/
export const fetchUpdates = (idbPersistence, beforeApplyUpdatesCallback = () => {}, afterApplyUpdatesCallback = () => {}) => {
const [updatesStore] = idb.transact(/** @type {IDBDatabase} */ (idbPersistence.db), [updatesStoreName]) // , 'readonly')
return idb.getAll(updatesStore, idb.createIDBKeyRangeLowerBound(idbPersistence._dbref, false)).then(updates => {
if (!idbPersistence._destroyed) {
beforeApplyUpdatesCallback(updatesStore)
Y.transact(idbPersistence.doc, () => {
updates.forEach(val => Y.applyUpdate(idbPersistence.doc, val))
}, idbPersistence, false)
afterApplyUpdatesCallback(updatesStore)
}
})
.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._dbref = 0
this._dbsize = 0
this._destroyed = false
/**
* @type {IDBDatabase|null}
*/
this.db = null
this.synced = false
this._db = idb.openDB(name, db =>
idb.createStores(db, [
['updates', { autoIncrement: true }],
['custom']
])
)
/**
* @type {Promise<IndexeddbPersistence>}
*/
this.whenSynced = promise.create(resolve => this.on('synced', () => resolve(this)))
this._db.then(db => {
this.db = db
/**
* @param {IDBObjectStore} updatesStore
*/
const beforeApplyUpdatesCallback = (updatesStore) => idb.addAutoKey(updatesStore, Y.encodeStateAsUpdate(doc))
const afterApplyUpdatesCallback = () => {
if (this._destroyed) return this
this.synced = true
this.emit('synced', [this])
}
fetchUpdates(this, beforeApplyUpdatesCallback, afterApplyUpdatesCallback)
})
/**
* Timeout in ms untill data is merged and persisted in idb.
*/
this._storeTimeout = 1000
/**
* @type {any}
*/
this._storeTimeoutId = null
/**
* @param {Uint8Array} update
* @param {any} origin
*/
this._storeUpdate = (update, origin) => {
if (this.db && origin !== this) {
const [updatesStore] = idb.transact(/** @type {IDBDatabase} */ (this.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.doc.off('update', this._storeUpdate)
this.doc.off('destroy', this.destroy)
this._destroyed = true
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._db.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._db.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._db.then(db => {
const [custom] = idb.transact(db, [customStoreName])
return idb.del(custom, key)
})
}
}