teamplay 0.3.6 → 0.3.8

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/connect/test.js CHANGED
@@ -1,24 +1,12 @@
1
1
  // mock of client connection to sharedb to use inside tests.
2
2
  // This just creates a sharedb server with in-memory database
3
3
  // and creates a server connection to it.
4
+ import ShareDbMingo from '@startupjs/sharedb-mingo-memory'
4
5
  import ShareBackend from 'sharedb'
5
- import ShareDbMingo from 'sharedb-mingo-memory'
6
6
  import { connection, setConnection } from '../orm/connection.js'
7
7
 
8
8
  export default function connect () {
9
9
  if (connection) return
10
- patchSharedbMingoAggregations()
11
10
  const backend = new ShareBackend({ db: new ShareDbMingo() })
12
11
  setConnection(backend.connect())
13
12
  }
14
-
15
- let patched
16
- function patchSharedbMingoAggregations () {
17
- if (patched) return
18
- patched = true
19
- const oldCanPollDoc = ShareDbMingo.prototype.canPollDoc
20
- ShareDbMingo.prototype.canPollDoc = function (collection, query) {
21
- if (query.hasOwnProperty('$aggregate')) return false // eslint-disable-line no-prototype-builtins
22
- return oldCanPollDoc.call(this, collection, query)
23
- }
24
- }
package/index.js CHANGED
@@ -17,6 +17,7 @@ export { default as sub } from './orm/sub.js'
17
17
  export { default as useSub, 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
+ export { useId, useNow, useScheduleUpdate, useTriggerUpdate } from './react/helpers.js'
20
21
  export { GUID_PATTERN, hasMany, hasOne, hasManyFlags, belongsTo, pickFormFields } from '@teamplay/schema'
21
22
  export { aggregation, aggregationHeader as __aggregationHeader } from '@teamplay/utils/aggregation'
22
23
  export { accessControl } from '@teamplay/utils/accessControl'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "teamplay",
3
- "version": "0.3.6",
3
+ "version": "0.3.8",
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.6",
27
- "@teamplay/cache": "^0.3.6",
28
- "@teamplay/channel": "^0.3.6",
29
- "@teamplay/debug": "^0.3.6",
30
- "@teamplay/schema": "^0.3.6",
31
- "@teamplay/utils": "^0.3.6",
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",
32
32
  "diff-match-patch": "^1.0.5",
33
33
  "events": "^3.3.0",
34
34
  "json0-ot-diff": "^1.1.2",
@@ -37,19 +37,19 @@
37
37
  },
38
38
  "devDependencies": {
39
39
  "@jest/globals": "^29.7.0",
40
+ "@startupjs/sharedb-mingo-memory": "^4.0.0-1",
40
41
  "@testing-library/react": "^15.0.7",
41
42
  "jest": "^29.7.0",
42
43
  "jest-environment-jsdom": "^29.7.0",
43
44
  "react": "^18.3.1",
44
- "react-dom": "^18.3.1",
45
- "sharedb-mingo-memory": "^3.0.1"
45
+ "react-dom": "^18.3.1"
46
46
  },
47
47
  "peerDependencies": {
48
- "react": "*",
49
- "sharedb-mingo-memory": "*"
48
+ "@startupjs/sharedb-mingo-memory": "*",
49
+ "react": "*"
50
50
  },
51
51
  "peerDependenciesMeta": {
52
- "sharedb-mingo-memory": {
52
+ "@startupjs/sharedb-mingo-memory": {
53
53
  "optional": true
54
54
  }
55
55
  },
@@ -63,5 +63,5 @@
63
63
  ]
64
64
  },
65
65
  "license": "MIT",
66
- "gitHead": "ea4cc604f47a48c67579af40eb1e719091bc2bcb"
66
+ "gitHead": "627be4811f622fe0202480b0c6f1bd2fead83e19"
67
67
  }
package/react/helpers.js CHANGED
@@ -22,6 +22,12 @@ export function pipeComponentMeta (SourceComponent, TargetComponent, suffix = ''
22
22
  return TargetComponent
23
23
  }
24
24
 
