@byearlybird/starling 0.9.1 → 0.9.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.
@@ -33,24 +33,31 @@ function unstoragePlugin(key, storage, config = {}) {
33
33
  let debounceTimer = null;
34
34
  let pollInterval = null;
35
35
  let store = null;
36
+ let persistPromise = null;
36
37
  const persistSnapshot = async () => {
37
38
  if (!store) return;
38
39
  const data = store.collection();
39
40
  const persisted = onBeforeSet !== void 0 ? await onBeforeSet(data) : data;
40
41
  await storage.set(key, persisted);
41
42
  };
43
+ const runPersist = async () => {
44
+ debounceTimer = null;
45
+ persistPromise = persistSnapshot();
46
+ await persistPromise;
47
+ persistPromise = null;
48
+ };
42
49
  const schedulePersist = () => {
43
50
  if (skip?.()) return;
44
- const runPersist = () => {
45
- debounceTimer = null;
46
- persistSnapshot();
47
- };
48
51
  if (debounceMs === 0) {
49
- runPersist();
52
+ persistPromise = persistSnapshot().finally(() => {
53
+ persistPromise = null;
54
+ });
50
55
  return;
51
56
  }
52
57
  if (debounceTimer !== null) clearTimeout(debounceTimer);
53
- debounceTimer = setTimeout(runPersist, debounceMs);
58
+ debounceTimer = setTimeout(() => {
59
+ runPersist();
60
+ }, debounceMs);
54
61
  };
55
62
  const pollStorage = async () => {
56
63
  if (!store) return;
@@ -68,15 +75,17 @@ function unstoragePlugin(key, storage, config = {}) {
68
75
  pollStorage();
69
76
  }, pollIntervalMs);
70
77
  },
71
- onDispose: () => {
78
+ onDispose: async () => {
72
79
  if (debounceTimer !== null) {
73
80
  clearTimeout(debounceTimer);
74
81
  debounceTimer = null;
82
+ await runPersist();
75
83
  }
76
84
  if (pollInterval !== null) {
77
85
  clearInterval(pollInterval);
78
86
  pollInterval = null;
79
87
  }
88
+ if (persistPromise !== null) await persistPromise;
80
89
  store = null;
81
90
  },
82
91
  onAdd: () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@byearlybird/starling",
3
- "version": "0.9.1",
3
+ "version": "0.9.2",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "main": "./dist/index.js",