svelte-common 4.18.0 → 4.18.3

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 +2 -2
  2. package/src/util.mjs +36 -18
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "svelte-common",
3
- "version": "4.18.0",
3
+ "version": "4.18.3",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -42,7 +42,7 @@
42
42
  "@sveltejs/vite-plugin-svelte": "^1.0.3",
43
43
  "ava": "^4.3.3",
44
44
  "documentation": "^14.0.0",
45
- "mf-styling": "^1.7.6",
45
+ "mf-styling": "^1.7.7",
46
46
  "npm-pkgbuild": "^10.14.8",
47
47
  "semantic-release": "^19.0.5",
48
48
  "stylelint": "^14.11.0",
package/src/util.mjs CHANGED
@@ -96,7 +96,7 @@ function liveDuration(seconds) {
96
96
  */
97
97
 
98
98
  /**
99
- * Create a store where al the object keys are prefixed
99
+ * Create a store where all the object keys are prefixed.
100
100
  * @param {WriteableStore} store
101
101
  * @param {string} prefix
102
102
  * @returns {WriteableStore}
@@ -104,28 +104,46 @@ 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 = Object.fromEntries(
109
- Object.entries(prefixedKeyObject).map(([k, v]) => [
110
- k.substring(prefix.length),
111
- v
112
- ])
113
- );
114
-
115
- subscriptions.forEach(subscription => subscription(object));
116
- });
107
+ let forwardSubscription;
108
+ let forwardObject;
117
109
 
118
110
  return {
119
- set: object => {
120
- const prefixedKeyObject = Object.fromEntries(
121
- Object.entries(object).map(([k, v]) => [prefix + k, v])
122
- );
123
- store.set(prefixedKeyObject);
124
- },
111
+ set: object =>
112
+ store.set(
113
+ Object.assign(
114
+ forwardObject,
115
+ Object.fromEntries(
116
+ Object.entries(object).map(([k, v]) => [prefix + k, v])
117
+ )
118
+ )
119
+ ),
125
120
 
126
121
  subscribe: s => {
127
122
  subscriptions.add(s);
128
- return () => subscriptions.delete(s);
123
+
124
+ if (!forwardSubscription) {
125
+ forwardSubscription = store.subscribe(o => {
126
+ forwardObject = o;
127
+ const object =
128
+ forwardObject === undefined
129
+ ? {}
130
+ : Object.fromEntries(
131
+ Object.entries(forwardObject)
132
+ .filter(([k, v]) => k.startsWith(prefix))
133
+ .map(([k, v]) => [k.substring(prefix.length), v])
134
+ );
135
+
136
+ subscriptions.forEach(subscription => subscription(object));
137
+ });
138
+ }
139
+
140
+ return () => {
141
+ subscriptions.delete(s);
142
+ if (subscriptions.size === 0) {
143
+ forwardSubscription();
144
+ forwardSubscription = undefined;
145
+ }
146
+ };
129
147
  }
130
148
  };
131
149
  }