@byearlybird/starling 0.2.1 → 0.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/index.d.ts CHANGED
@@ -63,10 +63,8 @@ declare const create$1: (iterable?: Iterable<readonly [string, EncodedDocument]>
63
63
  values(): MapIterator<EncodedDocument>;
64
64
  entries(): MapIterator<[string, EncodedDocument]>;
65
65
  readonly size: number;
66
- put(key: string, value: EncodedDocument): void;
67
- patch(key: string, value: EncodedDocument): void;
68
- del(key: string, eventstamp: string): void;
69
66
  begin(): {
67
+ get(key: string): EncodedDocument | null;
70
68
  put(key: string, value: EncodedDocument): void;
71
69
  patch(key: string, value: EncodedDocument): void;
72
70
  del(key: string, eventstamp: string): void;
@@ -76,7 +74,7 @@ declare const create$1: (iterable?: Iterable<readonly [string, EncodedDocument]>
76
74
  };
77
75
  };
78
76
  declare namespace store_d_exports {
79
- export { Plugin, PluginHandle, Store as StarlingStore, StoreHooks, StoreOnBeforeDelete, StoreOnBeforePatch, StoreOnBeforePut, StoreOnDelete, StoreOnPatch, StoreOnPut, StoreTransaction, create };
77
+ export { Plugin, PluginHandle, PluginMethods, Store as StarlingStore, StoreHooks, StoreOnBeforeDelete, StoreOnBeforePatch, StoreOnBeforePut, StoreOnDelete, StoreOnPatch, StoreOnPut, StoreTransaction, create };
80
78
  }
81
79
  type DeepPartial<T$1> = T$1 extends object ? { [P in keyof T$1]?: DeepPartial<T$1[P]> } : T$1;
82
80
  /**
@@ -133,13 +131,15 @@ type StoreTransaction<T$1 extends Record<string, unknown>> = {
133
131
  }) => void;
134
132
  rollback: () => void;
135
133
  };
136
- type PluginHandle<T$1 extends Record<string, unknown>> = {
134
+ type PluginMethods = Record<string, (...args: any[]) => any>;
135
+ type PluginHandle<T$1 extends Record<string, unknown>, M$1 extends PluginMethods = {}> = {
137
136
  init: () => Promise<void> | void;
138
137
  dispose: () => Promise<void> | void;
139
138
  hooks?: StoreHooks<T$1>;
139
+ methods?: M$1;
140
140
  };
141
- type Plugin<T$1 extends Record<string, unknown>> = (store: Store<T$1>) => PluginHandle<T$1>;
142
- type Store<T$1 extends Record<string, unknown>> = {
141
+ type Plugin<T$1 extends Record<string, unknown>, M$1 extends PluginMethods = {}> = (store: Store<T$1, any>) => PluginHandle<T$1, M$1>;
142
+ type Store<T$1 extends Record<string, unknown>, Extended = {}> = {
143
143
  get: (key: string) => T$1 | null;
144
144
  has: (key: string) => boolean;
145
145
  readonly size: number;
@@ -150,10 +150,10 @@ type Store<T$1 extends Record<string, unknown>> = {
150
150
  patch: (key: string, value: DeepPartial<T$1>) => void;
151
151
  del: (key: string) => void;
152
152
  begin: () => StoreTransaction<T$1>;
153
- use: (plugin: Plugin<T$1>) => Store<T$1>;
154
- init: () => Promise<Store<T$1>>;
153
+ use: <M extends PluginMethods>(plugin: Plugin<T$1, M>) => Store<T$1, Extended & M>;
154
+ init: () => Promise<Store<T$1, Extended>>;
155
155
  dispose: () => Promise<void>;
156
- };
157
- declare const create: <T extends Record<string, unknown>>() => Store<T>;
156
+ } & Extended;
157
+ declare const create: <T extends Record<string, unknown>>() => Store<T, {}>;
158
158
  //#endregion
159
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
@@ -181,27 +181,13 @@ const create$2 = (iterable) => {
181
181
  get size() {
182
182
  return readMap.size;
183
183
  },
184
- put(key, value) {
185
- const next = cloneMap(readMap);
186
- next.set(key, value);
187
- readMap = next;
188
- },
189
- patch(key, value) {
190
- const next = cloneMap(readMap);
191
- const prev = next.get(key);
192
- next.set(key, prev ? merge(prev, value) : value);
193
- readMap = next;
194
- },
195
- del(key, eventstamp) {
196
- const next = cloneMap(readMap);
197
- const prev = next.get(key);
198
- if (prev) next.set(key, del(prev, eventstamp));
199
- readMap = next;
200
- },
201
184
  begin() {
202
185
  const staging = cloneMap(readMap);
203
186
  let committed = false;
204
187
  return {
188
+ get(key) {
189
+ return staging.get(key) ?? null;
190
+ },
205
191
  put(key, value) {
206
192
  staging.set(key, value);
207
193
  },
@@ -214,8 +200,7 @@ const create$2 = (iterable) => {
214
200
  if (prev) staging.set(key, del(prev, eventstamp));
215
201
  },
216
202
  has(key) {
217
- const doc = staging.get(key);
218
- return doc !== void 0 && !doc["~deletedAt"];
203
+ return staging.get(key) !== void 0;
219
204
  },
220
205
  commit() {
221
206
  if (committed) return;
@@ -304,46 +289,30 @@ const create$1 = () => {
304
289
  const putKeyValues = [];
305
290
  const patchKeyValues = [];
306
291
  const deleteKeys = [];
307
- const txState = /* @__PURE__ */ new Map();
308
292
  return {
309
293
  put(key, value) {
310
294
  for (const fn of listeners.beforePut) fn(key, value);
311
295
  tx.put(key, encodeValue(key, value));
312
- txState.set(key, value);
313
296
  putKeyValues.push([key, value]);
314
297
  },
315
298
  patch(key, value) {
316
299
  for (const fn of listeners.beforePatch) fn(key, value);
317
300
  tx.patch(key, encode(key, value, clock.now()));
318
- let baseValue;
319
- if (txState.has(key)) baseValue = txState.get(key) ?? null;
320
- else baseValue = decodeActive(kv.get(key));
321
- if (baseValue) {
322
- const merged = {
323
- ...baseValue,
324
- ...value
325
- };
326
- txState.set(key, merged);
327
- patchKeyValues.push([key, merged]);
328
- }
301
+ const merged = decodeActive(tx.get(key));
302
+ if (merged) patchKeyValues.push([key, merged]);
329
303
  },
330
304
  merge(doc) {
331
- if (doc["~deletedAt"]) {
332
- this.del(doc["~id"]);
333
- return;
334
- }
335
305
  if (tx.has(doc["~id"])) tx.patch(doc["~id"], doc);
336
306
  else tx.put(doc["~id"], doc);
337
- const currentDoc = kv.get(doc["~id"]);
307
+ const currentDoc = tx.get(doc["~id"]);
338
308
  if (currentDoc && !currentDoc["~deletedAt"]) {
339
309
  const merged = decode(currentDoc)["~data"];
340
- txState.set(doc["~id"], merged);
341
310
  patchKeyValues.push([doc["~id"], merged]);
342
311
  }
343
312
  },
344
313
  del(key) {
345
314
  for (const fn of listeners.beforeDel) fn(key);
346
- if (!(txState.get(key) ?? kv.get(key))) return;
315
+ if (!tx.get(key)) return;
347
316
  tx.del(key, clock.now());
348
317
  deleteKeys.push(key);
349
318
  },
@@ -363,7 +332,7 @@ const create$1 = () => {
363
332
  };
364
333
  },
365
334
  use(plugin) {
366
- const { hooks: pluginHooks, init, dispose } = plugin(this);
335
+ const { hooks: pluginHooks, init, dispose, methods } = plugin(this);
367
336
  if (pluginHooks) {
368
337
  if (pluginHooks.onBeforePut) {
369
338
  const callback = pluginHooks.onBeforePut;
@@ -408,6 +377,7 @@ const create$1 = () => {
408
377
  });
409
378
  }
410
379
  }
380
+ if (methods) Object.assign(this, methods);
411
381
  initializers.add(init);
412
382
  disposers.add(dispose);
413
383
  return this;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@byearlybird/starling",
3
- "version": "0.2.1",
3
+ "version": "0.3.0",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "main": "./dist/index.js",