@live-change/dao 0.3.11 → 0.3.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/DaoCache.js +14 -14
- package/lib/DaoProxy.js +20 -6
- package/lib/ObservableList.js +1 -1
- package/package.json +1 -1
package/lib/DaoCache.js
CHANGED
|
@@ -30,14 +30,17 @@ class CacheState {
|
|
|
30
30
|
|
|
31
31
|
turnOff() {
|
|
32
32
|
if(!this.cached) throw new Error("uncache of not cached")
|
|
33
|
+
if(!this.observable) throw new Error("race condition")
|
|
33
34
|
this.cached = false
|
|
34
35
|
this.cache.cachedCount --
|
|
35
|
-
this.observable
|
|
36
|
+
const observable = this.observable
|
|
36
37
|
this.observable = null
|
|
38
|
+
observable.unobserve(this.cache.dummyObserver)
|
|
37
39
|
}
|
|
38
40
|
|
|
39
41
|
turnOn() {
|
|
40
42
|
if(this.cached) throw new Error("already cached")
|
|
43
|
+
if(this.observable) throw new Error("race condition")
|
|
41
44
|
this.cached = true
|
|
42
45
|
this.cache.cachedCount ++
|
|
43
46
|
this.cache.cache.push(this)
|
|
@@ -81,13 +84,7 @@ class CacheState {
|
|
|
81
84
|
if(delta > 0) {
|
|
82
85
|
this.score += delta * this.settings.singleReadScore
|
|
83
86
|
}
|
|
84
|
-
this.updateCacheState()
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
dipose() {
|
|
88
|
-
if(this.cached) {
|
|
89
|
-
this.observable.unobserve(this.cache.dummyObserver)
|
|
90
|
-
}
|
|
87
|
+
setTimeout(() => this.updateCacheState(), 0)
|
|
91
88
|
}
|
|
92
89
|
}
|
|
93
90
|
|
|
@@ -140,10 +137,13 @@ class DaoCache extends EventEmitter {
|
|
|
140
137
|
}
|
|
141
138
|
|
|
142
139
|
clear() {
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
140
|
+
const now = Date.now()
|
|
141
|
+
for(const cacheState of this.cache) {
|
|
142
|
+
cacheState.score = 0
|
|
143
|
+
cacheState.scoreTime = now
|
|
144
|
+
if(cacheState.cached) cacheState.turnOff()
|
|
146
145
|
}
|
|
146
|
+
this.cache = []
|
|
147
147
|
}
|
|
148
148
|
|
|
149
149
|
clean() {
|
|
@@ -177,7 +177,6 @@ class DaoCache extends EventEmitter {
|
|
|
177
177
|
}
|
|
178
178
|
for(const [key, value] of this.cacheState.entries()) {
|
|
179
179
|
if(value.score < value.settings.deleteStatsScore) {
|
|
180
|
-
this.cacheState.dispose()
|
|
181
180
|
this.cacheState.delete(key)
|
|
182
181
|
}
|
|
183
182
|
}
|
|
@@ -197,8 +196,9 @@ class DaoCache extends EventEmitter {
|
|
|
197
196
|
|
|
198
197
|
noticeObserverCount(what, count, delta) {
|
|
199
198
|
//console.log("OBSERVER COUNT", JSON.stringify(what), count, "D", delta)
|
|
200
|
-
|
|
201
|
-
if(!cacheState) return
|
|
199
|
+
let cacheState = this.getOrCreateCacheState(what)
|
|
200
|
+
if(!cacheState && delta <= 0) return
|
|
201
|
+
cacheState = this.getOrCreateCacheState(what)
|
|
202
202
|
if(delta > 0) {
|
|
203
203
|
//console.log("CACHE TEST", cacheState.cached)
|
|
204
204
|
if(cacheState.observable) {
|
package/lib/DaoProxy.js
CHANGED
|
@@ -20,9 +20,11 @@ class DaoProxy extends EventEmitter {
|
|
|
20
20
|
this.dao = dao
|
|
21
21
|
if(this.dao) {
|
|
22
22
|
for(let [id, observable] of this.observables.entries()) {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
23
|
+
if(!observable.disposed) {
|
|
24
|
+
let what = JSON.parse(id)
|
|
25
|
+
const target = this.dao.observable(what)
|
|
26
|
+
observable.setTarget(target)
|
|
27
|
+
}
|
|
26
28
|
}
|
|
27
29
|
if(this.dao.on) {
|
|
28
30
|
this.dao.on('connect', this.onConnect)
|
|
@@ -45,11 +47,23 @@ class DaoProxy extends EventEmitter {
|
|
|
45
47
|
} else {
|
|
46
48
|
observable = new ObservableProxy()
|
|
47
49
|
}
|
|
48
|
-
|
|
50
|
+
const oldDispose = observable.dispose
|
|
49
51
|
observable.dispose = (...args) => {
|
|
50
|
-
|
|
52
|
+
this.observables.delete(spath)
|
|
51
53
|
oldDispose.call(observable, ...args)
|
|
52
|
-
}
|
|
54
|
+
}
|
|
55
|
+
const oldRespawn = observable.respawn
|
|
56
|
+
observable.respawn = (...args) => {
|
|
57
|
+
const newObservable = this.observables.get(spath)
|
|
58
|
+
if(newObservable && newObservable !== observable) {
|
|
59
|
+
observable.target = newObservable
|
|
60
|
+
} else if(this.dao) {
|
|
61
|
+
observable.target = this.dao.observable(what)
|
|
62
|
+
} else {
|
|
63
|
+
observable.target = null
|
|
64
|
+
}
|
|
65
|
+
oldRespawn.call(observable, ...args)
|
|
66
|
+
}
|
|
53
67
|
this.observables.set(JSON.stringify(what), observable)
|
|
54
68
|
return observable
|
|
55
69
|
}
|
package/lib/ObservableList.js
CHANGED
package/package.json
CHANGED