@keepkit/core 0.28.8 → 0.28.10

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/react.js CHANGED
@@ -2,26 +2,26 @@
2
2
  import {
3
3
  KeepStore,
4
4
  createRediscoveryQuery,
5
- exportItems,
6
5
  getKeepNavigationState,
7
- importItems,
8
6
  isKeepItemMetadataStale,
9
7
  moveKeepItem,
10
8
  orderKeepItems,
11
9
  queryKeepItems,
12
10
  reorderKeepItems,
13
11
  revalidateKeepItems
14
- } from "./chunk-EMOPFB5D.js";
15
- import {
16
- parseKeepMeta
17
- } from "./chunk-5W4QSJHV.js";
12
+ } from "./chunk-CSBPFROD.js";
18
13
  import {
19
14
  createBrowserStorageAdapter,
15
+ exportItems,
16
+ importItems,
20
17
  isRecord,
21
18
  normalizeKeepTags,
22
19
  persistKeepItems,
23
20
  removeKeepItems
24
- } from "./chunk-SLH4DFYQ.js";
21
+ } from "./chunk-SS22UTBH.js";
22
+ import {
23
+ parseKeepMeta
24
+ } from "./chunk-5W4QSJHV.js";
25
25
 
26
26
  // src/react/components/KeepButton.tsx
