@live-change/dao 0.3.9 → 0.3.13
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 +18 -4
- package/lib/DaoProxy.js +20 -6
- package/lib/LoopbackConnection.js +1 -1
- package/lib/ObservableList.js +1 -1
- package/lib/ReactiveConnection.js +11 -1
- package/lib/ReactiveServerConnection.js +17 -14
- 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,7 +84,7 @@ class CacheState {
|
|
|
81
84
|
if(delta > 0) {
|
|
82
85
|
this.score += delta * this.settings.singleReadScore
|
|
83
86
|
}
|
|
84
|
-
this.updateCacheState()
|
|
87
|
+
setTimeout(() => this.updateCacheState(), 0)
|
|
85
88
|
}
|
|
86
89
|
}
|
|
87
90
|
|
|
@@ -133,6 +136,15 @@ class DaoCache extends EventEmitter {
|
|
|
133
136
|
this.dummyObserver = () => 0
|
|
134
137
|
}
|
|
135
138
|
|
|
139
|
+
clear() {
|
|
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()
|
|
145
|
+
}
|
|
146
|
+
this.cache = []
|
|
147
|
+
}
|
|
136
148
|
|
|
137
149
|
clean() {
|
|
138
150
|
//console.log("CACHE STATE:", this.cacheState)
|
|
@@ -184,8 +196,9 @@ class DaoCache extends EventEmitter {
|
|
|
184
196
|
|
|
185
197
|
noticeObserverCount(what, count, delta) {
|
|
186
198
|
//console.log("OBSERVER COUNT", JSON.stringify(what), count, "D", delta)
|
|
187
|
-
|
|
188
|
-
if(!cacheState) return
|
|
199
|
+
let cacheState = this.getOrCreateCacheState(what)
|
|
200
|
+
if(!cacheState && delta <= 0) return
|
|
201
|
+
cacheState = this.getOrCreateCacheState(what)
|
|
189
202
|
if(delta > 0) {
|
|
190
203
|
//console.log("CACHE TEST", cacheState.cached)
|
|
191
204
|
if(cacheState.observable) {
|
|
@@ -253,6 +266,7 @@ class DaoCache extends EventEmitter {
|
|
|
253
266
|
}
|
|
254
267
|
|
|
255
268
|
dispose() {
|
|
269
|
+
clear()
|
|
256
270
|
clearInterval(this.interval)
|
|
257
271
|
this.dao.dispose()
|
|
258
272
|
}
|
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
|
@@ -123,6 +123,8 @@ class Connection extends EventEmitter {
|
|
|
123
123
|
this.remoteObserveSent = new Map()
|
|
124
124
|
this.remoteUnobserveSent = new Map()
|
|
125
125
|
|
|
126
|
+
this.activeTimeouts = new Set()
|
|
127
|
+
|
|
126
128
|
this.autoReconnect = true
|
|
127
129
|
|
|
128
130
|
this.finished = false
|
|
@@ -173,7 +175,8 @@ class Connection extends EventEmitter {
|
|
|
173
175
|
}
|
|
174
176
|
|
|
175
177
|
if (settings.requestTimeout && settings.requestTimeout < Infinity) {
|
|
176
|
-
setTimeout(() => {
|
|
178
|
+
const timeout = setTimeout(() => {
|
|
179
|
+
this.activeTimeouts.delete(timeout)
|
|
177
180
|
let waiting = this.waitingRequests.get(msg.requestId)
|
|
178
181
|
if (waiting) {
|
|
179
182
|
waiting.handler('timeout')
|
|
@@ -189,6 +192,7 @@ class Connection extends EventEmitter {
|
|
|
189
192
|
}
|
|
190
193
|
}
|
|
191
194
|
}, settings.requestTimeout)
|
|
195
|
+
this.activeTimeouts.add(timeout)
|
|
192
196
|
}
|
|
193
197
|
|
|
194
198
|
if(this.connected) {
|
|
@@ -443,6 +447,12 @@ class Connection extends EventEmitter {
|
|
|
443
447
|
})
|
|
444
448
|
}
|
|
445
449
|
|
|
450
|
+
dispose() {
|
|
451
|
+
console.log("DISPOSE REACTIVE CONNECTION")
|
|
452
|
+
this.finished = true
|
|
453
|
+
for(const timeout of this.activeTimeouts) clearTimeout(timeout)
|
|
454
|
+
}
|
|
455
|
+
|
|
446
456
|
}
|
|
447
457
|
|
|
448
458
|
module.exports = Connection
|
|
@@ -389,8 +389,7 @@ class ReactiveServerConnection extends EventEmitter {
|
|
|
389
389
|
|
|
390
390
|
if(this.settings.fastAuth) {
|
|
391
391
|
try {
|
|
392
|
-
this.credentials
|
|
393
|
-
this.daoPromise = this.daoFactory(this.credentials, this.connection, this)
|
|
392
|
+
this.handleDaoPromise(this.daoFactory(this.credentials, this.connection, this))
|
|
394
393
|
} catch(error) {
|
|
395
394
|
return this.handleDaoFactoryError(error)
|
|
396
395
|
}
|
|
@@ -696,6 +695,21 @@ class ReactiveServerConnection extends EventEmitter {
|
|
|
696
695
|
this.closeConnection()
|
|
697
696
|
}
|
|
698
697
|
|
|
698
|
+
handleDaoPromise(daoPromise) {
|
|
699
|
+
this.daoPromise = daoPromise
|
|
700
|
+
if(!this.daoPromise.then) {
|
|
701
|
+
this.dao = this.daoPromise
|
|
702
|
+
this.daoPromise = null
|
|
703
|
+
} else {
|
|
704
|
+
this.daoPromise.catch(error => this.handleDaoFactoryError(error)).then(dd => {
|
|
705
|
+
if(!dd) return this.handleDaoFactoryError("dao not defined")
|
|
706
|
+
this.dao = dd
|
|
707
|
+
this.daoPromise = null
|
|
708
|
+
for(const message of this.daoGenerationQueue) this.handleAuthorizedMessage(message)
|
|
709
|
+
})
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
|
|
699
713
|
handleMessage(message) {
|
|
700
714
|
if (!this.dao && !this.daoPromise) {
|
|
701
715
|
if (!message) {
|
|
@@ -704,21 +718,10 @@ class ReactiveServerConnection extends EventEmitter {
|
|
|
704
718
|
}
|
|
705
719
|
try {
|
|
706
720
|
this.credentials = message
|
|
707
|
-
this.
|
|
721
|
+
this.handleDaoPromise(this.daoFactory(this.credentials, this.connection, this))
|
|
708
722
|
} catch(error) {
|
|
709
723
|
return this.handleDaoFactoryError(error)
|
|
710
724
|
}
|
|
711
|
-
if(!this.daoPromise.then) {
|
|
712
|
-
this.dao = this.daoPromise
|
|
713
|
-
this.daoPromise = null
|
|
714
|
-
} else {
|
|
715
|
-
this.daoPromise.catch(error => this.handleDaoFactoryError(error)).then(dd => {
|
|
716
|
-
if(!dd) return this.handleDaoFactoryError("dao not defined")
|
|
717
|
-
this.dao = dd
|
|
718
|
-
this.daoPromise = null
|
|
719
|
-
for(const message of this.daoGenerationQueue) this.handleAuthorizedMessage(message)
|
|
720
|
-
})
|
|
721
|
-
}
|
|
722
725
|
} else if(this.daoPromise && !this.dao) {
|
|
723
726
|
this.daoGenerationQueue.push(message)
|
|
724
727
|
} else {
|
package/package.json
CHANGED