@byearlybird/starling 0.2.0 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +23 -36
- package/dist/index.js +52 -96
- package/package.json +10 -2
package/dist/index.d.ts
CHANGED
|
@@ -31,29 +31,21 @@ declare const encode$2: <T extends Record<string, unknown>>(obj: T, eventstamp:
|
|
|
31
31
|
declare const decode$2: <T extends Record<string, unknown>>(obj: EncodedRecord) => T;
|
|
32
32
|
declare const merge$1: (into: EncodedRecord, from: EncodedRecord) => EncodedRecord;
|
|
33
33
|
declare namespace document_d_exports {
|
|
34
|
-
export { EncodedDocument, decode$1 as decode, del
|
|
34
|
+
export { EncodedDocument, decode$1 as decode, del, encode$1 as encode, merge };
|
|
35
35
|
}
|
|
36
36
|
type EncodedDocument = {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
37
|
+
"~id": string;
|
|
38
|
+
"~data": EncodedRecord;
|
|
39
|
+
"~deletedAt": string | null;
|
|
40
40
|
};
|
|
41
|
-
declare const encode$1: <T extends
|
|
42
|
-
declare const decode$1: <T extends
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
41
|
+
declare const encode$1: <T extends record_d_exports<string, unknown>>(id: string, obj: T, eventstamp: string, deletedAt?: string | null) => EncodedDocument;
|
|
42
|
+
declare const decode$1: <T extends record_d_exports<string, unknown>>(doc: EncodedDocument) => {
|
|
43
|
+
"~id": string;
|
|
44
|
+
"~data": T;
|
|
45
|
+
"~deletedAt": string | null;
|
|
46
46
|
};
|
|
47
47
|
declare const merge: (into: EncodedDocument, from: EncodedDocument) => EncodedDocument;
|
|
48
|
-
declare const del
|
|
49
|
-
declare namespace collection_d_exports {
|
|
50
|
-
export { Collection, del, from, insert, update };
|
|
51
|
-
}
|
|
52
|
-
type Collection = Map<string, EncodedDocument>;
|
|
53
|
-
declare const insert: (collection: Collection, doc: EncodedDocument) => Collection;
|
|
54
|
-
declare const update: (collection: Collection, doc: EncodedDocument) => Collection;
|
|
55
|
-
declare const del: (collection: Collection, id: string) => Collection;
|
|
56
|
-
declare const from: (docs: EncodedDocument[]) => Collection;
|
|
48
|
+
declare const del: (doc: EncodedDocument, eventstamp: string) => EncodedDocument;
|
|
57
49
|
declare namespace eventstamp_d_exports {
|
|
58
50
|
export { decode, encode };
|
|
59
51
|
}
|
|
@@ -62,7 +54,7 @@ declare const decode: (eventstamp: string) => {
|
|
|
62
54
|
timestampMs: number;
|
|
63
55
|
counter: number;
|
|
64
56
|
};
|
|
65
|
-
declare namespace
|
|
57
|
+
declare namespace kv_d_exports {
|
|
66
58
|
export { create$1 as create };
|
|
67
59
|
}
|
|
68
60
|
declare const create$1: (iterable?: Iterable<readonly [string, EncodedDocument]> | null) => {
|
|
@@ -71,19 +63,18 @@ declare const create$1: (iterable?: Iterable<readonly [string, EncodedDocument]>
|
|
|
71
63
|
values(): MapIterator<EncodedDocument>;
|
|
72
64
|
entries(): MapIterator<[string, EncodedDocument]>;
|
|
73
65
|
readonly size: number;
|
|
74
|
-
put(key: string, value: EncodedDocument): void;
|
|
75
|
-
patch(key: string, value: EncodedDocument): void;
|
|
76
|
-
del(key: string, eventstamp: string): void;
|
|
77
66
|
begin(): {
|
|
67
|
+
get(key: string): EncodedDocument | null;
|
|
78
68
|
put(key: string, value: EncodedDocument): void;
|
|
79
69
|
patch(key: string, value: EncodedDocument): void;
|
|
80
70
|
del(key: string, eventstamp: string): void;
|
|
71
|
+
has(key: string): boolean;
|
|
81
72
|
commit(): void;
|
|
82
73
|
rollback(): void;
|
|
83
74
|
};
|
|
84
75
|
};
|
|
85
76
|
declare namespace store_d_exports {
|
|
86
|
-
export { Plugin, PluginHandle, Store, StoreHooks, StoreOnBeforeDelete, StoreOnBeforePatch, StoreOnBeforePut, StoreOnDelete, StoreOnPatch, StoreOnPut,
|
|
77
|
+
export { Plugin, PluginHandle, Store as StarlingStore, StoreHooks, StoreOnBeforeDelete, StoreOnBeforePatch, StoreOnBeforePut, StoreOnDelete, StoreOnPatch, StoreOnPut, StoreTransaction, create };
|
|
87
78
|
}
|
|
88
79
|
type DeepPartial<T$1> = T$1 extends object ? { [P in keyof T$1]?: DeepPartial<T$1[P]> } : T$1;
|
|
89
80
|
/**
|
|
@@ -129,18 +120,15 @@ type StoreHooks<T$1 extends Record<string, unknown>> = {
|
|
|
129
120
|
onPatch?: StoreOnPatch<T$1>;
|
|
130
121
|
onDelete?: StoreOnDelete;
|
|
131
122
|
};
|
|
132
|
-
/**
|
|
133
|
-
* Configuration for Store instance.
|
|
134
|
-
* Hooks receive batches of decoded entries on commit.
|
|
135
|
-
*/
|
|
136
|
-
type StoreOptions<T$1 extends Record<string, unknown>> = {
|
|
137
|
-
hooks?: StoreHooks<T$1>;
|
|
138
|
-
};
|
|
139
123
|
type StoreTransaction<T$1 extends Record<string, unknown>> = {
|
|
140
124
|
put: (key: string, value: T$1) => void;
|
|
141
125
|
patch: (key: string, value: DeepPartial<T$1>) => void;
|
|
126
|
+
merge: (doc: EncodedDocument) => void;
|
|
142
127
|
del: (key: string) => void;
|
|
143
|
-
|
|
128
|
+
has: (key: string) => boolean;
|
|
129
|
+
commit: (opts?: {
|
|
130
|
+
silent: boolean;
|
|
131
|
+
}) => void;
|
|
144
132
|
rollback: () => void;
|
|
145
133
|
};
|
|
146
134
|
type PluginHandle<T$1 extends Record<string, unknown>> = {
|
|
@@ -155,16 +143,15 @@ type Store<T$1 extends Record<string, unknown>> = {
|
|
|
155
143
|
readonly size: number;
|
|
156
144
|
values: () => IterableIterator<T$1>;
|
|
157
145
|
entries: () => IterableIterator<readonly [string, T$1]>;
|
|
146
|
+
snapshot: () => EncodedDocument[];
|
|
158
147
|
put: (key: string, value: T$1) => void;
|
|
159
148
|
patch: (key: string, value: DeepPartial<T$1>) => void;
|
|
160
149
|
del: (key: string) => void;
|
|
161
150
|
begin: () => StoreTransaction<T$1>;
|
|
162
151
|
use: (plugin: Plugin<T$1>) => Store<T$1>;
|
|
163
|
-
init: () => Promise<
|
|
152
|
+
init: () => Promise<Store<T$1>>;
|
|
164
153
|
dispose: () => Promise<void>;
|
|
165
154
|
};
|
|
166
|
-
declare const create: <T extends Record<string, unknown>>(
|
|
167
|
-
hooks
|
|
168
|
-
}?: StoreOptions<T>) => Store<T>;
|
|
155
|
+
declare const create: <T extends Record<string, unknown>>() => Store<T>;
|
|
169
156
|
//#endregion
|
|
170
|
-
export { clock_d_exports as
|
|
157
|
+
export { type clock_d_exports as Clock, document_d_exports as Document, eventstamp_d_exports as Eventstamp, kv_d_exports as KV, record_d_exports as Record, store_d_exports as Store, value_d_exports as Value };
|
package/dist/index.js
CHANGED
|
@@ -60,9 +60,9 @@ const encode$2 = (value, eventstamp) => ({
|
|
|
60
60
|
__eventstamp: eventstamp
|
|
61
61
|
});
|
|
62
62
|
const decode$2 = (value) => value.__value;
|
|
63
|
-
const merge$2 = (into, from
|
|
64
|
-
__value: into.__eventstamp > from
|
|
65
|
-
__eventstamp: into.__eventstamp > from
|
|
63
|
+
const merge$2 = (into, from) => ({
|
|
64
|
+
__value: into.__eventstamp > from.__eventstamp ? into.__value : from.__value,
|
|
65
|
+
__eventstamp: into.__eventstamp > from.__eventstamp ? into.__eventstamp : from.__eventstamp
|
|
66
66
|
});
|
|
67
67
|
const isEncoded = (value) => !!(typeof value === "object" && value !== null && "__value" in value && "__eventstamp" in value);
|
|
68
68
|
|
|
@@ -105,7 +105,7 @@ const decode$1 = (obj) => {
|
|
|
105
105
|
step(obj, result);
|
|
106
106
|
return result;
|
|
107
107
|
};
|
|
108
|
-
const merge$1 = (into, from
|
|
108
|
+
const merge$1 = (into, from) => {
|
|
109
109
|
const result = {};
|
|
110
110
|
const step = (v1, v2, output) => {
|
|
111
111
|
for (const key in v1) {
|
|
@@ -125,7 +125,7 @@ const merge$1 = (into, from$1) => {
|
|
|
125
125
|
if (value !== void 0) output[key] = value;
|
|
126
126
|
}
|
|
127
127
|
};
|
|
128
|
-
step(into, from
|
|
128
|
+
step(into, from, result);
|
|
129
129
|
return result;
|
|
130
130
|
};
|
|
131
131
|
|
|
@@ -133,66 +133,33 @@ const merge$1 = (into, from$1) => {
|
|
|
133
133
|
//#region src/document.ts
|
|
134
134
|
var document_exports = /* @__PURE__ */ __export({
|
|
135
135
|
decode: () => decode,
|
|
136
|
-
del: () => del
|
|
136
|
+
del: () => del,
|
|
137
137
|
encode: () => encode,
|
|
138
138
|
merge: () => merge
|
|
139
139
|
});
|
|
140
140
|
const encode = (id, obj, eventstamp, deletedAt = null) => ({
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
141
|
+
"~id": id,
|
|
142
|
+
"~data": encode$1(obj, eventstamp),
|
|
143
|
+
"~deletedAt": deletedAt
|
|
144
144
|
});
|
|
145
145
|
const decode = (doc) => ({
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
146
|
+
"~id": doc["~id"],
|
|
147
|
+
"~data": decode$1(doc["~data"]),
|
|
148
|
+
"~deletedAt": doc["~deletedAt"]
|
|
149
149
|
});
|
|
150
|
-
const merge = (into, from
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
150
|
+
const merge = (into, from) => ({
|
|
151
|
+
"~id": into["~id"],
|
|
152
|
+
"~data": merge$1(into["~data"], from["~data"]),
|
|
153
|
+
"~deletedAt": into["~deletedAt"] && from["~deletedAt"] ? into["~deletedAt"] > from["~deletedAt"] ? into["~deletedAt"] : from["~deletedAt"] : into["~deletedAt"] || from["~deletedAt"] || null
|
|
154
154
|
});
|
|
155
|
-
const del
|
|
155
|
+
const del = (doc, eventstamp) => ({
|
|
156
156
|
...doc,
|
|
157
|
-
|
|
157
|
+
"~deletedAt": eventstamp
|
|
158
158
|
});
|
|
159
159
|
|
|
160
160
|
//#endregion
|
|
161
|
-
//#region src/
|
|
162
|
-
var
|
|
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);
|
|
171
|
-
};
|
|
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);
|
|
177
|
-
};
|
|
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;
|
|
183
|
-
};
|
|
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;
|
|
191
|
-
};
|
|
192
|
-
|
|
193
|
-
//#endregion
|
|
194
|
-
//#region src/map.ts
|
|
195
|
-
var map_exports = /* @__PURE__ */ __export({ create: () => create$2 });
|
|
161
|
+
//#region src/kv.ts
|
|
162
|
+
var kv_exports = /* @__PURE__ */ __export({ create: () => create$2 });
|
|
196
163
|
const create$2 = (iterable) => {
|
|
197
164
|
let readMap = new Map(iterable);
|
|
198
165
|
function cloneMap(src) {
|
|
@@ -214,27 +181,13 @@ const create$2 = (iterable) => {
|
|
|
214
181
|
get size() {
|
|
215
182
|
return readMap.size;
|
|
216
183
|
},
|
|
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
184
|
begin() {
|
|
235
185
|
const staging = cloneMap(readMap);
|
|
236
186
|
let committed = false;
|
|
237
187
|
return {
|
|
188
|
+
get(key) {
|
|
189
|
+
return staging.get(key) ?? null;
|
|
190
|
+
},
|
|
238
191
|
put(key, value) {
|
|
239
192
|
staging.set(key, value);
|
|
240
193
|
},
|
|
@@ -244,7 +197,10 @@ const create$2 = (iterable) => {
|
|
|
244
197
|
},
|
|
245
198
|
del(key, eventstamp) {
|
|
246
199
|
const prev = staging.get(key);
|
|
247
|
-
if (prev) staging.set(key, del
|
|
200
|
+
if (prev) staging.set(key, del(prev, eventstamp));
|
|
201
|
+
},
|
|
202
|
+
has(key) {
|
|
203
|
+
return staging.get(key) !== void 0;
|
|
248
204
|
},
|
|
249
205
|
commit() {
|
|
250
206
|
if (committed) return;
|
|
@@ -262,7 +218,7 @@ const create$2 = (iterable) => {
|
|
|
262
218
|
//#endregion
|
|
263
219
|
//#region src/store.ts
|
|
264
220
|
var store_exports = /* @__PURE__ */ __export({ create: () => create$1 });
|
|
265
|
-
const create$1 = (
|
|
221
|
+
const create$1 = () => {
|
|
266
222
|
const clock = create();
|
|
267
223
|
const encodeValue = (key, value) => encode(key, value, clock.now());
|
|
268
224
|
const kv = create$2();
|
|
@@ -277,8 +233,8 @@ const create$1 = ({ hooks } = {}) => {
|
|
|
277
233
|
const initializers = /* @__PURE__ */ new Set();
|
|
278
234
|
const disposers = /* @__PURE__ */ new Set();
|
|
279
235
|
const decodeActive = (doc) => {
|
|
280
|
-
if (!doc || doc
|
|
281
|
-
return decode(doc)
|
|
236
|
+
if (!doc || doc["~deletedAt"]) return null;
|
|
237
|
+
return decode(doc)["~data"];
|
|
282
238
|
};
|
|
283
239
|
return {
|
|
284
240
|
get(key) {
|
|
@@ -305,9 +261,12 @@ const create$1 = ({ hooks } = {}) => {
|
|
|
305
261
|
}
|
|
306
262
|
return iterator();
|
|
307
263
|
},
|
|
264
|
+
snapshot() {
|
|
265
|
+
return Array.from(kv.values());
|
|
266
|
+
},
|
|
308
267
|
get size() {
|
|
309
268
|
let count = 0;
|
|
310
|
-
for (const doc of kv.values()) if (doc && !doc
|
|
269
|
+
for (const doc of kv.values()) if (doc && !doc["~deletedAt"]) count++;
|
|
311
270
|
return count;
|
|
312
271
|
},
|
|
313
272
|
put(key, value) {
|
|
@@ -330,43 +289,39 @@ const create$1 = ({ hooks } = {}) => {
|
|
|
330
289
|
const putKeyValues = [];
|
|
331
290
|
const patchKeyValues = [];
|
|
332
291
|
const deleteKeys = [];
|
|
333
|
-
const txState = /* @__PURE__ */ new Map();
|
|
334
292
|
return {
|
|
335
293
|
put(key, value) {
|
|
336
|
-
hooks?.onBeforePut?.(key, value);
|
|
337
294
|
for (const fn of listeners.beforePut) fn(key, value);
|
|
338
295
|
tx.put(key, encodeValue(key, value));
|
|
339
|
-
txState.set(key, value);
|
|
340
296
|
putKeyValues.push([key, value]);
|
|
341
297
|
},
|
|
342
298
|
patch(key, value) {
|
|
343
|
-
hooks?.onBeforePatch?.(key, value);
|
|
344
299
|
for (const fn of listeners.beforePatch) fn(key, value);
|
|
345
300
|
tx.patch(key, encode(key, value, clock.now()));
|
|
346
|
-
|
|
347
|
-
if (
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
patchKeyValues.push([
|
|
301
|
+
const merged = decodeActive(tx.get(key));
|
|
302
|
+
if (merged) patchKeyValues.push([key, merged]);
|
|
303
|
+
},
|
|
304
|
+
merge(doc) {
|
|
305
|
+
if (tx.has(doc["~id"])) tx.patch(doc["~id"], doc);
|
|
306
|
+
else tx.put(doc["~id"], doc);
|
|
307
|
+
const currentDoc = tx.get(doc["~id"]);
|
|
308
|
+
if (currentDoc && !currentDoc["~deletedAt"]) {
|
|
309
|
+
const merged = decode(currentDoc)["~data"];
|
|
310
|
+
patchKeyValues.push([doc["~id"], merged]);
|
|
356
311
|
}
|
|
357
312
|
},
|
|
358
313
|
del(key) {
|
|
359
|
-
hooks?.onBeforeDelete?.(key);
|
|
360
314
|
for (const fn of listeners.beforeDel) fn(key);
|
|
361
|
-
if (!
|
|
315
|
+
if (!tx.get(key)) return;
|
|
362
316
|
tx.del(key, clock.now());
|
|
363
317
|
deleteKeys.push(key);
|
|
364
318
|
},
|
|
365
|
-
|
|
319
|
+
has(key) {
|
|
320
|
+
return tx.has(key);
|
|
321
|
+
},
|
|
322
|
+
commit(opts = { silent: false }) {
|
|
366
323
|
tx.commit();
|
|
367
|
-
if (
|
|
368
|
-
if (patchKeyValues.length > 0 && hooks?.onPatch) hooks.onPatch(Object.freeze([...patchKeyValues]));
|
|
369
|
-
if (deleteKeys.length > 0 && hooks?.onDelete) hooks.onDelete(Object.freeze([...deleteKeys]));
|
|
324
|
+
if (opts.silent) return;
|
|
370
325
|
if (putKeyValues.length > 0) for (const fn of listeners.put) fn(Object.freeze([...putKeyValues]));
|
|
371
326
|
if (patchKeyValues.length > 0) for (const fn of listeners.patch) fn(Object.freeze([...patchKeyValues]));
|
|
372
327
|
if (deleteKeys.length > 0) for (const fn of listeners.del) fn(Object.freeze([...deleteKeys]));
|
|
@@ -428,6 +383,7 @@ const create$1 = ({ hooks } = {}) => {
|
|
|
428
383
|
},
|
|
429
384
|
async init() {
|
|
430
385
|
for (const fn of initializers) await fn();
|
|
386
|
+
return this;
|
|
431
387
|
},
|
|
432
388
|
async dispose() {
|
|
433
389
|
for (const fn of Array.from(disposers).toReversed()) await fn();
|
|
@@ -436,4 +392,4 @@ const create$1 = ({ hooks } = {}) => {
|
|
|
436
392
|
};
|
|
437
393
|
|
|
438
394
|
//#endregion
|
|
439
|
-
export { clock_exports as
|
|
395
|
+
export { clock_exports as Clock, document_exports as Document, eventstamp_exports as Eventstamp, kv_exports as KV, record_exports as Record, store_exports as Store, value_exports as Value };
|
package/package.json
CHANGED
|
@@ -1,10 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@byearlybird/starling",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.2",
|
|
4
4
|
"type": "module",
|
|
5
|
+
"license": "MIT",
|
|
5
6
|
"main": "./dist/index.js",
|
|
6
7
|
"types": "./dist/index.d.ts",
|
|
7
8
|
"files": [
|
|
8
9
|
"dist"
|
|
9
|
-
]
|
|
10
|
+
],
|
|
11
|
+
"scripts": {
|
|
12
|
+
"build": "bun run build.ts",
|
|
13
|
+
"prepublishOnly": "bun run build.ts"
|
|
14
|
+
},
|
|
15
|
+
"publishConfig": {
|
|
16
|
+
"access": "public"
|
|
17
|
+
}
|
|
10
18
|
}
|