svelte-common 4.18.1 → 4.18.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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/util.mjs +30 -19
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "svelte-common",
3
- "version": "4.18.1",
3
+ "version": "4.18.2",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
package/src/util.mjs CHANGED
@@ -104,30 +104,41 @@ function liveDuration(seconds) {
104
104
  export function keyPrefixStore(store, prefix) {
105
105
  const subscriptions = new Set();
106
106
 
107
- store.subscribe(prefixedKeyObject => {
108
- const object =
109
- prefixedKeyObject === undefined
110
- ? {}
111
- : Object.fromEntries(
112
- Object.entries(prefixedKeyObject)
113
- .filter(([k, v]) => k.startsWith(prefix))
114
- .map(([k, v]) => [k.substring(prefix.length), v])
115
- );
116
-
117
- subscriptions.forEach(subscription => subscription(object));
118
- });
107
+ let forwardSubscription;
119
108
 
120
109
  return {
121
- set: object => {
122
- const prefixedKeyObject = Object.fromEntries(
123
- Object.entries(object).map(([k, v]) => [prefix + k, v])
124
- );
125
- store.set(prefixedKeyObject);
126
- },
110
+ set: object =>
111
+ store.set(
112
+ Object.fromEntries(
113
+ Object.entries(object).map(([k, v]) => [prefix + k, v])
114
+ )
115
+ ),
127
116
 
128
117
  subscribe: s => {
129
118
  subscriptions.add(s);
130
- return () => subscriptions.delete(s);
119
+
120
+ if (!forwardSubscription) {
121
+ forwardSubscription = store.subscribe(prefixedKeyObject => {
122
+ const object =
123
+ prefixedKeyObject === undefined
124
+ ? {}
125
+ : Object.fromEntries(
126
+ Object.entries(prefixedKeyObject)
127
+ .filter(([k, v]) => k.startsWith(prefix))
128
+ .map(([k, v]) => [k.substring(prefix.length), v])
129
+ );
130
+
131
+ subscriptions.forEach(subscription => subscription(object));
132
+ });
133
+ }
134
+
135
+ return () => {
136
+ subscriptions.delete(s);
137
+ if (subscriptions.size === 0) {
138
+ forwardSubscription();
139
+ forwardSubscription = undefined;
140
+ }
141
+ };
131
142
  }
132
143
  };
133
144
  }