@live-change/dao 0.4.12 → 0.5.1
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/browser.js +4 -0
- package/index.js +3 -0
- package/lib/InboxReader.js +97 -0
- package/lib/Observable.js +4 -1
- package/lib/ObservableList.js +14 -1
- package/lib/ObservableValue.js +5 -4
- package/package.json +2 -2
package/browser.js
CHANGED
|
@@ -34,6 +34,10 @@ import DaoCache from "./lib/DaoCache.js"
|
|
|
34
34
|
rd.DaoCache = DaoCache
|
|
35
35
|
export { DaoCache }
|
|
36
36
|
|
|
37
|
+
import InboxReader from "./lib/InboxReader.js"
|
|
38
|
+
rd.InboxReader = InboxReader
|
|
39
|
+
export { InboxReader }
|
|
40
|
+
|
|
37
41
|
import ReactiveConnection from "./lib/ReactiveConnection.js"
|
|
38
42
|
rd.ReactiveConnection = ReactiveConnection
|
|
39
43
|
export { ReactiveConnection }
|
package/index.js
CHANGED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
|
|
2
|
+
class InboxReader {
|
|
3
|
+
constructor(observableFunction, callback, start = '', bucketSize = 32) {
|
|
4
|
+
this.observableFunction = observableFunction
|
|
5
|
+
this.position = start
|
|
6
|
+
this.callback = callback
|
|
7
|
+
this.bucketSize = bucketSize
|
|
8
|
+
|
|
9
|
+
this.observable = null
|
|
10
|
+
this.queue = []
|
|
11
|
+
this.processingPromise = null
|
|
12
|
+
this.finished = false
|
|
13
|
+
this.readPromise = null
|
|
14
|
+
this.readPromiseResolve = null
|
|
15
|
+
|
|
16
|
+
this.observer = (signal, ...args) => {
|
|
17
|
+
if(signal == 'error') {
|
|
18
|
+
const error = args[0]
|
|
19
|
+
console.error("PEER MESSAGE ERROR", error.stack || error)
|
|
20
|
+
return
|
|
21
|
+
}
|
|
22
|
+
if(signal == 'putByField') {
|
|
23
|
+
const [field, id, message] = args
|
|
24
|
+
this.handleMessage(message)
|
|
25
|
+
} else if(signal == 'set') {
|
|
26
|
+
const value = args[0]
|
|
27
|
+
for (const message of value) {
|
|
28
|
+
this.handleMessage(message)
|
|
29
|
+
}
|
|
30
|
+
} else if(signal == 'push') {
|
|
31
|
+
const [message] = args
|
|
32
|
+
this.handleMessage(message)
|
|
33
|
+
} else {
|
|
34
|
+
console.error("INBOX READER SIGNAL NOT HANDLED", signal)
|
|
35
|
+
}
|
|
36
|
+
this.startProcessing()
|
|
37
|
+
this.observeNext()
|
|
38
|
+
}
|
|
39
|
+
this.observeNext()
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
observeNext() {
|
|
43
|
+
if(this.queue.length > this.bucketSize) return // no need to observe, there is work to do
|
|
44
|
+
if(this.observable) {
|
|
45
|
+
const observableValue = this.observable.getValue()
|
|
46
|
+
if(!observableValue) return // still loading data
|
|
47
|
+
if(observableValue.length < this.bucketSize) return // we are waiting for more
|
|
48
|
+
}
|
|
49
|
+
if(this.observable) {
|
|
50
|
+
this.observable.unobserve(this.observer)
|
|
51
|
+
this.observable = null
|
|
52
|
+
}
|
|
53
|
+
this.readPromise = new Promise(resolve => this.readPromiseResolve = resolve)
|
|
54
|
+
this.observable = this.observableFunction(this.position, this.bucketSize)
|
|
55
|
+
this.observable.observe(this.observer)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
handleMessage(message) {
|
|
59
|
+
if(message.id <= this.position) return // ignore
|
|
60
|
+
this.position = message.id
|
|
61
|
+
this.queue.push(message)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
startProcessing() {
|
|
65
|
+
if(!this.processingPromise) {
|
|
66
|
+
this.processingPromise = this.doProcessing()
|
|
67
|
+
this.processingPromise.then(() => this.processingPromise = null)
|
|
68
|
+
}
|
|
69
|
+
return this.processingPromise
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
async doProcessing() {
|
|
73
|
+
while(this.queue.length > 0 && !this.finished) {
|
|
74
|
+
const message = this.queue.shift()
|
|
75
|
+
await this.callback(message)
|
|
76
|
+
this.observeNext() // observe if needed
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
async sync() {
|
|
81
|
+
while(this.queue.length > 0 || this.readPromise) {
|
|
82
|
+
if(this.queue.length > 0) await this.startProcessing()
|
|
83
|
+
if(this.readPromise) await this.readPromise
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
dispose() {
|
|
88
|
+
this.finished = true
|
|
89
|
+
if(this.observable) {
|
|
90
|
+
this.observable.unobserve(this.observer)
|
|
91
|
+
this.observable = null
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
module.exports = InboxReader
|
package/lib/Observable.js
CHANGED
|
@@ -35,7 +35,8 @@ class Observable {
|
|
|
35
35
|
|
|
36
36
|
handleError(error) {
|
|
37
37
|
let handled = false
|
|
38
|
-
|
|
38
|
+
if(!error) return true /// ignore null errors!
|
|
39
|
+
for (let observer of this.errorObservers) {
|
|
39
40
|
handled = true
|
|
40
41
|
observer(error)
|
|
41
42
|
}
|
|
@@ -98,6 +99,8 @@ class Observable {
|
|
|
98
99
|
|
|
99
100
|
const waitPromise = new Promise((resolve, reject) => {
|
|
100
101
|
errorObserver = (error) => {
|
|
102
|
+
console.log("ERROR SIGNAL", error)
|
|
103
|
+
console.trace("WEER")
|
|
101
104
|
if(resultObserver) this.unobserve(resultObserver)
|
|
102
105
|
resultObserver = undefined
|
|
103
106
|
if(errorObserver) this.uncatch(errorObserver)
|
package/lib/ObservableList.js
CHANGED
|
@@ -18,6 +18,7 @@ class ObservableList extends Observable {
|
|
|
18
18
|
}
|
|
19
19
|
|
|
20
20
|
set(list) {
|
|
21
|
+
this.handleError(null)
|
|
21
22
|
if(list === this.list) return
|
|
22
23
|
try {
|
|
23
24
|
if (JSON.stringify(list) == JSON.stringify(this.list)) return
|
|
@@ -30,26 +31,32 @@ class ObservableList extends Observable {
|
|
|
30
31
|
}
|
|
31
32
|
|
|
32
33
|
push(value) {
|
|
34
|
+
this.handleError(null)
|
|
33
35
|
this.list.push(value)
|
|
34
36
|
this.fireObservers('push', value)
|
|
35
37
|
}
|
|
36
38
|
unshift(value) {
|
|
39
|
+
this.handleError(null)
|
|
37
40
|
this.list.unshift(value)
|
|
38
41
|
this.fireObservers('unshift', value)
|
|
39
42
|
}
|
|
40
43
|
pop() {
|
|
44
|
+
this.handleError(null)
|
|
41
45
|
this.list.pop()
|
|
42
46
|
this.fireObservers('pop')
|
|
43
47
|
}
|
|
44
48
|
shift() {
|
|
49
|
+
this.handleError(null)
|
|
45
50
|
this.list.shift()
|
|
46
51
|
this.fireObservers('shift')
|
|
47
52
|
}
|
|
48
53
|
splice(at, del, ...values) {
|
|
54
|
+
this.handleError(null)
|
|
49
55
|
this.list.splice(at, del, ...values)
|
|
50
56
|
this.fireObservers('splice', at, del, ...values)
|
|
51
57
|
}
|
|
52
58
|
putByField(field, value, element, reverse = false, oldElement) {
|
|
59
|
+
this.handleError(null)
|
|
53
60
|
if(!reverse) {
|
|
54
61
|
let i, l
|
|
55
62
|
for(i = 0, l = this.list.length; i < l; i++) {
|
|
@@ -80,6 +87,7 @@ class ObservableList extends Observable {
|
|
|
80
87
|
this.fireObservers('putByField', field, value, element, reverse, oldElement)
|
|
81
88
|
}
|
|
82
89
|
remove(exact) {
|
|
90
|
+
this.handleError(null)
|
|
83
91
|
let json = JSON.stringify(exact)
|
|
84
92
|
for(let i = 0, l = this.list.length; i < l; i++) {
|
|
85
93
|
if(JSON.stringify(this.list[i]) == json) this.list.splice(i, 1)
|
|
@@ -87,6 +95,7 @@ class ObservableList extends Observable {
|
|
|
87
95
|
this.fireObservers('remove', exact)
|
|
88
96
|
}
|
|
89
97
|
removeByField(field, value, oldElement) {
|
|
98
|
+
this.handleError(null)
|
|
90
99
|
let json = JSON.stringify(value)
|
|
91
100
|
for(let i = 0, l = this.list.length; i < l; i++) {
|
|
92
101
|
if(JSON.stringify(this.list[i][field]) == json) {
|
|
@@ -99,8 +108,9 @@ class ObservableList extends Observable {
|
|
|
99
108
|
this.fireObservers('removeByField', field, value, oldElement)
|
|
100
109
|
}
|
|
101
110
|
removeBy(fields) {
|
|
111
|
+
this.handleError(null)
|
|
102
112
|
let jsonf = []
|
|
103
|
-
for(
|
|
113
|
+
for(let k in fields) {
|
|
104
114
|
jsonf.push([k, JSON.stringify(fields[k])])
|
|
105
115
|
}
|
|
106
116
|
for(let i = 0, l = this.list.length; i < l; i++) {
|
|
@@ -118,6 +128,7 @@ class ObservableList extends Observable {
|
|
|
118
128
|
}
|
|
119
129
|
|
|
120
130
|
update(exact, element) {
|
|
131
|
+
this.handleError(null)
|
|
121
132
|
let json = JSON.stringify(exact)
|
|
122
133
|
for(let i = 0, l = this.list.length; i < l; i++) {
|
|
123
134
|
if(JSON.stringify(this.list[i]) == json) this.list.splice(i, 1, element)
|
|
@@ -125,6 +136,7 @@ class ObservableList extends Observable {
|
|
|
125
136
|
this.fireObservers('update', exact, element)
|
|
126
137
|
}
|
|
127
138
|
updateByField(field, value, element) {
|
|
139
|
+
this.handleError(null)
|
|
128
140
|
let json = JSON.stringify(value)
|
|
129
141
|
for(let i = 0, l = this.list.length; i < l; i++) {
|
|
130
142
|
if(JSON.stringify(this.list[i][field]) == json) this.list.splice(i, 1, element)
|
|
@@ -132,6 +144,7 @@ class ObservableList extends Observable {
|
|
|
132
144
|
this.fireObservers('updateByField', field, value, element)
|
|
133
145
|
}
|
|
134
146
|
updateBy(fields, element) {
|
|
147
|
+
this.handleError(null)
|
|
135
148
|
let jsonf = []
|
|
136
149
|
for(const k in fields) {
|
|
137
150
|
jsonf.push([k, JSON.stringify(fields[k])])
|
package/lib/ObservableValue.js
CHANGED
|
@@ -17,6 +17,7 @@ class ObservableValue extends Observable {
|
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
set(value) {
|
|
20
|
+
this.handleError(null)
|
|
20
21
|
if(value === this.value) return;
|
|
21
22
|
try {
|
|
22
23
|
if (JSON.stringify(value) == JSON.stringify(this.value)) return;
|
|
@@ -35,7 +36,7 @@ class ObservableValue extends Observable {
|
|
|
35
36
|
}
|
|
36
37
|
unbindProperty(object, property) {
|
|
37
38
|
for(let i = 0; i < this.properties.length; i++) {
|
|
38
|
-
|
|
39
|
+
let prop = this.properties[i]
|
|
39
40
|
if(prop[0] == object && prop[1] == property) {
|
|
40
41
|
this.properties.splice(i,1)
|
|
41
42
|
if(this.isUseless()) this.dispose()
|
|
@@ -51,8 +52,8 @@ class ObservableValue extends Observable {
|
|
|
51
52
|
if(this.savedError !== undefined) object[property] = this.savedError
|
|
52
53
|
}
|
|
53
54
|
unbindErrorProperty(object, property) {
|
|
54
|
-
for(
|
|
55
|
-
|
|
55
|
+
for(let i = 0; i < this.errorProperties.length; i++) {
|
|
56
|
+
let prop = this.errorProperties[i]
|
|
56
57
|
if(prop[0] == object && prop[1] == property) {
|
|
57
58
|
this.errorProperties.splice(i,1)
|
|
58
59
|
if(this.isUseless()) this.dispose()
|
|
@@ -65,7 +66,7 @@ class ObservableValue extends Observable {
|
|
|
65
66
|
handleError(error) {
|
|
66
67
|
this.savedError = error
|
|
67
68
|
let handled = super.handleError(error)
|
|
68
|
-
for(
|
|
69
|
+
for(let [object, property] of this.errorProperties) {
|
|
69
70
|
handled = true
|
|
70
71
|
object[property] = error
|
|
71
72
|
}
|
package/package.json
CHANGED