teamplay 0.3.16 → 0.3.18

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,12 @@ 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, useAsyncSub, setUseDeferredValue as __setUseDeferredValue } from './react/useSub.js'
17
+ export {
18
+ default as useSub,
19
+ useAsyncSub,
20
+ setUseDeferredValue as __setUseDeferredValue,
21
+ setDefaultDefer as __setDefaultDefer
22
+ } from './react/useSub.js'
18
23
  export { default as observer } from './react/observer.js'
19
24
  export { connection, setConnection, getConnection, fetchOnly, setFetchOnly, publicOnly, setPublicOnly } from './orm/connection.js'
20
25
  export { useId, useNow, useScheduleUpdate, useTriggerUpdate } from './react/helpers.js'
package/orm/Signal.js CHANGED
@@ -24,7 +24,7 @@ export const SEGMENTS = Symbol('path segments targeting the particular node in t
24
24
  export const ARRAY_METHOD = Symbol('run array method on the signal')
25
25
  export const GET = Symbol('get the value of the signal - either observed or raw')
26
26
  export const GETTERS = Symbol('get the list of this signal\'s getters')
27
- const DEFAULT_GETTERS = ['path', 'id', 'get', 'peek', 'getId', 'map', 'reduce', 'find', 'getIds']
27
+ const DEFAULT_GETTERS = ['path', 'id', 'get', 'peek', 'getId', 'map', 'reduce', 'find', 'getIds', 'getCollection']
28
28
 
29
29
  export default class Signal extends Function {
30
30
  static [GETTERS] = DEFAULT_GETTERS
@@ -108,6 +108,14 @@ export default class Signal extends Function {
108
108
  return this[SEGMENTS][this[SEGMENTS].length - 1]
109
109
  }
110
110
 
111
+ getCollection () {
112
+ if (this[SEGMENTS].length === 0) throw Error('Can\'t get the collection of the root signal')
113
+ if (this[SEGMENTS][0] === AGGREGATIONS) {
114
+ return getAggregationCollectionName(this[SEGMENTS])
115
+ }
116
+ return this[SEGMENTS][0]
117
+ }
118
+
111
119
  * [Symbol.iterator] () {
112
120
  if (this[IS_QUERY]) {
113
121
  const ids = _get([QUERIES, this[HASH], 'ids'])
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "teamplay",
3
- "version": "0.3.16",
3
+ "version": "0.3.18",
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.16",
27
- "@teamplay/cache": "^0.3.16",
28
- "@teamplay/channel": "^0.3.16",
29
- "@teamplay/debug": "^0.3.16",
30
- "@teamplay/schema": "^0.3.16",
31
- "@teamplay/utils": "^0.3.16",
26
+ "@teamplay/backend": "^0.3.18",
27
+ "@teamplay/cache": "^0.3.18",
28
+ "@teamplay/channel": "^0.3.18",
29
+ "@teamplay/debug": "^0.3.18",
30
+ "@teamplay/schema": "^0.3.18",
31
+ "@teamplay/utils": "^0.3.18",
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": "92ca8b74d176193e90f8a379eeaec31299b807bb"
66
+ "gitHead": "5c3e2b44f536b53adfc94ead762834ab2e907858"
67
67
  }
package/react/helpers.js CHANGED
@@ -46,6 +46,12 @@ export function useScheduleUpdate () {
46
46
  return context.scheduleUpdate
47
47
  }
48
48
 
49
+ export function useDefer () {
50
+ const context = useContext(ComponentMetaContext)
51
+ if (!context) throw Error(ERRORS.useScheduleUpdate)
52
+ return context.defer
53
+ }
54
+
49
55
  export function useCache (key) {
50
56
  const context = useContext(ComponentMetaContext)
51
57
  if (!context) throw Error(ERRORS.useCache)
package/react/useSub.js CHANGED
@@ -1,13 +1,15 @@
1
1
  import { useRef, useDeferredValue } from 'react'
2
2
  import sub from '../orm/sub.js'
3
- import { useScheduleUpdate, useCache } from './helpers.js'
3
+ import { useScheduleUpdate, useCache, useDefer } from './helpers.js'
4
4
  import executionContextTracker from './executionContextTracker.js'
5
5
 
6
6
  let TEST_THROTTLING = false
7
7
 
8
8
  // experimental feature to leverage useDeferredValue() to handle re-subscriptions.
9
9
  // Currently it does lead to issues with extra rerenders and requires further investigation
10
- let USE_DEFERRED_VALUE = false
10
+ let USE_DEFERRED_VALUE = true
11
+ // by default we want to defer stuff if possible instead of throwing promises
12
+ let DEFAULT_DEFER = true
11
13
 
12
14
  export function useAsyncSub (signal, params, options) {
13
15
  return useSub(signal, params, { ...options, async: true })
@@ -22,12 +24,16 @@ export default function useSub (signal, params, options) {
22
24
  }
23
25
 
24
26
  // version of sub() which works as a react hook and throws promise for Suspense
25
- export function useSubDeferred (signal, params, { async = false } = {}) {
27
+ export function useSubDeferred (signal, params, { async = false, defer } = {}) {
26
28
  const $signalRef = useRef() // eslint-disable-line react-hooks/rules-of-hooks
27
29
  const scheduleUpdate = useScheduleUpdate()
28
- signal = useDeferredValue(signal)
29
- params = useDeferredValue(params ? JSON.stringify(params) : undefined)
30
- params = params != null ? JSON.parse(params) : undefined
30
+ const observerDefer = useDefer()
31
+ defer ??= observerDefer ?? DEFAULT_DEFER
32
+ if (defer) {
33
+ signal = useDeferredValue(signal) // eslint-disable-line react-hooks/rules-of-hooks
34
+ params = useDeferredValue(params ? JSON.stringify(params) : undefined) // eslint-disable-line react-hooks/rules-of-hooks
35
+ params = params != null ? JSON.parse(params) : undefined
36
+ }
31
37
  const promiseOrSignal = params != null ? sub(signal, params) : sub(signal)
32
38
  // 1. if it's a promise, throw it so that Suspense can catch it and wait for subscription to finish
33
39
  if (promiseOrSignal.then) {
@@ -97,6 +103,9 @@ export function resetTestThrottling () {
97
103
  export function setUseDeferredValue (value) {
98
104
  USE_DEFERRED_VALUE = value
99
105
  }
106
+ export function setDefaultDefer (value) {
107
+ DEFAULT_DEFER = value
108
+ }
100
109
 
101
110
  // throttle to simulate slow network
102
111
  function maybeThrottle (promise) {
@@ -17,6 +17,7 @@ function destroyAdm (adm) {
17
17
  export default function wrapIntoSuspense ({
18
18
  Component,
19
19
  forwardRef,
20
+ defer,
20
21
  suspenseProps = DEFAULT_SUSPENSE_PROPS
21
22
  } = {}) {
22
23
  if (!suspenseProps?.fallback) throw Error(ERRORS.noFallback)
@@ -62,6 +63,7 @@ export default function wrapIntoSuspense ({
62
63
  componentMetaRef.current = {
63
64
  componentId,
64
65
  createdAt: Date.now(),
66
+ defer,
65
67
  triggerUpdate: () => adm.onStoreChange?.(),
66
68
  scheduleUpdate: promise => adm.scheduleUpdate?.(promise),
67
69
  cache: {