@live-change/vue3-ssr 0.8.59 → 0.8.61

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/clientApi.js +9 -1
  2. package/index.js +44 -2
  3. package/package.json +4 -4
package/clientApi.js CHANGED
@@ -37,6 +37,11 @@ function clientApi(settings = {}) {
37
37
  }
38
38
  }
39
39
 
40
+ let timeSynchronization
41
+ if(settings.timeSynchronization) {
42
+ timeSynchronization = new ReactiveDao.TimeSynchronization(settings.timeSynchronization)
43
+ }
44
+
40
45
  const dao = new ReactiveDao(credentials, {
41
46
  remoteUrl: settings.remoteUrl || document.location.protocol + '//' + document.location.host + "/api/sockjs",
42
47
  protocols: {
@@ -64,7 +69,9 @@ function clientApi(settings = {}) {
64
69
  pongInterval: 200
65
70
  })*/
66
71
 
67
- ...(settings && settings.connectionSettings)
72
+ timeSynchronization,
73
+
74
+ ...(settings && settings.connectionSettings),
68
75
  },
69
76
 
70
77
  defaultRoute: {
@@ -79,6 +86,7 @@ function clientApi(settings = {}) {
79
86
  ssr: (typeof window == "undefined") || !!window.__DAO_CACHE__,
80
87
  cache: true,
81
88
  ...settings,
89
+ timeSynchronization: timeSynchronization,
82
90
  createReactiveObject(definition) {
83
91
  //console.log("CREATE REACTIVE OBJECT", definition)
84
92
  return createReactiveObject(definition, reactiveMixin(api), reactivePrefetchMixin(api) )
package/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { getCurrentInstance, onUnmounted } from 'vue'
1
+ import { getCurrentInstance, onUnmounted, ref, computed, unref } from 'vue'
2
2
  import { live as d3live, fetch as d3fetch, RangeBuckets } from '@live-change/dao-vue3'
3
3
  import { InboxReader } from '@live-change/dao'
4
4
 
@@ -87,6 +87,48 @@ function useUid(context) {
87
87
  return useApi(context).uid
88
88
  }
89
89
 
90
+ function useTimeSynchronization(context, onUnmountedCb) {
91
+ const api = useApi(context)
92
+ const timeSynchronization = api.settings.timeSynchronization
93
+ if(!timeSynchronization) throw new Error("Time synchronization not configured")
94
+ if(!onUnmountedCb && typeof window != 'undefined') {
95
+ if(getCurrentInstance()) {
96
+ onUnmountedCb = onUnmounted
97
+ } else {
98
+ onUnmountedCb = () => {
99
+ console.error("live fetch outside component instance - possible memory leak")
100
+ }
101
+ }
102
+ }
103
+ const diffRef = ref(timeSynchronization.timeDiffObservable.getValue())
104
+ const observer = {
105
+ set(value) {
106
+ diffRef.value = value
107
+ }
108
+ }
109
+ timeSynchronization.timeDiffObservable.observe(observer)
110
+ const synchronizedRef = ref(timeSynchronization.synchronizedObservable.getValue())
111
+ const synchronizedObserver = {
112
+ set(value) {
113
+ synchronizedRef.value = value
114
+ }
115
+ }
116
+ timeSynchronization.synchronizedObservable.observe(synchronizedObserver)
117
+ onUnmountedCb(() => {
118
+ timeSynchronization.timeDiffObservable.unobserve(observer)
119
+ timeSynchronization.synchronizedObservable.unobserve(synchronizedObserver)
120
+ })
121
+ return {
122
+ waitForSynchronized: () => timeSynchronization.readyPromise,
123
+ diff: computed(() => diffRef.value),
124
+ synchronized: computed(() => synchronizedRef.value),
125
+ serverToLocal: (ts) => unref(ts) - diffRef.value,
126
+ localToServer: (ts) => unref(ts) + diffRef.value,
127
+ serverToLocalComputed: (ts) => computed(() => unref(ts) - diffRef.value),
128
+ localToServerComputed: (ts) => computed(() => unref(ts) + diffRef.value),
129
+ }
130
+ }
131
+
90
132
  function serviceDefinition(service, context = getCurrentInstance().appContext) {
91
133
  const api = useApi(context)
92
134
  return [...api.metadata.api.value.services].find(x => x.name === service)
@@ -103,7 +145,7 @@ const client = useClient
103
145
  const uid = useUid
104
146
 
105
147
  export {
106
- usePath, useLive, useFetch, useApi, useView, useActions, useUid, useClient,
148
+ usePath, useLive, useFetch, useApi, useView, useActions, useUid, useClient, useTimeSynchronization,
107
149
  path, live, fetch, api, view, actions, uid, client,
108
150
  rangeBuckets, reverseRange,
109
151
  inboxReader,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@live-change/vue3-ssr",
3
- "version": "0.8.59",
3
+ "version": "0.8.61",
4
4
  "description": "Live Change Framework - vue components",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -21,10 +21,10 @@
21
21
  },
22
22
  "homepage": "https://github.com/live-change/live-change-stack",
23
23
  "dependencies": {
24
- "@live-change/dao-vue3": "^0.8.59",
25
- "@live-change/uid": "^0.8.59",
24
+ "@live-change/dao-vue3": "^0.8.61",
25
+ "@live-change/uid": "^0.8.61",
26
26
  "@vueuse/core": "^10.11.0",
27
27
  "debug": "^4.3.4"
28
28
  },
29
- "gitHead": "69e6b3004d32994e5fd64df03bf025e04073df3f"
29
+ "gitHead": "3d88dcb3758467da1440e5d4500af531595aa310"
30
30
  }