@byearlybird/starling 0.2.0 → 0.2.1
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 +22 -33
- package/dist/index.js +54 -67
- 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) => {
|
|
@@ -78,12 +70,13 @@ declare const create$1: (iterable?: Iterable<readonly [string, EncodedDocument]>
|
|
|
78
70
|
put(key: string, value: EncodedDocument): void;
|
|
79
71
|
patch(key: string, value: EncodedDocument): void;
|
|
80
72
|
del(key: string, eventstamp: string): void;
|
|
73
|
+
has(key: string): boolean;
|
|
81
74
|
commit(): void;
|
|
82
75
|
rollback(): void;
|
|
83
76
|
};
|
|
84
77
|
};
|
|
85
78
|
declare namespace store_d_exports {
|
|
86
|
-
export { Plugin, PluginHandle, Store, StoreHooks, StoreOnBeforeDelete, StoreOnBeforePatch, StoreOnBeforePut, StoreOnDelete, StoreOnPatch, StoreOnPut,
|
|
79
|
+
export { Plugin, PluginHandle, Store as StarlingStore, StoreHooks, StoreOnBeforeDelete, StoreOnBeforePatch, StoreOnBeforePut, StoreOnDelete, StoreOnPatch, StoreOnPut, StoreTransaction, create };
|
|
87
80
|
}
|
|
88
81
|
type DeepPartial<T$1> = T$1 extends object ? { [P in keyof T$1]?: DeepPartial<T$1[P]> } : T$1;
|
|
89
82
|
/**
|
|
@@ -129,18 +122,15 @@ type StoreHooks<T$1 extends Record<string, unknown>> = {
|
|
|
129
122
|
onPatch?: StoreOnPatch<T$1>;
|
|
130
123
|
onDelete?: StoreOnDelete;
|
|
131
124
|
};
|
|
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
125
|
type StoreTransaction<T$1 extends Record<string, unknown>> = {
|
|
140
126
|
put: (key: string, value: T$1) => void;
|
|
141
127
|
patch: (key: string, value: DeepPartial<T$1>) => void;
|
|
128
|
+
merge: (doc: EncodedDocument) => void;
|
|
142
129
|
del: (key: string) => void;
|
|
143
|
-
|
|
130
|
+
has: (key: string) => boolean;
|
|
131
|
+
commit: (opts?: {
|
|
132
|
+
silent: boolean;
|
|
133
|
+
}) => void;
|
|
144
134
|
rollback: () => void;
|
|
145
135
|
};
|
|
146
136
|
type PluginHandle<T$1 extends Record<string, unknown>> = {
|
|
@@ -155,16 +145,15 @@ type Store<T$1 extends Record<string, unknown>> = {
|
|
|
155
145
|
readonly size: number;
|
|
156
146
|
values: () => IterableIterator<T$1>;
|
|
157
147
|
entries: () => IterableIterator<readonly [string, T$1]>;
|
|
148
|
+
snapshot: () => EncodedDocument[];
|
|
158
149
|
put: (key: string, value: T$1) => void;
|
|
159
150
|
patch: (key: string, value: DeepPartial<T$1>) => void;
|
|
160
151
|
del: (key: string) => void;
|
|
161
152
|
begin: () => StoreTransaction<T$1>;
|
|
162
153
|
use: (plugin: Plugin<T$1>) => Store<T$1>;
|
|
163
|
-
init: () => Promise<
|
|
154
|
+
init: () => Promise<Store<T$1>>;
|
|
164
155
|
dispose: () => Promise<void>;
|
|
165
156
|
};
|
|
166
|
-
declare const create: <T extends Record<string, unknown>>(
|
|
167
|
-
hooks
|
|
168
|
-
}?: StoreOptions<T>) => Store<T>;
|
|
157
|
+
declare const create: <T extends Record<string, unknown>>() => Store<T>;
|
|
169
158
|
//#endregion
|
|
170
|
-
export { clock_d_exports as
|
|
159
|
+
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
|
-
|
|
158
|
-
});
|
|
159
|
-
|
|
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
|
|
157
|
+
"~deletedAt": eventstamp
|
|
167
158
|
});
|
|
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
159
|
|
|
193
160
|
//#endregion
|
|
194
|
-
//#region src/
|
|
195
|
-
var
|
|
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) {
|
|
@@ -228,7 +195,7 @@ const create$2 = (iterable) => {
|
|
|
228
195
|
del(key, eventstamp) {
|
|
229
196
|
const next = cloneMap(readMap);
|
|
230
197
|
const prev = next.get(key);
|
|
231
|
-
if (prev) next.set(key, del
|
|
198
|
+
if (prev) next.set(key, del(prev, eventstamp));
|
|
232
199
|
readMap = next;
|
|
233
200
|
},
|
|
234
201
|
begin() {
|
|
@@ -244,7 +211,11 @@ const create$2 = (iterable) => {
|
|
|
244
211
|
},
|
|
245
212
|
del(key, eventstamp) {
|
|
246
213
|
const prev = staging.get(key);
|
|
247
|
-
if (prev) staging.set(key, del
|
|
214
|
+
if (prev) staging.set(key, del(prev, eventstamp));
|
|
215
|
+
},
|
|
216
|
+
has(key) {
|
|
217
|
+
const doc = staging.get(key);
|
|
218
|
+
return doc !== void 0 && !doc["~deletedAt"];
|
|
248
219
|
},
|
|
249
220
|
commit() {
|
|
250
221
|
if (committed) return;
|
|
@@ -262,7 +233,7 @@ const create$2 = (iterable) => {
|
|
|
262
233
|
//#endregion
|
|
263
234
|
//#region src/store.ts
|
|
264
235
|
var store_exports = /* @__PURE__ */ __export({ create: () => create$1 });
|
|
265
|
-
const create$1 = (
|
|
236
|
+
const create$1 = () => {
|
|
266
237
|
const clock = create();
|
|
267
238
|
const encodeValue = (key, value) => encode(key, value, clock.now());
|
|
268
239
|
const kv = create$2();
|
|
@@ -277,8 +248,8 @@ const create$1 = ({ hooks } = {}) => {
|
|
|
277
248
|
const initializers = /* @__PURE__ */ new Set();
|
|
278
249
|
const disposers = /* @__PURE__ */ new Set();
|
|
279
250
|
const decodeActive = (doc) => {
|
|
280
|
-
if (!doc || doc
|
|
281
|
-
return decode(doc)
|
|
251
|
+
if (!doc || doc["~deletedAt"]) return null;
|
|
252
|
+
return decode(doc)["~data"];
|
|
282
253
|
};
|
|
283
254
|
return {
|
|
284
255
|
get(key) {
|
|
@@ -305,9 +276,12 @@ const create$1 = ({ hooks } = {}) => {
|
|
|
305
276
|
}
|
|
306
277
|
return iterator();
|
|
307
278
|
},
|
|
279
|
+
snapshot() {
|
|
280
|
+
return Array.from(kv.values());
|
|
281
|
+
},
|
|
308
282
|
get size() {
|
|
309
283
|
let count = 0;
|
|
310
|
-
for (const doc of kv.values()) if (doc && !doc
|
|
284
|
+
for (const doc of kv.values()) if (doc && !doc["~deletedAt"]) count++;
|
|
311
285
|
return count;
|
|
312
286
|
},
|
|
313
287
|
put(key, value) {
|
|
@@ -333,14 +307,12 @@ const create$1 = ({ hooks } = {}) => {
|
|
|
333
307
|
const txState = /* @__PURE__ */ new Map();
|
|
334
308
|
return {
|
|
335
309
|
put(key, value) {
|
|
336
|
-
hooks?.onBeforePut?.(key, value);
|
|
337
310
|
for (const fn of listeners.beforePut) fn(key, value);
|
|
338
311
|
tx.put(key, encodeValue(key, value));
|
|
339
312
|
txState.set(key, value);
|
|
340
313
|
putKeyValues.push([key, value]);
|
|
341
314
|
},
|
|
342
315
|
patch(key, value) {
|
|
343
|
-
hooks?.onBeforePatch?.(key, value);
|
|
344
316
|
for (const fn of listeners.beforePatch) fn(key, value);
|
|
345
317
|
tx.patch(key, encode(key, value, clock.now()));
|
|
346
318
|
let baseValue;
|
|
@@ -355,18 +327,32 @@ const create$1 = ({ hooks } = {}) => {
|
|
|
355
327
|
patchKeyValues.push([key, merged]);
|
|
356
328
|
}
|
|
357
329
|
},
|
|
330
|
+
merge(doc) {
|
|
331
|
+
if (doc["~deletedAt"]) {
|
|
332
|
+
this.del(doc["~id"]);
|
|
333
|
+
return;
|
|
334
|
+
}
|
|
335
|
+
if (tx.has(doc["~id"])) tx.patch(doc["~id"], doc);
|
|
336
|
+
else tx.put(doc["~id"], doc);
|
|
337
|
+
const currentDoc = kv.get(doc["~id"]);
|
|
338
|
+
if (currentDoc && !currentDoc["~deletedAt"]) {
|
|
339
|
+
const merged = decode(currentDoc)["~data"];
|
|
340
|
+
txState.set(doc["~id"], merged);
|
|
341
|
+
patchKeyValues.push([doc["~id"], merged]);
|
|
342
|
+
}
|
|
343
|
+
},
|
|
358
344
|
del(key) {
|
|
359
|
-
hooks?.onBeforeDelete?.(key);
|
|
360
345
|
for (const fn of listeners.beforeDel) fn(key);
|
|
361
346
|
if (!(txState.get(key) ?? kv.get(key))) return;
|
|
362
347
|
tx.del(key, clock.now());
|
|
363
348
|
deleteKeys.push(key);
|
|
364
349
|
},
|
|
365
|
-
|
|
350
|
+
has(key) {
|
|
351
|
+
return tx.has(key);
|
|
352
|
+
},
|
|
353
|
+
commit(opts = { silent: false }) {
|
|
366
354
|
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]));
|
|
355
|
+
if (opts.silent) return;
|
|
370
356
|
if (putKeyValues.length > 0) for (const fn of listeners.put) fn(Object.freeze([...putKeyValues]));
|
|
371
357
|
if (patchKeyValues.length > 0) for (const fn of listeners.patch) fn(Object.freeze([...patchKeyValues]));
|
|
372
358
|
if (deleteKeys.length > 0) for (const fn of listeners.del) fn(Object.freeze([...deleteKeys]));
|
|
@@ -428,6 +414,7 @@ const create$1 = ({ hooks } = {}) => {
|
|
|
428
414
|
},
|
|
429
415
|
async init() {
|
|
430
416
|
for (const fn of initializers) await fn();
|
|
417
|
+
return this;
|
|
431
418
|
},
|
|
432
419
|
async dispose() {
|
|
433
420
|
for (const fn of Array.from(disposers).toReversed()) await fn();
|
|
@@ -436,4 +423,4 @@ const create$1 = ({ hooks } = {}) => {
|
|
|
436
423
|
};
|
|
437
424
|
|
|
438
425
|
//#endregion
|
|
439
|
-
export { clock_exports as
|
|
426
|
+
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.1",
|
|
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
|
}
|