@signaldb/core 1.2.3 → 1.3.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/dist/.vite/manifest.json +28 -47
- package/dist/index.cjs.js +12 -18
- package/dist/index.cjs10.js +6 -27
- package/dist/index.cjs11.js +3 -6
- package/dist/index.cjs12.js +136 -3
- package/dist/index.cjs13.js +40 -134
- package/dist/index.cjs14.js +5 -38
- package/dist/index.cjs15.js +42 -2
- package/dist/index.cjs16.js +11 -5
- package/dist/index.cjs17.js +17 -40
- package/dist/index.cjs18.js +262 -11
- package/dist/index.cjs19.js +66 -16
- package/dist/index.cjs2.js +688 -96
- package/dist/index.cjs20.js +71 -247
- package/dist/index.cjs21.js +8 -68
- package/dist/index.cjs22.js +22 -83
- package/dist/index.cjs23.js +144 -8
- package/dist/index.cjs24.js +5 -25
- package/dist/index.cjs25.js +16 -144
- package/dist/index.cjs26.js +27 -5
- package/dist/index.cjs27.js +39 -15
- package/dist/index.cjs28.js +25 -24
- package/dist/index.cjs29.js +7 -40
- package/dist/index.cjs3.js +139 -664
- package/dist/index.cjs4.js +62 -166
- package/dist/index.cjs5.js +3 -67
- package/dist/index.cjs6.js +2 -2
- package/dist/index.cjs7.js +2 -2
- package/dist/index.cjs8.js +2 -2
- package/dist/index.cjs9.js +27 -3
- package/dist/index.d.ts +0 -1
- package/dist/index.mjs +12 -18
- package/dist/index10.mjs +6 -27
- package/dist/index11.mjs +3 -6
- package/dist/index12.mjs +136 -3
- package/dist/index13.mjs +39 -134
- package/dist/index14.mjs +5 -37
- package/dist/index15.mjs +41 -2
- package/dist/index16.mjs +11 -5
- package/dist/index17.mjs +17 -39
- package/dist/index18.mjs +261 -11
- package/dist/index19.mjs +65 -16
- package/dist/index2.mjs +688 -94
- package/dist/index20.mjs +71 -247
- package/dist/index21.mjs +8 -67
- package/dist/index22.mjs +22 -82
- package/dist/index23.mjs +144 -8
- package/dist/index24.mjs +5 -25
- package/dist/index25.mjs +16 -144
- package/dist/index26.mjs +27 -5
- package/dist/index27.mjs +39 -15
- package/dist/index28.mjs +25 -24
- package/dist/index29.mjs +7 -40
- package/dist/index3.mjs +139 -664
- package/dist/index4.mjs +61 -166
- package/dist/index5.mjs +3 -66
- package/dist/index6.mjs +2 -2
- package/dist/index7.mjs +2 -2
- package/dist/index8.mjs +2 -2
- package/dist/index9.mjs +27 -3
- package/dist/types/Selector.d.ts +33 -35
- package/dist/utils/getMatchingKeys.d.ts +11 -6
- package/package.json +1 -1
- package/dist/devtools.d.ts +0 -4
- package/dist/index.cjs30.js +0 -29
- package/dist/index.cjs31.js +0 -9
- package/dist/index30.mjs +0 -30
- package/dist/index31.mjs +0 -10
package/dist/index20.mjs
CHANGED
|
@@ -1,264 +1,88 @@
|
|
|
1
1
|
var __defProp = Object.defineProperty;
|
|
2
2
|
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
3
|
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
import
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
4
|
+
import Collection from "./index2.mjs";
|
|
5
|
+
import combinePersistenceAdapters from "./index4.mjs";
|
|
6
|
+
import createPersistenceAdapter from "./index7.mjs";
|
|
7
|
+
import createSignal from "./index17.mjs";
|
|
8
|
+
function createReplicationAdapter(options) {
|
|
9
|
+
return createPersistenceAdapter({
|
|
10
|
+
async register(onChange) {
|
|
11
|
+
if (!options.registerRemoteChange)
|
|
12
|
+
return;
|
|
13
|
+
await options.registerRemoteChange(onChange);
|
|
14
|
+
},
|
|
15
|
+
load: () => options.pull(),
|
|
16
|
+
save: (items, changes) => {
|
|
17
|
+
if (!options.push)
|
|
18
|
+
throw new Error("Pushing is not configured for this collection. Try to pass a `push` function to the collection options.");
|
|
19
|
+
return options.push(changes, items);
|
|
20
|
+
}
|
|
21
|
+
});
|
|
13
22
|
}
|
|
14
|
-
class
|
|
23
|
+
class ReplicatedCollection extends Collection {
|
|
15
24
|
/**
|
|
16
|
-
* Creates a new instance of the `
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
* @
|
|
20
|
-
* @param
|
|
21
|
-
* @param options -
|
|
22
|
-
* @param options.
|
|
23
|
-
* @param options.
|
|
24
|
-
* @param options.
|
|
25
|
-
* @param options.
|
|
26
|
-
* @param options.
|
|
27
|
-
* @param options.
|
|
28
|
-
* @param options.reactive - A reactivity adapter to enable observing changes in the cursor's result set.
|
|
29
|
-
* @param options.fieldTracking - A boolean to enable fine-grained field tracking for reactivity.
|
|
25
|
+
* Creates a new instance of the `ReplicatedCollection` class.
|
|
26
|
+
* Sets up the replication adapter, combining it with an optional persistence adapter, and
|
|
27
|
+
* initializes signals for tracking remote pull and push operations.
|
|
28
|
+
* @param options - The configuration options for the replicated collection.
|
|
29
|
+
* @param options.pull - A function to fetch data from the remote source.
|
|
30
|
+
* @param options.push - An optional function to send changes and items to the remote source.
|
|
31
|
+
* @param options.registerRemoteChange - An optional function to register a listener for remote changes.
|
|
32
|
+
* @param options.persistence - An optional persistence adapter to combine with replication.
|
|
33
|
+
* @param options.reactivity - A reactivity adapter for observing changes in the collection.
|
|
34
|
+
* @param options.transform - A transformation function to apply to items when retrieving them.
|
|
35
|
+
* @param options.indices - An array of index providers for optimized querying.
|
|
36
|
+
* @param options.enableDebugMode - A boolean to enable or disable debug mode.
|
|
30
37
|
*/
|
|
31
|
-
constructor(
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
return item;
|
|
42
|
-
const depend = this.depend.bind(this);
|
|
43
|
-
return Object.entries(item).reduce((memo, [key, value]) => {
|
|
44
|
-
Object.defineProperty(memo, key, {
|
|
45
|
-
get() {
|
|
46
|
-
depend({
|
|
47
|
-
changedField: (notify) => (changedItem, changedFieldName) => {
|
|
48
|
-
if (changedFieldName !== key || changedItem.id !== item.id)
|
|
49
|
-
return;
|
|
50
|
-
notify();
|
|
51
|
-
}
|
|
52
|
-
});
|
|
53
|
-
return value;
|
|
54
|
-
},
|
|
55
|
-
enumerable: true,
|
|
56
|
-
configurable: true
|
|
57
|
-
});
|
|
58
|
-
return memo;
|
|
59
|
-
}, {});
|
|
60
|
-
}
|
|
61
|
-
transform(rawItem) {
|
|
62
|
-
const item = this.options.fieldTracking ? this.addGetters(rawItem) : rawItem;
|
|
63
|
-
if (!this.options.transform)
|
|
64
|
-
return item;
|
|
65
|
-
return this.options.transform(item);
|
|
66
|
-
}
|
|
67
|
-
getItems() {
|
|
68
|
-
const items = this.getFilteredItems();
|
|
69
|
-
const { sort, skip, limit } = this.options;
|
|
70
|
-
const sorted = sort ? sortItems(items, sort) : items;
|
|
71
|
-
const skipped = skip ? sorted.slice(skip) : sorted;
|
|
72
|
-
const limited = limit ? skipped.slice(0, limit) : skipped;
|
|
73
|
-
const idExcluded = this.options.fields && this.options.fields.id === 0;
|
|
74
|
-
return limited.map((item) => {
|
|
75
|
-
if (!this.options.fields)
|
|
76
|
-
return item;
|
|
77
|
-
return {
|
|
78
|
-
...idExcluded ? {} : { id: item.id },
|
|
79
|
-
...project(item, this.options.fields)
|
|
80
|
-
};
|
|
81
|
-
});
|
|
82
|
-
}
|
|
83
|
-
depend(changeEvents) {
|
|
84
|
-
if (!this.options.reactive)
|
|
85
|
-
return;
|
|
86
|
-
if (!isInReactiveScope(this.options.reactive))
|
|
87
|
-
return;
|
|
88
|
-
const signal = this.options.reactive.create();
|
|
89
|
-
signal.depend();
|
|
90
|
-
const notify = () => signal.notify();
|
|
91
|
-
function buildNotifier(event) {
|
|
92
|
-
const eventHandler = changeEvents[event];
|
|
93
|
-
return (...args) => {
|
|
94
|
-
if (eventHandler === true) {
|
|
95
|
-
notify();
|
|
96
|
-
return;
|
|
38
|
+
constructor(options) {
|
|
39
|
+
var _a, _b;
|
|
40
|
+
const replicationAdapter = createReplicationAdapter({
|
|
41
|
+
registerRemoteChange: options.registerRemoteChange,
|
|
42
|
+
pull: async () => {
|
|
43
|
+
this.isPullingRemoteSignal.set(true);
|
|
44
|
+
try {
|
|
45
|
+
return await options.pull();
|
|
46
|
+
} finally {
|
|
47
|
+
this.isPullingRemoteSignal.set(false);
|
|
97
48
|
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
removed: buildNotifier("removed")
|
|
110
|
-
}, true);
|
|
111
|
-
if (this.options.reactive.onDispose) {
|
|
112
|
-
this.options.reactive.onDispose(() => stop(), signal);
|
|
113
|
-
}
|
|
114
|
-
this.onCleanup(stop);
|
|
115
|
-
}
|
|
116
|
-
ensureObserver() {
|
|
117
|
-
if (!this.observer) {
|
|
118
|
-
const observer = new Observer(() => {
|
|
119
|
-
const requery = () => {
|
|
120
|
-
observer.runChecks(this.getItems());
|
|
121
|
-
};
|
|
122
|
-
const cleanup = this.options.bindEvents && this.options.bindEvents(requery);
|
|
123
|
-
return () => {
|
|
124
|
-
if (cleanup)
|
|
125
|
-
cleanup();
|
|
126
|
-
};
|
|
127
|
-
});
|
|
128
|
-
this.onCleanup(() => observer.stop());
|
|
129
|
-
this.observer = observer;
|
|
130
|
-
}
|
|
131
|
-
return this.observer;
|
|
132
|
-
}
|
|
133
|
-
observeRawChanges(callbacks, skipInitial = false) {
|
|
134
|
-
const observer = this.ensureObserver();
|
|
135
|
-
observer.addCallbacks(callbacks, skipInitial);
|
|
136
|
-
observer.runChecks(this.getItems());
|
|
137
|
-
return () => {
|
|
138
|
-
observer.removeCallbacks(callbacks);
|
|
139
|
-
if (!observer.isEmpty())
|
|
140
|
-
return;
|
|
141
|
-
observer.stop();
|
|
142
|
-
this.observer = void 0;
|
|
143
|
-
};
|
|
144
|
-
}
|
|
145
|
-
/**
|
|
146
|
-
* Cleans up all resources associated with the cursor, such as reactive bindings
|
|
147
|
-
* and event listeners. This method should be called when the cursor is no longer needed
|
|
148
|
-
* to prevent memory leaks.
|
|
149
|
-
*/
|
|
150
|
-
cleanup() {
|
|
151
|
-
this.onCleanupCallbacks.forEach((callback) => {
|
|
152
|
-
callback();
|
|
153
|
-
});
|
|
154
|
-
this.onCleanupCallbacks = [];
|
|
155
|
-
}
|
|
156
|
-
/**
|
|
157
|
-
* Registers a cleanup callback to be executed when the `cleanup` method is called.
|
|
158
|
-
* Useful for managing resources and ensuring proper cleanup of bindings or listeners.
|
|
159
|
-
* @param callback - A function to be executed during cleanup.
|
|
160
|
-
*/
|
|
161
|
-
onCleanup(callback) {
|
|
162
|
-
this.onCleanupCallbacks.push(callback);
|
|
163
|
-
}
|
|
164
|
-
/**
|
|
165
|
-
* Iterates over each item in the cursor's result set, applying the provided callback
|
|
166
|
-
* function to each transformed item.
|
|
167
|
-
* ⚡️ this function is reactive!
|
|
168
|
-
* @param callback - A function to execute for each item in the result set.
|
|
169
|
-
* @param callback.item - The transformed item.
|
|
170
|
-
*/
|
|
171
|
-
forEach(callback) {
|
|
172
|
-
const items = this.getItems();
|
|
173
|
-
this.depend({
|
|
174
|
-
addedBefore: true,
|
|
175
|
-
removed: true,
|
|
176
|
-
movedBefore: true,
|
|
177
|
-
...this.options.fieldTracking ? {} : { changed: true }
|
|
49
|
+
},
|
|
50
|
+
push: options.push ? async (changes, items) => {
|
|
51
|
+
if (!options.push)
|
|
52
|
+
throw new Error("Pushing is not configured for this collection. Try to pass a `push` function to the collection options.");
|
|
53
|
+
this.isPushingRemoteSignal.set(true);
|
|
54
|
+
try {
|
|
55
|
+
await options.push(changes, items);
|
|
56
|
+
} finally {
|
|
57
|
+
this.isPushingRemoteSignal.set(false);
|
|
58
|
+
}
|
|
59
|
+
} : void 0
|
|
178
60
|
});
|
|
179
|
-
|
|
180
|
-
|
|
61
|
+
const persistenceAdapter = (options == null ? void 0 : options.persistence) ? combinePersistenceAdapters(replicationAdapter, options.persistence) : replicationAdapter;
|
|
62
|
+
super({
|
|
63
|
+
...options,
|
|
64
|
+
persistence: persistenceAdapter
|
|
181
65
|
});
|
|
66
|
+
__publicField(this, "isPullingRemoteSignal");
|
|
67
|
+
__publicField(this, "isPushingRemoteSignal");
|
|
68
|
+
this.isPullingRemoteSignal = createSignal((_a = options.reactivity) == null ? void 0 : _a.create(), false);
|
|
69
|
+
this.isPushingRemoteSignal = createSignal((_b = options.reactivity) == null ? void 0 : _b.create(), false);
|
|
182
70
|
}
|
|
183
71
|
/**
|
|
184
|
-
*
|
|
185
|
-
*
|
|
72
|
+
* Checks whether the collection is currently performing any loading operation,
|
|
73
|
+
* including pulling or pushing data from/to the remote source, or standard
|
|
74
|
+
* persistence adapter operations.
|
|
186
75
|
* ⚡️ this function is reactive!
|
|
187
|
-
* @
|
|
188
|
-
* @param callback - A function to execute for each item in the result set.
|
|
189
|
-
* @param callback.item - The transformed item.
|
|
190
|
-
* @returns An array of results after applying the callback to each item.
|
|
191
|
-
*/
|
|
192
|
-
map(callback) {
|
|
193
|
-
const results = [];
|
|
194
|
-
this.forEach((item) => {
|
|
195
|
-
results.push(callback(item));
|
|
196
|
-
});
|
|
197
|
-
return results;
|
|
198
|
-
}
|
|
199
|
-
/**
|
|
200
|
-
* Fetches all transformed items from the cursor's result set as an array.
|
|
201
|
-
* Automatically applies filtering, sorting, and limiting as per the cursor's options.
|
|
202
|
-
* ⚡️ this function is reactive!
|
|
203
|
-
* @returns An array of transformed items in the result set.
|
|
204
|
-
*/
|
|
205
|
-
fetch() {
|
|
206
|
-
return this.map((item) => item);
|
|
207
|
-
}
|
|
208
|
-
/**
|
|
209
|
-
* Counts the total number of items in the cursor's result set after applying
|
|
210
|
-
* filtering and other criteria.
|
|
211
|
-
* ⚡️ this function is reactive!
|
|
212
|
-
* @returns The total number of items in the result set.
|
|
213
|
-
*/
|
|
214
|
-
count() {
|
|
215
|
-
const items = this.getItems();
|
|
216
|
-
this.depend({
|
|
217
|
-
added: true,
|
|
218
|
-
removed: true
|
|
219
|
-
});
|
|
220
|
-
return items.length;
|
|
221
|
-
}
|
|
222
|
-
/**
|
|
223
|
-
* Observes changes to the cursor's result set and triggers the specified callbacks
|
|
224
|
-
* when items are added, removed, or updated. Supports reactivity and transformation.
|
|
225
|
-
* @param callbacks - An object containing the callback functions to handle different change events.
|
|
226
|
-
* @param callbacks.added - Triggered when an item is added to the result set.
|
|
227
|
-
* @param callbacks.removed - Triggered when an item is removed from the result set.
|
|
228
|
-
* @param callbacks.changed - Triggered when an item in the result set is modified.
|
|
229
|
-
* @param callbacks.addedBefore - Triggered when an item is added before another item in the result set.
|
|
230
|
-
* @param callbacks.movedBefore - Triggered when an item is moved before another item in the result set.
|
|
231
|
-
* @param callbacks.changedField - Triggered when a specific field of an item changes.
|
|
232
|
-
* @param skipInitial - A boolean indicating whether to skip the initial notification of the current result set.
|
|
233
|
-
* @returns A function to stop observing changes.
|
|
234
|
-
*/
|
|
235
|
-
observeChanges(callbacks, skipInitial = false) {
|
|
236
|
-
return this.observeRawChanges(Object.entries(callbacks).reduce((memo, [callbackName, callback]) => {
|
|
237
|
-
if (!callback)
|
|
238
|
-
return memo;
|
|
239
|
-
return {
|
|
240
|
-
...memo,
|
|
241
|
-
[callbackName]: (item, before) => {
|
|
242
|
-
const transformedValue = this.transform(item);
|
|
243
|
-
const hasBeforeParameter = before !== void 0;
|
|
244
|
-
const transformedBeforeValue = hasBeforeParameter && before ? this.transform(before) : null;
|
|
245
|
-
return callback(transformedValue, ...hasBeforeParameter ? [transformedBeforeValue] : []);
|
|
246
|
-
}
|
|
247
|
-
};
|
|
248
|
-
}, {}), skipInitial);
|
|
249
|
-
}
|
|
250
|
-
/**
|
|
251
|
-
* Forces the cursor to re-evaluate its result set by re-fetching items
|
|
252
|
-
* from the collection. This is useful when the underlying data or query
|
|
253
|
-
* criteria have changed, and you want to ensure the cursor reflects the latest state.
|
|
76
|
+
* @returns A boolean indicating if the collection is currently loading or synchronizing.
|
|
254
77
|
*/
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
78
|
+
isLoading() {
|
|
79
|
+
const isPullingRemote = this.isPullingRemoteSignal.get();
|
|
80
|
+
const isPushingRemote = this.isPushingRemoteSignal.get();
|
|
81
|
+
const isLoading = super.isLoading();
|
|
82
|
+
return isPullingRemote || isPushingRemote || isLoading;
|
|
259
83
|
}
|
|
260
84
|
}
|
|
261
85
|
export {
|
|
262
|
-
|
|
263
|
-
|
|
86
|
+
createReplicationAdapter,
|
|
87
|
+
ReplicatedCollection as default
|
|
264
88
|
};
|
package/dist/index21.mjs
CHANGED
|
@@ -1,70 +1,11 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
return {
|
|
9
|
-
matched: true,
|
|
10
|
-
positions: [...new Set(memo.matched ? intersection(memo.positions, info.positions) : info.positions)],
|
|
11
|
-
optimizedSelector
|
|
12
|
-
};
|
|
13
|
-
}, {
|
|
14
|
-
matched: false,
|
|
15
|
-
positions: [],
|
|
16
|
-
optimizedSelector: { ...selector }
|
|
17
|
-
});
|
|
18
|
-
}
|
|
19
|
-
function getIndexInfo(indexProviders, selector) {
|
|
20
|
-
if (selector == null || Object.keys(selector).length <= 0) {
|
|
21
|
-
return { matched: false, positions: [], optimizedSelector: selector };
|
|
22
|
-
}
|
|
23
|
-
const { $and, $or, ...rest } = selector;
|
|
24
|
-
const flatInfo = getMergedIndexInfo(indexProviders, rest);
|
|
25
|
-
let { matched, positions } = flatInfo;
|
|
26
|
-
const newSelector = flatInfo.optimizedSelector;
|
|
27
|
-
if (Array.isArray($and)) {
|
|
28
|
-
const $andNew = [];
|
|
29
|
-
for (const sel of $and) {
|
|
30
|
-
const { matched: selMatched, positions: selPositions, optimizedSelector } = getIndexInfo(indexProviders, sel);
|
|
31
|
-
if (selMatched) {
|
|
32
|
-
positions = matched ? intersection(positions, selPositions) : selPositions;
|
|
33
|
-
matched = true;
|
|
34
|
-
if (Object.keys(optimizedSelector).length > 0) {
|
|
35
|
-
$andNew.push(optimizedSelector);
|
|
36
|
-
}
|
|
37
|
-
} else {
|
|
38
|
-
$andNew.push(sel);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
if ($andNew.length > 0)
|
|
42
|
-
newSelector.$and = $andNew;
|
|
43
|
-
}
|
|
44
|
-
if (Array.isArray($or)) {
|
|
45
|
-
const $orNew = [];
|
|
46
|
-
for (const sel of $or) {
|
|
47
|
-
const { matched: selMatched, positions: selPositions, optimizedSelector } = getIndexInfo(indexProviders, sel);
|
|
48
|
-
if (selMatched) {
|
|
49
|
-
positions = [.../* @__PURE__ */ new Set([...positions, ...selPositions])];
|
|
50
|
-
matched = true;
|
|
51
|
-
if (Object.keys(optimizedSelector).length > 0) {
|
|
52
|
-
$orNew.push(optimizedSelector);
|
|
53
|
-
}
|
|
54
|
-
} else {
|
|
55
|
-
$orNew.push(sel);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
if ($orNew.length > 0)
|
|
59
|
-
newSelector.$or = $orNew;
|
|
60
|
-
}
|
|
61
|
-
return {
|
|
62
|
-
matched,
|
|
63
|
-
positions: positions || [],
|
|
64
|
-
optimizedSelector: newSelector
|
|
65
|
-
};
|
|
1
|
+
import { sort } from "fast-sort";
|
|
2
|
+
import get from "./index25.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
|
+
}));
|
|
66
8
|
}
|
|
67
9
|
export {
|
|
68
|
-
|
|
69
|
-
getMergedIndexInfo
|
|
10
|
+
sortItems as default
|
|
70
11
|
};
|
package/dist/index22.mjs
CHANGED
|
@@ -1,88 +1,28 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
async register(onChange) {
|
|
11
|
-
if (!options.registerRemoteChange)
|
|
1
|
+
import get from "./index25.mjs";
|
|
2
|
+
import set from "./index28.mjs";
|
|
3
|
+
function project(item, fields) {
|
|
4
|
+
const allFieldsDeactivated = Object.values(fields).every((value) => value === 0);
|
|
5
|
+
if (allFieldsDeactivated) {
|
|
6
|
+
const result2 = { ...item };
|
|
7
|
+
Object.keys(fields).forEach((key) => {
|
|
8
|
+
const fieldValue = get(item, key);
|
|
9
|
+
if (fieldValue === void 0)
|
|
12
10
|
return;
|
|
13
|
-
|
|
14
|
-
},
|
|
15
|
-
load: () => options.pull(),
|
|
16
|
-
save: (items, changes) => {
|
|
17
|
-
if (!options.push)
|
|
18
|
-
throw new Error("Pushing is not configured for this collection. Try to pass a `push` function to the collection options.");
|
|
19
|
-
return options.push(changes, items);
|
|
20
|
-
}
|
|
21
|
-
});
|
|
22
|
-
}
|
|
23
|
-
class ReplicatedCollection extends Collection {
|
|
24
|
-
/**
|
|
25
|
-
* Creates a new instance of the `ReplicatedCollection` class.
|
|
26
|
-
* Sets up the replication adapter, combining it with an optional persistence adapter, and
|
|
27
|
-
* initializes signals for tracking remote pull and push operations.
|
|
28
|
-
* @param options - The configuration options for the replicated collection.
|
|
29
|
-
* @param options.pull - A function to fetch data from the remote source.
|
|
30
|
-
* @param options.push - An optional function to send changes and items to the remote source.
|
|
31
|
-
* @param options.registerRemoteChange - An optional function to register a listener for remote changes.
|
|
32
|
-
* @param options.persistence - An optional persistence adapter to combine with replication.
|
|
33
|
-
* @param options.reactivity - A reactivity adapter for observing changes in the collection.
|
|
34
|
-
* @param options.transform - A transformation function to apply to items when retrieving them.
|
|
35
|
-
* @param options.indices - An array of index providers for optimized querying.
|
|
36
|
-
* @param options.enableDebugMode - A boolean to enable or disable debug mode.
|
|
37
|
-
*/
|
|
38
|
-
constructor(options) {
|
|
39
|
-
var _a, _b;
|
|
40
|
-
const replicationAdapter = createReplicationAdapter({
|
|
41
|
-
registerRemoteChange: options.registerRemoteChange,
|
|
42
|
-
pull: async () => {
|
|
43
|
-
this.isPullingRemoteSignal.set(true);
|
|
44
|
-
try {
|
|
45
|
-
return await options.pull();
|
|
46
|
-
} finally {
|
|
47
|
-
this.isPullingRemoteSignal.set(false);
|
|
48
|
-
}
|
|
49
|
-
},
|
|
50
|
-
push: options.push ? async (changes, items) => {
|
|
51
|
-
if (!options.push)
|
|
52
|
-
throw new Error("Pushing is not configured for this collection. Try to pass a `push` function to the collection options.");
|
|
53
|
-
this.isPushingRemoteSignal.set(true);
|
|
54
|
-
try {
|
|
55
|
-
await options.push(changes, items);
|
|
56
|
-
} finally {
|
|
57
|
-
this.isPushingRemoteSignal.set(false);
|
|
58
|
-
}
|
|
59
|
-
} : void 0
|
|
60
|
-
});
|
|
61
|
-
const persistenceAdapter = (options == null ? void 0 : options.persistence) ? combinePersistenceAdapters(replicationAdapter, options.persistence) : replicationAdapter;
|
|
62
|
-
super({
|
|
63
|
-
...options,
|
|
64
|
-
persistence: persistenceAdapter
|
|
11
|
+
set(result2, key, void 0, true);
|
|
65
12
|
});
|
|
66
|
-
|
|
67
|
-
__publicField(this, "isPushingRemoteSignal");
|
|
68
|
-
this.isPullingRemoteSignal = createSignal((_a = options.reactivity) == null ? void 0 : _a.create(), false);
|
|
69
|
-
this.isPushingRemoteSignal = createSignal((_b = options.reactivity) == null ? void 0 : _b.create(), false);
|
|
70
|
-
}
|
|
71
|
-
/**
|
|
72
|
-
* Checks whether the collection is currently performing any loading operation,
|
|
73
|
-
* including pulling or pushing data from/to the remote source, or standard
|
|
74
|
-
* persistence adapter operations.
|
|
75
|
-
* ⚡️ this function is reactive!
|
|
76
|
-
* @returns A boolean indicating if the collection is currently loading or synchronizing.
|
|
77
|
-
*/
|
|
78
|
-
isLoading() {
|
|
79
|
-
const isPullingRemote = this.isPullingRemoteSignal.get();
|
|
80
|
-
const isPushingRemote = this.isPushingRemoteSignal.get();
|
|
81
|
-
const isLoading = super.isLoading();
|
|
82
|
-
return isPullingRemote || isPushingRemote || isLoading;
|
|
13
|
+
return result2;
|
|
83
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;
|
|
84
25
|
}
|
|
85
26
|
export {
|
|
86
|
-
|
|
87
|
-
ReplicatedCollection as default
|
|
27
|
+
project as default
|
|
88
28
|
};
|