@live-change/dao-vue3 0.4.11 → 0.4.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.
Files changed (3) hide show
  1. package/index.js +2 -2
  2. package/lib/live.js +183 -178
  3. package/package.json +3 -3
package/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import reactiveMixin from './lib/reactiveMixin.js'
2
2
  import reactivePrefetchMixin from './lib/reactivePrefetchMixin.js'
3
- import live from './lib/live.js'
3
+ import { live, fetch } from './lib/live.js'
4
4
  import ReactiveObservableList from './lib/ReactiveObservableList.js'
5
5
  import RangeBuckets from './lib/RangeBuckets.js'
6
6
 
@@ -14,6 +14,6 @@ const ReactiveDaoVue = {
14
14
  }
15
15
 
16
16
  //// TODO: rename reactive to live
17
- export { ReactiveDaoVue, reactiveMixin, reactivePrefetchMixin, ReactiveObservableList, RangeBuckets, live }
17
+ export { ReactiveDaoVue, reactiveMixin, reactivePrefetchMixin, ReactiveObservableList, RangeBuckets, live, fetch }
18
18
 
19
19
  export default ReactiveDaoVue
package/lib/live.js CHANGED
@@ -24,6 +24,56 @@ function createActionFunction(action, object) {
24
24
  return func
25
25
  }
26
26
 
