@syncular/react 0.55.0 → 0.56.0

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.
@@ -14,6 +14,10 @@ export interface SyncClientResource {
14
14
  getSnapshot(): SyncClientResourceSnapshot;
15
15
  /** Re-run a failed initialization attempt. Pending/ready calls are no-ops. */
16
16
  retry(): Promise<void>;
17
+ /**
18
+ * Wait for initialization, then close the client. Every call returns the
19
+ * same promise, so concurrent and repeated calls observe one outcome.
20
+ */
17
21
  dispose(): Promise<void>;
18
22
  }
19
23
  export declare function createSyncClientResource(factory: () => SyncClientLike | Promise<SyncClientLike>): SyncClientResource;
package/dist/resource.js CHANGED
@@ -1,8 +1,7 @@
1
1
  export function createSyncClientResource(factory) {
2
2
  const listeners = new Set();
3
3
  let snapshot = { phase: 'pending' };
4
- let disposed = false;
5
- let closed = false;
4
+ let disposing;
6
5
  let initialized;
7
6
  const notify = () => {
8
7
  for (const listener of listeners)
@@ -10,10 +9,8 @@ export function createSyncClientResource(factory) {
10
9
  };
11
10
  const closeClient = async (client) => {
12
11
  const close = client.close;
13
- if (close !== undefined && !closed) {
14
- closed = true;
12
+ if (close !== undefined)
15
13
  await close.call(client);
16
- }
17
14
  };
18
15
  const initialize = (publishPending) => {
19
16
  if (publishPending) {
@@ -23,14 +20,14 @@ export function createSyncClientResource(factory) {
23
20
  return Promise.resolve()
24
21
  .then(factory)
25
22
  .then(async (client) => {
26
- if (disposed) {
23
+ if (disposing !== undefined) {
27
24
  await closeClient(client);
28
25
  return;
29
26
  }
30
27
  snapshot = { phase: 'ready', client };
31
28
  notify();
32
29
  }, (error) => {
33
- if (disposed)
30
+ if (disposing !== undefined)
34
31
  return;
35
32
  snapshot = {
36
33
  phase: 'error',
@@ -48,20 +45,20 @@ export function createSyncClientResource(factory) {
48
45
  },
49
46
  getSnapshot: () => snapshot,
50
47
  retry() {
51
- if (disposed || snapshot.phase !== 'error')
48
+ if (disposing !== undefined || snapshot.phase !== 'error') {
52
49
  return initialized;
50
+ }
53
51
  initialized = initialize(true);
54
52
  return initialized;
55
53
  },
56
- async dispose() {
57
- if (disposed)
58
- return;
59
- disposed = true;
60
- await initialized;
61
- if (snapshot.phase === 'ready' && !closed) {
62
- await closeClient(snapshot.client);
63
- }
64
- listeners.clear();
54
+ dispose() {
55
+ disposing ??= (async () => {
56
+ await initialized;
57
+ if (snapshot.phase === 'ready')
58
+ await closeClient(snapshot.client);
59
+ listeners.clear();
60
+ })();
61
+ return disposing;
65
62
  },
66
63
  };
67
64
  }
@@ -3,4 +3,4 @@
3
3
  * Vite HMR uses it to replace a worker owner after a Syncular package upgrade
4
4
  * even when the application's generated schema version did not change.
5
5
  */
6
- export declare const SYNCULAR_REACT_RUNTIME_VERSION = "0.55.0";
6
+ export declare const SYNCULAR_REACT_RUNTIME_VERSION = "0.56.0";
@@ -3,4 +3,4 @@
3
3
  * Vite HMR uses it to replace a worker owner after a Syncular package upgrade
4
4
  * even when the application's generated schema version did not change.
5
5
  */
6
- export const SYNCULAR_REACT_RUNTIME_VERSION = '0.55.0';
6
+ export const SYNCULAR_REACT_RUNTIME_VERSION = '0.56.0';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@syncular/react",
3
- "version": "0.55.0",
3
+ "version": "0.56.0",
4
4
  "description": "React hooks for Syncular offline-first sync",
5
5
  "license": "Apache-2.0",
6
6
  "author": "Benjamin Kniffler",
@@ -55,15 +55,15 @@
55
55
  "test": "bun test --preload ./test/setup.ts"
56
56
  },
57
57
  "dependencies": {
58
- "@syncular/client": "0.55.0"
58
+ "@syncular/client": "0.56.0"
59
59
  },
60
60
  "peerDependencies": {
61
61
  "react": ">=18.0.0"
62
62
  },
63
63
  "devDependencies": {
64
64
  "@happy-dom/global-registrator": "^20.0.0",
65
- "@syncular/core": "0.55.0",
66
- "@syncular/server": "0.55.0",
65
+ "@syncular/core": "0.56.0",
66
+ "@syncular/server": "0.56.0",
67
67
  "@testing-library/react": "^16.1.0",
68
68
  "@types/react": "^18.3.0",
69
69
  "@types/react-dom": "^18.3.0",
package/src/resource.ts CHANGED
@@ -11,6 +11,10 @@ export interface SyncClientResource {
11
11
  getSnapshot(): SyncClientResourceSnapshot;
12
12
  /** Re-run a failed initialization attempt. Pending/ready calls are no-ops. */
13
13
  retry(): Promise<void>;
14
+ /**
15
+ * Wait for initialization, then close the client. Every call returns the
16
+ * same promise, so concurrent and repeated calls observe one outcome.
17
+ */
14
18
  dispose(): Promise<void>;
15
19
  }
16
20
 
@@ -19,8 +23,7 @@ export function createSyncClientResource(
19
23
  ): SyncClientResource {
20
24
  const listeners = new Set<() => void>();
21
25
  let snapshot: SyncClientResourceSnapshot = { phase: 'pending' };
22
- let disposed = false;
23
- let closed = false;
26
+ let disposing: Promise<void> | undefined;
24
27
  let initialized: Promise<void>;
25
28
 
26
29
  const notify = (): void => {
@@ -29,10 +32,7 @@ export function createSyncClientResource(
29
32
 
30
33
  const closeClient = async (client: SyncClientLike): Promise<void> => {
31
34
  const close = (client as { close?: () => void | Promise<void> }).close;
32
- if (close !== undefined && !closed) {
33
- closed = true;
34
- await close.call(client);
35
- }
35
+ if (close !== undefined) await close.call(client);
36
36
  };
37
37
 
38
38
  const initialize = (publishPending: boolean): Promise<void> => {
@@ -44,7 +44,7 @@ export function createSyncClientResource(
44
44
  .then(factory)
45
45
  .then(
46
46
  async (client) => {
47
- if (disposed) {
47
+ if (disposing !== undefined) {
48
48
  await closeClient(client);
49
49
  return;
50
50
  }
@@ -52,7 +52,7 @@ export function createSyncClientResource(
52
52
  notify();
53
53
  },
54
54
  (error: unknown) => {
55
- if (disposed) return;
55
+ if (disposing !== undefined) return;
56
56
  snapshot = {
57
57
  phase: 'error',
58
58
  error: error instanceof Error ? error : new Error(String(error)),
@@ -72,18 +72,19 @@ export function createSyncClientResource(
72
72
  },
73
73
  getSnapshot: () => snapshot,
74
74
  retry() {
75
- if (disposed || snapshot.phase !== 'error') return initialized;
75
+ if (disposing !== undefined || snapshot.phase !== 'error') {
76
+ return initialized;
77
+ }
76
78
  initialized = initialize(true);
77
79
  return initialized;
78
80
  },
79
- async dispose() {
80
- if (disposed) return;
81
- disposed = true;
82
- await initialized;
83
- if (snapshot.phase === 'ready' && !closed) {
84
- await closeClient(snapshot.client);
85
- }
86
- listeners.clear();
81
+ dispose() {
82
+ disposing ??= (async () => {
83
+ await initialized;
84
+ if (snapshot.phase === 'ready') await closeClient(snapshot.client);
85
+ listeners.clear();
86
+ })();
87
+ return disposing;
87
88
  },
88
89
  };
89
90
  }
@@ -3,4 +3,4 @@
3
3
  * Vite HMR uses it to replace a worker owner after a Syncular package upgrade
4
4
  * even when the application's generated schema version did not change.
5
5
  */
6
- export const SYNCULAR_REACT_RUNTIME_VERSION = '0.55.0';
6
+ export const SYNCULAR_REACT_RUNTIME_VERSION = '0.56.0';