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