@byearlybird/starling 0.1.2 → 0.2.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/chunk-Bp6m_JJh.js +13 -0
- package/dist/index.d.ts +170 -2
- package/dist/index.js +423 -438
- package/package.json +2 -57
- package/README.md +0 -375
- package/dist/core/crdt/clock.d.ts +0 -15
- package/dist/core/crdt/clock.d.ts.map +0 -1
- package/dist/core/crdt/operations.d.ts +0 -8
- package/dist/core/crdt/operations.d.ts.map +0 -1
- package/dist/core/index.d.ts +0 -5
- package/dist/core/index.d.ts.map +0 -1
- package/dist/core/index.js +0 -454
- package/dist/core/shared/types.d.ts +0 -25
- package/dist/core/shared/types.d.ts.map +0 -1
- package/dist/core/shared/utils.d.ts +0 -5
- package/dist/core/shared/utils.d.ts.map +0 -1
- package/dist/core/store/mutations.d.ts +0 -16
- package/dist/core/store/mutations.d.ts.map +0 -1
- package/dist/core/store/query.d.ts +0 -16
- package/dist/core/store/query.d.ts.map +0 -1
- package/dist/core/store/store.d.ts +0 -77
- package/dist/core/store/store.d.ts.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/plugins/index.d.ts +0 -3
- package/dist/plugins/index.d.ts.map +0 -1
- package/dist/plugins/index.js +0 -188
- package/dist/plugins/persistence/index.d.ts +0 -2
- package/dist/plugins/persistence/index.d.ts.map +0 -1
- package/dist/plugins/persistence/unstorage-plugin.d.ts +0 -5
- package/dist/plugins/persistence/unstorage-plugin.d.ts.map +0 -1
- package/dist/plugins/push-pull-plugin.d.ts +0 -13
- package/dist/plugins/push-pull-plugin.d.ts.map +0 -1
- package/dist/plugins/sync/index.d.ts +0 -3
- package/dist/plugins/sync/index.d.ts.map +0 -1
- package/dist/plugins/sync/index.js +0 -65
- package/dist/plugins/sync/push-pull-plugin.d.ts +0 -13
- package/dist/plugins/sync/push-pull-plugin.d.ts.map +0 -1
- package/dist/plugins/unstorage-plugin.d.ts +0 -5
- package/dist/plugins/unstorage-plugin.d.ts.map +0 -1
- package/dist/react/index.d.ts +0 -3
- package/dist/react/index.d.ts.map +0 -1
- package/dist/react/index.js +0 -893
- package/dist/react/use-data.d.ts +0 -9
- package/dist/react/use-data.d.ts.map +0 -1
- package/dist/react/use-query.d.ts +0 -7
- package/dist/react/use-query.d.ts.map +0 -1
- package/dist/solid/index.d.ts +0 -3
- package/dist/solid/index.d.ts.map +0 -1
- package/dist/solid/index.js +0 -299
- package/dist/solid/use-data.d.ts +0 -3
- package/dist/solid/use-data.d.ts.map +0 -1
- package/dist/solid/use-query.d.ts +0 -3
- package/dist/solid/use-query.d.ts.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,454 +1,439 @@
|
|
|
1
|
-
|
|
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);
|
|
1
|
+
import { t as __export } from "./chunk-Bp6m_JJh.js";
|
|
18
2
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
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")}`;
|
|
3
|
+
//#region src/eventstamp.ts
|
|
4
|
+
var eventstamp_exports = /* @__PURE__ */ __export({
|
|
5
|
+
decode: () => decode$3,
|
|
6
|
+
encode: () => encode$3
|
|
7
|
+
});
|
|
8
|
+
const encode$3 = (timestampMs, counter) => {
|
|
9
|
+
return `${new Date(timestampMs).toISOString()}|${counter.toString(16).padStart(8, "0")}`;
|
|
133
10
|
};
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
11
|
+
const decode$3 = (eventstamp) => {
|
|
12
|
+
const pipeIndex = eventstamp.indexOf("|");
|
|
13
|
+
const isoString = eventstamp.slice(0, pipeIndex);
|
|
14
|
+
const hexCounter = eventstamp.slice(pipeIndex + 1);
|
|
15
|
+
return {
|
|
16
|
+
timestampMs: new Date(isoString).getTime(),
|
|
17
|
+
counter: parseInt(hexCounter, 16)
|
|
18
|
+
};
|
|
142
19
|
};
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
20
|
+
|
|
21
|
+
//#endregion
|
|
22
|
+
//#region src/clock.ts
|
|
23
|
+
var clock_exports = /* @__PURE__ */ __export({ create: () => create });
|
|
24
|
+
const create = () => {
|
|
25
|
+
let counter = 0;
|
|
26
|
+
let lastMs = Date.now();
|
|
27
|
+
return {
|
|
28
|
+
now: () => {
|
|
29
|
+
const nowMs = Date.now();
|
|
30
|
+
if (nowMs <= lastMs) counter++;
|
|
31
|
+
else {
|
|
32
|
+
lastMs = nowMs;
|
|
33
|
+
counter = 0;
|
|
34
|
+
}
|
|
35
|
+
return encode$3(nowMs, counter);
|
|
36
|
+
},
|
|
37
|
+
latest() {
|
|
38
|
+
return encode$3(lastMs, counter);
|
|
39
|
+
},
|
|
40
|
+
forward(eventstamp) {
|
|
41
|
+
if (eventstamp > this.latest()) {
|
|
42
|
+
const newer = decode$3(eventstamp);
|
|
43
|
+
lastMs = newer.timestampMs;
|
|
44
|
+
counter = newer.counter;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
};
|
|
169
48
|
};
|
|
170
49
|
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
50
|
+
//#endregion
|
|
51
|
+
//#region src/value.ts
|
|
52
|
+
var value_exports = /* @__PURE__ */ __export({
|
|
53
|
+
decode: () => decode$2,
|
|
54
|
+
encode: () => encode$2,
|
|
55
|
+
isEncoded: () => isEncoded,
|
|
56
|
+
merge: () => merge$2
|
|
57
|
+
});
|
|
58
|
+
const encode$2 = (value, eventstamp) => ({
|
|
59
|
+
__value: value,
|
|
60
|
+
__eventstamp: eventstamp
|
|
61
|
+
});
|
|
62
|
+
const decode$2 = (value) => value.__value;
|
|
63
|
+
const merge$2 = (into, from$1) => ({
|
|
64
|
+
__value: into.__eventstamp > from$1.__eventstamp ? into.__value : from$1.__value,
|
|
65
|
+
__eventstamp: into.__eventstamp > from$1.__eventstamp ? into.__eventstamp : from$1.__eventstamp
|
|
66
|
+
});
|
|
67
|
+
const isEncoded = (value) => !!(typeof value === "object" && value !== null && "__value" in value && "__eventstamp" in value);
|
|
68
|
+
|
|
69
|
+
//#endregion
|
|
70
|
+
//#region src/record.ts
|
|
71
|
+
var record_exports = /* @__PURE__ */ __export({
|
|
72
|
+
decode: () => decode$1,
|
|
73
|
+
encode: () => encode$1,
|
|
74
|
+
merge: () => merge$1
|
|
75
|
+
});
|
|
76
|
+
const isObject = (value) => !!(value != null && typeof value === "object" && !Array.isArray(value) && Object.getPrototypeOf(value) === Object.prototype);
|
|
77
|
+
const encode$1 = (obj, eventstamp) => {
|
|
78
|
+
const result = {};
|
|
79
|
+
const step = (target, output) => {
|
|
80
|
+
for (const key in target) {
|
|
81
|
+
if (!Object.hasOwn(target, key)) continue;
|
|
82
|
+
const value = target[key];
|
|
83
|
+
if (isObject(value)) {
|
|
84
|
+
output[key] = {};
|
|
85
|
+
step(value, output[key]);
|
|
86
|
+
} else output[key] = encode$2(value, eventstamp);
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
step(obj, result);
|
|
90
|
+
return result;
|
|
91
|
+
};
|
|
92
|
+
const decode$1 = (obj) => {
|
|
93
|
+
const result = {};
|
|
94
|
+
const step = (source, output) => {
|
|
95
|
+
for (const key in source) {
|
|
96
|
+
if (!Object.hasOwn(source, key)) continue;
|
|
97
|
+
const value = source[key];
|
|
98
|
+
if (isEncoded(value)) output[key] = decode$2(value);
|
|
99
|
+
else if (isObject(value)) {
|
|
100
|
+
output[key] = {};
|
|
101
|
+
step(value, output[key]);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
step(obj, result);
|
|
106
|
+
return result;
|
|
107
|
+
};
|
|
108
|
+
const merge$1 = (into, from$1) => {
|
|
109
|
+
const result = {};
|
|
110
|
+
const step = (v1, v2, output) => {
|
|
111
|
+
for (const key in v1) {
|
|
112
|
+
if (!Object.hasOwn(v1, key)) continue;
|
|
113
|
+
const value1 = v1[key];
|
|
114
|
+
const value2 = v2[key];
|
|
115
|
+
if (isEncoded(value1) && isEncoded(value2)) output[key] = merge$2(value1, value2);
|
|
116
|
+
else if (isEncoded(value1)) output[key] = value1;
|
|
117
|
+
else if (isObject(value1) && isObject(value2)) {
|
|
118
|
+
output[key] = {};
|
|
119
|
+
step(value1, value2, output[key]);
|
|
120
|
+
} else if (value1) output[key] = value1;
|
|
121
|
+
}
|
|
122
|
+
for (const key in v2) {
|
|
123
|
+
if (!Object.hasOwn(v2, key) || Object.hasOwn(output, key)) continue;
|
|
124
|
+
const value = v2[key];
|
|
125
|
+
if (value !== void 0) output[key] = value;
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
step(into, from$1, result);
|
|
129
|
+
return result;
|
|
187
130
|
};
|
|
188
131
|
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
132
|
+
//#endregion
|
|
133
|
+
//#region src/document.ts
|
|
134
|
+
var document_exports = /* @__PURE__ */ __export({
|
|
135
|
+
decode: () => decode,
|
|
136
|
+
del: () => del$1,
|
|
137
|
+
encode: () => encode,
|
|
138
|
+
merge: () => merge
|
|
139
|
+
});
|
|
140
|
+
const encode = (id, obj, eventstamp, deletedAt = null) => ({
|
|
141
|
+
__id: id,
|
|
142
|
+
__data: encode$1(obj, eventstamp),
|
|
143
|
+
__deletedAt: deletedAt
|
|
144
|
+
});
|
|
145
|
+
const decode = (doc) => ({
|
|
146
|
+
__id: doc.__id,
|
|
147
|
+
__data: decode$1(doc.__data),
|
|
148
|
+
__deletedAt: doc.__deletedAt
|
|
149
|
+
});
|
|
150
|
+
const merge = (into, from$1) => ({
|
|
151
|
+
__id: into.__id,
|
|
152
|
+
__data: merge$1(into.__data, from$1.__data),
|
|
153
|
+
__deletedAt: into.__deletedAt && from$1.__deletedAt ? into.__deletedAt > from$1.__deletedAt ? into.__deletedAt : from$1.__deletedAt : into.__deletedAt || from$1.__deletedAt || null
|
|
154
|
+
});
|
|
155
|
+
const del$1 = (doc, eventstamp) => ({
|
|
156
|
+
...doc,
|
|
157
|
+
__deletedAt: eventstamp
|
|
158
|
+
});
|
|
206
159
|
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
160
|
+
//#endregion
|
|
161
|
+
//#region src/collection.ts
|
|
162
|
+
var collection_exports = /* @__PURE__ */ __export({
|
|
163
|
+
del: () => del,
|
|
164
|
+
from: () => from,
|
|
165
|
+
insert: () => insert,
|
|
166
|
+
update: () => update
|
|
167
|
+
});
|
|
168
|
+
const insert = (collection, doc) => {
|
|
169
|
+
if (collection.has(doc.__id)) throw new Error(`Key already exists: ${doc.__id}`);
|
|
170
|
+
return new Map(collection).set(doc.__id, doc);
|
|
215
171
|
};
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
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
|
-
};
|
|
172
|
+
const update = (collection, doc) => {
|
|
173
|
+
const current = collection.get(doc.__id);
|
|
174
|
+
if (!current) throw new Error(`Key not found: ${doc.__id}`);
|
|
175
|
+
const merged = merge(current, doc);
|
|
176
|
+
return new Map(collection).set(merged.__id, merged);
|
|
239
177
|
};
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
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
|
-
};
|
|
178
|
+
const del = (collection, id) => {
|
|
179
|
+
if (!collection.has(id)) throw new Error(`Key not found: ${id}`);
|
|
180
|
+
const final = new Map(collection);
|
|
181
|
+
final.delete(id);
|
|
182
|
+
return final;
|
|
256
183
|
};
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
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
|
-
};
|
|
184
|
+
const from = (docs) => {
|
|
185
|
+
const final = /* @__PURE__ */ new Map();
|
|
186
|
+
for (const doc of docs) {
|
|
187
|
+
if (final.has(doc.__id)) throw new Error(`Duplicate key found: ${doc.__id}`);
|
|
188
|
+
final.set(doc.__id, doc);
|
|
189
|
+
}
|
|
190
|
+
return final;
|
|
294
191
|
};
|
|
295
192
|
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
193
|
+
//#endregion
|
|
194
|
+
//#region src/map.ts
|
|
195
|
+
var map_exports = /* @__PURE__ */ __export({ create: () => create$2 });
|
|
196
|
+
const create$2 = (iterable) => {
|
|
197
|
+
let readMap = new Map(iterable);
|
|
198
|
+
function cloneMap(src) {
|
|
199
|
+
return new Map(src);
|
|
200
|
+
}
|
|
201
|
+
return {
|
|
202
|
+
get(key) {
|
|
203
|
+
return readMap.get(key) ?? null;
|
|
204
|
+
},
|
|
205
|
+
has(key) {
|
|
206
|
+
return readMap.has(key);
|
|
207
|
+
},
|
|
208
|
+
values() {
|
|
209
|
+
return readMap.values();
|
|
210
|
+
},
|
|
211
|
+
entries() {
|
|
212
|
+
return readMap.entries();
|
|
213
|
+
},
|
|
214
|
+
get size() {
|
|
215
|
+
return readMap.size;
|
|
216
|
+
},
|
|
217
|
+
put(key, value) {
|
|
218
|
+
const next = cloneMap(readMap);
|
|
219
|
+
next.set(key, value);
|
|
220
|
+
readMap = next;
|
|
221
|
+
},
|
|
222
|
+
patch(key, value) {
|
|
223
|
+
const next = cloneMap(readMap);
|
|
224
|
+
const prev = next.get(key);
|
|
225
|
+
next.set(key, prev ? merge(prev, value) : value);
|
|
226
|
+
readMap = next;
|
|
227
|
+
},
|
|
228
|
+
del(key, eventstamp) {
|
|
229
|
+
const next = cloneMap(readMap);
|
|
230
|
+
const prev = next.get(key);
|
|
231
|
+
if (prev) next.set(key, del$1(prev, eventstamp));
|
|
232
|
+
readMap = next;
|
|
233
|
+
},
|
|
234
|
+
begin() {
|
|
235
|
+
const staging = cloneMap(readMap);
|
|
236
|
+
let committed = false;
|
|
237
|
+
return {
|
|
238
|
+
put(key, value) {
|
|
239
|
+
staging.set(key, value);
|
|
240
|
+
},
|
|
241
|
+
patch(key, value) {
|
|
242
|
+
const prev = staging.get(key);
|
|
243
|
+
staging.set(key, prev ? merge(prev, value) : value);
|
|
244
|
+
},
|
|
245
|
+
del(key, eventstamp) {
|
|
246
|
+
const prev = staging.get(key);
|
|
247
|
+
if (prev) staging.set(key, del$1(prev, eventstamp));
|
|
248
|
+
},
|
|
249
|
+
commit() {
|
|
250
|
+
if (committed) return;
|
|
251
|
+
committed = true;
|
|
252
|
+
readMap = staging;
|
|
253
|
+
},
|
|
254
|
+
rollback() {
|
|
255
|
+
committed = true;
|
|
256
|
+
}
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
};
|
|
328
260
|
};
|
|
329
261
|
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
262
|
+
//#endregion
|
|
263
|
+
//#region src/store.ts
|
|
264
|
+
var store_exports = /* @__PURE__ */ __export({ create: () => create$1 });
|
|
265
|
+
const create$1 = ({ hooks } = {}) => {
|
|
266
|
+
const clock = create();
|
|
267
|
+
const encodeValue = (key, value) => encode(key, value, clock.now());
|
|
268
|
+
const kv = create$2();
|
|
269
|
+
const listeners = {
|
|
270
|
+
beforePut: /* @__PURE__ */ new Set(),
|
|
271
|
+
beforePatch: /* @__PURE__ */ new Set(),
|
|
272
|
+
beforeDel: /* @__PURE__ */ new Set(),
|
|
273
|
+
put: /* @__PURE__ */ new Set(),
|
|
274
|
+
patch: /* @__PURE__ */ new Set(),
|
|
275
|
+
del: /* @__PURE__ */ new Set()
|
|
276
|
+
};
|
|
277
|
+
const initializers = /* @__PURE__ */ new Set();
|
|
278
|
+
const disposers = /* @__PURE__ */ new Set();
|
|
279
|
+
const decodeActive = (doc) => {
|
|
280
|
+
if (!doc || doc.__deletedAt) return null;
|
|
281
|
+
return decode(doc).__data;
|
|
282
|
+
};
|
|
283
|
+
return {
|
|
284
|
+
get(key) {
|
|
285
|
+
return decodeActive(kv.get(key));
|
|
286
|
+
},
|
|
287
|
+
has(key) {
|
|
288
|
+
return decodeActive(kv.get(key)) !== null;
|
|
289
|
+
},
|
|
290
|
+
values() {
|
|
291
|
+
function* iterator() {
|
|
292
|
+
for (const doc of kv.values()) {
|
|
293
|
+
const data = decodeActive(doc);
|
|
294
|
+
if (data) yield data;
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
return iterator();
|
|
298
|
+
},
|
|
299
|
+
entries() {
|
|
300
|
+
function* iterator() {
|
|
301
|
+
for (const [key, doc] of kv.entries()) {
|
|
302
|
+
const data = decodeActive(doc);
|
|
303
|
+
if (data) yield [key, data];
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
return iterator();
|
|
307
|
+
},
|
|
308
|
+
get size() {
|
|
309
|
+
let count = 0;
|
|
310
|
+
for (const doc of kv.values()) if (doc && !doc.__deletedAt) count++;
|
|
311
|
+
return count;
|
|
312
|
+
},
|
|
313
|
+
put(key, value) {
|
|
314
|
+
const tx = this.begin();
|
|
315
|
+
tx.put(key, value);
|
|
316
|
+
tx.commit();
|
|
317
|
+
},
|
|
318
|
+
patch(key, value) {
|
|
319
|
+
const tx = this.begin();
|
|
320
|
+
tx.patch(key, value);
|
|
321
|
+
tx.commit();
|
|
322
|
+
},
|
|
323
|
+
del(key) {
|
|
324
|
+
const tx = this.begin();
|
|
325
|
+
tx.del(key);
|
|
326
|
+
tx.commit();
|
|
327
|
+
},
|
|
328
|
+
begin() {
|
|
329
|
+
const tx = kv.begin();
|
|
330
|
+
const putKeyValues = [];
|
|
331
|
+
const patchKeyValues = [];
|
|
332
|
+
const deleteKeys = [];
|
|
333
|
+
const txState = /* @__PURE__ */ new Map();
|
|
334
|
+
return {
|
|
335
|
+
put(key, value) {
|
|
336
|
+
hooks?.onBeforePut?.(key, value);
|
|
337
|
+
for (const fn of listeners.beforePut) fn(key, value);
|
|
338
|
+
tx.put(key, encodeValue(key, value));
|
|
339
|
+
txState.set(key, value);
|
|
340
|
+
putKeyValues.push([key, value]);
|
|
341
|
+
},
|
|
342
|
+
patch(key, value) {
|
|
343
|
+
hooks?.onBeforePatch?.(key, value);
|
|
344
|
+
for (const fn of listeners.beforePatch) fn(key, value);
|
|
345
|
+
tx.patch(key, encode(key, value, clock.now()));
|
|
346
|
+
let baseValue;
|
|
347
|
+
if (txState.has(key)) baseValue = txState.get(key) ?? null;
|
|
348
|
+
else baseValue = decodeActive(kv.get(key));
|
|
349
|
+
if (baseValue) {
|
|
350
|
+
const merged = {
|
|
351
|
+
...baseValue,
|
|
352
|
+
...value
|
|
353
|
+
};
|
|
354
|
+
txState.set(key, merged);
|
|
355
|
+
patchKeyValues.push([key, merged]);
|
|
356
|
+
}
|
|
357
|
+
},
|
|
358
|
+
del(key) {
|
|
359
|
+
hooks?.onBeforeDelete?.(key);
|
|
360
|
+
for (const fn of listeners.beforeDel) fn(key);
|
|
361
|
+
if (!(txState.get(key) ?? kv.get(key))) return;
|
|
362
|
+
tx.del(key, clock.now());
|
|
363
|
+
deleteKeys.push(key);
|
|
364
|
+
},
|
|
365
|
+
commit() {
|
|
366
|
+
tx.commit();
|
|
367
|
+
if (putKeyValues.length > 0 && hooks?.onPut) hooks.onPut(Object.freeze([...putKeyValues]));
|
|
368
|
+
if (patchKeyValues.length > 0 && hooks?.onPatch) hooks.onPatch(Object.freeze([...patchKeyValues]));
|
|
369
|
+
if (deleteKeys.length > 0 && hooks?.onDelete) hooks.onDelete(Object.freeze([...deleteKeys]));
|
|
370
|
+
if (putKeyValues.length > 0) for (const fn of listeners.put) fn(Object.freeze([...putKeyValues]));
|
|
371
|
+
if (patchKeyValues.length > 0) for (const fn of listeners.patch) fn(Object.freeze([...patchKeyValues]));
|
|
372
|
+
if (deleteKeys.length > 0) for (const fn of listeners.del) fn(Object.freeze([...deleteKeys]));
|
|
373
|
+
},
|
|
374
|
+
rollback() {
|
|
375
|
+
tx.rollback();
|
|
376
|
+
}
|
|
377
|
+
};
|
|
378
|
+
},
|
|
379
|
+
use(plugin) {
|
|
380
|
+
const { hooks: pluginHooks, init, dispose } = plugin(this);
|
|
381
|
+
if (pluginHooks) {
|
|
382
|
+
if (pluginHooks.onBeforePut) {
|
|
383
|
+
const callback = pluginHooks.onBeforePut;
|
|
384
|
+
listeners.beforePut.add(callback);
|
|
385
|
+
disposers.add(() => {
|
|
386
|
+
listeners.beforePut.delete(callback);
|
|
387
|
+
});
|
|
388
|
+
}
|
|
389
|
+
if (pluginHooks.onBeforePatch) {
|
|
390
|
+
const callback = pluginHooks.onBeforePatch;
|
|
391
|
+
listeners.beforePatch.add(callback);
|
|
392
|
+
disposers.add(() => {
|
|
393
|
+
listeners.beforePatch.delete(callback);
|
|
394
|
+
});
|
|
395
|
+
}
|
|
396
|
+
if (pluginHooks.onBeforeDelete) {
|
|
397
|
+
const callback = pluginHooks.onBeforeDelete;
|
|
398
|
+
listeners.beforeDel.add(callback);
|
|
399
|
+
disposers.add(() => {
|
|
400
|
+
listeners.beforeDel.delete(callback);
|
|
401
|
+
});
|
|
402
|
+
}
|
|
403
|
+
if (pluginHooks.onPut) {
|
|
404
|
+
const callback = pluginHooks.onPut;
|
|
405
|
+
listeners.put.add(callback);
|
|
406
|
+
disposers.add(() => {
|
|
407
|
+
listeners.put.delete(callback);
|
|
408
|
+
});
|
|
409
|
+
}
|
|
410
|
+
if (pluginHooks.onPatch) {
|
|
411
|
+
const callback = pluginHooks.onPatch;
|
|
412
|
+
listeners.patch.add(callback);
|
|
413
|
+
disposers.add(() => {
|
|
414
|
+
listeners.patch.delete(callback);
|
|
415
|
+
});
|
|
416
|
+
}
|
|
417
|
+
if (pluginHooks.onDelete) {
|
|
418
|
+
const callback = pluginHooks.onDelete;
|
|
419
|
+
listeners.del.add(callback);
|
|
420
|
+
disposers.add(() => {
|
|
421
|
+
listeners.del.delete(callback);
|
|
422
|
+
});
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
initializers.add(init);
|
|
426
|
+
disposers.add(dispose);
|
|
427
|
+
return this;
|
|
428
|
+
},
|
|
429
|
+
async init() {
|
|
430
|
+
for (const fn of initializers) await fn();
|
|
431
|
+
},
|
|
432
|
+
async dispose() {
|
|
433
|
+
for (const fn of Array.from(disposers).toReversed()) await fn();
|
|
434
|
+
}
|
|
435
|
+
};
|
|
454
436
|
};
|
|
437
|
+
|
|
438
|
+
//#endregion
|
|
439
|
+
export { clock_exports as $clock, collection_exports as $collection, document_exports as $document, eventstamp_exports as $eventsamp, map_exports as $map, record_exports as $record, store_exports as $store, value_exports as $value };
|