@byearlybird/starling 0.1.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/README.md +375 -0
- package/dist/core/crdt/clock.d.ts +15 -0
- package/dist/core/crdt/clock.d.ts.map +1 -0
- package/dist/core/crdt/operations.d.ts +8 -0
- package/dist/core/crdt/operations.d.ts.map +1 -0
- package/dist/core/index.d.ts +5 -0
- package/dist/core/index.d.ts.map +1 -0
- package/dist/core/index.js +454 -0
- package/dist/core/shared/types.d.ts +25 -0
- package/dist/core/shared/types.d.ts.map +1 -0
- package/dist/core/shared/utils.d.ts +5 -0
- package/dist/core/shared/utils.d.ts.map +1 -0
- package/dist/core/store/mutations.d.ts +16 -0
- package/dist/core/store/mutations.d.ts.map +1 -0
- package/dist/core/store/query.d.ts +16 -0
- package/dist/core/store/query.d.ts.map +1 -0
- package/dist/core/store/store.d.ts +77 -0
- package/dist/core/store/store.d.ts.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +454 -0
- package/dist/plugins/index.d.ts +3 -0
- package/dist/plugins/index.d.ts.map +1 -0
- package/dist/plugins/index.js +188 -0
- package/dist/plugins/persistence/index.d.ts +2 -0
- package/dist/plugins/persistence/index.d.ts.map +1 -0
- package/dist/plugins/persistence/unstorage-plugin.d.ts +5 -0
- package/dist/plugins/persistence/unstorage-plugin.d.ts.map +1 -0
- package/dist/plugins/push-pull-plugin.d.ts +13 -0
- package/dist/plugins/push-pull-plugin.d.ts.map +1 -0
- package/dist/plugins/sync/index.d.ts +3 -0
- package/dist/plugins/sync/index.d.ts.map +1 -0
- package/dist/plugins/sync/index.js +65 -0
- package/dist/plugins/sync/push-pull-plugin.d.ts +13 -0
- package/dist/plugins/sync/push-pull-plugin.d.ts.map +1 -0
- package/dist/plugins/unstorage-plugin.d.ts +5 -0
- package/dist/plugins/unstorage-plugin.d.ts.map +1 -0
- package/dist/react/index.d.ts +3 -0
- package/dist/react/index.d.ts.map +1 -0
- package/dist/react/index.js +893 -0
- package/dist/react/use-data.d.ts +9 -0
- package/dist/react/use-data.d.ts.map +1 -0
- package/dist/react/use-query.d.ts +7 -0
- package/dist/react/use-query.d.ts.map +1 -0
- package/dist/solid/index.d.ts +3 -0
- package/dist/solid/index.d.ts.map +1 -0
- package/dist/solid/index.js +299 -0
- package/dist/solid/use-data.d.ts +3 -0
- package/dist/solid/use-data.d.ts.map +1 -0
- package/dist/solid/use-query.d.ts +3 -0
- package/dist/solid/use-query.d.ts.map +1 -0
- package/package.json +65 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,454 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __toESM = (mod, isNodeMode, target) => {
|
|
7
|
+
target = mod != null ? __create(__getProtoOf(mod)) : {};
|
|
8
|
+
const to = isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target;
|
|
9
|
+
for (let key of __getOwnPropNames(mod))
|
|
10
|
+
if (!__hasOwnProp.call(to, key))
|
|
11
|
+
__defProp(to, key, {
|
|
12
|
+
get: () => mod[key],
|
|
13
|
+
enumerable: true
|
|
14
|
+
});
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __commonJS = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports);
|
|
18
|
+
|
|
19
|
+
// lib/core/crdt/operations.ts
|
|
20
|
+
function encode(obj, eventstamp) {
|
|
21
|
+
const result = {};
|
|
22
|
+
function step(target, output) {
|
|
23
|
+
for (const key in target) {
|
|
24
|
+
if (!Object.hasOwn(target, key))
|
|
25
|
+
continue;
|
|
26
|
+
const value = target[key];
|
|
27
|
+
if (value != null && typeof value === "object" && !Array.isArray(value) && Object.getPrototypeOf(value) === Object.prototype) {
|
|
28
|
+
output[key] = {};
|
|
29
|
+
step(value, output[key]);
|
|
30
|
+
} else {
|
|
31
|
+
output[key] = {
|
|
32
|
+
__value: value,
|
|
33
|
+
__eventstamp: eventstamp
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
step(obj, result);
|
|
39
|
+
return result;
|
|
40
|
+
}
|
|
41
|
+
function decode(obj) {
|
|
42
|
+
const result = {};
|
|
43
|
+
function step(source, output) {
|
|
44
|
+
for (const key in source) {
|
|
45
|
+
if (!Object.hasOwn(source, key))
|
|
46
|
+
continue;
|
|
47
|
+
if (key === "__deleted")
|
|
48
|
+
continue;
|
|
49
|
+
const value = source[key];
|
|
50
|
+
if (value && "__value" in value && "__eventstamp" in value) {
|
|
51
|
+
output[key] = value.__value;
|
|
52
|
+
} else if (typeof value === "object" && value !== null && !Array.isArray(value)) {
|
|
53
|
+
output[key] = {};
|
|
54
|
+
step(value, output[key]);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
step(obj, result);
|
|
59
|
+
return result;
|
|
60
|
+
}
|
|
61
|
+
function merge(obj1, obj2) {
|
|
62
|
+
const result = {};
|
|
63
|
+
let changed = false;
|
|
64
|
+
function isEncodedValue(val) {
|
|
65
|
+
return val != null && typeof val === "object" && "__value" in val && "__eventstamp" in val;
|
|
66
|
+
}
|
|
67
|
+
function step(v1, v2, output) {
|
|
68
|
+
for (const key in v1) {
|
|
69
|
+
if (!Object.hasOwn(v1, key))
|
|
70
|
+
continue;
|
|
71
|
+
const value1 = v1[key];
|
|
72
|
+
const value2 = v2[key];
|
|
73
|
+
if (isEncodedValue(value1) && isEncodedValue(value2)) {
|
|
74
|
+
if (value1.__eventstamp >= value2.__eventstamp) {
|
|
75
|
+
output[key] = value1;
|
|
76
|
+
} else {
|
|
77
|
+
output[key] = value2;
|
|
78
|
+
changed = true;
|
|
79
|
+
}
|
|
80
|
+
} else if (isEncodedValue(value1)) {
|
|
81
|
+
output[key] = value1;
|
|
82
|
+
} else if (value1 && value2 && typeof value1 === "object" && typeof value2 === "object") {
|
|
83
|
+
output[key] = {};
|
|
84
|
+
step(value1, value2, output[key]);
|
|
85
|
+
} else if (value1) {
|
|
86
|
+
output[key] = value1;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
for (const key in v2) {
|
|
90
|
+
if (!Object.hasOwn(v2, key) || Object.hasOwn(result, key))
|
|
91
|
+
continue;
|
|
92
|
+
const value = v2[key];
|
|
93
|
+
if (value) {
|
|
94
|
+
output[key] = value;
|
|
95
|
+
changed = true;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
step(obj1, obj2, result);
|
|
100
|
+
return [result, changed];
|
|
101
|
+
}
|
|
102
|
+
function mergeArray(current, updates) {
|
|
103
|
+
const updatesMap = new Map(updates.map((item) => [item.key, item.value]));
|
|
104
|
+
const result = [];
|
|
105
|
+
let changed = false;
|
|
106
|
+
const seenKeys = new Set;
|
|
107
|
+
for (const { key, value: obj1 } of current) {
|
|
108
|
+
seenKeys.add(key);
|
|
109
|
+
const obj2 = updatesMap.get(key);
|
|
110
|
+
if (obj2) {
|
|
111
|
+
const [merged, objChanged] = merge(obj1, obj2);
|
|
112
|
+
result.push({ key, value: merged });
|
|
113
|
+
if (objChanged) {
|
|
114
|
+
changed = true;
|
|
115
|
+
}
|
|
116
|
+
} else {
|
|
117
|
+
result.push({ key, value: obj1 });
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
for (const { key, value: obj2 } of updates) {
|
|
121
|
+
if (!seenKeys.has(key)) {
|
|
122
|
+
result.push({ key, value: obj2 });
|
|
123
|
+
changed = true;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
return [result, changed];
|
|
127
|
+
}
|
|
128
|
+
var encodeMany = (data, eventstampFn) => data.map(({ key, value }) => ({ key, value: encode(value, eventstampFn()) }));
|
|
129
|
+
// lib/core/crdt/clock.ts
|
|
130
|
+
var formatEventstamp = (timestampMs, counter) => {
|
|
131
|
+
const isoString = new Date(timestampMs).toISOString();
|
|
132
|
+
return `${isoString}|${counter.toString(16).padStart(8, "0")}`;
|
|
133
|
+
};
|
|
134
|
+
var parseEventstamp = (eventstamp) => {
|
|
135
|
+
const pipeIndex = eventstamp.indexOf("|");
|
|
136
|
+
const isoString = eventstamp.slice(0, pipeIndex);
|
|
137
|
+
const hexCounter = eventstamp.slice(pipeIndex + 1);
|
|
138
|
+
return {
|
|
139
|
+
timestampMs: new Date(isoString).getTime(),
|
|
140
|
+
counter: parseInt(hexCounter, 16)
|
|
141
|
+
};
|
|
142
|
+
};
|
|
143
|
+
var createClock = () => {
|
|
144
|
+
let counter = 0;
|
|
145
|
+
let lastMs = Date.now();
|
|
146
|
+
return {
|
|
147
|
+
now() {
|
|
148
|
+
const nowMs = Date.now();
|
|
149
|
+
if (nowMs <= lastMs) {
|
|
150
|
+
counter++;
|
|
151
|
+
} else {
|
|
152
|
+
lastMs = nowMs;
|
|
153
|
+
counter = 0;
|
|
154
|
+
}
|
|
155
|
+
return formatEventstamp(nowMs, counter);
|
|
156
|
+
},
|
|
157
|
+
latest() {
|
|
158
|
+
return formatEventstamp(lastMs, counter);
|
|
159
|
+
},
|
|
160
|
+
forward(eventstamp) {
|
|
161
|
+
const latest = this.latest();
|
|
162
|
+
if (eventstamp > latest) {
|
|
163
|
+
const newer = parseEventstamp(eventstamp);
|
|
164
|
+
lastMs = newer.timestampMs;
|
|
165
|
+
counter = newer.counter;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
};
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
// lib/core/shared/utils.ts
|
|
172
|
+
function mapToArray(map) {
|
|
173
|
+
return Array.from(map.entries()).map(([key, value]) => ({ key, value }));
|
|
174
|
+
}
|
|
175
|
+
var mergeItems = (map, items) => {
|
|
176
|
+
const merged = [];
|
|
177
|
+
for (const { key, value } of items) {
|
|
178
|
+
const current = map.get(key);
|
|
179
|
+
if (!current)
|
|
180
|
+
continue;
|
|
181
|
+
const [mergedValue, changed] = merge(current, value);
|
|
182
|
+
if (changed) {
|
|
183
|
+
merged.push({ key, value: mergedValue });
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
return merged;
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
// node_modules/.bun/mitt@3.0.1/node_modules/mitt/dist/mitt.mjs
|
|
190
|
+
function mitt_default(n) {
|
|
191
|
+
return { all: n = n || new Map, on: function(t, e) {
|
|
192
|
+
var i = n.get(t);
|
|
193
|
+
i ? i.push(e) : n.set(t, [e]);
|
|
194
|
+
}, off: function(t, e) {
|
|
195
|
+
var i = n.get(t);
|
|
196
|
+
i && (e ? i.splice(i.indexOf(e) >>> 0, 1) : n.set(t, []));
|
|
197
|
+
}, emit: function(t, e) {
|
|
198
|
+
var i = n.get(t);
|
|
199
|
+
i && i.slice().map(function(n2) {
|
|
200
|
+
n2(e);
|
|
201
|
+
}), (i = n.get("*")) && i.slice().map(function(n2) {
|
|
202
|
+
n2(t, e);
|
|
203
|
+
});
|
|
204
|
+
} };
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// lib/core/store/mutations.ts
|
|
208
|
+
var createPutMany = (map, clock, emitter) => {
|
|
209
|
+
return (data) => {
|
|
210
|
+
encodeMany(data, () => clock.now()).forEach(({ key, value }) => {
|
|
211
|
+
map.set(key, value);
|
|
212
|
+
});
|
|
213
|
+
emitter.emit("put", data);
|
|
214
|
+
};
|
|
215
|
+
};
|
|
216
|
+
var createUpdateMany = (map, clock, emitter) => {
|
|
217
|
+
return (data) => {
|
|
218
|
+
const validData = data.filter((d) => map.has(d.key));
|
|
219
|
+
if (validData.length === 0)
|
|
220
|
+
return;
|
|
221
|
+
const updateEvents = [];
|
|
222
|
+
let anyChanged = false;
|
|
223
|
+
for (const { key, value } of validData) {
|
|
224
|
+
const current = map.get(key);
|
|
225
|
+
if (!current)
|
|
226
|
+
continue;
|
|
227
|
+
const encoded = encode(value, clock.now());
|
|
228
|
+
const [mergedValue, itemChanged] = merge(current, encoded);
|
|
229
|
+
if (itemChanged) {
|
|
230
|
+
map.set(key, mergedValue);
|
|
231
|
+
updateEvents.push({ key, value: decode(mergedValue) });
|
|
232
|
+
anyChanged = true;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
if (!anyChanged)
|
|
236
|
+
return;
|
|
237
|
+
emitter.emit("update", updateEvents);
|
|
238
|
+
};
|
|
239
|
+
};
|
|
240
|
+
var createDeleteMany = (map, clock, emitter) => {
|
|
241
|
+
return (keys) => {
|
|
242
|
+
const validKeys = keys.filter((key) => map.has(key));
|
|
243
|
+
if (validKeys.length === 0)
|
|
244
|
+
return;
|
|
245
|
+
const deletionMarkers = encodeMany(validKeys.map((key) => ({ key, value: { __deleted: true } })), () => clock.now());
|
|
246
|
+
const merged = mergeItems(map, deletionMarkers);
|
|
247
|
+
if (merged.length === 0)
|
|
248
|
+
return;
|
|
249
|
+
const deleteEvents = [];
|
|
250
|
+
merged.forEach(({ key, value }) => {
|
|
251
|
+
map.set(key, value);
|
|
252
|
+
deleteEvents.push({ key });
|
|
253
|
+
});
|
|
254
|
+
emitter.emit("delete", deleteEvents);
|
|
255
|
+
};
|
|
256
|
+
};
|
|
257
|
+
var createMerge = (map, emitter) => {
|
|
258
|
+
return (snapshot, opts = { silent: false }) => {
|
|
259
|
+
const putEvents = [];
|
|
260
|
+
const updateEvents = [];
|
|
261
|
+
const deleteEvents = [];
|
|
262
|
+
for (const { key, value: snapshotValue } of snapshot) {
|
|
263
|
+
const currentValue = map.get(key);
|
|
264
|
+
if (!currentValue) {
|
|
265
|
+
putEvents.push({ key, value: decode(snapshotValue) });
|
|
266
|
+
map.set(key, snapshotValue);
|
|
267
|
+
} else {
|
|
268
|
+
const [mergedValue, changed] = merge(currentValue, snapshotValue);
|
|
269
|
+
if (changed) {
|
|
270
|
+
const wasDeleted = currentValue.__deleted !== undefined;
|
|
271
|
+
const isDeleted = mergedValue.__deleted !== undefined;
|
|
272
|
+
if (!wasDeleted && isDeleted) {
|
|
273
|
+
map.set(key, mergedValue);
|
|
274
|
+
deleteEvents.push({ key });
|
|
275
|
+
} else {
|
|
276
|
+
map.set(key, mergedValue);
|
|
277
|
+
updateEvents.push({ key, value: decode(mergedValue) });
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
if (opts.silent)
|
|
283
|
+
return;
|
|
284
|
+
if (putEvents.length > 0) {
|
|
285
|
+
emitter.emit("put", putEvents);
|
|
286
|
+
}
|
|
287
|
+
if (updateEvents.length > 0) {
|
|
288
|
+
emitter.emit("update", updateEvents);
|
|
289
|
+
}
|
|
290
|
+
if (deleteEvents.length > 0) {
|
|
291
|
+
emitter.emit("delete", deleteEvents);
|
|
292
|
+
}
|
|
293
|
+
};
|
|
294
|
+
};
|
|
295
|
+
|
|
296
|
+
// lib/core/store/query.ts
|
|
297
|
+
var createQuery = (map, queries) => {
|
|
298
|
+
return (predicate) => {
|
|
299
|
+
const results = new Map;
|
|
300
|
+
for (const [key, rawValue] of map.entries()) {
|
|
301
|
+
const value = decode(rawValue);
|
|
302
|
+
if (predicate(value)) {
|
|
303
|
+
results.set(key, value);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
const internal = {
|
|
307
|
+
predicate,
|
|
308
|
+
results,
|
|
309
|
+
callbacks: new Set
|
|
310
|
+
};
|
|
311
|
+
queries.add(internal);
|
|
312
|
+
return {
|
|
313
|
+
results() {
|
|
314
|
+
return results;
|
|
315
|
+
},
|
|
316
|
+
onChange: (callback) => {
|
|
317
|
+
internal.callbacks.add(callback);
|
|
318
|
+
return () => {
|
|
319
|
+
internal.callbacks.delete(callback);
|
|
320
|
+
};
|
|
321
|
+
},
|
|
322
|
+
dispose: () => {
|
|
323
|
+
internal.callbacks.clear();
|
|
324
|
+
queries.delete(internal);
|
|
325
|
+
}
|
|
326
|
+
};
|
|
327
|
+
};
|
|
328
|
+
};
|
|
329
|
+
|
|
330
|
+
// lib/core/store/store.ts
|
|
331
|
+
var createStore = (collectionKey) => {
|
|
332
|
+
const $map = new Map;
|
|
333
|
+
const $emitter = mitt_default();
|
|
334
|
+
const $clock = createClock();
|
|
335
|
+
const $handles = new Set;
|
|
336
|
+
const $queries = new Set;
|
|
337
|
+
$emitter.on("*", (event) => {
|
|
338
|
+
if (event === "change")
|
|
339
|
+
return;
|
|
340
|
+
$emitter.emit("change");
|
|
341
|
+
});
|
|
342
|
+
const runCallbacks = (dirtyQueries) => {
|
|
343
|
+
for (const query2 of dirtyQueries) {
|
|
344
|
+
for (const callback of query2.callbacks) {
|
|
345
|
+
callback();
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
dirtyQueries.clear();
|
|
349
|
+
};
|
|
350
|
+
$emitter.on("put", (data) => {
|
|
351
|
+
const dirtyQueries = new Set;
|
|
352
|
+
for (const query2 of $queries) {
|
|
353
|
+
for (const item of data) {
|
|
354
|
+
if (query2.predicate(item.value)) {
|
|
355
|
+
query2.results.set(item.key, item.value);
|
|
356
|
+
dirtyQueries.add(query2);
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
runCallbacks(dirtyQueries);
|
|
361
|
+
});
|
|
362
|
+
$emitter.on("delete", (data) => {
|
|
363
|
+
const dirtyQueries = new Set;
|
|
364
|
+
for (const query2 of $queries) {
|
|
365
|
+
for (const item of data) {
|
|
366
|
+
if (query2.results.has(item.key)) {
|
|
367
|
+
query2.results.delete(item.key);
|
|
368
|
+
dirtyQueries.add(query2);
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
runCallbacks(dirtyQueries);
|
|
373
|
+
});
|
|
374
|
+
$emitter.on("update", (data) => {
|
|
375
|
+
const dirtyQueries = new Set;
|
|
376
|
+
for (const query2 of $queries) {
|
|
377
|
+
for (const item of data) {
|
|
378
|
+
const matches = query2.predicate(item.value);
|
|
379
|
+
const inResults = query2.results.has(item.key);
|
|
380
|
+
if (matches) {
|
|
381
|
+
query2.results.set(item.key, item.value);
|
|
382
|
+
dirtyQueries.add(query2);
|
|
383
|
+
} else if (inResults) {
|
|
384
|
+
query2.results.delete(item.key);
|
|
385
|
+
dirtyQueries.add(query2);
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
runCallbacks(dirtyQueries);
|
|
390
|
+
});
|
|
391
|
+
const putMany = createPutMany($map, $clock, $emitter);
|
|
392
|
+
const updateMany = createUpdateMany($map, $clock, $emitter);
|
|
393
|
+
const deleteMany = createDeleteMany($map, $clock, $emitter);
|
|
394
|
+
const merge2 = createMerge($map, $emitter);
|
|
395
|
+
const query = createQuery($map, $queries);
|
|
396
|
+
return {
|
|
397
|
+
collectionKey,
|
|
398
|
+
putMany,
|
|
399
|
+
updateMany,
|
|
400
|
+
deleteMany,
|
|
401
|
+
merge: merge2,
|
|
402
|
+
put(key, value) {
|
|
403
|
+
this.putMany([{ key, value }]);
|
|
404
|
+
},
|
|
405
|
+
update(key, value) {
|
|
406
|
+
this.updateMany([{ key, value }]);
|
|
407
|
+
},
|
|
408
|
+
delete(key) {
|
|
409
|
+
this.deleteMany([key]);
|
|
410
|
+
},
|
|
411
|
+
values() {
|
|
412
|
+
const result = [];
|
|
413
|
+
for (const [key, value] of $map) {
|
|
414
|
+
if (!value.__deleted) {
|
|
415
|
+
result.push({ key, value: decode(value) });
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
return result;
|
|
419
|
+
},
|
|
420
|
+
query,
|
|
421
|
+
snapshot() {
|
|
422
|
+
return mapToArray($map);
|
|
423
|
+
},
|
|
424
|
+
on(event, callback) {
|
|
425
|
+
$emitter.on(event, callback);
|
|
426
|
+
return () => {
|
|
427
|
+
$emitter.off(event, callback);
|
|
428
|
+
};
|
|
429
|
+
},
|
|
430
|
+
use(plugin) {
|
|
431
|
+
$handles.add(plugin(this));
|
|
432
|
+
return this;
|
|
433
|
+
},
|
|
434
|
+
async init() {
|
|
435
|
+
for (const handle of $handles) {
|
|
436
|
+
await handle.init();
|
|
437
|
+
}
|
|
438
|
+
return this;
|
|
439
|
+
},
|
|
440
|
+
async dispose() {
|
|
441
|
+
$handles.forEach(async (handle) => {
|
|
442
|
+
await handle.dispose();
|
|
443
|
+
});
|
|
444
|
+
$emitter.all.clear();
|
|
445
|
+
}
|
|
446
|
+
};
|
|
447
|
+
};
|
|
448
|
+
export {
|
|
449
|
+
mergeArray,
|
|
450
|
+
merge,
|
|
451
|
+
encode,
|
|
452
|
+
decode,
|
|
453
|
+
createStore
|
|
454
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../lib/plugins/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AACpD,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC"}
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __toESM = (mod, isNodeMode, target) => {
|
|
7
|
+
target = mod != null ? __create(__getProtoOf(mod)) : {};
|
|
8
|
+
const to = isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target;
|
|
9
|
+
for (let key of __getOwnPropNames(mod))
|
|
10
|
+
if (!__hasOwnProp.call(to, key))
|
|
11
|
+
__defProp(to, key, {
|
|
12
|
+
get: () => mod[key],
|
|
13
|
+
enumerable: true
|
|
14
|
+
});
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __commonJS = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports);
|
|
18
|
+
|
|
19
|
+
// lib/plugins/push-pull-plugin.ts
|
|
20
|
+
var pushPullPlugin = (config) => {
|
|
21
|
+
const {
|
|
22
|
+
push,
|
|
23
|
+
pull,
|
|
24
|
+
preprocess,
|
|
25
|
+
pullInterval = 1000 * 60 * 5,
|
|
26
|
+
immediate = true
|
|
27
|
+
} = config;
|
|
28
|
+
const plugin = (store) => {
|
|
29
|
+
let intervalId = null;
|
|
30
|
+
let unwatch = null;
|
|
31
|
+
async function pullData() {
|
|
32
|
+
const data = await pull();
|
|
33
|
+
const processed = preprocess ? await preprocess("pull", data) : data;
|
|
34
|
+
store.merge(processed);
|
|
35
|
+
}
|
|
36
|
+
async function pushData(data) {
|
|
37
|
+
const processed = preprocess ? await preprocess("push", data) : data;
|
|
38
|
+
await push(processed);
|
|
39
|
+
}
|
|
40
|
+
return {
|
|
41
|
+
init: async () => {
|
|
42
|
+
unwatch = store.on("change", async () => {
|
|
43
|
+
const latest = store.snapshot();
|
|
44
|
+
if (latest.length > 0) {
|
|
45
|
+
await pushData(latest);
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
if (immediate)
|
|
49
|
+
await pullData();
|
|
50
|
+
intervalId = setInterval(pullData, pullInterval);
|
|
51
|
+
},
|
|
52
|
+
dispose: async () => {
|
|
53
|
+
if (intervalId !== null) {
|
|
54
|
+
clearInterval(intervalId);
|
|
55
|
+
intervalId = null;
|
|
56
|
+
}
|
|
57
|
+
unwatch?.();
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
};
|
|
61
|
+
return plugin;
|
|
62
|
+
};
|
|
63
|
+
// node_modules/.bun/unstorage@1.17.1/node_modules/unstorage/dist/shared/unstorage.zVDD2mZo.mjs
|
|
64
|
+
var storageKeyProperties = [
|
|
65
|
+
"has",
|
|
66
|
+
"hasItem",
|
|
67
|
+
"get",
|
|
68
|
+
"getItem",
|
|
69
|
+
"getItemRaw",
|
|
70
|
+
"set",
|
|
71
|
+
"setItem",
|
|
72
|
+
"setItemRaw",
|
|
73
|
+
"del",
|
|
74
|
+
"remove",
|
|
75
|
+
"removeItem",
|
|
76
|
+
"getMeta",
|
|
77
|
+
"setMeta",
|
|
78
|
+
"removeMeta",
|
|
79
|
+
"getKeys",
|
|
80
|
+
"clear",
|
|
81
|
+
"mount",
|
|
82
|
+
"unmount"
|
|
83
|
+
];
|
|
84
|
+
function prefixStorage(storage, base) {
|
|
85
|
+
base = normalizeBaseKey(base);
|
|
86
|
+
if (!base) {
|
|
87
|
+
return storage;
|
|
88
|
+
}
|
|
89
|
+
const nsStorage = { ...storage };
|
|
90
|
+
for (const property of storageKeyProperties) {
|
|
91
|
+
nsStorage[property] = (key = "", ...args) => storage[property](base + key, ...args);
|
|
92
|
+
}
|
|
93
|
+
nsStorage.getKeys = (key = "", ...arguments_) => storage.getKeys(base + key, ...arguments_).then((keys) => keys.map((key2) => key2.slice(base.length)));
|
|
94
|
+
nsStorage.keys = nsStorage.getKeys;
|
|
95
|
+
nsStorage.getItems = async (items, commonOptions) => {
|
|
96
|
+
const prefixedItems = items.map((item) => typeof item === "string" ? base + item : { ...item, key: base + item.key });
|
|
97
|
+
const results = await storage.getItems(prefixedItems, commonOptions);
|
|
98
|
+
return results.map((entry) => ({
|
|
99
|
+
key: entry.key.slice(base.length),
|
|
100
|
+
value: entry.value
|
|
101
|
+
}));
|
|
102
|
+
};
|
|
103
|
+
nsStorage.setItems = async (items, commonOptions) => {
|
|
104
|
+
const prefixedItems = items.map((item) => ({
|
|
105
|
+
key: base + item.key,
|
|
106
|
+
value: item.value,
|
|
107
|
+
options: item.options
|
|
108
|
+
}));
|
|
109
|
+
return storage.setItems(prefixedItems, commonOptions);
|
|
110
|
+
};
|
|
111
|
+
return nsStorage;
|
|
112
|
+
}
|
|
113
|
+
function normalizeKey(key) {
|
|
114
|
+
if (!key) {
|
|
115
|
+
return "";
|
|
116
|
+
}
|
|
117
|
+
return key.split("?")[0]?.replace(/[/\\]/g, ":").replace(/:+/g, ":").replace(/^:|:$/g, "") || "";
|
|
118
|
+
}
|
|
119
|
+
function normalizeBaseKey(base) {
|
|
120
|
+
base = normalizeKey(base);
|
|
121
|
+
return base ? base + ":" : "";
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// node_modules/.bun/unstorage@1.17.1/node_modules/unstorage/dist/index.mjs
|
|
125
|
+
function defineDriver(factory) {
|
|
126
|
+
return factory;
|
|
127
|
+
}
|
|
128
|
+
var DRIVER_NAME = "memory";
|
|
129
|
+
var memory = defineDriver(() => {
|
|
130
|
+
const data = /* @__PURE__ */ new Map;
|
|
131
|
+
return {
|
|
132
|
+
name: DRIVER_NAME,
|
|
133
|
+
getInstance: () => data,
|
|
134
|
+
hasItem(key) {
|
|
135
|
+
return data.has(key);
|
|
136
|
+
},
|
|
137
|
+
getItem(key) {
|
|
138
|
+
return data.get(key) ?? null;
|
|
139
|
+
},
|
|
140
|
+
getItemRaw(key) {
|
|
141
|
+
return data.get(key) ?? null;
|
|
142
|
+
},
|
|
143
|
+
setItem(key, value) {
|
|
144
|
+
data.set(key, value);
|
|
145
|
+
},
|
|
146
|
+
setItemRaw(key, value) {
|
|
147
|
+
data.set(key, value);
|
|
148
|
+
},
|
|
149
|
+
removeItem(key) {
|
|
150
|
+
data.delete(key);
|
|
151
|
+
},
|
|
152
|
+
getKeys() {
|
|
153
|
+
return [...data.keys()];
|
|
154
|
+
},
|
|
155
|
+
clear() {
|
|
156
|
+
data.clear();
|
|
157
|
+
},
|
|
158
|
+
dispose() {
|
|
159
|
+
data.clear();
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
// lib/plugins/unstorage-plugin.ts
|
|
165
|
+
var unstoragePlugin = (baseStorage) => {
|
|
166
|
+
let unwatch = null;
|
|
167
|
+
let storage = null;
|
|
168
|
+
const plugin = (store) => ({
|
|
169
|
+
init: async () => {
|
|
170
|
+
storage = prefixStorage(baseStorage, store.collectionKey);
|
|
171
|
+
unwatch = store.on("change", async () => {
|
|
172
|
+
await storage?.set(store.collectionKey, store.snapshot());
|
|
173
|
+
});
|
|
174
|
+
const persisted = await storage.get(store.collectionKey);
|
|
175
|
+
if (persisted) {
|
|
176
|
+
store.merge(persisted, { silent: true });
|
|
177
|
+
}
|
|
178
|
+
},
|
|
179
|
+
dispose: () => {
|
|
180
|
+
unwatch?.();
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
return plugin;
|
|
184
|
+
};
|
|
185
|
+
export {
|
|
186
|
+
unstoragePlugin,
|
|
187
|
+
pushPullPlugin
|
|
188
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../lib/plugins/persistence/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"unstorage-plugin.d.ts","sourceRoot":"","sources":["../../../lib/plugins/persistence/unstorage-plugin.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,EAAiB,KAAK,OAAO,EAAE,MAAM,WAAW,CAAC;AAExD,QAAA,MAAM,eAAe,GAAI,CAAC,SAAS,MAAM,EAAE,aAAa,OAAO,KAAG,MAAM,CAAC,CAAC,CAyBzE,CAAC;AAEF,OAAO,EAAE,eAAe,EAAE,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { ArrayKV, EncodedObject } from "@core/shared/types";
|
|
2
|
+
import type { Plugin } from "@core/store/store";
|
|
3
|
+
type PushPullConfig = {
|
|
4
|
+
push: (data: ArrayKV<EncodedObject>) => Promise<void>;
|
|
5
|
+
pull: () => Promise<ArrayKV<EncodedObject>>;
|
|
6
|
+
pullInterval?: number;
|
|
7
|
+
preprocess?: (event: "pull" | "push", data: ArrayKV<EncodedObject>) => Promise<ArrayKV<EncodedObject>>;
|
|
8
|
+
immediate?: boolean;
|
|
9
|
+
};
|
|
10
|
+
declare const pushPullPlugin: <TValue extends object>(config: PushPullConfig) => Plugin<TValue>;
|
|
11
|
+
export { pushPullPlugin };
|
|
12
|
+
export type { PushPullConfig };
|
|
13
|
+
//# sourceMappingURL=push-pull-plugin.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"push-pull-plugin.d.ts","sourceRoot":"","sources":["../../lib/plugins/push-pull-plugin.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACjE,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAEhD,KAAK,cAAc,GAAG;IACrB,IAAI,EAAE,CAAC,IAAI,EAAE,OAAO,CAAC,aAAa,CAAC,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IACtD,IAAI,EAAE,MAAM,OAAO,CAAC,OAAO,CAAC,aAAa,CAAC,CAAC,CAAC;IAC5C,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,UAAU,CAAC,EAAE,CACZ,KAAK,EAAE,MAAM,GAAG,MAAM,EACtB,IAAI,EAAE,OAAO,CAAC,aAAa,CAAC,KACxB,OAAO,CAAC,OAAO,CAAC,aAAa,CAAC,CAAC,CAAC;IACrC,SAAS,CAAC,EAAE,OAAO,CAAC;CACpB,CAAC;AAEF,QAAA,MAAM,cAAc,GAAI,MAAM,SAAS,MAAM,EAC5C,QAAQ,cAAc,KACpB,MAAM,CAAC,MAAM,CAiDf,CAAC;AAEF,OAAO,EAAE,cAAc,EAAE,CAAC;AAC1B,YAAY,EAAE,cAAc,EAAE,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../lib/plugins/sync/index.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AACzD,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC"}
|