@signaldb/core 1.1.0 → 1.2.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.
@@ -100,6 +100,7 @@ export default class Collection<T extends BaseItem<I> = BaseItem, I = any, U = T
100
100
  private isDisposed;
101
101
  private postBatchCallbacks;
102
102
  private fieldTracking;
103
+ private persistenceReadyPromise;
103
104
  /**
104
105
  * Initializes a new instance of the `Collection` class with optional configuration.
105
106
  * Sets up memory, persistence, reactivity, and indices as specified in the options.
@@ -154,6 +155,20 @@ export default class Collection<T extends BaseItem<I> = BaseItem, I = any, U = T
154
155
  * @param enable - A boolean indicating whether to enable (`true`) or disable (`false`) field tracking.
155
156
  */
156
157
  setFieldTracking(enable: boolean): void;
158
+ /**
159
+ * Resolves when the persistence adapter finished initializing
160
+ * and the collection is ready to be used.
161
+ * @returns A promise that resolves when the collection is ready.
162
+ * @example
163
+ * ```ts
164
+ * const collection = new Collection({
165
+ * persistence: // ...
166
+ * })
167
+ * await collection.isReady()
168
+ *
169
+ * collection.insert({ name: 'Item 1' })
170
+ */
171
+ isReady(): Promise<void>;
157
172
  private profile;
158
173
  private executeInDebugMode;
159
174
  private rebuildIndices;
package/dist/index.cjs.js CHANGED
@@ -13,7 +13,10 @@ const modify = require("./index.cjs11.js");
13
13
  const randomId = require("./index.cjs12.js");
14
14
  const EventEmitter = require("./index.cjs13.js");
15
15
  const createIndex = require("./index.cjs14.js");
16
- devtools();
16
+ /* istanbul ignore if -- @preserve */
17
+ if (process.env.NODE_ENV !== "production") {
18
+ devtools();
19
+ }
17
20
  exports.loadDeveloperTools = devtools;
18
21
  exports.Collection = index.default;
19
22
  exports.AutoFetchCollection = AutoFetchCollection;
@@ -69,6 +69,7 @@ const _Collection = class _Collection extends EventEmitter {
69
69
  __publicField(this, "isDisposed", false);
70
70
  __publicField(this, "postBatchCallbacks", /* @__PURE__ */ new Set());
71
71
  __publicField(this, "fieldTracking", false);
72
+ __publicField(this, "persistenceReadyPromise");
72
73
  _Collection.collections.push(this);
73
74
  this.name = (options == null ? void 0 : options.name) ?? `${this.constructor.name}-${randomId()}`;
74
75
  this.options = {
@@ -221,6 +222,12 @@ const _Collection = class _Collection extends EventEmitter {
221
222
  this.emit("persistence.error", error instanceof Error ? error : new Error(error));
222
223
  });
223
224
  }
225
+ this.persistenceReadyPromise = new Promise((resolve, reject) => {
226
+ if (!this.persistenceAdapter)
227
+ return resolve();
228
+ this.once("persistence.init", resolve);
229
+ this.once("persistence.error", reject);
230
+ });
224
231
  _Collection.onCreationCallbacks.forEach((callback) => callback(this));
225
232
  }
