@live-change/dao-vue3 0.5.4 → 0.5.6

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 (2) hide show
  1. package/lib/live.js +27 -3
  2. package/package.json +3 -3
package/lib/live.js CHANGED
@@ -1,4 +1,4 @@
1
- import { ref, onUnmounted, getCurrentInstance, unref, reactive, isRef } from 'vue'
1
+ import { ref, onUnmounted, getCurrentInstance, unref, reactive, isRef, shallowRef, watch, computed } from 'vue'
2
2
  import { collectPointers, ExtendedObservableList } from '@live-change/dao'
3
3
  import nodeDebug from 'debug'
4
4
  const debug = nodeDebug('dao-vue3')
@@ -76,8 +76,32 @@ async function fetch(api, path) {
76
76
 
77
77
  async function live(api, path, onUnmountedCb) {
78
78
  if(isRef(path)) {
79
- /// TODO: support path as ref/computed
80
- throw new Error('reactive paths not implemented')
79
+ if(typeof window == 'undefined') return fetch(api, path.value)
80
+ let liveRef = shallowRef()
81
+ let onUnmountedCallbacks = []
82
+ let oldPath = null
83
+ let updatePromise = null
84
+ async function update() {
85
+ const newPath = path.value
86
+ if(JSON.stringify(newPath) == oldPath) return
87
+ if(!updatePromise) updatePromise = (async () => {
88
+ const newUnmountedCallbacks = []
89
+ let newLive = null
90
+ if(path.value) {
91
+ newLive = await live(api, newPath, (cb) => newUnmountedCallbacks.push(cb))
92
+ }
93
+ for(const callback of onUnmountedCallbacks) callback()
94
+ liveRef.value = newLive
95
+ onUnmountedCallbacks = newUnmountedCallbacks
96
+ oldPath = JSON.stringify(newPath)
97
+ })().then(() => updatePromise = null)
98
+ if(updatePromise) await updatePromise
99
+ await update()
100
+ }
101
+ await update()
102
+ watch(() => path.value, () => update())
103
+ const result = computed(() => liveRef.value?.value)
104
+ return result
81
105
  }
82
106
 
83
107
  if(!onUnmountedCb && typeof window != 'undefined') {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@live-change/dao-vue3",
3
- "version": "0.5.4",
3
+ "version": "0.5.6",
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.5.4"
13
+ "@live-change/dao": "^0.5.6"
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": "dfc8cd2f8de139aeb910a1bf0d3ca5818edcedd4"
35
+ "gitHead": "8a0894ea0f57b6e1c5a627258617b8353e70533d"
36
36
  }