@baukit/sync-client 0.18.2 → 0.19.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.
Files changed (45) hide show
  1. package/dist/browser.d.ts.map +1 -1
  2. package/dist/browser.js +9 -7
  3. package/dist/browser.js.map +1 -1
  4. package/dist/conformance-cases.d.ts +4 -0
  5. package/dist/conformance-cases.d.ts.map +1 -0
  6. package/dist/conformance-cases.js +242 -0
  7. package/dist/conformance-cases.js.map +1 -0
  8. package/dist/conformance-kit.d.ts +23 -0
  9. package/dist/conformance-kit.d.ts.map +1 -0
  10. package/dist/conformance-kit.js +85 -0
  11. package/dist/conformance-kit.js.map +1 -0
  12. package/dist/conformance-resync-cases.d.ts +4 -0
  13. package/dist/conformance-resync-cases.d.ts.map +1 -0
  14. package/dist/conformance-resync-cases.js +143 -0
  15. package/dist/conformance-resync-cases.js.map +1 -0
  16. package/dist/conformance-support.d.ts +17 -0
  17. package/dist/conformance-support.d.ts.map +1 -0
  18. package/dist/conformance-support.js +62 -0
  19. package/dist/conformance-support.js.map +1 -0
  20. package/dist/conformance.d.ts.map +1 -1
  21. package/dist/conformance.js +6 -437
  22. package/dist/conformance.js.map +1 -1
  23. package/dist/expo.d.ts.map +1 -1
  24. package/dist/expo.js +3 -2
  25. package/dist/expo.js.map +1 -1
  26. package/dist/push-batch.js +1 -1
  27. package/dist/push-batch.js.map +1 -1
  28. package/dist/timer-handles.d.ts +12 -0
  29. package/dist/timer-handles.d.ts.map +1 -0
  30. package/dist/timer-handles.js +19 -0
  31. package/dist/timer-handles.js.map +1 -0
  32. package/dist/transport.js +1 -1
  33. package/dist/transport.js.map +1 -1
  34. package/package.json +3 -3
  35. package/src/browser.ts +11 -8
  36. package/src/conformance-cases.ts +496 -0
  37. package/src/conformance-kit.ts +159 -0
  38. package/src/conformance-resync-cases.ts +378 -0
  39. package/src/conformance-support.ts +91 -0
  40. package/src/conformance.ts +6 -671
  41. package/src/expo.ts +6 -6
  42. package/src/push-batch.ts +1 -1
  43. package/src/timer-handles.test.ts +33 -0
  44. package/src/timer-handles.ts +31 -0
  45. package/src/transport.ts +1 -1
package/src/expo.ts CHANGED
@@ -2,6 +2,7 @@ import * as Network from 'expo-network';
2
2
  import { AppState, Platform } from 'react-native';
3
3
 
4
4
  import type { SyncSchedulerEnvironment, SyncSchedulerTimer } from './scheduler.js';
5
+ import { createOpaqueIntervalTimers } from './timer-handles.js';
5
6
 
