2018-01-31 05:55:23 +00:00
|
|
|
// A store where you can divide data into "realms" that are backed with an LRU cache.
|
|
|
|
// Each realm has self-contained data that you can set with setForRealm() and compute
|
|
|
|
// with computeForRealm(). The maxSize determines how many realms to keep in the LRU cache.
|
|
|
|
import { Store } from 'svelte/store.js'
|
|
|
|
import QuickLRU from 'quick-lru'
|
2018-01-31 16:37:59 +00:00
|
|
|
import { mark, stop } from './marks'
|
2018-01-31 05:55:23 +00:00
|
|
|
|
|
|
|
export class RealmStore extends Store {
|
2018-02-09 06:29:29 +00:00
|
|
|
constructor (init, maxSize) {
|
2018-01-31 05:55:23 +00:00
|
|
|
super(init)
|
2018-08-30 04:42:57 +00:00
|
|
|
this.set({ realms: new QuickLRU({ maxSize: maxSize }) })
|
2018-01-31 16:37:59 +00:00
|
|
|
this._batches = {}
|
2018-01-31 05:55:23 +00:00
|
|
|
}
|
|
|
|
|
2018-02-09 06:29:29 +00:00
|
|
|
setCurrentRealm (realm) {
|
2018-08-30 04:42:57 +00:00
|
|
|
this.set({ currentRealm: realm })
|
2018-01-31 05:55:23 +00:00
|
|
|
}
|
|
|
|
|
2018-02-09 06:29:29 +00:00
|
|
|
setForRealm (obj) {
|
2018-04-20 04:38:11 +00:00
|
|
|
let { currentRealm, realms } = this.get()
|
2018-04-19 16:37:05 +00:00
|
|
|
realms.set(currentRealm, Object.assign(realms.get(currentRealm) || {}, obj))
|
2018-08-30 04:42:57 +00:00
|
|
|
this.set({ realms: realms })
|
2018-01-31 05:55:23 +00:00
|
|
|
}
|
|
|
|
|
2018-02-09 06:29:29 +00:00
|
|
|
computeForRealm (key, defaultValue) {
|
2018-01-31 05:55:23 +00:00
|
|
|
this.compute(key,
|
2018-04-19 03:43:13 +00:00
|
|
|
['realms', 'currentRealm'],
|
|
|
|
(realms, currentRealm) => {
|
|
|
|
let realmData = realms.get(currentRealm)
|
|
|
|
return (realmData && realmData[key]) || defaultValue
|
|
|
|
})
|
2018-01-31 05:55:23 +00:00
|
|
|
}
|
2018-01-31 16:37:59 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Update several values at once in a realm, assuming the key points
|
|
|
|
* to a plain old javascript object.
|
|
|
|
*/
|
2018-02-09 06:29:29 +00:00
|
|
|
batchUpdateForRealm (key, subKey, value) {
|
2018-04-19 16:37:05 +00:00
|
|
|
let { currentRealm } = this.get()
|
|
|
|
let realmBatches = this._batches[currentRealm]
|
2018-01-31 16:37:59 +00:00
|
|
|
if (!realmBatches) {
|
2018-04-19 16:37:05 +00:00
|
|
|
realmBatches = this._batches[currentRealm] = {}
|
2018-01-31 16:37:59 +00:00
|
|
|
}
|
|
|
|
let batch = realmBatches[key]
|
|
|
|
if (!batch) {
|
|
|
|
batch = realmBatches[key] = {}
|
|
|
|
}
|
|
|
|
batch[subKey] = value
|
|
|
|
|
|
|
|
requestAnimationFrame(() => {
|
2018-04-19 16:37:05 +00:00
|
|
|
let batch = this._batches[currentRealm] && this._batches[currentRealm][key]
|
2018-01-31 16:37:59 +00:00
|
|
|
if (!batch) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
let updatedKeys = Object.keys(batch)
|
|
|
|
if (!updatedKeys.length) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
mark('batchUpdate')
|
2018-04-20 04:38:11 +00:00
|
|
|
let obj = this.get()[key]
|
2018-01-31 16:37:59 +00:00
|
|
|
for (let otherKey of updatedKeys) {
|
|
|
|
obj[otherKey] = batch[otherKey]
|
|
|
|
}
|
2018-04-19 16:37:05 +00:00
|
|
|
delete this._batches[currentRealm][key]
|
|
|
|
let { realms } = this.get()
|
2018-08-30 04:42:57 +00:00
|
|
|
realms.set(currentRealm, Object.assign(realms.get(currentRealm) || {}, { [key]: obj }))
|
|
|
|
this.set({ realms: realms })
|
2018-01-31 16:37:59 +00:00
|
|
|
stop('batchUpdate')
|
|
|
|
})
|
|
|
|
}
|
2018-02-09 06:29:29 +00:00
|
|
|
}
|