226
233
  static getCollections() {
@@ -295,6 +302,22 @@ const _Collection = class _Collection extends EventEmitter {
295
302
  setFieldTracking(enable) {
296
303
  this.fieldTracking = enable;
297
304
  }
305
+ /**
306
+ * Resolves when the persistence adapter finished initializing
307
+ * and the collection is ready to be used.
308
+ * @returns A promise that resolves when the collection is ready.
309
+ * @example
310
+ * ```ts
311
+ * const collection = new Collection({
312
+ * persistence: // ...
313
+ * })
314
+ * await collection.isReady()
315
+ *
316
+ * collection.insert({ name: 'Item 1' })
317
+ */
318
+ async isReady() {
319
+ return this.persistenceReadyPromise;
320
+ }
298
321
  profile(fn, measureFunction) {
299
322
  if (!this.debugMode)
300
323
  return fn();
@@ -346,7 +369,8 @@ const _Collection = class _Collection extends EventEmitter {
346
369
  const indexInfo = this.getIndexInfo(selector);
347
370
  const items = indexInfo.matched ? indexInfo.positions.map((index2) => memory[index2]) : memory;
348
371
  const item = items.find((document) => match(document, selector));
349
- const index = indexInfo.matched && indexInfo.positions.find((itemIndex) => memory[itemIndex] === item) || memory.findIndex((document) => document === item);
372
+ const foundInIndex = indexInfo.matched && indexInfo.positions.find((itemIndex) => memory[itemIndex] === item);
373
+ const index = foundInIndex || memory.findIndex((document) => document === item);
350
374
  if (item == null)
351
375
  return { item: null, index: -1 };
352
376
  if (index === -1)
package/dist/index.mjs CHANGED
@@ -11,7 +11,10 @@ import { default as default10 } from "./index11.mjs";
11
11
  import { default as default11 } from "./index12.mjs";
12
12
  import { default as default12 } from "./index13.mjs";
13
13
  import { default as default13 } from "./index14.mjs";
14
- loadDeveloperTools();
14
+ /* istanbul ignore if -- @preserve */
15
+ if (process.env.NODE_ENV !== "production") {
16
+ loadDeveloperTools();
17
+ }
15
18
  export {
16
19
  default3 as AutoFetchCollection,
17
20
  default2 as Collection,
package/dist/index3.mjs CHANGED
@@ -68,6 +68,7 @@ const _Collection = class _Collection extends EventEmitter {
68
68
  __publicField(this, "isDisposed", false);
69
69
  __publicField(this, "postBatchCallbacks", /* @__PURE__ */ new Set());
70
70
  __publicField(this, "fieldTracking", false);
71
+ __publicField(this, "persistenceReadyPromise");
71
72
  _Collection.collections.push(this);
72
73
  this.name = (options == null ? void 0 : options.name) ?? `${this.constructor.name}-${randomId()}`;
73
74
  this.options = {
@@ -220,6 +221,12 @@ const _Collection = class _Collection extends EventEmitter {
220
221
  this.emit("persistence.error", error instanceof Error ? error : new Error(error));
221
222
  });
222
223
  }
224
+ this.persistenceReadyPromise = new Promise((resolve, reject) => {
225
+ if (!this.persistenceAdapter)
226
+ return resolve();
227
+ this.once("persistence.init", resolve);
228
+ this.once("persistence.error", reject);
229
+ });
223
230
  _Collection.onCreationCallbacks.forEach((callback) => callback(this));
224
231
  }
225
232
  static getCollections() {
@@ -294,6 +301,22 @@ const _Collection = class _Collection extends EventEmitter {
294
301
  setFieldTracking(enable) {
295
302
  this.fieldTracking = enable;
296
303
  }
304
+ /**
305
+ * Resolves when the persistence adapter finished initializing
306
+ * and the collection is ready to be used.
307
+ * @returns A promise that resolves when the collection is ready.
308
+ * @example
309
+ * ```ts
310
+ * const collection = new Collection({
311
+ * persistence: // ...
312
+ * })
313
+ * await collection.isReady()
314
+ *
315
+ * collection.insert({ name: 'Item 1' })
316
+ */
317
+ async isReady() {
318
+ return this.persistenceReadyPromise;
319
+ }
297
320
  profile(fn, measureFunction) {
298
321
  if (!this.debugMode)
299
322
  return fn();
@@ -345,7 +368,8 @@ const _Collection = class _Collection extends EventEmitter {
345
368
  const indexInfo = this.getIndexInfo(selector);
346
369
  const items = indexInfo.matched ? indexInfo.positions.map((index2) => memory[index2]) : memory;
347
370
  const item = items.find((document) => match(document, selector));
348
- const index = indexInfo.matched && indexInfo.positions.find((itemIndex) => memory[itemIndex] === item) || memory.findIndex((document) => document === item);
371
+ const foundInIndex = indexInfo.matched && indexInfo.positions.find((itemIndex) => memory[itemIndex] === item);
372
+ const index = foundInIndex || memory.findIndex((document) => document === item);
349
373
  if (item == null)
350
374
  return { item: null, index: -1 };
351
375
  if (index === -1)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@signaldb/core",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "description": "SignalDB is a client-side database that provides a simple MongoDB-like interface to the data with first-class typescript support to achieve an optimistic UI. Data persistence can be achieved by using storage providers that store the data through a JSON interface to places such as localStorage.",
5
5
  "scripts": {
6
6
  "build": "rimraf dist && vite build"