teamplay 0.3.8 → 0.3.10

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/index.js CHANGED
@@ -14,7 +14,7 @@ export { GLOBAL_ROOT_ID } from './orm/Root.js'
14
14
  export const $ = _getRootSignal({ rootId: GLOBAL_ROOT_ID, rootFunction: universal$ })
15
15
  export default $
16
16
  export { default as sub } from './orm/sub.js'
17
- export { default as useSub, setUseDeferredValue as __setUseDeferredValue } from './react/useSub.js'
17
+ export { default as useSub, useAsyncSub, setUseDeferredValue as __setUseDeferredValue } from './react/useSub.js'
18
18
  export { default as observer } from './react/observer.js'
19
19
  export { connection, setConnection, getConnection, fetchOnly, setFetchOnly, publicOnly, setPublicOnly } from './orm/connection.js'
20
20
  export { useId, useNow, useScheduleUpdate, useTriggerUpdate } from './react/helpers.js'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "teamplay",
3
- "version": "0.3.8",
3
+ "version": "0.3.10",
4
4
  "description": "Full-stack signals ORM with multiplayer",
5
5
  "type": "module",
6
6
  "main": "index.js",
@@ -23,12 +23,12 @@
23
23
  },
24
24
  "dependencies": {
25
25
  "@nx-js/observer-util": "^4.1.3",
26
- "@teamplay/backend": "^0.3.8",
27
- "@teamplay/cache": "^0.3.8",
28
- "@teamplay/channel": "^0.3.8",
29
- "@teamplay/debug": "^0.3.8",
30
- "@teamplay/schema": "^0.3.8",
31
- "@teamplay/utils": "^0.3.8",
26
+ "@teamplay/backend": "^0.3.10",
27
+ "@teamplay/cache": "^0.3.10",
28
+ "@teamplay/channel": "^0.3.10",
29
+ "@teamplay/debug": "^0.3.10",
30
+ "@teamplay/schema": "^0.3.10",
31
+ "@teamplay/utils": "^0.3.10",
32
32
  "diff-match-patch": "^1.0.5",
33
33
  "events": "^3.3.0",
34
34
  "json0-ot-diff": "^1.1.2",
@@ -63,5 +63,5 @@
63
63
  ]
64
64
  },
65
65
  "license": "MIT",
66
- "gitHead": "627be4811f622fe0202480b0c6f1bd2fead83e19"
66
+ "gitHead": "c6f36be025ca89afbf56a43d1a0960b30494d772"
67
67
  }
package/react/useSub.js CHANGED
@@ -8,70 +8,79 @@ let TEST_THROTTLING = false
8
8
  // Currently it does lead to issues with extra rerenders and requires further investigation
9
9
  let USE_DEFERRED_VALUE = false
10
10
 
