@slimr/dbsync 0.0.5 → 0.0.7

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 (51) hide show
  1. package/README.md +5 -5
  2. package/cjs/DbSync.d.ts +47 -3
  3. package/cjs/DbSync.js +54 -18
  4. package/cjs/DbSync.js.map +1 -1
  5. package/cjs/adapters/RestAdapter.d.ts +17 -0
  6. package/cjs/adapters/RestAdapter.js +13 -0
  7. package/cjs/adapters/RestAdapter.js.map +1 -1
  8. package/cjs/adapters/types.d.ts +13 -0
  9. package/cjs/internal/AuthManager.d.ts +17 -2
  10. package/cjs/internal/AuthManager.js +19 -4
  11. package/cjs/internal/AuthManager.js.map +1 -1
  12. package/cjs/internal/EventBus.d.ts +12 -0
  13. package/cjs/internal/EventBus.js +12 -0
  14. package/cjs/internal/EventBus.js.map +1 -1
  15. package/cjs/internal/StorageManager.d.ts +21 -0
  16. package/cjs/internal/StorageManager.js +21 -0
  17. package/cjs/internal/StorageManager.js.map +1 -1
  18. package/cjs/internal/SyncEngine.d.ts +29 -3
  19. package/cjs/internal/SyncEngine.js +31 -5
  20. package/cjs/internal/SyncEngine.js.map +1 -1
  21. package/cjs/useDbQuery.js +2 -0
  22. package/cjs/useDbQuery.js.map +1 -1
  23. package/cjs/util/promiseWithResolvers.js +3 -0
  24. package/cjs/util/promiseWithResolvers.js.map +1 -1
  25. package/docs/Adapters.md +58 -0
  26. package/docs/LocalAdapter.md +7 -0
  27. package/docs/RestAdapter.md +69 -0
  28. package/esm/DbSync.d.ts +47 -3
  29. package/esm/DbSync.js +54 -18
  30. package/esm/DbSync.js.map +1 -1
  31. package/esm/adapters/RestAdapter.d.ts +17 -0
  32. package/esm/adapters/RestAdapter.js +13 -0
  33. package/esm/adapters/RestAdapter.js.map +1 -1
  34. package/esm/adapters/types.d.ts +13 -0
  35. package/esm/internal/AuthManager.d.ts +17 -2
  36. package/esm/internal/AuthManager.js +19 -4
  37. package/esm/internal/AuthManager.js.map +1 -1
  38. package/esm/internal/EventBus.d.ts +12 -0
  39. package/esm/internal/EventBus.js +12 -0
  40. package/esm/internal/EventBus.js.map +1 -1
  41. package/esm/internal/StorageManager.d.ts +21 -0
  42. package/esm/internal/StorageManager.js +21 -0
  43. package/esm/internal/StorageManager.js.map +1 -1
  44. package/esm/internal/SyncEngine.d.ts +29 -3
  45. package/esm/internal/SyncEngine.js +31 -5
  46. package/esm/internal/SyncEngine.js.map +1 -1
  47. package/esm/useDbQuery.js +2 -0
  48. package/esm/useDbQuery.js.map +1 -1
  49. package/esm/util/promiseWithResolvers.js +3 -0
  50. package/esm/util/promiseWithResolvers.js.map +1 -1
  51. package/package.json +2 -1
package/README.md CHANGED
@@ -23,7 +23,7 @@ npm install @slimr/dbsync
23
23
  ```
24
24
 
25
25
  ## Quick Start
26
- You instantiate `dbsync` by supplying your backend [adapter](./src/adapters/Adapters.md) and your table/index definitions. Afterwards, you must call `.init()` to safely generate the database.
26
+ You instantiate `dbsync` by supplying your backend [adapter](./docs/Adapters.md) and your table/index definitions. Afterwards, you must call `.init()` to safely generate the database.
27
27
 
28
28
  ```typescript
29
29
  import { DbSync } from '@slimr/dbsync';
@@ -39,8 +39,8 @@ const db = new DbSync({
39
39
  }
40
40
  });
41
41
 
42
- // Await init before usage
43
- await db.init();
42
+ // Init the db and boot up the background sync engine!
43
+ await db.start();
44
44
  ```
45
45
 
46
46
  > **Local-Only No-Sync Database:** If you want all the IndexedDB ORM and reactive features, but completely lack a remote database to sync to, simply import `LocalAdapter` from `@slimr/dbsync/adapters` and pass that into the constructor instead. It acts as an empty black box so your operations succeed cleanly entirely on the client.
@@ -164,10 +164,10 @@ To boot up the network synchronizer to clear those queues and fetch server updat
164
164
 
165
165
  ```typescript
