@live-change/dao-vue3 0.1.21 → 0.4.0

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.
@@ -19,7 +19,11 @@ class Bucket {
19
19
  this.data = computed(() => {
20
20
  const ldv = this.liveData.value
21
21
  if(!ldv) return []
22
- const source = unref(ldv)
22
+ let source = unref(ldv)
23
+ if(this.range.reverse) {
24
+ source = source.slice()
25
+ source.reverse()
26
+ }
23
27
  if(this.hardClose) {
24
28
  return source
25
29
  }
@@ -46,16 +50,6 @@ class Bucket {
46
50
  this.dataPromise = live(this.api(), this.path, fun => onDispose.push(fun))
47
51
  this.dataPromise.then(data => {
48
52
  this.liveData.value = data
49
- // if(this.hardClose) {
50
- // this.data.value = this.liveData.value
51
- // return
52
- // }
53
- // return this.data.value = computed(() => source.filter(element => (
54
- // (!this.range.gt || element.id > this.range.gt ) &&
55
- // (!this.range.gte || element.id >= this.range.gte) &&
56
- // (!this.range.lt || element.id < this.range.lt ) &&
57
- // (!this.range.lte || element.id <= this.range.lte)
58
- // )))
59
53
  })
60
54
  return this.dataPromise
61
55
  }
@@ -77,14 +71,14 @@ class Bucket {
77
71
  const data = unref(await this.dataPromise)
78
72
  if(!this.data) throw new Error("can't close - bucket not loaded!")
79
73
  if(this.data.length < this.bucketSize) throw new Error("can't close - bucket not full")
80
- this.range.gte = data[0].id
74
+ this.range.gte = data[this.range.reverse ? data.length - 1 : 0].id
81
75
  if(this.hardClose) await this.load()
82
76
  }
83
77
 
84
78
  async closeBottom() {
85
79
  const data = unref(await this.dataPromise)
86
80
  if(data.length < this.bucketSize) throw new Error("can't close - bucket not full")
87
- this.range.lte = data[data.length - 1].id
81
+ this.range.lte = data[this.range.reverse ? 0 : data.length - 1].id
88
82
  if(this.hardClose) await this.load()
89
83
  }
90
84
 
@@ -140,17 +134,20 @@ class RangeBuckets {
140
134
  }
141
135
 
142
136
  async wait() {
143
- console.log("WAIT FOR BUCKETS", this.buckets.length)
137
+ //console.log("WAIT FOR BUCKETS", this.buckets.length)
144
138
  await Promise.all(this.buckets.map(bucket => bucket.promise)).then(loaded => this)
145
139
  }
146
140
 
147
141
  async loadTop() {
142
+ //console.log("LOAD TOP!", this.isTopLoadPossible())
148
143
  if(this.buckets.length == 0) return this.loadFirstBucket()
149
144
  const firstBucket = this.buckets[0]
150
145
  await firstBucket.promise
151
146
  if(!this.isTopLoadPossible()) return
152
- if(firstBucket != this.buckets[0]) return this.buckets[0].promise
153
- let range = { limit: this.bucketSize }
147
+ if(firstBucket != this.buckets[0]) {
148
+ return this.buckets[0].promise
149
+ }
150
+ let range = { limit: this.bucketSize, reverse: true }
154
151
  if(!firstBucket.isTopClosed()) {
155
152
  if(firstBucket.canClose()) {
156
153
  await firstBucket.closeTop()
@@ -175,11 +172,14 @@ class RangeBuckets {
175
172
  }
176
173
 
177
174
  async loadBottom() {
175
+ //console.log("LOAD BOTTOM!", this.isBottomLoadPossible())
178
176
  if(this.buckets.length == 0) return this.loadFirstBucket()
179
177
  const lastBucket = this.buckets[this.buckets.length - 1]
180
178
  await lastBucket.promise
181
179
  if(!this.isBottomLoadPossible()) return
182
- if(lastBucket != this.buckets[this.buckets.length - 1]) return this.buckets[this.buckets.length - 1].promise
180
+ if(lastBucket != this.buckets[this.buckets.length - 1]) {
181
+ return this.buckets[this.buckets.length - 1].promise
182
+ }
183
183
  let range = { limit: this.bucketSize }
184
184
  if(!lastBucket.isBottomClosed()) {
185
185
  if(lastBucket.canClose()) {
@@ -205,14 +205,17 @@ class RangeBuckets {
205
205
 
206
206
  dropTop() {
207
207
  if(this.buckets.length == 0) throw new Error('impossible to drop from empty')
208
+ //console.log("DROP TOP!")
208
209
  const droppedBucket = this.buckets[0]
209
- const height = droppedBucket.domElements.reduce((acc, el) => acc + el?.offsetHeight, 0)
210
+ const height = droppedBucket.domElements.reduce((acc, el) => acc + (el?.offsetHeight || 0), 0)
211
+ //console.log("DOM ELEMENTS", droppedBucket.domElements.map(el => el?.offsetHeight || 0), height)
210
212
  this.buckets.shift()
211
213
  droppedBucket.dispose()
212
214
  return height
213
215
  }
214
216
 
215
217
  dropBottom() {
218
+ //console.log("DROP BOTTOM!")
216
219
  if(this.buckets.length == 0) throw new Error('impossible to drop from empty')
217
220
  const droppedBucket = this.buckets[this.buckets.length - 1]
218
221
  const height = droppedBucket.domElements.reduce((acc, el) => acc + el?.offsetHeight, 0)
package/lib/live.js CHANGED
@@ -1,5 +1,7 @@
1
1
  import { ref, onUnmounted, getCurrentInstance, unref, reactive } from 'vue'
2
2
  import { collectPointers, ExtendedObservableList } from '@live-change/dao'
3
+ import nodeDebug from "debug"
4
+ const debug = nodeDebug('dao-vue3')
3
5
 
4
6
  const liveSymbol = Symbol('live')
5
7
 
@@ -18,24 +20,24 @@ async function live(api, path, onUnmountedCb) {
18
20
  const paths = [ path ]
19
21
  if(typeof window == 'undefined') {
20
22
  const preFetchPaths = await api.get({ paths })
21
- console.log("PRE FETCH DATA", preFetchPaths)
23
+ debug("PRE FETCH DATA", preFetchPaths)
22
24
  const preFetchMap = new Map(preFetchPaths.map((res) => [JSON.stringify(res.what), res] ))
23
25
  function createObject(what, more) {
24
26
  const res = preFetchMap.get(JSON.stringify(what))
25
27
  if(res.error) throw new Error(res.error)
26
28
  const data = res.data
27
- if(more) {
29
+ if(data && more) {
28
30
  if(Array.isArray(data)) {
29
31
  for(let i = 0; i < data.length; i ++) {
30
32
  for(const moreElement of more) {
31
33
  if(moreElement.to) {
32
- console.log("COLLECT POINTERS FROM", data[i], "SC", moreElement.schema)
34
+ debug("COLLECT POINTERS FROM", data[i], "SC", moreElement.schema)
33
35
  const pointers = collectPointers(data[i], moreElement.schema ,
34
36
  (what) => preFetchMap.get(JSON.stringify(what)))
35
- console.log("POINTERS COLLECTED", pointers)
37
+ debug("POINTERS COLLECTED", pointers)
36
38
  const values = pointers.map(pointer => createObject(pointer, moreElement.more))
37
- console.log("VALUES", values)
38
- console.log("MANY", pointers.many)
39
+ debug("VALUES", values)
40
+ debug("MANY", pointers.many)
39
41
  if(pointers.many) {
40
42
  data[i][moreElement.to] = values
41
43
  } else {
@@ -59,7 +61,7 @@ async function live(api, path, onUnmountedCb) {
59
61
  }
60
62
  }
61
63
  }
62
- return data
64
+ return ref(data)
63
65
  }
64
66
  return createObject(path.what, path.more)
65
67
  } else {
@@ -148,7 +150,8 @@ async function live(api, path, onUnmountedCb) {
148
150
  }
149
151
  const propSources = prop.sources
150
152
  for(const propSource of propSources) {
151
- //console.log("PROP SOURCE DISPOSE", propSource)
153
+ debug("PROP SOURCE DISPOSE", propSource)
154
+ debug("UNOBSERVE PROPERTY", what, object, property)
152
155
  propSource.observable.unobserve(propSource.observer)
153
156
  }
154
157
  }
@@ -166,6 +169,7 @@ async function live(api, path, onUnmountedCb) {
166
169
  what,
167
170
  property,
168
171
  dispose() {
172
+ debug("UNBIND PROPERTY", what, object, property)
169
173
  extendedObservable.unbindProperty(object, property)
170
174
  }
171
175
  }
@@ -174,14 +178,20 @@ async function live(api, path, onUnmountedCb) {
174
178
  return {
175
179
  what, property,
176
180
  dispose() {
181
+ debug("UNBIND PROPERTY", what, object, property)
177
182
  observable.unbindProperty(object, property)
178
183
  }
179
184
  }
180
185
  }
181
186
  }
182
187
  const resultRef = ref()
183
- bindResult(path.what, path.more, resultRef, 'value')
184
- /// TODO: unobserve on unmounted
188
+ const bound = bindResult(path.what, path.more, resultRef, 'value')
189
+ const pathsObserver = (signal, value) => {}
190
+ preFetchPaths.observe(pathsObserver)
191
+ onUnmountedCb(() => {
192
+ preFetchPaths.unobserve(pathsObserver)
193
+ bound.dispose()
194
+ })
185
195
  await preFetchPaths.wait()
186
196
  while(unref(resultRef) === undefined) { // wait for next tick
187
197
  await new Promise((resolve) => setTimeout(resolve, 0))
package/package.json CHANGED
@@ -1,21 +1,20 @@
1
1
  {
2
2
  "name": "@live-change/dao-vue3",
3
- "version": "0.1.21",
3
+ "version": "0.4.0",
4
4
  "author": {
5
5
  "email": "m8@em8.pl",
6
6
  "name": "Michał Łaszczewski",
7
7
  "url": "http://www.viamage.com/"
8
8
  },
9
9
  "bugs": {
10
- "url": "https://github.com/live-change/dao-vue3/issues"
10
+ "url": "https://github.com/live-change/live-change-dao/issues"
11
11
  },
12
12
  "dependencies": {
13
- "@live-change/dao": "^0.3.9"
13
+ "@live-change/dao": "^0.4.0"
14
14
  },
15
- "devDependencies": {},
16
15
  "description": "Vue.js integration for live-change dao",
17
16
  "directories": {},
18
- "homepage": "https://github.com/live-change/dao-vue3",
17
+ "homepage": "https://github.com/live-change/live-change-dao",
19
18
  "license": "MIT",
20
19
  "main": "index.js",
21
20
  "module": "index.js",
@@ -25,7 +24,6 @@
25
24
  "name": "Michał Łaszczewski"
26
25
  }
27
26
  ],
28
- "optionalDependencies": {},
29
27
  "readme": "README.md",
30
28
  "repository": {
31
29
  "type": "git",
@@ -33,5 +31,6 @@
33
31
  },
34
32
  "scripts": {
35
33
  "compileTests": "webpack test/*.js tests-bundle.js"
36
- }
34
+ },
35
+ "gitHead": "106431c736de8c92ba07cbba1bb85aac4925a93f"
37
36
  }