11
- export default function useSub (signal, params) {
11
+ export function useAsyncSub (signal, params, options) {
12
+ return useSub(signal, params, { ...options, async: true })
13
+ }
14
+
15
+ export default function useSub (signal, params, options) {
12
16
  if (USE_DEFERRED_VALUE) {
13
- return useSubDeferred(signal, params) // eslint-disable-line react-hooks/rules-of-hooks
17
+ return useSubDeferred(signal, params, options) // eslint-disable-line react-hooks/rules-of-hooks
14
18
  } else {
15
- return useSubClassic(signal, params) // eslint-disable-line react-hooks/rules-of-hooks
19
+ return useSubClassic(signal, params, options) // eslint-disable-line react-hooks/rules-of-hooks
16
20
  }
17
21
  }
18
22
 
19
23
  // version of sub() which works as a react hook and throws promise for Suspense
20
- export function useSubDeferred (signal, params) {
24
+ export function useSubDeferred (signal, params, { async = false } = {}) {
25
+ const $signalRef = useRef() // eslint-disable-line react-hooks/rules-of-hooks
26
+ const scheduleUpdate = useScheduleUpdate()
21
27
  signal = useDeferredValue(signal)
22
28
  params = useDeferredValue(params ? JSON.stringify(params) : undefined)
23
29
  params = params != null ? JSON.parse(params) : undefined
24
30
  const promiseOrSignal = params != null ? sub(signal, params) : sub(signal)
25
31
  // 1. if it's a promise, throw it so that Suspense can catch it and wait for subscription to finish
26
32
  if (promiseOrSignal.then) {
27
- if (TEST_THROTTLING) {
28
- // simulate slow network
29
- throw new Promise((resolve, reject) => {
30
- setTimeout(() => {
31
- promiseOrSignal.then(resolve, reject)
32
- }, TEST_THROTTLING)
33
- })
33
+ const promise = maybeThrottle(promiseOrSignal)
34
+ if (async) {
35
+ scheduleUpdate(promise)
36
+ return
34
37
  }
35
- throw promiseOrSignal
36
- }
38
+ throw promise
37
39
  // 2. if it's a signal, we save it into ref to make sure it's not garbage collected while component exists
38
- const $signalRef = useRef() // eslint-disable-line react-hooks/rules-of-hooks
39
- if ($signalRef.current !== promiseOrSignal) $signalRef.current = promiseOrSignal
40
- return promiseOrSignal
40
+ } else {
41
+ const $signal = promiseOrSignal
42
+ if ($signalRef.current !== $signal) $signalRef.current = $signal
43
+ return $signal
44
+ }
41
45
  }
42
46
 
43
47
  // classic version which initially throws promise for Suspense
44
48
  // but if we get a promise second time, we return the last signal and wait for promise to resolve
45
- export function useSubClassic (signal, params) {
49
+ export function useSubClassic (signal, params, { async = false } = {}) {
46
50
  const $signalRef = useRef()
47
51
  const activePromiseRef = useRef()
48
52
  const scheduleUpdate = useScheduleUpdate()
49
53
  const promiseOrSignal = params != null ? sub(signal, params) : sub(signal)
50
54
  // 1. if it's a promise, throw it so that Suspense can catch it and wait for subscription to finish
51
55
  if (promiseOrSignal.then) {
52
- let promise
53
- if (TEST_THROTTLING) {
54
- // simulate slow network
55
- promise = new Promise((resolve, reject) => {
56
- setTimeout(() => {
57
- promiseOrSignal.then(resolve, reject)
58
- }, TEST_THROTTLING)
59
- })
60
- } else {
61
- promise = promiseOrSignal
62
- }
56
+ const promise = maybeThrottle(promiseOrSignal)
63
57
  // first time we just throw the promise to be caught by Suspense
64
- if (!$signalRef.current) throw promise
58
+ if (!$signalRef.current) {
59
+ // if we are in async mode, we just return nothing and let the user
60
+ // handle appearance of signal on their own.
61
+ // We manually schedule an update when promise resolves since we can't
62
+ // rely on Suspense in this case to automatically trigger component's re-render
63
+ if (async) {
64
+ scheduleUpdate(promise)
65
+ return
66
+ }
67
+ // in regular mode we throw the promise to be caught by Suspense
68
+ // this way we guarantee that the signal with all the data
69
+ // will always be there when component is rendered
70
+ throw promise
71
+ }
65
72
  // if we already have a previous signal, we return it and wait for new promise to resolve
66
73
  scheduleUpdate(promise)
67
74
  return $signalRef.current
68
- }
69
75
  // 2. if it's a signal, we save it into ref to make sure it's not garbage collected while component exists
70
- if ($signalRef.current !== promiseOrSignal) {
71
- activePromiseRef.current = undefined
72
- $signalRef.current = promiseOrSignal
76
+ } else {
77
+ const $signal = promiseOrSignal
78
+ if ($signalRef.current !== $signal) {
79
+ activePromiseRef.current = undefined
80
+ $signalRef.current = $signal
81
+ }
82
+ return $signal
73
83
  }
74
- return promiseOrSignal
75
84
  }
76
85
 
77
86
  export function setTestThrottling (ms) {
@@ -86,3 +95,13 @@ export function resetTestThrottling () {
86
95
  export function setUseDeferredValue (value) {
87
96
  USE_DEFERRED_VALUE = value
88
97
  }
98
+
99
+ // throttle to simulate slow network
100
+ function maybeThrottle (promise) {
101
+ if (!TEST_THROTTLING) return promise
102
+ return new Promise((resolve, reject) => {
103
+ setTimeout(() => {
104
+ promise.then(resolve, reject)
105
+ }, TEST_THROTTLING)
106
+ })
107
+ }
@@ -19,21 +19,21 @@ export default function wrapIntoSuspense ({
19
19
  stateVersion: Symbol(), // eslint-disable-line symbol-description
20
20
  onStoreChange: undefined,
21
21
  scheduledUpdatePromise: undefined,
22
+ scheduleUpdate: promise => {
23
+ if (!promise?.then) throw Error('scheduleUpdate() expects a promise')
24
+ if (adm.scheduledUpdatePromise === promise) return
25
+ adm.scheduledUpdatePromise = promise
26
+ promise.then(() => {
27
+ if (adm.scheduledUpdatePromise !== promise) return
28
+ adm.scheduledUpdatePromise = undefined
29
+ adm.onStoreChange?.()
30
+ })
31
+ },
22
32
  subscribe (onStoreChange) {
23
33
  adm.onStoreChange = () => {
24
34
  adm.stateVersion = Symbol() // eslint-disable-line symbol-description
25
35
  onStoreChange()
26
36
  }
27
- adm.scheduleUpdate = promise => {
28
- if (!promise?.then) throw Error('scheduleUpdate() expects a promise')
29
- if (adm.scheduledUpdatePromise === promise) return
30
- adm.scheduledUpdatePromise = promise
31
- promise.then(() => {
32
- if (adm.scheduledUpdatePromise !== promise) return
33
- adm.scheduledUpdatePromise = undefined
34
- adm.onStoreChange?.()
35
- })
36
- }
37
37
  return () => {
38
38
  adm.onStoreChange = undefined
39
39
  adm.scheduledUpdatePromise = undefined