6
7
  export interface ExpoSyncTimers {
7
8
  setInterval(callback: () => void, milliseconds: number): SyncSchedulerTimer;
@@ -18,13 +19,12 @@ interface ExpoNetworkModule {
18
19
  addNetworkStateListener?: (listener: (state: Network.NetworkState) => void) => { remove(): void };
19
20
  }
20
21
 
21
- const defaultTimers: ExpoSyncTimers = {
22
- setInterval: (callback, milliseconds) =>
23
- globalThis.setInterval(callback, milliseconds) as unknown as SyncSchedulerTimer,
24
- clearInterval: (handle) => {
25
- globalThis.clearInterval(handle as unknown as ReturnType<typeof globalThis.setInterval>);
22
+ const defaultTimers: ExpoSyncTimers = createOpaqueIntervalTimers({
23
+ setInterval: (callback, milliseconds) => globalThis.setInterval(callback, milliseconds),
24
+ clearInterval: (handle: ReturnType<typeof globalThis.setInterval>) => {
25
+ globalThis.clearInterval(handle);
26
26
  },
27
- };
27
+ });
28
28
 
29
29
  function isUsableNetwork(state: Network.NetworkState): boolean {
30
30
  return state.isConnected === true && state.isInternetReachable !== false;
package/src/push-batch.ts CHANGED
@@ -96,7 +96,7 @@ export function rankPushBatch<T extends PushCandidate>(
96
96
  });
97
97
 
98
98
  return [...grouped.values()]
99
- .filter(({ change }) => !isHeldBack?.(change))
99
+ .filter(({ change }) => isHeldBack?.(change) !== true)
100
100
  .sort(
101
101
  (left, right) =>
102
102
  rank(left.change.entityType) - rank(right.change.entityType) || left.order - right.order,
@@ -0,0 +1,33 @@
1
+ import { describe, expect, it, vi } from 'vitest';
2
+
3
+ import { createOpaqueIntervalTimers } from './timer-handles.js';
4
+
5
+ describe('createOpaqueIntervalTimers', () => {
6
+ it('returns an object handle and clears the matching host id', () => {
7
+ const host = { setInterval: vi.fn(() => 42), clearInterval: vi.fn() };
8
+ const timers = createOpaqueIntervalTimers(host);
9
+ const callback = vi.fn();
10
+
11
+ const handle = timers.setInterval(callback, 500);
12
+ timers.clearInterval(handle);
13
+
14
+ expect(typeof handle).toBe('object');
15
+ expect(host.setInterval).toHaveBeenCalledWith(callback, 500);
16
+ expect(host.clearInterval).toHaveBeenCalledWith(42);
17
+ });
18
+
19
+ it('keeps distinct handles for repeated host ids and ignores unknown or cleared handles', () => {
20
+ const host = { setInterval: vi.fn(() => 0), clearInterval: vi.fn() };
21
+ const timers = createOpaqueIntervalTimers(host);
22
+
23
+ const first = timers.setInterval(vi.fn(), 1);
24
+ const second = timers.setInterval(vi.fn(), 1);
25
+ timers.clearInterval(first);
26
+ timers.clearInterval(first);
27
+ timers.clearInterval({});
28
+
29
+ expect(first).not.toBe(second);
30
+ expect(host.clearInterval).toHaveBeenCalledTimes(1);
31
+ expect(host.clearInterval).toHaveBeenCalledWith(0);
32
+ });
33
+ });
@@ -0,0 +1,31 @@
1
+ import type { SyncSchedulerTimer } from './scheduler.js';
2
+
3
+ export interface HostIntervalTimers<THandle> {
4
+ setInterval(callback: () => void, milliseconds: number): THandle;
5
+ clearInterval(handle: THandle): void;
6
+ }
7
+
8
+ export interface OpaqueIntervalTimers {
9
+ setInterval(callback: () => void, milliseconds: number): SyncSchedulerTimer;
10
+ clearInterval(handle: SyncSchedulerTimer): void;
11
+ }
12
+
13
+ /** Wraps host interval ids, whatever their runtime type, in opaque object handles. */
14
+ export function createOpaqueIntervalTimers<THandle>(
15
+ host: HostIntervalTimers<THandle>,
16
+ ): OpaqueIntervalTimers {
17
+ const hostHandles = new WeakMap<SyncSchedulerTimer, { readonly id: THandle }>();
18
+ return {
19
+ setInterval(callback, milliseconds) {
20
+ const handle: SyncSchedulerTimer = {};
21
+ hostHandles.set(handle, { id: host.setInterval(callback, milliseconds) });
22
+ return handle;
23
+ },
24
+ clearInterval(handle) {
25
+ const hostHandle = hostHandles.get(handle);
26
+ if (hostHandle === undefined) return;
27
+ hostHandles.delete(handle);
28
+ host.clearInterval(hostHandle.id);
29
+ },
30
+ };
31
+ }
package/src/transport.ts CHANGED
@@ -278,7 +278,7 @@ export class SyncTransport {
278
278
  const forwardAbort = (): void => {
279
279
  controller.abort(callerSignal?.reason);
280
280
  };
281
- if (callerSignal?.aborted) forwardAbort();
281
+ if (callerSignal?.aborted === true) forwardAbort();
282
282
  callerSignal?.addEventListener('abort', forwardAbort, { once: true });
283
283
  let timer: ReturnType<typeof setTimeout> | undefined;
284
284
  const deadline = new Promise<never>((_, reject) => {