25
+ export function useNow () {
26
+ const context = useContext(ComponentMetaContext)
27
+ if (!context) throw Error(ERRORS.useNow)
28
+ return context.createdAt
29
+ }
30
+
25
31
  export function useId () {
26
32
  const context = useContext(ComponentMetaContext)
27
33
  if (!context) throw Error(ERRORS.useId)
@@ -63,5 +69,9 @@ const ERRORS = {
63
69
  useId: `
64
70
  useId() can only be used inside a component wrapped with observer().
65
71
  You have probably forgot to wrap your component with observer().
72
+ `,
73
+ useNow: `
74
+ useNow() can only be used inside a component wrapped with observer().
75
+ You have probably forgot to wrap your component with observer().
66
76
  `
67
77
  }
@@ -1,17 +1,10 @@
1
- export const REGISTRY_FINALIZE_AFTER = 10_000
2
1
  export const REGISTRY_SWEEP_INTERVAL = 10_000
2
+ const PERMANENT = false
3
3
 
4
- // This is a mock implementation of FinalizationRegistry that uses setTimeout to
5
- // schedule the sweep of outdated objects.
6
- // It is used in environments where FinalizationRegistry is not available.
7
- // For now we permanently keep the values in the registry until they are
8
- // manually unregistered since we don't have a way to know when the object is
9
- // no longer needed. In the future we might add the control logic to properly
10
- // invalidate the objects.
11
- export let PERMANENT = true
12
- export function setPermanent (permanent) { PERMANENT = permanent }
13
-
14
- export class TimerBasedFinalizationRegistry {
4
+ // This is a mock implementation of FinalizationRegistry which doesn't actually
5
+ // finalize anything. It's used in environments where FinalizationRegistry is not
6
+ // available and it can not be simulated using WeakRef (e.g. React Native <0.75 or Old Architecture).
7
+ export class PermanentFinalizationRegistry {
15
8
  registrations = new Map()
16
9
  sweepTimeout
17
10
 
@@ -25,7 +18,31 @@ export class TimerBasedFinalizationRegistry {
25
18
  value,
26
19
  registeredAt: Date.now()
27
20
  })
28
- if (!PERMANENT) this.scheduleSweep()
21
+ }
22
+
23
+ unregister (token) {
24
+ this.registrations.delete(token)
25
+ }
26
+ }
27
+
28
+ // This is a mock implementation of FinalizationRegistry which uses WeakRef to
29
+ // track the target objects. It's used in environments where FinalizationRegistry
30
+ // is not available but WeakRef is (e.g. React Native >=0.75 on New Architecture).
31
+ export class WeakRefBasedFinalizationRegistry {
32
+ registrations = new Map()
33
+ sweepTimeout
34
+
35
+ constructor (finalize) {
36
+ this.finalize = finalize
37
+ }
38
+
39
+ // Token is actually required with this impl
40
+ register (target, value, token) {
41
+ this.registrations.set(token, {
42
+ targetRef: new WeakRef(target),
43
+ value
44
+ })
45
+ this.scheduleSweep()
29
46
  }
30
47
 
31
48
  unregister (token) {
@@ -33,34 +50,37 @@ export class TimerBasedFinalizationRegistry {
33
50
  }
34
51
 
35
52
  // Bound so it can be used directly as setTimeout callback.
36
- sweep = (maxAge = REGISTRY_FINALIZE_AFTER) => {
37
- // cancel timeout so we can force sweep anytime
53
+ sweep = () => {
38
54
  clearTimeout(this.sweepTimeout)
39
55
  this.sweepTimeout = undefined
40
56
 
41
- const now = Date.now()
42
57
  this.registrations.forEach((registration, token) => {
43
- if (now - registration.registeredAt >= maxAge) {
44
- this.finalize(registration.value)
45
- this.registrations.delete(token)
46
- }
58
+ if (registration.targetRef.deref() !== undefined) return
59
+ this.finalize(registration.value)
60
+ this.registrations.delete(token)
47
61
  })
48
62
 
49
- if (this.registrations.size > 0) {
50
- this.scheduleSweep()
51
- }
52
- }
53
-
54
- // Bound so it can be exported directly as clearTimers test utility.
55
- finalizeAllImmediately = () => {
56
- this.sweep(0)
63
+ if (this.registrations.size > 0) this.scheduleSweep()
57
64
  }
58
65
 
59
66
  scheduleSweep () {
60
- if (this.sweepTimeout === undefined) {
61
- this.sweepTimeout = setTimeout(this.sweep, REGISTRY_SWEEP_INTERVAL)
62
- }
67
+ if (this.sweepTimeout) return
68
+ this.sweepTimeout = setTimeout(this.sweep, REGISTRY_SWEEP_INTERVAL)
63
69
  }
64
70
  }
65
71
 
66
- export default (typeof FinalizationRegistry !== 'undefined' ? FinalizationRegistry : TimerBasedFinalizationRegistry)
72
+ let ExportedFinalizationRegistry
73
+
74
+ if (typeof FinalizationRegistry !== 'undefined') {
75
+ ExportedFinalizationRegistry = FinalizationRegistry
76
+ } else if (typeof WeakRef !== 'undefined' && !PERMANENT) {
77
+ console.warn('FinalizationRegistry is not available in this environment. ' +
78
+ 'Using a mock implementation: WeakRefBasedFinalizationRegistry')
79
+ ExportedFinalizationRegistry = WeakRefBasedFinalizationRegistry
80
+ } else {
81
+ console.warn('FinalizationRegistry is not available in this environment. ' +
82
+ 'Using a mock implementation: PermanentFinalizationRegistry')
83
+ ExportedFinalizationRegistry = PermanentFinalizationRegistry
84
+ }
85
+
86
+ export default ExportedFinalizationRegistry
@@ -13,4 +13,13 @@ export function destroyMockWeakRef (weakRef) {
13
13
  weakRef.value = undefined
14
14
  }
15
15
 
16
- export default (typeof WeakRef !== 'undefined' ? WeakRef : MockWeakRef)
16
+ let ExportedWeakRef
17
+
18
+ if (typeof WeakRef !== 'undefined') {
19
+ ExportedWeakRef = WeakRef
20
+ } else {
21
+ console.warn('WeakRef is not available in this environment. Using a mock implementation: MockWeakRef')
22
+ ExportedWeakRef = MockWeakRef
23
+ }
24
+
25
+ export default ExportedWeakRef