@signaldb/core 1.0.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.
- package/README.md +68 -0
- package/dist/.vite/manifest.json +222 -0
- package/dist/AutoFetchCollection.d.ts +60 -0
- package/dist/Collection/Cursor.d.ts +112 -0
- package/dist/Collection/Observer.d.ts +61 -0
- package/dist/Collection/createIndex.d.ts +15 -0
- package/dist/Collection/getIndexInfo.d.ts +41 -0
- package/dist/Collection/index.d.ts +235 -0
- package/dist/Collection/types.d.ts +25 -0
- package/dist/ReplicatedCollection.d.ts +60 -0
- package/dist/createIndexProvider.d.ts +8 -0
- package/dist/createMemoryAdapter.d.ts +7 -0
- package/dist/createReactivityAdapter.d.ts +8 -0
- package/dist/index.cjs.js +24 -0
- package/dist/index.cjs10.js +8 -0
- package/dist/index.cjs11.js +5 -0
- package/dist/index.cjs12.js +37 -0
- package/dist/index.cjs13.js +26 -0
- package/dist/index.cjs14.js +7 -0
- package/dist/index.cjs15.js +43 -0
- package/dist/index.cjs16.js +13 -0
- package/dist/index.cjs17.js +20 -0
- package/dist/index.cjs18.js +247 -0
- package/dist/index.cjs19.js +71 -0
- package/dist/index.cjs2.js +631 -0
- package/dist/index.cjs20.js +80 -0
- package/dist/index.cjs21.js +10 -0
- package/dist/index.cjs22.js +27 -0
- package/dist/index.cjs23.js +137 -0
- package/dist/index.cjs24.js +18 -0
- package/dist/index.cjs25.js +7 -0
- package/dist/index.cjs26.js +19 -0
- package/dist/index.cjs27.js +24 -0
- package/dist/index.cjs28.js +42 -0
- package/dist/index.cjs29.js +29 -0
- package/dist/index.cjs3.js +165 -0
- package/dist/index.cjs30.js +9 -0
- package/dist/index.cjs4.js +70 -0
- package/dist/index.cjs5.js +5 -0
- package/dist/index.cjs6.js +5 -0
- package/dist/index.cjs7.js +5 -0
- package/dist/index.cjs8.js +5 -0
- package/dist/index.cjs9.js +30 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.mjs +24 -0
- package/dist/index10.mjs +9 -0
- package/dist/index11.mjs +6 -0
- package/dist/index12.mjs +37 -0
- package/dist/index13.mjs +27 -0
- package/dist/index14.mjs +8 -0
- package/dist/index15.mjs +43 -0
- package/dist/index16.mjs +14 -0
- package/dist/index17.mjs +21 -0
- package/dist/index18.mjs +247 -0
- package/dist/index19.mjs +71 -0
- package/dist/index2.mjs +632 -0
- package/dist/index20.mjs +80 -0
- package/dist/index21.mjs +11 -0
- package/dist/index22.mjs +28 -0
- package/dist/index23.mjs +138 -0
- package/dist/index24.mjs +18 -0
- package/dist/index25.mjs +8 -0
- package/dist/index26.mjs +20 -0
- package/dist/index27.mjs +25 -0
- package/dist/index28.mjs +43 -0
- package/dist/index29.mjs +30 -0
- package/dist/index3.mjs +166 -0
- package/dist/index30.mjs +10 -0
- package/dist/index4.mjs +70 -0
- package/dist/index5.mjs +6 -0
- package/dist/index6.mjs +6 -0
- package/dist/index7.mjs +6 -0
- package/dist/index8.mjs +6 -0
- package/dist/index9.mjs +31 -0
- package/dist/persistence/combinePersistenceAdapters.d.ts +32 -0
- package/dist/persistence/createPersistenceAdapter.d.ts +9 -0
- package/dist/types/Dependency.d.ts +4 -0
- package/dist/types/EventEmitter.d.ts +25 -0
- package/dist/types/IndexProvider.d.ts +19 -0
- package/dist/types/MemoryAdapter.d.ts +15 -0
- package/dist/types/Modifier.d.ts +39 -0
- package/dist/types/PersistenceAdapter.d.ts +20 -0
- package/dist/types/ReactivityAdapter.d.ts +6 -0
- package/dist/types/Selector.d.ts +47 -0
- package/dist/types/Signal.d.ts +4 -0
- package/dist/utils/compact.d.ts +9 -0
- package/dist/utils/createSignal.d.ts +14 -0
- package/dist/utils/deepClone.d.ts +17 -0
- package/dist/utils/get.d.ts +9 -0
- package/dist/utils/getMatchingKeys.d.ts +14 -0
- package/dist/utils/intersection.d.ts +9 -0
- package/dist/utils/isEqual.d.ts +14 -0
- package/dist/utils/isFieldExpression.d.ts +11 -0
- package/dist/utils/match.d.ts +12 -0
- package/dist/utils/modify.d.ts +14 -0
- package/dist/utils/project.d.ts +15 -0
- package/dist/utils/randomId.d.ts +7 -0
- package/dist/utils/serializeValue.d.ts +12 -0
- package/dist/utils/set.d.ts +13 -0
- package/dist/utils/sortItems.d.ts +12 -0
- package/dist/utils/uniqueBy.d.ts +10 -0
- package/package.json +58 -0
package/dist/index20.mjs
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import Collection from "./index2.mjs";
|
|
2
|
+
import combinePersistenceAdapters from "./index4.mjs";
|
|
3
|
+
import createPersistenceAdapter from "./index7.mjs";
|
|
4
|
+
import createSignal from "./index17.mjs";
|
|
5
|
+
function createReplicationAdapter(options) {
|
|
6
|
+
return createPersistenceAdapter({
|
|
7
|
+
async register(onChange) {
|
|
8
|
+
if (!options.registerRemoteChange)
|
|
9
|
+
return;
|
|
10
|
+
await options.registerRemoteChange(onChange);
|
|
11
|
+
},
|
|
12
|
+
load: () => options.pull(),
|
|
13
|
+
save: (items, changes) => {
|
|
14
|
+
if (!options.push)
|
|
15
|
+
throw new Error("Pushing is not configured for this collection. Try to pass a `push` function to the collection options.");
|
|
16
|
+
return options.push(changes, items);
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
class ReplicatedCollection extends Collection {
|
|
21
|
+
/**
|
|
22
|
+
* Creates a new instance of the `ReplicatedCollection` class.
|
|
23
|
+
* Sets up the replication adapter, combining it with an optional persistence adapter, and
|
|
24
|
+
* initializes signals for tracking remote pull and push operations.
|
|
25
|
+
* @param options - The configuration options for the replicated collection.
|
|
26
|
+
* @param options.pull - A function to fetch data from the remote source.
|
|
27
|
+
* @param options.push - An optional function to send changes and items to the remote source.
|
|
28
|
+
* @param options.registerRemoteChange - An optional function to register a listener for remote changes.
|
|
29
|
+
* @param options.persistence - An optional persistence adapter to combine with replication.
|
|
30
|
+
* @param options.reactivity - A reactivity adapter for observing changes in the collection.
|
|
31
|
+
* @param options.transform - A transformation function to apply to items when retrieving them.
|
|
32
|
+
* @param options.indices - An array of index providers for optimized querying.
|
|
33
|
+
* @param options.enableDebugMode - A boolean to enable or disable debug mode.
|
|
34
|
+
*/
|
|
35
|
+
constructor(options) {
|
|
36
|
+
var _a, _b;
|
|
37
|
+
const replicationAdapter = createReplicationAdapter({
|
|
38
|
+
registerRemoteChange: options.registerRemoteChange,
|
|
39
|
+
pull: async () => {
|
|
40
|
+
this.isPullingRemoteSignal.set(true);
|
|
41
|
+
try {
|
|
42
|
+
return await options.pull();
|
|
43
|
+
} finally {
|
|
44
|
+
this.isPullingRemoteSignal.set(false);
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
push: options.push ? async (changes, items) => {
|
|
48
|
+
if (!options.push)
|
|
49
|
+
throw new Error("Pushing is not configured for this collection. Try to pass a `push` function to the collection options.");
|
|
50
|
+
this.isPushingRemoteSignal.set(true);
|
|
51
|
+
try {
|
|
52
|
+
await options.push(changes, items);
|
|
53
|
+
} finally {
|
|
54
|
+
this.isPushingRemoteSignal.set(false);
|
|
55
|
+
}
|
|
56
|
+
} : void 0
|
|
57
|
+
});
|
|
58
|
+
const persistenceAdapter = (options === null || options === void 0 ? void 0 : options.persistence) ? combinePersistenceAdapters(replicationAdapter, options.persistence) : replicationAdapter;
|
|
59
|
+
super(Object.assign(Object.assign({}, options), { persistence: persistenceAdapter }));
|
|
60
|
+
this.isPullingRemoteSignal = createSignal((_a = options.reactivity) === null || _a === void 0 ? void 0 : _a.create(), false);
|
|
61
|
+
this.isPushingRemoteSignal = createSignal((_b = options.reactivity) === null || _b === void 0 ? void 0 : _b.create(), false);
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Checks whether the collection is currently performing any loading operation,
|
|
65
|
+
* including pulling or pushing data from/to the remote source, or standard
|
|
66
|
+
* persistence adapter operations.
|
|
67
|
+
* ⚡️ this function is reactive!
|
|
68
|
+
* @returns A boolean indicating if the collection is currently loading or synchronizing.
|
|
69
|
+
*/
|
|
70
|
+
isLoading() {
|
|
71
|
+
const isPullingRemote = this.isPullingRemoteSignal.get();
|
|
72
|
+
const isPushingRemote = this.isPushingRemoteSignal.get();
|
|
73
|
+
const isLoading = super.isLoading();
|
|
74
|
+
return isPullingRemote || isPushingRemote || isLoading;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
export {
|
|
78
|
+
createReplicationAdapter,
|
|
79
|
+
ReplicatedCollection as default
|
|
80
|
+
};
|
package/dist/index21.mjs
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { sort } from "fast-sort";
|
|
2
|
+
import get from "./index26.mjs";
|
|
3
|
+
function sortItems(items, sortFields) {
|
|
4
|
+
return sort(items).by(Object.entries(sortFields).map(([key, value]) => {
|
|
5
|
+
const order = value === 1 ? "asc" : "desc";
|
|
6
|
+
return { [order]: (i) => get(i, key) };
|
|
7
|
+
}));
|
|
8
|
+
}
|
|
9
|
+
export {
|
|
10
|
+
sortItems as default
|
|
11
|
+
};
|
package/dist/index22.mjs
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import get from "./index26.mjs";
|
|
2
|
+
import set from "./index29.mjs";
|
|
3
|
+
function project(item, fields) {
|
|
4
|
+
const allFieldsDeactivated = Object.values(fields).every((value) => value === 0);
|
|
5
|
+
if (allFieldsDeactivated) {
|
|
6
|
+
const result2 = Object.assign({}, item);
|
|
7
|
+
Object.keys(fields).forEach((key) => {
|
|
8
|
+
const fieldValue = get(item, key);
|
|
9
|
+
if (fieldValue === void 0)
|
|
10
|
+
return;
|
|
11
|
+
set(result2, key, void 0, true);
|
|
12
|
+
});
|
|
13
|
+
return result2;
|
|
14
|
+
}
|
|
15
|
+
const result = {};
|
|
16
|
+
Object.entries(fields).forEach(([key, value]) => {
|
|
17
|
+
const fieldValue = get(item, key);
|
|
18
|
+
if (fieldValue === void 0)
|
|
19
|
+
return;
|
|
20
|
+
if (fieldValue == null && value !== 1)
|
|
21
|
+
return;
|
|
22
|
+
set(result, key, value === 1 ? fieldValue : void 0);
|
|
23
|
+
});
|
|
24
|
+
return result;
|
|
25
|
+
}
|
|
26
|
+
export {
|
|
27
|
+
project as default
|
|
28
|
+
};
|
package/dist/index23.mjs
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import isEqual from "./index9.mjs";
|
|
2
|
+
import uniqueBy from "./index30.mjs";
|
|
3
|
+
class Observer {
|
|
4
|
+
/**
|
|
5
|
+
* Creates a new instance of the `Observer` class.
|
|
6
|
+
* Sets up event bindings and initializes the callbacks for tracking changes in a collection.
|
|
7
|
+
* @param bindEvents - A function to bind external events to the observer. Must return a cleanup function to unbind those events.
|
|
8
|
+
*/
|
|
9
|
+
constructor(bindEvents) {
|
|
10
|
+
this.previousItems = [];
|
|
11
|
+
this.callbacks = {
|
|
12
|
+
added: [],
|
|
13
|
+
addedBefore: [],
|
|
14
|
+
changed: [],
|
|
15
|
+
changedField: [],
|
|
16
|
+
movedBefore: [],
|
|
17
|
+
removed: []
|
|
18
|
+
};
|
|
19
|
+
this.unbindEvents = bindEvents();
|
|
20
|
+
}
|
|
21
|
+
call(event, ...args) {
|
|
22
|
+
this.callbacks[event].forEach(({ callback, options }) => {
|
|
23
|
+
if (!options.skipInitial || !options.isInitial) {
|
|
24
|
+
callback(...args);
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
hasCallbacks(events) {
|
|
29
|
+
return events.some((event) => this.callbacks[event].length > 0);
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Determines if the observer has no active callbacks registered for any events.
|
|
33
|
+
* @returns A boolean indicating whether the observer is empty (i.e., no callbacks are registered).
|
|
34
|
+
*/
|
|
35
|
+
isEmpty() {
|
|
36
|
+
return !this.hasCallbacks([
|
|
37
|
+
"added",
|
|
38
|
+
"addedBefore",
|
|
39
|
+
"changed",
|
|
40
|
+
"changedField",
|
|
41
|
+
"movedBefore",
|
|
42
|
+
"removed"
|
|
43
|
+
]);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Compares the previous state of items with the new state and triggers the appropriate callbacks
|
|
47
|
+
* for events such as added, removed, changed, or moved items.
|
|
48
|
+
* @param newItems - The new list of items to compare against the previous state.
|
|
49
|
+
*/
|
|
50
|
+
runChecks(newItems) {
|
|
51
|
+
const oldItemsMap = new Map(this.previousItems.map((item, index) => [
|
|
52
|
+
item.id,
|
|
53
|
+
{ item, index, beforeItem: this.previousItems[index + 1] || null }
|
|
54
|
+
]));
|
|
55
|
+
const newItemsMap = new Map(newItems.map((item, index) => [
|
|
56
|
+
item.id,
|
|
57
|
+
{ item, index, beforeItem: newItems[index + 1] || null }
|
|
58
|
+
]));
|
|
59
|
+
if (this.hasCallbacks(["changed", "changedField", "movedBefore", "removed"])) {
|
|
60
|
+
oldItemsMap.forEach(({ item: oldItem, index, beforeItem: oldBeforeItem }) => {
|
|
61
|
+
var _a;
|
|
62
|
+
const newItem = newItemsMap.get(oldItem.id);
|
|
63
|
+
if (newItem) {
|
|
64
|
+
if (this.hasCallbacks(["changed", "changedField"])) {
|
|
65
|
+
if (!isEqual(newItem.item, oldItem)) {
|
|
66
|
+
this.call("changed", newItem.item);
|
|
67
|
+
if (this.hasCallbacks(["changedField"])) {
|
|
68
|
+
const keys = uniqueBy([
|
|
69
|
+
...Object.keys(newItem.item),
|
|
70
|
+
...Object.keys(oldItem)
|
|
71
|
+
], (value) => value);
|
|
72
|
+
keys.forEach((key) => {
|
|
73
|
+
if (isEqual(newItem.item[key], oldItem[key]))
|
|
74
|
+
return;
|
|
75
|
+
this.call("changedField", newItem.item, key, oldItem[key], newItem.item[key]);
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
if (newItem.index !== index && ((_a = newItem.beforeItem) === null || _a === void 0 ? void 0 : _a.id) !== (oldBeforeItem === null || oldBeforeItem === void 0 ? void 0 : oldBeforeItem.id)) {
|
|
81
|
+
this.call("movedBefore", newItem.item, newItem.beforeItem);
|
|
82
|
+
}
|
|
83
|
+
} else {
|
|
84
|
+
this.call("removed", oldItem);
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
if (this.hasCallbacks(["added", "addedBefore"])) {
|
|
89
|
+
newItems.forEach((newItem, index) => {
|
|
90
|
+
const oldItem = oldItemsMap.get(newItem.id);
|
|
91
|
+
if (oldItem)
|
|
92
|
+
return;
|
|
93
|
+
this.call("added", newItem);
|
|
94
|
+
this.call("addedBefore", newItem, newItems[index + 1] || null);
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
this.previousItems = newItems;
|
|
98
|
+
Object.keys(this.callbacks).forEach((key) => {
|
|
99
|
+
const event = key;
|
|
100
|
+
const callbacks = this.callbacks[event];
|
|
101
|
+
this.callbacks[event] = callbacks.map((callback) => Object.assign(Object.assign({}, callback), { options: Object.assign(Object.assign({}, callback.options), { isInitial: false }) }));
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Stops the observer by unbinding all events and cleaning up resources.
|
|
106
|
+
*/
|
|
107
|
+
stop() {
|
|
108
|
+
this.unbindEvents();
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Registers callbacks for specific events to observe changes in the collection.
|
|
112
|
+
* @param callbacks - An object containing the callbacks for various events (e.g., 'added', 'removed').
|
|
113
|
+
* @param skipInitial - A boolean indicating whether to skip invoking the callbacks for the initial state of the collection.
|
|
114
|
+
*/
|
|
115
|
+
addCallbacks(callbacks, skipInitial = false) {
|
|
116
|
+
Object.keys(callbacks).forEach((key) => {
|
|
117
|
+
const typedKey = key;
|
|
118
|
+
this.callbacks[typedKey].push({
|
|
119
|
+
callback: callbacks[typedKey],
|
|
120
|
+
options: { skipInitial, isInitial: true }
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Removes the specified callbacks for specific events, unregistering them from the observer.
|
|
126
|
+
* @param callbacks - An object containing the callbacks to be removed for various events.
|
|
127
|
+
*/
|
|
128
|
+
removeCallbacks(callbacks) {
|
|
129
|
+
Object.keys(callbacks).forEach((key) => {
|
|
130
|
+
const typedKey = key;
|
|
131
|
+
const index = this.callbacks[typedKey].findIndex(({ callback }) => callback === callbacks[typedKey]);
|
|
132
|
+
this.callbacks[typedKey].splice(index, 1);
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
export {
|
|
137
|
+
Observer as default
|
|
138
|
+
};
|
package/dist/index24.mjs
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
function __rest(s, e) {
|
|
2
|
+
var t = {};
|
|
3
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
4
|
+
t[p] = s[p];
|
|
5
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
6
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
7
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
8
|
+
t[p[i]] = s[p[i]];
|
|
9
|
+
}
|
|
10
|
+
return t;
|
|
11
|
+
}
|
|
12
|
+
typeof SuppressedError === "function" ? SuppressedError : function(error, suppressed, message) {
|
|
13
|
+
var e = new Error(message);
|
|
14
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
15
|
+
};
|
|
16
|
+
export {
|
|
17
|
+
__rest
|
|
18
|
+
};
|
package/dist/index25.mjs
ADDED
package/dist/index26.mjs
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
function get(value, path) {
|
|
2
|
+
const segments = path.split(/[.[\]]/g);
|
|
3
|
+
if (segments[0] === "")
|
|
4
|
+
segments.shift();
|
|
5
|
+
if (segments[segments.length - 1] === "")
|
|
6
|
+
segments.pop();
|
|
7
|
+
let current = value;
|
|
8
|
+
for (let i = 0; i < segments.length; i += 1) {
|
|
9
|
+
const key = segments[i];
|
|
10
|
+
if (current == null || key.trim() === "")
|
|
11
|
+
return void 0;
|
|
12
|
+
current = current[key];
|
|
13
|
+
}
|
|
14
|
+
if (current === void 0)
|
|
15
|
+
return void 0;
|
|
16
|
+
return current;
|
|
17
|
+
}
|
|
18
|
+
export {
|
|
19
|
+
get as default
|
|
20
|
+
};
|
package/dist/index27.mjs
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import isFieldExpression from "./index28.mjs";
|
|
2
|
+
import serializeValue from "./index16.mjs";
|
|
3
|
+
function getMatchingKeys(field, selector) {
|
|
4
|
+
if (selector[field] instanceof RegExp)
|
|
5
|
+
return null;
|
|
6
|
+
if (selector[field] != null) {
|
|
7
|
+
if (isFieldExpression(selector[field])) {
|
|
8
|
+
const is$in = isFieldExpression(selector[field]) && Array.isArray(selector[field].$in) && selector[field].$in.length;
|
|
9
|
+
if (is$in) {
|
|
10
|
+
const optimizedSelector = Object.assign(Object.assign({}, selector), { [field]: Object.assign({}, selector[field]) });
|
|
11
|
+
delete optimizedSelector[field].$in;
|
|
12
|
+
if (Object.keys(optimizedSelector[field]).length === 0) {
|
|
13
|
+
delete optimizedSelector[field];
|
|
14
|
+
}
|
|
15
|
+
return selector[field].$in.map(serializeValue);
|
|
16
|
+
}
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
return [serializeValue(selector[field])];
|
|
20
|
+
}
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
export {
|
|
24
|
+
getMatchingKeys as default
|
|
25
|
+
};
|
package/dist/index28.mjs
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
const expressionKeys = [
|
|
2
|
+
"$eq",
|
|
3
|
+
"$gt",
|
|
4
|
+
"$gte",
|
|
5
|
+
"$lt",
|
|
6
|
+
"$lte",
|
|
7
|
+
"$in",
|
|
8
|
+
"$nin",
|
|
9
|
+
"$ne",
|
|
10
|
+
"$exists",
|
|
11
|
+
"$not",
|
|
12
|
+
"$expr",
|
|
13
|
+
"$jsonSchema",
|
|
14
|
+
"$mod",
|
|
15
|
+
"$regex",
|
|
16
|
+
"$options",
|
|
17
|
+
"$text",
|
|
18
|
+
"$where",
|
|
19
|
+
"$all",
|
|
20
|
+
"$elemMatch",
|
|
21
|
+
"$size",
|
|
22
|
+
"$bitsAllClear",
|
|
23
|
+
"$bitsAllSet",
|
|
24
|
+
"$bitsAnyClear",
|
|
25
|
+
"$bitsAnySet"
|
|
26
|
+
];
|
|
27
|
+
function isFieldExpression(expression) {
|
|
28
|
+
if (typeof expression !== "object" || expression == null) {
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
const keys = Object.keys(expression);
|
|
32
|
+
if (keys.length === 0) {
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
const hasInvalidKeys = keys.some((key) => !expressionKeys.includes(key));
|
|
36
|
+
if (hasInvalidKeys)
|
|
37
|
+
return false;
|
|
38
|
+
const hasValidKeys = keys.every((key) => expressionKeys.includes(key));
|
|
39
|
+
return hasValidKeys;
|
|
40
|
+
}
|
|
41
|
+
export {
|
|
42
|
+
isFieldExpression as default
|
|
43
|
+
};
|
package/dist/index29.mjs
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
function set(obj, path, value, deleteIfUndefined = false) {
|
|
2
|
+
if (obj == null)
|
|
3
|
+
return obj;
|
|
4
|
+
const segments = path.split(/[.[\]]/g);
|
|
5
|
+
if (segments[0] === "")
|
|
6
|
+
segments.shift();
|
|
7
|
+
if (segments[segments.length - 1] === "")
|
|
8
|
+
segments.pop();
|
|
9
|
+
const apply = (node) => {
|
|
10
|
+
if (segments.length > 1) {
|
|
11
|
+
const key = segments.shift();
|
|
12
|
+
const nextIsNum = !Number.isNaN(parseInt(segments[0], 10));
|
|
13
|
+
if (node[key] === void 0) {
|
|
14
|
+
node[key] = nextIsNum ? [] : {};
|
|
15
|
+
}
|
|
16
|
+
apply(node[key]);
|
|
17
|
+
} else {
|
|
18
|
+
if (deleteIfUndefined && value === void 0) {
|
|
19
|
+
delete node[segments[0]];
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
node[segments[0]] = value;
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
apply(obj);
|
|
26
|
+
return obj;
|
|
27
|
+
}
|
|
28
|
+
export {
|
|
29
|
+
set as default
|
|
30
|
+
};
|
package/dist/index3.mjs
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import ReplicatedCollection from "./index20.mjs";
|
|
2
|
+
import createSignal from "./index17.mjs";
|
|
3
|
+
class AutoFetchCollection extends ReplicatedCollection {
|
|
4
|
+
/**
|
|
5
|
+
* @param options {Object} - Options for the collection.
|
|
6
|
+
* @param options.fetchQueryItems {Function} - A function that fetches items from the server. It takes the selector as an argument and returns a promise that resolves to an object with an `items` property.
|
|
7
|
+
* @param options.purgeDelay {Number} - The delay in milliseconds before purging an item from the cache.
|
|
8
|
+
*/
|
|
9
|
+
constructor(options) {
|
|
10
|
+
var _a, _b, _c, _d;
|
|
11
|
+
let triggerRemoteChange;
|
|
12
|
+
super(Object.assign(Object.assign({}, options), { pull: () => Promise.resolve({
|
|
13
|
+
items: [...this.itemsCache.values()].reduce((memo, items) => {
|
|
14
|
+
const newItems = [...memo];
|
|
15
|
+
items.forEach((item) => {
|
|
16
|
+
const index = newItems.findIndex((i) => i.id === item.id);
|
|
17
|
+
if (index === -1) {
|
|
18
|
+
newItems.push(item);
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
newItems[index] = this.mergeItems(newItems[index], item);
|
|
22
|
+
});
|
|
23
|
+
return newItems;
|
|
24
|
+
}, [])
|
|
25
|
+
}), registerRemoteChange: async (onChange) => {
|
|
26
|
+
triggerRemoteChange = onChange;
|
|
27
|
+
return Promise.resolve();
|
|
28
|
+
} }));
|
|
29
|
+
this.activeObservers = /* @__PURE__ */ new Map();
|
|
30
|
+
this.observerTimeouts = /* @__PURE__ */ new Map();
|
|
31
|
+
this.idQueryCache = /* @__PURE__ */ new Map();
|
|
32
|
+
this.itemsCache = /* @__PURE__ */ new Map();
|
|
33
|
+
this.triggerReload = null;
|
|
34
|
+
this.reactivityAdapter = null;
|
|
35
|
+
this.loadingSignals = /* @__PURE__ */ new Map();
|
|
36
|
+
this.mergeItems = (_a = options.mergeItems) !== null && _a !== void 0 ? _a : (itemA, itemB) => Object.assign(Object.assign({}, itemA), itemB);
|
|
37
|
+
this.purgeDelay = (_b = options.purgeDelay) !== null && _b !== void 0 ? _b : 1e4;
|
|
38
|
+
this.isFetchingSignal = createSignal((_c = options.reactivity) === null || _c === void 0 ? void 0 : _c.create(), false);
|
|
39
|
+
if (!triggerRemoteChange)
|
|
40
|
+
throw new Error("No triggerRemoteChange method found. Looks like your persistence adapter was not registered");
|
|
41
|
+
this.triggerReload = triggerRemoteChange;
|
|
42
|
+
this.reactivityAdapter = (_d = options.reactivity) !== null && _d !== void 0 ? _d : null;
|
|
43
|
+
this.fetchQueryItems = options.fetchQueryItems;
|
|
44
|
+
this.on("observer.created", (selector) => this.handleObserverCreation(selector !== null && selector !== void 0 ? selector : {}));
|
|
45
|
+
this.on("observer.disposed", (selector) => setTimeout(() => this.handleObserverDisposal(selector !== null && selector !== void 0 ? selector : {}), 100));
|
|
46
|
+
if (options.registerRemoteChange) {
|
|
47
|
+
void options.registerRemoteChange(() => this.forceRefetch());
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Registers a query manually that items should be fetched for it
|
|
52
|
+
* @param selector {Object} Selector of the query
|
|
53
|
+
*/
|
|
54
|
+
registerQuery(selector) {
|
|
55
|
+
this.handleObserverCreation(selector);
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Unregisters a query manually that items are not fetched anymore for it
|
|
59
|
+
* @param selector {Object} Selector of the query
|
|
60
|
+
*/
|
|
61
|
+
unregisterQuery(selector) {
|
|
62
|
+
this.handleObserverDisposal(selector);
|
|
63
|
+
}
|
|
64
|
+
// eslint-disable-next-line class-methods-use-this
|
|
65
|
+
getKeyForSelector(selector) {
|
|
66
|
+
return JSON.stringify(selector);
|
|
67
|
+
}
|
|
68
|
+
async forceRefetch() {
|
|
69
|
+
return Promise.all([...this.activeObservers.values()].map(({ selector }) => this.fetchSelector(selector))).then(() => {
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
fetchSelector(selector) {
|
|
73
|
+
this.isFetchingSignal.set(true);
|
|
74
|
+
return this.fetchQueryItems(selector).then((response) => {
|
|
75
|
+
if (!response.items)
|
|
76
|
+
throw new Error("AutoFetchCollection currently only works with a full item response");
|
|
77
|
+
this.itemsCache.set(this.getKeyForSelector(selector), response.items);
|
|
78
|
+
response.items.forEach((item) => {
|
|
79
|
+
var _a;
|
|
80
|
+
const queries = (_a = this.idQueryCache.get(item.id)) !== null && _a !== void 0 ? _a : [];
|
|
81
|
+
queries.push(selector);
|
|
82
|
+
this.idQueryCache.set(item.id, queries);
|
|
83
|
+
});
|
|
84
|
+
this.setLoading(selector, true);
|
|
85
|
+
this.once("persistence.received", () => {
|
|
86
|
+
this.setLoading(selector, false);
|
|
87
|
+
});
|
|
88
|
+
if (!this.triggerReload)
|
|
89
|
+
throw new Error("No triggerReload method found. Looks like your persistence adapter was not registered");
|
|
90
|
+
void this.triggerReload();
|
|
91
|
+
}).catch((error) => {
|
|
92
|
+
this.emit("persistence.error", error);
|
|
93
|
+
}).finally(() => {
|
|
94
|
+
this.isFetchingSignal.set(false);
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
handleObserverCreation(selector) {
|
|
98
|
+
var _a, _b;
|
|
99
|
+
const activeObservers = (_b = (_a = this.activeObservers.get(this.getKeyForSelector(selector))) === null || _a === void 0 ? void 0 : _a.count) !== null && _b !== void 0 ? _b : 0;
|
|
100
|
+
this.activeObservers.set(this.getKeyForSelector(selector), {
|
|
101
|
+
selector,
|
|
102
|
+
count: activeObservers + 1
|
|
103
|
+
});
|
|
104
|
+
const timeout = this.observerTimeouts.get(this.getKeyForSelector(selector));
|
|
105
|
+
if (timeout)
|
|
106
|
+
clearTimeout(timeout);
|
|
107
|
+
if (activeObservers === 0)
|
|
108
|
+
void this.fetchSelector(selector);
|
|
109
|
+
}
|
|
110
|
+
handleObserverDisposal(selector) {
|
|
111
|
+
var _a, _b;
|
|
112
|
+
const currentObservers = (_b = (_a = this.activeObservers.get(this.getKeyForSelector(selector))) === null || _a === void 0 ? void 0 : _a.count) !== null && _b !== void 0 ? _b : 0;
|
|
113
|
+
const activeObservers = currentObservers - 1;
|
|
114
|
+
if (activeObservers > 0) {
|
|
115
|
+
this.activeObservers.set(this.getKeyForSelector(selector), {
|
|
116
|
+
selector,
|
|
117
|
+
count: activeObservers
|
|
118
|
+
});
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
const timeout = this.observerTimeouts.get(this.getKeyForSelector(selector));
|
|
122
|
+
if (timeout)
|
|
123
|
+
clearTimeout(timeout);
|
|
124
|
+
const removeObserver = () => {
|
|
125
|
+
this.activeObservers.delete(this.getKeyForSelector(selector));
|
|
126
|
+
this.itemsCache.delete(this.getKeyForSelector(selector));
|
|
127
|
+
if (!this.triggerReload)
|
|
128
|
+
throw new Error("No triggerReload method found. Looks like your persistence adapter was not registered");
|
|
129
|
+
void this.triggerReload();
|
|
130
|
+
};
|
|
131
|
+
if (this.purgeDelay === 0) {
|
|
132
|
+
removeObserver();
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
this.observerTimeouts.set(this.getKeyForSelector(selector), setTimeout(removeObserver, this.purgeDelay));
|
|
136
|
+
}
|
|
137
|
+
ensureSignal(selector) {
|
|
138
|
+
if (!this.reactivityAdapter)
|
|
139
|
+
throw new Error("No reactivity adapter found");
|
|
140
|
+
if (!this.loadingSignals.has(this.getKeyForSelector(selector))) {
|
|
141
|
+
this.loadingSignals.set(this.getKeyForSelector(selector), createSignal(this.reactivityAdapter.create(), false));
|
|
142
|
+
}
|
|
143
|
+
return this.loadingSignals.get(this.getKeyForSelector(selector));
|
|
144
|
+
}
|
|
145
|
+
setLoading(selector, value) {
|
|
146
|
+
const signal = this.ensureSignal(selector);
|
|
147
|
+
signal.set(value);
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Indicates wether a query is currently been loaded
|
|
151
|
+
* ⚡️ this function is reactive!
|
|
152
|
+
* @param selector {Object} Selector of the query
|
|
153
|
+
* @returns The loading state
|
|
154
|
+
*/
|
|
155
|
+
isLoading(selector) {
|
|
156
|
+
const isPushing = this.isPushing();
|
|
157
|
+
if (!selector) {
|
|
158
|
+
return this.isFetchingSignal.get() || isPushing;
|
|
159
|
+
}
|
|
160
|
+
const signal = this.ensureSignal(selector);
|
|
161
|
+
return signal.get() || isPushing;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
export {
|
|
165
|
+
AutoFetchCollection as default
|
|
166
|
+
};
|
package/dist/index30.mjs
ADDED
package/dist/index4.mjs
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import createPersistenceAdapter from "./index7.mjs";
|
|
2
|
+
function createTemporaryFallbackExecutor(firstResolvingPromiseFn, secondResolvingPromiseFn, options) {
|
|
3
|
+
var _a;
|
|
4
|
+
const cacheTimeout = (_a = options === null || options === void 0 ? void 0 : options.cacheTimeout) !== null && _a !== void 0 ? _a : 0;
|
|
5
|
+
let isResolved = false;
|
|
6
|
+
let resolvedValue = null;
|
|
7
|
+
let timeout = null;
|
|
8
|
+
let secondaryPromise = null;
|
|
9
|
+
return (...args) => {
|
|
10
|
+
if (secondaryPromise == null) {
|
|
11
|
+
if (timeout) {
|
|
12
|
+
clearTimeout(timeout);
|
|
13
|
+
timeout = null;
|
|
14
|
+
}
|
|
15
|
+
secondaryPromise = secondResolvingPromiseFn(...args).then((result) => {
|
|
16
|
+
if (cacheTimeout > 0) {
|
|
17
|
+
timeout = setTimeout(() => {
|
|
18
|
+
isResolved = false;
|
|
19
|
+
resolvedValue = null;
|
|
20
|
+
secondaryPromise = null;
|
|
21
|
+
}, cacheTimeout);
|
|
22
|
+
}
|
|
23
|
+
isResolved = true;
|
|
24
|
+
resolvedValue = result;
|
|
25
|
+
if (options === null || options === void 0 ? void 0 : options.onResolve)
|
|
26
|
+
options.onResolve(resolvedValue);
|
|
27
|
+
return result;
|
|
28
|
+
});
|
|
29
|
+
} else if (isResolved) {
|
|
30
|
+
return secondaryPromise;
|
|
31
|
+
}
|
|
32
|
+
return firstResolvingPromiseFn(...args);
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
function combinePersistenceAdapters(slowAdapter, fastAdapter) {
|
|
36
|
+
let handleChange = null;
|
|
37
|
+
const readExecutor = createTemporaryFallbackExecutor(() => fastAdapter.load(), () => slowAdapter.load(), {
|
|
38
|
+
cacheTimeout: 100,
|
|
39
|
+
onResolve: (result) => {
|
|
40
|
+
var _a, _b, _c;
|
|
41
|
+
if (handleChange)
|
|
42
|
+
void handleChange();
|
|
43
|
+
void fastAdapter.save(result.items || [], {
|
|
44
|
+
added: ((_a = result.changes) === null || _a === void 0 ? void 0 : _a.added) || [],
|
|
45
|
+
modified: ((_b = result.changes) === null || _b === void 0 ? void 0 : _b.modified) || [],
|
|
46
|
+
removed: ((_c = result.changes) === null || _c === void 0 ? void 0 : _c.removed) || []
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
return createPersistenceAdapter({
|
|
51
|
+
async register(onChange) {
|
|
52
|
+
handleChange = onChange;
|
|
53
|
+
await Promise.all([slowAdapter.register(onChange), fastAdapter.register(onChange)]);
|
|
54
|
+
},
|
|
55
|
+
async load() {
|
|
56
|
+
const promise = readExecutor();
|
|
57
|
+
return promise;
|
|
58
|
+
},
|
|
59
|
+
async save(items, changes) {
|
|
60
|
+
await Promise.all([
|
|
61
|
+
fastAdapter.save(items, changes),
|
|
62
|
+
slowAdapter.save(items, changes)
|
|
63
|
+
]);
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
export {
|
|
68
|
+
createTemporaryFallbackExecutor,
|
|
69
|
+
combinePersistenceAdapters as default
|
|
70
|
+
};
|
package/dist/index5.mjs
ADDED
package/dist/index6.mjs
ADDED