27
27
  import {
@@ -197,8 +197,16 @@ function KeepProviderContent({
197
197
  const reportError = useCallback(
198
198
  (cause, context) => {
199
199
  store.setState({ error: cause });
200
- handlersRef.current.onError?.(cause, context);
201
- for (const plugin of pluginsRef.current) plugin.onError?.(cause, context);
200
+ const handlers = [
201
+ () => handlersRef.current.onError?.(cause, context),
202
+ ...pluginsRef.current.map((plugin) => () => plugin.onError?.(cause, context))
203
+ ];
204
+ for (const handler of handlers) {
205
+ try {
206
+ void Promise.resolve(handler()).catch(() => void 0);
207
+ } catch {
208
+ }
209
+ }
202
210
  },
203
211
  [store]
204
212
  );
@@ -213,9 +221,24 @@ function KeepProviderContent({
213
221
  for (const plugin of pluginsRef.current) await plugin.before?.(context);
214
222
  return context;
215
223
  }, []);
216
- const runAfterPlugins = useCallback(async (context) => {
217
- for (const plugin of pluginsRef.current) await plugin.after?.(context);
218
- }, []);
224
+ const notify = useCallback(
225
+ async (callback, context) => {
226
+ try {
227
+ await callback();
228
+ } catch (cause) {
229
+ reportError(cause, { ...context, phase: "notification" });
230
+ }
231
+ },
232
+ [reportError]
233
+ );
234
+ const runAfterPlugins = useCallback(
235
+ async (context) => {
236
+ const { action, id } = context;
237
+ const errorContext = id === void 0 ? { action } : { action, id };
238
+ for (const plugin of pluginsRef.current) await notify(() => plugin.after?.(context), errorContext);
239
+ },
240
+ [notify]
241
+ );
219
242
  const enqueueOperation = useCallback((operation) => {
220
243
  const run = operationTailRef.current.then(operation, operation);
221
244
  operationTailRef.current = run.then(
@@ -322,27 +345,23 @@ function KeepProviderContent({
322
345
  setItems(plan.next);
323
346
  store.setState({ error: null });
324
347
  await plan.persist();
325
- plan.onSuccess?.();
326
- if (plan.pluginContext) await runAfterPlugins(plan.pluginContext);
327
- if (plan.pluginContext) {
328
- const change = { ...plan.pluginContext, phase: "local" };
329
- store.setState({ lastChange: change });
330
- void Promise.resolve(handlersRef.current.onChange?.(change)).catch(
331
- (cause) => reportError(cause, { action, id })
332
- );
333
- }
334
348
  } catch (cause) {
335
349
  setItems(previous);
336
350
  reportError(cause, { action, id });
337
351
  throw cause;
338
352
  }
353
+ const change = plan.pluginContext ? { ...plan.pluginContext, phase: "local" } : void 0;
354
+ if (change) store.setState({ lastChange: change });
355
+ for (const callback of plan.onSuccess ?? []) void notify(callback, { action, id });
356
+ if (plan.pluginContext) await runAfterPlugins(plan.pluginContext);
357
+ if (change) void notify(() => handlersRef.current.onChange?.(change), { action, id });
339
358
  });
340
359
  return run.finally(() => {
341
360
  pendingMutationsRef.current -= 1;
342
361
  if (pendingMutationsRef.current === 0) store.setState({ isMutating: false });
343
362
  });
344
363
  },
345
- [enqueueOperation, reportError, runAfterPlugins, runBeforePlugins, setItems, store]
364
+ [enqueueOperation, notify, reportError, runAfterPlugins, runBeforePlugins, setItems, store]
346
365
  );
347
366
  const updateItem = useCallback(
348
367
  (action, id, update, onSuccess) => runMutation(action, id, (previous) => {
@@ -352,7 +371,7 @@ function KeepProviderContent({
352
371
  return {
353
372
  next: previous.map((item) => item.id === id ? next : item),
354
373
  persist: () => storage.set(next),
355
- onSuccess: () => onSuccess?.(next),
374
+ onSuccess: [() => onSuccess?.(next)],
356
375
  pluginContext: { action, id, item: next }
357
376
  };
358
377
  }),
@@ -389,7 +408,7 @@ function KeepProviderContent({
389
408
  return {
390
409
  next,
391
410
  persist: () => storage.set(nextItem),
392
- onSuccess: () => handlersRef.current.onSave?.(nextItem),
411
+ onSuccess: [() => handlersRef.current.onSave?.(nextItem)],
393
412
  pluginContext: { action: "save", id: nextItem.id, item: nextItem }
394
413
  };
395
414
  });
@@ -563,41 +582,58 @@ function KeepProviderContent({
563
582
  },
564
583
  [updateItemsBatch]
565
584
  );
585
+ const getCollectionPersistence = useCallback(
586
+ (id) => {
587
+ if (storage.getCollections && storage.setCollection && storage.removeCollection) {
588
+ return {
589
+ setCollection: storage.setCollection.bind(storage),
590
+ removeCollection: storage.removeCollection.bind(storage)
591
+ };
592
+ }
593
+ const cause = new Error("This storage adapter does not support collection persistence.");
594
+ reportError(cause, { action: "collection", id });
595
+ throw cause;
596
+ },
597
+ [reportError, storage]
598
+ );
566
599
  const createCollection = useCallback(
567
600
  (id, name) => enqueueOperation(async () => {
568
601
  const trimmedId = id.trim();
569
602
  const trimmedName = name.trim();
603
+ const persistence = getCollectionPersistence(trimmedId);
570
604
  if (!trimmedId || !trimmedName) throw new Error("Collection id and name are required.");
571
605
  const current = store.getSnapshot().collections;
572
606
  if (current[trimmedId]) throw new Error(`Collection "${trimmedId}" already exists.`);
573
607
  try {
574
- await storage.setCollection?.({ id: trimmedId, name: trimmedName });
608
+ await persistence.setCollection({ id: trimmedId, name: trimmedName });
575
609
  } catch (cause) {
576
610
  reportError(cause, { action: "collection", id: trimmedId });
577
611
  throw cause;
578
612
  }
579
613
  store.setState({ collections: { ...current, [trimmedId]: { name: trimmedName } } });
580
614
  }),
581
- [enqueueOperation, reportError, storage, store]
615
+ [enqueueOperation, getCollectionPersistence, reportError, store]
582
616
  );
583
617
  const renameCollection = useCallback(
584
618
  (id, name) => enqueueOperation(async () => {
619
+ const persistence = getCollectionPersistence(id);
585
620
  const trimmedName = name.trim();
586
621
  if (!trimmedName) throw new Error("Collection name is required.");
587
622
  const current = store.getSnapshot().collections;
588
623
  if (!current[id]) throw new Error(`Collection "${id}" does not exist.`);
589
624
  try {
590
- await storage.setCollection?.({ id, name: trimmedName });
625
+ await persistence.setCollection({ id, name: trimmedName });
591
626
  } catch (cause) {
592
627
  reportError(cause, { action: "collection", id });
593
628
  throw cause;
594
629
  }
595
630
  store.setState({ collections: { ...current, [id]: { name: trimmedName } } });
596
631
  }),
597
- [enqueueOperation, reportError, storage, store]
632
+ [enqueueOperation, getCollectionPersistence, reportError, store]
598
633
  );
599
634
  const removeCollection = useCallback(
600
635
  (id) => enqueueOperation(async () => {
636
+ const persistence = getCollectionPersistence(id);
601
637
  const current = store.getSnapshot().collections;
602
638
  if (!current[id]) throw new Error(`Collection "${id}" does not exist.`);
603
639
  const { [id]: _removed, ...remaining } = current;
@@ -608,7 +644,7 @@ function KeepProviderContent({
608
644
  });
609
645
  try {
610
646
  await persistKeepItems(storage, updated);
611
- await storage.removeCollection?.(id);
647
+ await persistence.removeCollection(id);
612
648
  } catch (cause) {
613
649
  if (affected.length > 0) {
614
650
  try {
@@ -626,7 +662,7 @@ function KeepProviderContent({
626
662
  }
627
663
  store.setState({ collections: remaining });
628
664
  }),
629
- [enqueueOperation, reportError, setItems, storage, store]
665
+ [enqueueOperation, getCollectionPersistence, reportError, setItems, storage, store]
630
666
  );
631
667
  const updateTagsBatch = useCallback(
632
668
  async (ids, tags) => {
@@ -665,11 +701,7 @@ function KeepProviderContent({
665
701
  throw cause;
666
702
  }
667
703
  },
668
- onSuccess: () => {
669
- updatedItems.forEach((item) => {
670
- handlersRef.current.onTagsUpdate?.(item.id, nextTags);
671
- });
672
- },
704
+ onSuccess: updatedItems.map((item) => () => handlersRef.current.onTagsUpdate?.(item.id, nextTags)),
673
705
  pluginContext: { action: "updateTagsBatch", items: updatedItems }
674
706
  };
675
707
  });
@@ -708,7 +740,7 @@ function KeepProviderContent({
708
740
  return {
709
741
  next: previous.filter((item) => item.id !== id),
710
742
  persist: () => storage.remove(id),
711
- onSuccess: () => handlersRef.current.onRemove?.(current),
743
+ onSuccess: [() => handlersRef.current.onRemove?.(current)],
712
744
  pluginContext: { action: "remove", id, item: current }
713
745
  };
714
746
  });
@@ -739,11 +771,7 @@ function KeepProviderContent({
739
771
  throw cause;
740
772
  }
741
773
  },
742
- onSuccess: () => {
743
- removedItems.forEach((item) => {
744
- handlersRef.current.onRemove?.(item);
745
- });
746
- },
774
+ onSuccess: removedItems.map((item) => () => handlersRef.current.onRemove?.(item)),
747
775
  pluginContext: { action: "removeBatch", items: removedItems }
748
776
  };
749
777
  });
@@ -804,7 +832,7 @@ function KeepProviderContent({
804
832
  persist: async () => {
805
833
  await persistKeepItems(storage, pending.items);
806
834
  },
807
- onSuccess: () => handlersRef.current.onUndo?.(pending.items),
835
+ onSuccess: [() => handlersRef.current.onUndo?.(pending.items)],
808
836
  pluginContext: { action: "undo", items: pending.items }
809
837
  };
810
838
  });
@@ -828,16 +856,17 @@ function KeepProviderContent({
828
856
  const run = enqueueOperation(async () => {
829
857
  const previous = itemsRef.current;
830
858
  let persistenceStarted = false;
859
+ let summary;
831
860
  try {
832
861
  const activeRevalidator = revalidator ?? validateItem;
833
862
  if (!activeRevalidator)
834
863
  throw new Error("KeepProvider.revalidateItems requires a revalidator or validateItem.");
835
- const summary = await revalidateKeepItems(previous, activeRevalidator, {
864
+ summary = await revalidateKeepItems(previous, activeRevalidator, {
836
865
  ...options,
837
866
  resolveItem: options.resolveItem ?? resolveItem
838
867
  });
839
- const pluginContext = { action: "revalidate", items: summary.updatedItems };
840
- await runBeforePlugins(pluginContext);
868
+ const pluginContext2 = { action: "revalidate", items: summary.updatedItems };
869
+ await runBeforePlugins(pluginContext2);
841
870
  if (summary.updatedItems.length > 0) {
842
871
  persistenceStarted = true;
843
872
  await persistKeepItems(storage, summary.updatedItems);
@@ -848,21 +877,24 @@ function KeepProviderContent({
848
877
  }
849
878
  setItems(summary.items);
850
879
  store.setState({ error: null });
851
- const removedIdSet = new Set(summary.removedIds);
852
- for (const result of summary.results) {
853
- if (removedIdSet.has(result.item.id)) handlersRef.current.onRemove?.(result.item);
854
- }
855
- await runAfterPlugins(pluginContext);
856
- void Promise.resolve(handlersRef.current.onChange?.({ ...pluginContext, phase: "local" })).catch(
857
- (cause) => reportError(cause, { action: "revalidate" })
858
- );
859
- return summary;
860
880
  } catch (cause) {
861
881
  if (persistenceStarted) await restoreItems(storage, previous);
862
882
  setItems(previous);
863
883
  reportError(cause, { action: "revalidate" });
864
884
  throw cause;
865
885
  }
886
+ const pluginContext = { action: "revalidate", items: summary.updatedItems };
887
+ const change = { ...pluginContext, phase: "local" };
888
+ store.setState({ lastChange: change });
889
+ const removedIdSet = new Set(summary.removedIds);
890
+ for (const result of summary.results) {
891
+ if (removedIdSet.has(result.item.id)) {
892
+ void notify(() => handlersRef.current.onRemove?.(result.item), { action: "revalidate" });
893
+ }
894
+ }
895
+ await runAfterPlugins(pluginContext);
896
+ void notify(() => handlersRef.current.onChange?.(change), { action: "revalidate" });
897
+ return summary;
866
898
  });
867
899
  return run.finally(() => {
868
900
  pendingMutationsRef.current -= 1;
@@ -871,6 +903,7 @@ function KeepProviderContent({
871
903
  },
872
904
  [
873
905
  enqueueOperation,
906
+ notify,
874
907
  reportError,
875
908
  resolveItem,
876
909
  runAfterPlugins,