166
166
  // Start polling the swift-crud backend every 5 seconds
167
- db.enable();
167
+ await db.start();
168
168
 
169
169
  // Stop polling
170
- db.disable();
170
+ await db.stop();
171
171
  ```
172
172
 
173
173
  Only one tab on a user's device will act as the "Leader" and poll the network at any time using **Web Locks**. All other tabs remain completely idle and rely on `BroadcastChannel` messages from the Leader tab to reactively update their UI when fresh data drops.
package/cjs/DbSync.d.ts CHANGED
@@ -2,50 +2,94 @@ import type { BackendAdapter } from "./adapters/types.js";
2
2
  import { type SyncState } from "./internal/EventBus.js";
3
3
  import { StorageManager } from "./internal/StorageManager.js";
4
4
  export interface DbSyncConfig {
5
+ /** The backend adapter used for authentication and synchronization. */
5
6
  adapter: BackendAdapter;
7
+ /** Optional fixed IndexedDB version. */
6
8
  version?: number;
9
+ /** The object stores and their index definitions. */
7
10
  tables: Record<string, {
8
11
  indexes?: string[];
9
12
  }>;
10
13
  }
11
14
  export type { SyncState };
15
+ /**
16
+ * The public facade for the dbsync engine.
17
+ */
12
18
  export declare class DbSync {
19
+ /** The polling interval used by the sync engine in milliseconds. */
13
20
  syncInterval: number;
21
+ /** The active configuration passed by the consumer. */
14
22
  config: DbSyncConfig;
23
+ /** The event bus used for local and cross-tab notifications. */
15
24
  private events;
25
+ /** The storage manager handling IndexedDB state and transactions. */
16
26
  storage: StorageManager;
27
+ /** The engine responsible for network synchronization. */
17
28
  private syncEngine;
29
+ /** The manager handling authentication and logout/reset behavior. */
18
30
  private authManager;
31
+ /**
32
+ * Creates a new `DbSync` instance using the supplied configuration.
33
+ *
34
+ * @param config The backend adapter, schema version, and table definitions.
35
+ */
19
36
  constructor(config: DbSyncConfig);
37
+ /** Returns a fresh RFC-4122 UUID. */
20
38
  genUuid(): `${string}-${string}-${string}-${string}-${string}`;
39
+ /** Whether the storage layer has finished initializing. */
21
40
  get initted(): boolean;
41
+ /** Initializes the underlying IndexedDB stores. */
22
42
  init(): Promise<void>;
43
+ /** Returns a queued transaction object for batched writes. */
23
44
  getTransaction(): import("./internal/DbTransaction.js").DbTransaction;
45
+ /** Reads a typed record by primary key. */
24
46
  get<T>(storeName: string, id: string | number): Promise<T | undefined>;
47
+ /** Reads all records from the given object store. */
25
48
  findAll<T>(storeName: string): Promise<T[]>;
49
+ /** Inserts a new record into the given object store. */
26
50
  add<T>(storeName: string, value: any, key?: string | number): Promise<T>;
51
+ /** Upserts a record into the given object store. */
27
52
  put<T>(storeName: string, value: any, key?: string | number): Promise<T>;
53
+ /** Deletes a record from the given object store. */
28
54
  delete(storeName: string, key: string | number): Promise<void>;
55
+ /** Clears all records from the given object store. */
29
56
  clear(storeName: string): Promise<void>;
57
+ /** Subscribes to store update notifications. */
30
58
  subscribe(callback: (stores: string[]) => void): {
31
59
  close: () => boolean;
32
60
  };
61
+ /** Subscribes to sync state changes. */
33
62
  onSyncStateChange(callback: (state: SyncState) => void): {
34
63
  close: () => boolean;
35
64
  };
65
+ /** Whether the current backend session is authenticated. */
36
66
  get isAuth(): boolean;
67
+ /** Verifies authentication against the backend adapter. */
37
68
  checkAuth(): Promise<boolean>;
69
+ /** Logs in with the given credentials. */
38
70
  login(email: string, code: string): Promise<void>;
71
+ /** Logs out and clears auth state. */
39
72
  logout(): Promise<void>;
73
+ /** Logs out and clears the local database. */
40
74
  reset(): Promise<void>;
41
- get isEnabled(): boolean;
75
+ /** Whether background sync is currently enabled. */
76
+ get isStarted(): boolean;
77
+ /** Whether the sync engine has recently connected successfully. */
42
78
  get isLive(): boolean;
43
- enable(): void;
44
- disable(): void;
79
+ /** Starts periodic background synchronization. */
80
+ start(): Promise<void>;
81
+ /** Stops periodic background synchronization. */
82
+ stop(): Promise<void>;
83
+ /** Waits until the sync engine reports a live connection. */
45
84
  waitForLive(): Promise<void>;
85
+ /** Triggers a single sync cycle immediately. */
46
86
  triggerSync(): Promise<void>;
87
+ /** Legacy alias for `enable()`. */
47
88
  startSyncInterval(): void;
89
+ /** Legacy alias for `disable()`. */
48
90
  stopSyncInterval(): void;
91
+ /** Responds to schema changes by disposing state and reloading the page. */
49
92
  protected onSchemaChangeDetected(): void;
93
+ /** Disposes all background resources and event listeners. */
50
94
  dispose(): void;
51
95
  }
package/cjs/DbSync.js CHANGED
@@ -6,115 +6,151 @@ const EventBus_js_1 = require("./internal/EventBus.js");
6
6
  const StorageManager_js_1 = require("./internal/StorageManager.js");
7
7
  const SyncEngine_js_1 = require("./internal/SyncEngine.js");
8
8
  const utils_js_1 = require("./internal/utils.js");
9
+ /**
10
+ * The public facade for the dbsync engine.
11
+ */
9
12
  class DbSync {
13
+ /** The polling interval used by the sync engine in milliseconds. */
10
14
  syncInterval = 5000;
15
+ /** The active configuration passed by the consumer. */
11
16
  config;
12
- // Internal managers
17
+ /** The event bus used for local and cross-tab notifications. */
13
18
  events;
19
+ /** The storage manager handling IndexedDB state and transactions. */
14
20
  storage; // Public for tests currently (we can wrap it later)
21
+ /** The engine responsible for network synchronization. */
15
22
  syncEngine;
23
+ /** The manager handling authentication and logout/reset behavior. */
16
24
  authManager;
25
+ /**
26
+ * Creates a new `DbSync` instance using the supplied configuration.
27
+ *
28
+ * @param config The backend adapter, schema version, and table definitions.
29
+ */
17
30
  constructor(config) {
18
31
  this.config = config;
19
32
  this.events = new EventBus_js_1.EventBus();
20
33
  const adapter = config.adapter;
21
34
  const onSchemaChange = () => this.onSchemaChangeDetected();
22
35
  this.storage = new StorageManager_js_1.StorageManager(config, this.events, onSchemaChange);
23
- this.authManager = new AuthManager_js_1.AuthManager(adapter, this.storage, () => this.disable());
36
+ this.authManager = new AuthManager_js_1.AuthManager(adapter, this.storage, () => { void this.stop(); });
24
37
  this.syncEngine = new SyncEngine_js_1.SyncEngine(config, this.syncInterval, this.events, this.storage, this.authManager, adapter, onSchemaChange);
25
38
  }
26
- // Facade to Utils
39
+ /** Returns a fresh RFC-4122 UUID. */
27
40
  genUuid() {
28
41
  return (0, utils_js_1.genUuid)();
29
42
  }
30
- // Facade to StorageManager
43
+ /** Whether the storage layer has finished initializing. */
31
44
  get initted() {
32
45
  return this.storage.initted;
33
46
  }
47
+ /** Initializes the underlying IndexedDB stores. */
34
48
  async init() {
35
49
  return this.storage.init();
36
50
  }
51
+ /** Returns a queued transaction object for batched writes. */
37
52
  getTransaction() {
38
53
  return this.storage.getTransaction();
39
54
  } // Exported temporarily for tests
55
+ /** Reads a typed record by primary key. */
40
56
  async get(storeName, id) {
41
57
  return this.storage.get(storeName, id);
42
58
  }
59
+ /** Reads all records from the given object store. */
43
60
  async findAll(storeName) {
44
61
  return this.storage.findAll(storeName);
45
62
  }
63
+ /** Inserts a new record into the given object store. */
46
64
  async add(storeName, value, key) {
47
65
  await this.storage.executeTransaction([{ type: "add", storeName, value, key }]);
48
66
  return value;
49
67
  }
68
+ /** Upserts a record into the given object store. */
50
69
  async put(storeName, value, key) {
51
70
  await this.storage.executeTransaction([{ type: "put", storeName, value, key }]);
52
71
  return value;
53
72
  }
73
+ /** Deletes a record from the given object store. */
54
74
  async delete(storeName, key) {
55
75
  await this.storage.executeTransaction([{ type: "delete", storeName, key }]);
56
76
  }
77
+ /** Clears all records from the given object store. */
57
78
  async clear(storeName) {
58
79
  await this.storage.executeTransaction([{ type: "clear", storeName }]);
59
80
  }
60
- // Facade to EventBus
81
+ /** Subscribes to store update notifications. */
61
82
  subscribe(callback) {
62
83
  return this.events.subscribe(callback);
63
84
  }
85
+ /** Subscribes to sync state changes. */
64
86
  onSyncStateChange(callback) {
65
87
  return this.events.onSyncStateChange(callback);
66
88
  }
67
- // Facade to AuthManager
89
+ /** Whether the current backend session is authenticated. */
68
90
  get isAuth() {
69
91
  return this.authManager.isAuth;
70
92
  }
93
+ /** Verifies authentication against the backend adapter. */
71
94
  async checkAuth() {
72
95
  return this.authManager.checkAuth();
73
96
  }
97
+ /** Logs in with the given credentials. */
74
98
  async login(email, code) {
75
99
  return this.authManager.login(email, code);
76
100
  }
101
+ /** Logs out and clears auth state. */
77
102
  async logout() {
78
103
  return this.authManager.logout();
79
104
  }
105
+ /** Logs out and clears the local database. */
80
106
  async reset() {
81
107
  return this.authManager.reset();
82
108
  }
83
- // Facade to SyncEngine
84
- get isEnabled() {
85
- return this.syncEngine.isEnabled;
109
+ /** Whether background sync is currently enabled. */
110
+ get isStarted() {
111
+ return this.syncEngine.isStarted;
86
112
  }
113
+ /** Whether the sync engine has recently connected successfully. */
87
114
  get isLive() {
88
115
  return this.syncEngine.isLive;
89
116
  }
90
- enable() {
91
- this.syncEngine.enable();
117
+ /** Starts periodic background synchronization. */
118
+ async start() {
119
+ if (!this.initted)
120
+ await this.init();
121
+ this.syncEngine.start();
92
122
  }
93
- disable() {
94
- this.syncEngine.disable();
123
+ /** Stops periodic background synchronization. */
124
+ async stop() {
125
+ this.syncEngine.stop();
95
126
  }
127
+ /** Waits until the sync engine reports a live connection. */
96
128
  async waitForLive() {
97
129
  return this.syncEngine.waitForLive();
98
130
  }
131
+ /** Triggers a single sync cycle immediately. */
99
132
  async triggerSync() {
100
133
  return this.syncEngine.triggerSync();
101
134
  }
102
- // Legacy aliases
135
+ /** Legacy alias for `enable()`. */
103
136
  startSyncInterval() {
104
- this.enable();
137
+ this.start();
105
138
  }
139
+ /** Legacy alias for `disable()`. */
106
140
  stopSyncInterval() {
107
- this.disable();
141
+ this.stop();
108
142
  }
143
+ /** Responds to schema changes by disposing state and reloading the page. */
109
144
  onSchemaChangeDetected() {
110
145
  this.storage.dispose();
111
- this.disable();
146
+ this.stop();
112
147
  if (typeof window !== "undefined") {
113
148
  window.location.reload();
114
149
  }
115
150
  }
151
+ /** Disposes all background resources and event listeners. */
116
152
  dispose() {
117
- this.disable();
153
+ this.stop();
118
154
  this.events.dispose();
119
155
  this.storage.dispose();
120
156
  }
package/cjs/DbSync.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"DbSync.js","sourceRoot":"","sources":["../src/DbSync.ts"],"names":[],"mappings":";;;AACA,8DAAuD;AACvD,wDAAiE;AACjE,oEAA6D;AAC7D,4DAAqD;AACrD,kDAA6C;AAW7C,MAAa,MAAM;IACX,YAAY,GAAG,IAAI,CAAA;IACnB,MAAM,CAAc;IAE3B,oBAAoB;IACZ,MAAM,CAAU;IACjB,OAAO,CAAgB,CAAC,oDAAoD;IAC3E,UAAU,CAAY;IACtB,WAAW,CAAa;IAEhC,YAA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  import type { BackendAdapter, SyncPullResult } from "./types.js";
2
+ /**
3
+ * Configuration for the built-in REST adapter.
4
+ */
2
5
  export interface RestAdapterConfig {
6
+ /** The base URL of the remote API. */
3
7
  url: string;
4
8
  }
9
+ /**
10
+ * A REST-backed implementation of the backend adapter contract.
11
+ */
5
12
  export declare class RestAdapter implements BackendAdapter {
6
13
  private config;
14
+ /**
15
+ * Stores the adapter configuration, including the API base URL.
16
+ *
17
+ * @param config The REST adapter configuration.
18
+ */
7
19
  constructor(config: RestAdapterConfig);
20
+ /** Checks whether the server session cookie is currently authenticated. */
8
21
  checkAuth(): Promise<boolean>;
22
+ /** Logs the user in using an email/code pair. */
9
23
  login(email: string, code: string): Promise<boolean>;
24
+ /** Logs the current user out of the remote session. */
10
25
  logout(): Promise<void>;
26
+ /** Pulls remote records from the REST backend using the provided cursor. */
11
27
  pull(cursor: string): Promise<SyncPullResult>;
28
+ /** Pushes queued local mutations to the REST backend. */
12
29
  push(payload: any[]): Promise<void>;
13
30
  }
@@ -1,15 +1,25 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.RestAdapter = void 0;
4
+ /**
5
+ * A REST-backed implementation of the backend adapter contract.
6
+ */
4
7
  class RestAdapter {
5
8
  config;
9
+ /**
10
+ * Stores the adapter configuration, including the API base URL.
11
+ *
12
+ * @param config The REST adapter configuration.
13
+ */
6
14
  constructor(config) {
7
15
  this.config = config;
8
16
  }
17
+ /** Checks whether the server session cookie is currently authenticated. */
9
18
  async checkAuth() {
10
19
  const res = await fetch(`${this.config.url}/api/session`, { credentials: "include" });
11
20
  return res.ok;
12
21
  }
22
+ /** Logs the user in using an email/code pair. */
13
23
  async login(email, code) {
14
24
  const res = await fetch(`${this.config.url}/api/session/login`, {
15
25
  method: "POST",
@@ -21,9 +31,11 @@ class RestAdapter {
21
31
  throw new Error("Login failed");
22
32
  return true;
23
33
  }
34
+ /** Logs the current user out of the remote session. */
24
35
  async logout() {
25
36
  await fetch(`${this.config.url}/api/session/logout`, { method: "POST", credentials: "include" });
26
37
  }
38
+ /** Pulls remote records from the REST backend using the provided cursor. */
27
39
  async pull(cursor) {
28
40
  const res = await fetch(`${this.config.url}/api/posts?after=${cursor}&limit=40`, {
29
41
  credentials: "include",
@@ -32,6 +44,7 @@ class RestAdapter {
32
44
  throw { status: res.status };
33
45
  return await res.json();
34
46
  }
47
+ /** Pushes queued local mutations to the REST backend. */
35
48
  async push(payload) {
36
49
  const res = await fetch(`${this.config.url}/api/posts/upsert-many`, {
37
50
  method: "POST",
@@ -1 +1 @@
1
- {"version":3,"file":"RestAdapter.js","sourceRoot":"","sources":["../../src/adapters/RestAdapter.ts"],"names":[],"mappings":";;;AAMA,MAAa,WAAW;IACH;IAApB,YAAoB,MAAyB;QAAzB,WAAM,GAAN,MAAM,CAAmB;IAAG,CAAC;IAE1C,KAAK,CAAC,SAAS;QACrB,MAAM,GAAG,GAAG,MAAM,KAAK,CAAC,GAAG,IAAI,CAAC,MAAM,CAAC,GAAG,cAAc,EAAE,EAAE,WAAW,EAAE,SAAS,EAAE,CAAC,CAAA;QACrF,OAAO,GAAG,CAAC,EAAE,CAAA;IACd,CAAC;IAEM,KAAK,CAAC,KAAK,CAAC,KAAa,EAAE,IAAY;QAC7C,MAAM,GAAG,GAAG,MAAM,KAAK,CAAC,GAAG,IAAI,CAAC,MAAM,CAAC,GAAG,oBAAoB,EAAE;YAC/D,MAAM,EAAE,MAAM;YACd,OAAO,EAAE,EAAE,cAAc,EAAE,kBAAkB,EAAE;YAC/C,WAAW,EAAE,SAAS;YACtB,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC;SACrC,CAAC,CAAA;QACF,IAAI,CAAC,GAAG,CAAC,EAAE;YAAE,MAAM,IAAI,KAAK,CAAC,cAAc,CAAC,CAAA;QAC5C,OAAO,IAAI,CAAA;IACZ,CAAC;IAEM,KAAK,CAAC,MAAM;QAClB,MAAM,KAAK,C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G,wBAAwB,EAAE;YACnE,MAAM,EAAE,MAAM;YACd,OAAO,EAAE,EAAE,cAAc,EAAE,kBAAkB,EAAE;YAC/C,WAAW,EAAE,SAAS;YACtB,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC;SAC7B,CAAC,CAAA;QACF,IAAI,CAAC,GAAG,CAAC,EAAE;YAAE,MAAM,EAAE,MAAM,EAAE,GAAG,CAAC,MAAM,EAAE,CAAA;IAC1C,CAAC;CACD;AAlDD,kCAkDC"}
@@ -1,11 +1,24 @@
1
+ /**
2
+ * The standard response shape returned by a backend pull request.
3
+ */
1
4
  export interface SyncPullResult {
5
+ /** The records fetched from the remote backend. */
2
6
  items: any[];
7
+ /** Whether more results remain to be fetched for the current cursor. */
3
8
  hasMore: boolean;
4
9
  }
10
+ /**
11
+ * The contract implemented by all sync backends.
12
+ */
5
13
  export interface BackendAdapter {
14
+ /** Checks whether the current session is authenticated. */
6
15
  checkAuth(): Promise<boolean>;
16
+ /** Logs the current user in against the backend. */
7
17
  login(email: string, code: string): Promise<boolean>;
18
+ /** Logs the current user out of the backend. */
8
19
  logout(): Promise<void>;
20
+ /** Pulls remote changes since the provided cursor. */
9
21
  pull(cursor: string): Promise<SyncPullResult>;
22
+ /** Pushes queued local mutations to the backend. */
10
23
  push(payload: any[]): Promise<void>;
11
24
  }
@@ -1,13 +1,28 @@
1
1
  import type { BackendAdapter } from "../adapters/types.js";
2
2
  import type { StorageManager } from "./StorageManager.js";
3
+ /**
4
+ * Coordinates authentication state and logout/reset behavior for `DbSync`.
5
+ */
3
6
  export declare class AuthManager {
4
7
  private adapter;
5
8
  private storage;
6
- private disableSync;
9
+ private stopSync;
10
+ /** Tracks whether the current backend session is authenticated. */
7
11
  isAuth: boolean;
8
- constructor(adapter: BackendAdapter, storage: StorageManager, disableSync: () => void);
12
+ /**
13
+ * Creates a new auth manager for the provided backend adapter and storage layer.
14
+ *
15
+ * @param adapter The backend adapter used for auth requests.
16
+ * @param storage The storage manager used for reset operations.
17
+ * @param stopSync The callback used to stop background sync.
18
+ */
19
+ constructor(adapter: BackendAdapter, storage: StorageManager, stopSync: () => Promise<void> | void);
20
+ /** Checks authentication against the backend adapter and stores the result. */
9
21
  checkAuth(): Promise<boolean>;
22
+ /** Logs in through the backend adapter and updates auth state. */
10
23
  login(email: string, code: string): Promise<void>;
24
+ /** Logs out, stops sync, and clears auth state. */
11
25
  logout(): Promise<void>;
26
+ /** Logs out and clears all local IndexedDB stores and auth markers. */
12
27
  reset(): Promise<void>;
13
28
  }
@@ -1,28 +1,43 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.AuthManager = void 0;
4
+ /**
5
+ * Coordinates authentication state and logout/reset behavior for `DbSync`.
6
+ */
4
7
  class AuthManager {
5
8
  adapter;
6
9
  storage;
7
- disableSync;
10
+ stopSync;
11
+ /** Tracks whether the current backend session is authenticated. */
8
12
  isAuth = false;
9
- constructor(adapter, storage, disableSync) {
13
+ /**
14
+ * Creates a new auth manager for the provided backend adapter and storage layer.
15
+ *
16
+ * @param adapter The backend adapter used for auth requests.
17
+ * @param storage The storage manager used for reset operations.
18
+ * @param stopSync The callback used to stop background sync.
19
+ */
20
+ constructor(adapter, storage, stopSync) {
10
21
  this.adapter = adapter;
11
22
  this.storage = storage;
12
- this.disableSync = disableSync;
23
+ this.stopSync = stopSync;
13
24
  }
25
+ /** Checks authentication against the backend adapter and stores the result. */
14
26
  async checkAuth() {
15
27
  this.isAuth = await this.adapter.checkAuth();
16
28
  return this.isAuth;
17
29
  }
30
+ /** Logs in through the backend adapter and updates auth state. */
18
31
  async login(email, code) {
19
32
  this.isAuth = await this.adapter.login(email, code);
20
33
  }
34
+ /** Logs out, stops sync, and clears auth state. */
21
35
  async logout() {
22
- this.disableSync();
36
+ await this.stopSync();
23
37
  this.isAuth = false;
24
38
  await this.adapter.logout();
25
39
  }
40
+ /** Logs out and clears all local IndexedDB stores and auth markers. */
26
41
  async reset() {
27
42
  await this.logout();
28
43
  await this.storage.clearAllStores();
@@ -1 +1 @@
1
- {"version":3,"file":"AuthManager.js","sourceRoot":"","sources":["../../src/internal/AuthManager.ts"],"names":[],"mappings":";;;AAGA,MAAa,WAAW;IAId;IACA;IACA;IALF,MAAM,GAAG,KAAK,CAAA;IAErB,YACS,OAAuB,EACvB,OAAuB,EACvB,WAAuB;QAFvB,YAAO,GAAP,OAAO,CAAgB;QACvB,YAAO,GAAP,OAAO,CAAgB;QACvB,gBAAW,GAAX,WAAW,CAAY;IAC7B,CAAC;IAEG,KAAK,CAAC,SAAS;QACrB,IAAI,CAAC,MAAM,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,SAAS,EAAE,CAAA;QAC5C,OAAO,IAAI,CAAC,MAAM,CAAA;IACnB,CAAC;IAEM,KAAK,CAAC,KAAK,CAAC,KAAa,EAAE,IAAY;QAC7C,IAAI,CAAC,MAAM,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,KAAK,EAAE,IAAI,CAAC,CAAA;IACpD,CAAC;IAEM,KAAK,CAAC,MAAM;QAClB,IAAI,CAAC,WAAW,EAAE,CAAA;QAClB,IAAI,CAAC,MAAM,GAAG,KAAK,CAAA;QACnB,MAAM,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,CAAA;IAC5B,CAAC;IAEM,KAAK,CAAC,KAAK;QACjB,MAAM,IAAI,CAAC,MAAM,EAAE,CAAA;QACnB,MAAM,IAAI,CAAC,OAAO,CAAC,cAAc,EAAE,CAAA;QACnC,YAAY,CAAC,UAAU,CAAC,sBAAsB,CAAC,CAAA;QAC/C,YAAY,CAAC,UAAU,CAAC,uBAAuB,CAAC,CAAA;IACjD,CAAC;CACD;AA9BD,kCA8BC"}
1
+ {"version":3,"file":"AuthManager.js","sourceRoot":"","sources":["../../src/internal/AuthManager.ts"],"names":[],"mappings":";;;AAGA;;GAEG;AACH,MAAa,WAAW;IAYd;IACA;IACA;IAbT,mEAAmE;IAC5D,MAAM,GAAG,KAAK,CAAA;IAErB;;;;;;OAMG;IACH,YACS,OAAuB,EACvB,OAAuB,EACvB,QAAoC;QAFpC,YAAO,GAAP,OAAO,CAAgB;QACvB,YAAO,GAAP,OAAO,CAAgB;QACvB,aAAQ,GAAR,QAAQ,CAA4B;IAC1C,CAAC;IAEJ,+EAA+E;IACxE,KAAK,CAAC,SAAS;QACrB,IAAI,CAAC,MAAM,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,SAAS,EAAE,CAAA;QAC5C,OAAO,IAAI,CAAC,MAAM,CAAA;IACnB,CAAC;IAED,kEAAkE;IAC3D,KAAK,CAAC,KAAK,CAAC,KAAa,EAAE,IAAY;QAC7C,IAAI,CAAC,MAAM,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,KAAK,EAAE,IAAI,CAAC,CAAA;IACpD,CAAC;IAED,mDAAmD;IAC5C,KAAK,CAAC,MAAM;QAClB,MAAM,IAAI,CAAC,QAAQ,EAAE,CAAA;QACrB,IAAI,CAAC,MAAM,GAAG,KAAK,CAAA;QACnB,MAAM,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,CAAA;IAC5B,CAAC;IAED,uEAAuE;IAChE,KAAK,CAAC,KAAK;QACjB,MAAM,IAAI,CAAC,MAAM,EAAE,CAAA;QACnB,MAAM,IAAI,CAAC,OAAO,CAAC,cAAc,EAAE,CAAA;QACnC,YAAY,CAAC,UAAU,CAAC,sBAAsB,CAAC,CAAA;QAC/C,YAAY,CAAC,UAAU,CAAC,uBAAuB,CAAC,CAAA;IACjD,CAAC;CACD;AA1CD,kCA0CC"}
@@ -1,16 +1,28 @@
1
1
  export type SyncState = "idle" | "syncing" | "offline" | "error";
2
+ /**
3
+ * Broadcasts store updates and sync state changes to local subscribers.
4
+ */
2
5
  export declare class EventBus {
6
+ /** Subscribers that listen for store updates. */
3
7
  private subscribers;
8
+ /** Subscribers that listen for sync state transitions. */
4
9
  private stateListeners;
10
+ /** Broadcast channel used to mirror updates across tabs. */
5
11
  private bc;
12
+ /** Initializes the broadcast listener for cross-tab update propagation. */
6
13
  constructor();
14
+ /** Adds a store-update subscriber and returns a handle for removing it. */
7
15
  subscribe(callback: (stores: string[]) => void): {
8
16
  close: () => boolean;
9
17
  };
18
+ /** Notifies all store subscribers and mirrors the update to other tabs. */
10
19
  notifySubscribers(stores: string[]): void;
20
+ /** Adds a sync-state subscriber and returns a handle for removing it. */
11
21
  onSyncStateChange(callback: (state: SyncState) => void): {
12
22
  close: () => boolean;
13
23
  };
24
+ /** Broadcasts a sync-state transition to all subscribers. */
14
25
  setState(state: SyncState): void;
26
+ /** Tears down the broadcast channel and stops cross-tab propagation. */
15
27
  dispose(): void;
16
28
  }
@@ -1,10 +1,17 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.EventBus = void 0;
4
+ /**
5
+ * Broadcasts store updates and sync state changes to local subscribers.
6
+ */
4
7
  class EventBus {
8
+ /** Subscribers that listen for store updates. */
5
9
  subscribers = new Set();
10
+ /** Subscribers that listen for sync state transitions. */
6
11
  stateListeners = new Set();
12
+ /** Broadcast channel used to mirror updates across tabs. */
7
13
  bc = typeof BroadcastChannel !== "undefined" ? new BroadcastChannel("dbsync_events") : null;
14
+ /** Initializes the broadcast listener for cross-tab update propagation. */
8
15
  constructor() {
9
16
  if (this.bc) {
10
17
  this.bc.onmessage = (e) => {
@@ -14,22 +21,27 @@ class EventBus {
14
21
  };
15
22
  }
16
23
  }
24
+ /** Adds a store-update subscriber and returns a handle for removing it. */
17
25
  subscribe(callback) {
18
26
  this.subscribers.add(callback);
19
27
  return { close: () => this.subscribers.delete(callback) };
20
28
  }
29
+ /** Notifies all store subscribers and mirrors the update to other tabs. */
21
30
  notifySubscribers(stores) {
22
31
  this.subscribers.forEach((cb) => cb(stores));
23
32
  if (this.bc)
24
33
  this.bc.postMessage({ type: "DATA_UPDATED", stores });
25
34
  }
35
+ /** Adds a sync-state subscriber and returns a handle for removing it. */
26
36
  onSyncStateChange(callback) {
27
37
  this.stateListeners.add(callback);
28
38
  return { close: () => this.stateListeners.delete(callback) };
29
39
  }
40
+ /** Broadcasts a sync-state transition to all subscribers. */
30
41
  setState(state) {
31
42
  this.stateListeners.forEach((cb) => cb(state));
32
43
  }
44
+ /** Tears down the broadcast channel and stops cross-tab propagation. */
33
45
  dispose() {
34
46
  if (this.bc) {
35
47
  this.bc.close();
@@ -1 +1 @@
1
- {"version":3,"file":"EventBus.js","sourceRoot":"","sources":["../../src/internal/EventBus.ts"],"names":[],"mappings":";;;AAEA,MAAa,QAAQ;IACZ,WAAW,GAAG,IAAI,GAAG,EAA8B,CAAA;IACnD,cAAc,GAAG,IAAI,GAAG,EAA8B,CAAA;IACtD,EAAE,GACT,OAAO,gBAAgB,KAAK,WAAW,CAAC,CAAC,CAAC,IAAI,gBAAgB,CAAC,eAAe,CAAC,CAAC,CAAC,CAAC,IAAI,CAAA;IAEvF;QACC,IAAI,IAAI,CAAC,EAAE,EAAE,CAAC;YACb,IAAI,CAAC,EAAE,CAAC,SAAS,GAAG,CAAC,CAAC,EAAE,EAAE;gBACzB,IAAI,CAAC,CAAC,IAAI,CAAC,IAAI,KAAK,cAAc,EAAE,CAAC;oBACpC,IAAI,CAAC,iBAAiB,CAAC,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAA;gBACtC,CAAC;YACF,CAAC,CAAA;QACF,CAAC;IACF,CAAC;IAEM,SAAS,CAAC,QAAoC;QACpD,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAA;QAC9B,OAAO,EAAE,KAAK,EAAE,GAAG,EAAE,CAAC,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,QAAQ,CAAC,EAAE,CAAA;IAC1D,CAAC;IAEM,iBAAiB,CAAC,MAAgB;QACxC,IAAI,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,CAAC,MAAM,CAAC,CAAC,CAAA;QAC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