live-cache 0.2.0 → 0.2.2

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/dist/index.mjs CHANGED
@@ -525,14 +525,17 @@ class Controller {
525
525
  abort() {
526
526
  if (this.abortController) {
527
527
  this.abortController.abort();
528
+ this.abortController = null;
528
529
  }
529
- this.abortController = new AbortController();
530
530
  }
531
531
  updateTotal(total) {
532
532
  this.total = total;
533
533
  }
534
- updatePageSize(pageSize) {
535
- this.pageSize = pageSize;
534
+ updatePage(page) {
535
+ this.page = page;
536
+ }
537
+ updateLimit(limit) {
538
+ this.limit = limit;
536
539
  }
537
540
  /**
538
541
  * Fetch the complete dataset for this controller.
@@ -540,11 +543,23 @@ class Controller {
540
543
  * Subclasses must implement this. Return `[rows, total]` where `total` is the
541
544
  * total number of rows available on the backend (useful for pagination).
542
545
  */
543
- fetchAll() {
546
+ fetch(where) {
544
547
  return __awaiter(this, void 0, void 0, function* () {
545
548
  throw Error("Not Implemented");
546
549
  });
547
550
  }
551
+ nextPage(where) {
552
+ return __awaiter(this, void 0, void 0, function* () {
553
+ this.updatePage(this.page + 1);
554
+ yield this.update(where);
555
+ });
556
+ }
557
+ previousPage(where) {
558
+ return __awaiter(this, void 0, void 0, function* () {
559
+ this.updatePage(this.page - 1);
560
+ yield this.update(where);
561
+ });
562
+ }
548
563
  /**
549
564
  * Initialise (hydrate) the controller's collection.
550
565
  *
@@ -555,37 +570,41 @@ class Controller {
555
570
  *
556
571
  * A successful initialise ends with `commit()` so subscribers receive the latest snapshot.
557
572
  */
558
- initialise() {
573
+ initialise(where) {
559
574
  return __awaiter(this, void 0, void 0, function* () {
560
- var _a;
561
- if (this.loading)
562
- return;
575
+ this.abortController = new AbortController();
563
576
  // If the collection is not empty, return.
564
- let data = this.collection.find().map((doc) => doc.toData());
577
+ let data = this.collection.find(where).map((doc) => doc.toData());
565
578
  if (data.length !== 0) {
566
- return;
567
- }
568
- // If the collection is empty, check the storage manager.
569
- data = (_a = (yield this.storageManager.get(this.name))) !== null && _a !== void 0 ? _a : [];
570
- if (data.length !== 0) {
571
- this.updateTotal(this.collection.find().length);
572
- this.collection.insertMany(data);
579
+ this.updateTotal(data.length);
573
580
  yield this.commit();
574
581
  return;
575
582
  }
583
+ const fromStorage = yield this.storageManager.get(this.name);
584
+ if (fromStorage && fromStorage.length !== 0) {
585
+ const __collection = new Collection(this.name);
586
+ __collection.insertMany(fromStorage);
587
+ const __data = __collection.find(where).map(x => x.toData());
588
+ if (__data.length !== 0) {
589
+ this.collection.insertMany(__data);
590
+ this.updateTotal(__data.length);
591
+ yield this.commit();
592
+ return;
593
+ }
594
+ }
576
595
  // If the storage manager is empty, fetch the data from the server.
577
596
  try {
578
597
  this.loading = true;
579
- const [_data, total] = yield this.fetchAll();
598
+ const [_data, total] = yield this.fetch(where);
580
599
  this.collection.insertMany(_data);
581
600
  this.updateTotal(total);
582
- yield this.commit();
583
601
  }
584
602
  catch (error) {
585
603
  this.error = error;
586
604
  }
587
605
  finally {
588
606
  this.loading = false;
607
+ yield this.commit();
589
608
  }
590
609
  });
591
610
  }
@@ -602,7 +621,7 @@ class Controller {
602
621
  * unsubscribe();
603
622
  * ```
604
623
  */
605
- publish(onChange) {
624
+ subscribe(onChange) {
606
625
  this.subscribers.add(onChange);
607
626
  return () => this.subscribers.delete(onChange);
608
627
  }
@@ -611,12 +630,12 @@ class Controller {
611
630
  *
612
631
  * This is intentionally private: consumers should use `commit()` which computes the snapshot.
613
632
  */
614
- subscribe(model) {
633
+ publish(models) {
615
634
  return __awaiter(this, void 0, void 0, function* () {
616
635
  // Persist the full cache snapshot for hydration.
617
636
  yield this.storageManager.set(this.name, this.collection.find().map((doc) => doc.toModel()));
618
637
  this.subscribers.forEach((sub) => {
619
- sub(model);
638
+ sub(models);
620
639
  });
621
640
  });
622
641
  }
@@ -630,7 +649,7 @@ class Controller {
630
649
  commit() {
631
650
  return __awaiter(this, void 0, void 0, function* () {
632
651
  const models = this.collection.find().map((doc) => doc.toModel());
633
- yield this.subscribe(models);
652
+ yield this.publish(models);
634
653
  });
635
654
  }
636
655
  /**
@@ -638,8 +657,13 @@ class Controller {
638
657
  *
639
658
  * Subclasses typically use this inside `invalidate()`.
640
659
  */
641
- refetch() {
642
- return this.initialise();
660
+ update(where) {
661
+ return __awaiter(this, void 0, void 0, function* () {
662
+ const [response, total] = yield this.fetch(where);
663
+ this.collection.insertMany(response);
664
+ this.updateTotal(total);
665
+ yield this.commit();
666
+ });
643
667
  }
644
668
  /**
645
669
  * Invalidate the cache for this controller.
@@ -663,10 +687,11 @@ class Controller {
663
687
  void this.storageManager.delete(this.name);
664
688
  this.collection.clear();
665
689
  this.updateTotal(0);
666
- this.updatePageSize(-1);
690
+ this.updatePage(0);
691
+ this.updateLimit(10);
667
692
  this.error = null;
668
693
  this.loading = false;
669
- void this.subscribe([]);
694
+ void this.publish([]);
670
695
  }
671
696
  /**
672
697
  * Create a controller.
@@ -675,22 +700,22 @@ class Controller {
675
700
  * @param storageManager - where snapshots are persisted (defaults to no-op)
676
701
  * @param pageSize - optional pagination hint (userland)
677
702
  */
678
- constructor(name, { storageManager = new DefaultStorageManager("live-cache:"), pageSize = -1, invalidator = new DefaultInvalidator(), initialiseOnMount = true, }) {
703
+ constructor(name, { storageManager = new DefaultStorageManager("live-cache:"), pageSize = 10, invalidator = new DefaultInvalidator(), }) {
679
704
  this.subscribers = new Set();
680
705
  this.loading = false;
681
706
  this.error = null;
682
707
  this.total = -1;
683
- this.pageSize = -1;
708
+ this.page = 0;
709
+ this.limit = 10;
684
710
  this.abortController = null;
711
+ this.initialised = false;
685
712
  this.name = name;
686
713
  this.collection = new Collection(name);
687
714
  this.storageManager = storageManager;
688
- this.pageSize = pageSize;
715
+ this.page = 0;
716
+ this.limit = pageSize;
689
717
  this.invalidator = invalidator;
690
718
  this.invalidator.bind(this.invalidate.bind(this));
691
- if (initialiseOnMount) {
692
- this.initialise();
693
- }
694
719
  }
695
720
  }
696
721
 
@@ -943,12 +968,12 @@ class ObjectStore {
943
968
  /**
944
969
  * Initialise a controller once per store, even if multiple callers request it.
945
970
  */
946
- initialiseOnce(name) {
971
+ initialiseOnce(name, where) {
947
972
  const controller = this.get(name);
948
973
  const existing = this.initialisePromises.get(controller);
949
974
  if (existing)
950
975
  return existing;
951
- const promise = controller.initialise().finally(() => {
976
+ const promise = controller.initialise(where).finally(() => {
952
977
  if (this.initialisePromises.get(controller) === promise) {
953
978
  this.initialisePromises.delete(controller);
954
979
  }
@@ -1298,7 +1323,7 @@ function useRegister(controller, store = getDefaultObjectStore()) {
1298
1323
  */
1299
1324
  function useController(name, where, options) {
1300
1325
  var _a, _b, _c, _d;
1301
- const initialise = (_a = options === null || options === void 0 ? void 0 : options.initialise) !== null && _a !== void 0 ? _a : true;
1326
+ (_a = options === null || options === void 0 ? void 0 : options.initialise) !== null && _a !== void 0 ? _a : true;
1302
1327
  const optionalStore = options === null || options === void 0 ? void 0 : options.store;
1303
1328
  const abortOnUnmount = (_b = options === null || options === void 0 ? void 0 : options.abortOnUnmount) !== null && _b !== void 0 ? _b : true;
1304
1329
  const withInvalidation = (_c = options === null || options === void 0 ? void 0 : options.withInvalidation) !== null && _c !== void 0 ? _c : true;
@@ -1320,13 +1345,12 @@ function useController(name, where, options) {
1320
1345
  };
1321
1346
  // Prime state immediately.
1322
1347
  callback();
1323
- const cleanup = controller.publish(callback);
1348
+ const cleanup = controller.subscribe(callback);
1324
1349
  if (withInvalidation) {
1325
1350
  controller.invalidator.registerInvalidation();
1326
1351
  }
1327
- if (initialise) {
1328
- void store.initialiseOnce(name);
1329
- }
1352
+ void store.initialiseOnce(name, where);
1353
+ // controller.initialise(where);
1330
1354
  return () => {
1331
1355
  if (abortOnUnmount) {
1332
1356
  controller.abort();
@@ -1334,7 +1358,7 @@ function useController(name, where, options) {
1334
1358
  cleanup();
1335
1359
  controller.invalidator.unregisterInvalidation();
1336
1360
  };
1337
- }, [controller, where, initialise, abortOnUnmount, withInvalidation]);
1361
+ }, [controller, where, abortOnUnmount, withInvalidation]);
1338
1362
  return { controller, data, loading, error };
1339
1363
  }
1340
1364
 
@@ -1357,7 +1381,7 @@ function useJoinController({ from, where, select }) {
1357
1381
  setData(join(from, where, select));
1358
1382
  };
1359
1383
  callback();
1360
- const cleanup = from.map((c) => c.publish(callback));
1384
+ const cleanup = from.map((c) => c.subscribe(callback));
1361
1385
  return () => {
1362
1386
  cleanup.forEach((c) => c());
1363
1387
  };