27
+ async function fetch(api, path) {
28
+ if(Array.isArray(path)) path = { what: path }
29
+ const paths = [ path ]
30
+ const preFetchPaths = await api.get({ paths })
31
+ debug("PRE FETCH DATA", preFetchPaths)
32
+ const preFetchMap = new Map(preFetchPaths.map((res) => [JSON.stringify(res.what), res] ))
33
+ function createObject(what, more) {
34
+ const res = preFetchMap.get(JSON.stringify(what))
35
+ if(res.error) throw new Error(res.error)
36
+ const data = res.data
37
+ if(data && more) {
38
+ if(Array.isArray(data)) {
39
+ for(let i = 0; i < data.length; i ++) {
40
+ for(const moreElement of more) {
41
+ if(moreElement.to) {
42
+ debug("COLLECT POINTERS FROM", data[i], "SC", moreElement.schema)
43
+ const pointers = collectPointers(data[i], moreElement.schema ,
44
+ (what) => preFetchMap.get(JSON.stringify(what)))
45
+ debug("POINTERS COLLECTED", pointers)
46
+ const values = pointers.map(pointer => createObject(pointer, moreElement.more))
47
+ debug("VALUES", values)
48
+ debug("MANY", pointers.many)
49
+ if(pointers.many) {
50
+ data[i][moreElement.to] = values
51
+ } else {
52
+ data[i][moreElement.to] = values[0] || null
53
+ }
54
+ }
55
+ }
56
+ }
57
+ } else {
58
+ for(const moreElement of more) {
59
+ if(moreElement.to) {
60
+ const pointers = collectPointers(data, moreElement.schema,
61
+ (what) => preFetchMap.get(JSON.stringify(what)))
62
+ const values = pointers.map(pointer => createObject(pointer, moreElement.more))
63
+ if(pointers.many) {
64
+ data[moreElement.to] = values
65
+ } else {
66
+ data[moreElement.to] = values[0] || null
67
+ }
68
+ }
69
+ }
70
+ }
71
+ }
72
+ return ref(data)
73
+ }
74
+ return createObject(path.what, path.more)
75
+ }
76
+
27
77
  async function live(api, path, onUnmountedCb) {
28
78
  if(isRef(path)) {
29
79
  /// TODO: support path as ref/computed
@@ -40,205 +90,160 @@ async function live(api, path, onUnmountedCb) {
40
90
  }
41
91
  }
42
92
 
93
+ if(typeof window == 'undefined') return fetch(api, path)
43
94
  if(Array.isArray(path)) path = { what: path }
44
95
  const paths = [ path ]
45
- if(typeof window == 'undefined') {
46
- const preFetchPaths = await api.get({ paths })
47
- debug("PRE FETCH DATA", preFetchPaths)
48
- const preFetchMap = new Map(preFetchPaths.map((res) => [JSON.stringify(res.what), res] ))
49
- function createObject(what, more) {
50
- const res = preFetchMap.get(JSON.stringify(what))
51
- if(res.error) throw new Error(res.error)
52
- const data = res.data
53
- if(data && more) {
54
- if(Array.isArray(data)) {
55
- for(let i = 0; i < data.length; i ++) {
56
- for(const moreElement of more) {
57
- if(moreElement.to) {
58
- debug("COLLECT POINTERS FROM", data[i], "SC", moreElement.schema)
59
- const pointers = collectPointers(data[i], moreElement.schema ,
60
- (what) => preFetchMap.get(JSON.stringify(what)))
61
- debug("POINTERS COLLECTED", pointers)
62
- const values = pointers.map(pointer => createObject(pointer, moreElement.more))
63
- debug("VALUES", values)
64
- debug("MANY", pointers.many)
65
- if(pointers.many) {
66
- data[i][moreElement.to] = values
67
- } else {
68
- data[i][moreElement.to] = values[0] || null
69
- }
70
- }
71
- }
72
- }
73
- } else {
74
- for(const moreElement of more) {
96
+ const preFetchPaths = api.observable({ paths })
97
+ function bindResult(what, more, actions, object, property, onError) {
98
+ if(!what) throw new Error("what parameter required!")
99
+ const observable = api.observable(what)
100
+ const errorObserver = { error: onError }
101
+ let dispose
102
+ if((more && more.some(m => m.to)) || actions) {
103
+ const extendedObservable = new ExtendedObservableList(observable,
104
+ newElement => {
105
+ if(!newElement) return newElement
106
+ const extendedElement = reactive({ ...newElement })
107
+ const props = {}
108
+ if(more) for(const moreElement of more) {
75
109
  if(moreElement.to) {
76
- const pointers = collectPointers(data, moreElement.schema,
77
- (what) => preFetchMap.get(JSON.stringify(what)))
78
- const values = pointers.map(pointer => createObject(pointer, moreElement.more))
79
- if(pointers.many) {
80
- data[moreElement.to] = values
81
- } else {
82
- data[moreElement.to] = values[0] || null
110
+ const prop = {
111
+ bounds: [],
112
+ sources: []
83
113
  }
84
- }
85
- }
86
- }
87
- }
88
- return ref(data)
89
- }
90
- return createObject(path.what, path.more)
91
- } else {
92
- const preFetchPaths = api.observable({ paths })
93
- function bindResult(what, more, actions, object, property, onError) {
94
- if(!what) throw new Error("what parameter required!")
95
- const observable = api.observable(what)
96
- const errorObserver = { error: onError }
97
- let dispose
98
- if((more && more.some(m => m.to)) || actions) {
99
- const extendedObservable = new ExtendedObservableList(observable,
100
- newElement => {
101
- if(!newElement) return newElement
102
- const extendedElement = reactive({ ...newElement })
103
- const props = {}
104
- if(more) for(const moreElement of more) {
105
- if(moreElement.to) {
106
- const prop = {
107
- bounds: [],
108
- sources: []
109
- }
110
- props[moreElement.to] = prop
111
- let requiredSrcs = []
112
- const srcs = new Map()
113
- function getSource(ptr) {
114
- const exists = srcs.get(ptr)
115
- if(exists !== undefined) return exists.list
116
- requiredSrcs.push(exists)
117
- return undefined
118
- }
119
- function computePointers() {
120
- while(true) {
121
- const pointers = collectPointers(newElement, moreElement.schema, getSource)
122
- if(requiredSrcs.length == 0) return pointers
123
- for(const requiredSrc of requiredSrcs) {
124
- const observable = api.observable(requiredSrc)
125
- const observer = () => {
126
- bindPointers(computePointers())
127
- }
128
- srcs.set(JSON.stringify(requiredSrc), observable)
129
- prop.sources.push({ observable, observer })
130
- observable.observe(observer)
114
+ props[moreElement.to] = prop
115
+ let requiredSrcs = []
116
+ const srcs = new Map()
117
+ function getSource(ptr) {
118
+ const exists = srcs.get(ptr)
119
+ if(exists !== undefined) return exists.list
120
+ requiredSrcs.push(exists)
121
+ return undefined
122
+ }
123
+ function computePointers() {
124
+ while(true) {
125
+ const pointers = collectPointers(newElement, moreElement.schema, getSource)
126
+ if(requiredSrcs.length == 0) return pointers
127
+ for(const requiredSrc of requiredSrcs) {
128
+ const observable = api.observable(requiredSrc)
129
+ const observer = () => {
130
+ bindPointers(computePointers())
131
131
  }
132
+ srcs.set(JSON.stringify(requiredSrc), observable)
133
+ prop.sources.push({ observable, observer })
134
+ observable.observe(observer)
132
135
  }
133
136
  }
134
- function bindPointers(pointers) {
135
- if(pointers.many) {
136
- const oldBound = prop.bounds.slice()
137
- const newArray = new Array(pointers.length)
138
- const newBounds = new Array(pointers.length)
139
- for(let i = 0; i < pointers.length; i++) {
140
- newBounds[i] = bindResult(pointers[i], moreElement.more, moreElement.actions,
141
- newArray, i, onError)
137
+ }
138
+ function bindPointers(pointers) {
139
+ if(pointers.many) {
140
+ const oldBound = prop.bounds.slice()
141
+ const newArray = new Array(pointers.length)
142
+ const newBounds = new Array(pointers.length)
143
+ for(let i = 0; i < pointers.length; i++) {
144
+ newBounds[i] = bindResult(pointers[i], moreElement.more, moreElement.actions,
145
+ newArray, i, onError)
146
+ }
147
+ prop.bounds = newBounds
148
+ oldBound.forEach(b => b.dispose())
149
+ extendedElement[moreElement.to] = newArray
150
+ } else if(pointers.length > 0) {
151
+ const oldBound = prop.bounds
152
+ if(!oldBound || oldBound.length == 0 ||
153
+ JSON.stringify(oldBound[0].what) != JSON.stringify(pointers[0])) {
154
+ if(oldBound) {
155
+ prop.bounds.forEach(b => b.dispose())
142
156
  }
143
- prop.bounds = newBounds
144
- oldBound.forEach(b => b.dispose())
145
- extendedElement[moreElement.to] = newArray
146
- } else if(pointers.length > 0) {
147
- const oldBound = prop.bounds
148
- if(!oldBound || oldBound.length == 0 ||
149
- JSON.stringify(oldBound[0].what) != JSON.stringify(pointers[0])) {
150
- if(oldBound) {
151
- prop.bounds.forEach(b => b.dispose())
152
- }
153
- if(pointers[0]) {
154
- prop.bounds = [
155
- bindResult(pointers[0], moreElement.more, moreElement.actions,
156
- extendedElement, moreElement.to, onError)
157
- ]
158
- }
157
+ if(pointers[0]) {
158
+ prop.bounds = [
159
+ bindResult(pointers[0], moreElement.more, moreElement.actions,
160
+ extendedElement, moreElement.to, onError)
161
+ ]
159
162
  }
160
163
  }
161
164
  }
162
- bindPointers(computePointers())
163
165
  }
166
+ bindPointers(computePointers())
164
167
  }
165
- if(actions) for(const action of actions) {
166
- if(!action.name) continue
167
- debug("BIND ACTION", action.name, "TO", object)
168
- extendedElement[action.name] = createActionFunction(action, extendedElement)
169
- }
170
- extendedElement[liveSymbol] = props
171
- return extendedElement
172
- },
173
- disposedElement => {
174
- if(!disposedElement) return
175
- const boundProps = disposedElement[liveSymbol]
176
- for(const propName in boundProps) {
177
- const prop = boundProps[propName]
178
- const propBounds = prop.bounds
179
- for(const propBound of propBounds) {
180
- //console.log("PROP BOUND DISPOSE", propBound)
181
- propBound.dispose()
182
- }
183
- const propSources = prop.sources
184
- for(const propSource of propSources) {
185
- debug("PROP SOURCE DISPOSE", propSource)
186
- debug("UNOBSERVE PROPERTY", what, object, property)
187
- propSource.observable.unobserve(propSource.observer)
188
- }
168
+ }
169
+ if(actions) for(const action of actions) {
170
+ if(!action.name) continue
171
+ debug("BIND ACTION", action.name, "TO", object)
172
+ extendedElement[action.name] = createActionFunction(action, extendedElement)
173
+ }
174
+ extendedElement[liveSymbol] = props
175
+ return extendedElement
176
+ },
177
+ disposedElement => {
178
+ if(!disposedElement) return
179
+ const boundProps = disposedElement[liveSymbol]
180
+ for(const propName in boundProps) {
181
+ const prop = boundProps[propName]
182
+ const propBounds = prop.bounds
183
+ for(const propBound of propBounds) {
184
+ //console.log("PROP BOUND DISPOSE", propBound)
185
+ propBound.dispose()
189
186
  }
190
- },
191
- (data) => {
192
- if(data && typeof data == 'object') {
193
- const activated = reactive(data)
194
- return activated
187
+ const propSources = prop.sources
188
+ for(const propSource of propSources) {
189
+ debug("PROP SOURCE DISPOSE", propSource)
190
+ debug("UNOBSERVE PROPERTY", what, object, property)
191
+ propSource.observable.unobserve(propSource.observer)
195
192
  }
196
- return data
197
193
  }
198
- )
199
- extendedObservable.bindProperty(object, property)
200
- observable.observe(errorObserver)
201
- dispose = () => {
202
- debug("UNBIND PROPERTY", what, object, property)
203
- extendedObservable.unbindProperty(object, property)
204
- observable.unobserve(errorObserver)
205
- }
206
- } else {
207
- observable.bindProperty(object, property)
208
- observable.observe(errorObserver)
209
- dispose = () => {
210
- debug("UNBIND PROPERTY", what, object, property)
211
- observable.unbindProperty(object, property)
212
- observable.unobserve(errorObserver)
194
+ },
195
+ (data) => {
196
+ if(data && typeof data == 'object') {
197
+ const activated = reactive(data)
198
+ return activated
199
+ }
200
+ return data
213
201
  }
202
+ )
203
+ extendedObservable.bindProperty(object, property)
204
+ observable.observe(errorObserver)
205
+ dispose = () => {
206
+ debug("UNBIND PROPERTY", what, object, property)
207
+ extendedObservable.unbindProperty(object, property)
208
+ observable.unobserve(errorObserver)
214
209
  }
215
- return {
216
- what, property, dispose
210
+ } else {
211
+ observable.bindProperty(object, property)
212
+ observable.observe(errorObserver)
213
+ dispose = () => {
214
+ debug("UNBIND PROPERTY", what, object, property)
215
+ observable.unbindProperty(object, property)
216
+ observable.unobserve(errorObserver)
217
217
  }
218
218
  }
219
- const resultRef = ref()
220
- await new Promise((resolve, reject) => {
221
- let error = null
222
- const onError = (msg) => {
223
- console.error("LIVE ERROR", msg)
224
- if(error) return
225
- error = msg
226
- reject(error)
227
- }
228
- const bound = bindResult(path.what, path.more, path.actions, resultRef, 'value', onError)
229
- const pathsObserver = (signal, value) => {}
230
- preFetchPaths.observe(pathsObserver)
231
- onUnmountedCb(() => {
232
- preFetchPaths.unobserve(pathsObserver)
233
- bound.dispose()
234
- })
235
- preFetchPaths.wait().then(resolve).catch(onError)
236
- })
237
- while(unref(resultRef) === undefined) { // wait for next tick
238
- await new Promise((resolve) => setTimeout(resolve, 0))
219
+ return {
220
+ what, property, dispose
239
221
  }
240
- return resultRef
241
222
  }
223
+ const resultRef = ref()
224
+ await new Promise((resolve, reject) => {
225
+ let error = null
226
+ const onError = (msg) => {
227
+ console.error("LIVE ERROR", msg)
228
+ if(error) return
229
+ error = msg
230
+ reject(error)
231
+ }
232
+ const bound = bindResult(path.what, path.more, path.actions, resultRef, 'value', onError)
233
+ const pathsObserver = (signal, value) => {}
234
+ preFetchPaths.observe(pathsObserver)
235
+ onUnmountedCb(() => {
236
+ preFetchPaths.unobserve(pathsObserver)
237
+ bound.dispose()
238
+ })
239
+ preFetchPaths.wait().then(resolve).catch(onError)
240
+ })
241
+ while(unref(resultRef) === undefined) { // wait for next tick
242
+ await new Promise((resolve) => setTimeout(resolve, 0))
243
+ }
244
+ return resultRef
245
+
242
246
  }
243
247
 
244
- export default live
248
+ export default live
249
+ export { live, fetch }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@live-change/dao-vue3",
3
- "version": "0.4.11",
3
+ "version": "0.4.12",
4
4
  "author": {
5
5
  "email": "m8@em8.pl",
6
6
  "name": "Michał Łaszczewski",
@@ -10,7 +10,7 @@
10
10
  "url": "https://github.com/live-change/live-change-dao/issues"
11
11
  },
12
12
  "dependencies": {
13
- "@live-change/dao": "^0.4.10"
13
+ "@live-change/dao": "^0.4.12"
14
14
  },
15
15
  "description": "Vue.js integration for live-change dao",
16
16
  "directories": {},
@@ -32,5 +32,5 @@
32
32
  "scripts": {
33
33
  "compileTests": "webpack test/*.js tests-bundle.js"
34
34
  },
35
- "gitHead": "dce5205dd62478b96648556e865959f51f4ca737"
35
+ "gitHead": "49986db53465eb08a50e6090343fc8cbaf230670"
36
36
  }