svelte-common 4.19.15 → 4.19.16

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 +25 -20
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "svelte-common",
3
- "version": "4.19.15",
3
+ "version": "4.19.16",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
package/src/util.mjs CHANGED
@@ -98,10 +98,10 @@ function liveDuration(seconds) {
98
98
  /**
99
99
  * Create a derived store where all the object keys are prefixed.
100
100
  * ```
101
- * { a: 1, b: 2 } -> { prefix_a: 1 prefix_b: 2 }
101
+ * { a: 1, b: 2 } -> { foo_a: 1 foo_b: 2 } // prefix: foo_
102
102
  * ```
103
- * @param {WriteableStore} store
104
- * @param {string} prefix
103
+ * @param {WriteableStore} store we derive from
104
+ * @param {string} prefix for each key
105
105
  * @returns {WriteableStore}
106
106
  */
107
107
  export function keyPrefixStore(store, prefix) {
@@ -110,8 +110,27 @@ export function keyPrefixStore(store, prefix) {
110
110
  let forwardSubscription;
111
111
  let forwardObject;
112
112
 
113
+ function subscribeMyself() {
114
+ if (!forwardSubscription) {
115
+ forwardSubscription = store.subscribe(o => {
116
+ forwardObject = o;
117
+ const object =
118
+ forwardObject === undefined
119
+ ? {}
120
+ : Object.fromEntries(
121
+ Object.entries(forwardObject)
122
+ .filter(([k, v]) => k.startsWith(prefix))
123
+ .map(([k, v]) => [k.substring(prefix.length), v])
124
+ );
125
+
126
+ subscriptions.forEach(subscription => subscription(object));
127
+ });
128
+ }
129
+ }
130
+
113
131
  return {
114
- set: object =>
132
+ set: object => {
133
+ subscribeMyself();
115
134
  store.set(
116
135
  Object.assign(
117
136
  forwardObject,
@@ -119,26 +138,12 @@ export function keyPrefixStore(store, prefix) {
119
138
  Object.entries(object).map(([k, v]) => [prefix + k, v])
120
139
  )
121
140
  )
122
- ),
141
+ ); },
123
142
 
124
143
  subscribe: s => {
125
144
  subscriptions.add(s);
126
145
 
127
- if (!forwardSubscription) {
128
- forwardSubscription = store.subscribe(o => {
129
- forwardObject = o;
130
- const object =
131
- forwardObject === undefined
132
- ? {}
133
- : Object.fromEntries(
134
- Object.entries(forwardObject)
135
- .filter(([k, v]) => k.startsWith(prefix))
136
- .map(([k, v]) => [k.substring(prefix.length), v])
137
- );
138
-
139
- subscriptions.forEach(subscription => subscription(object));
140
- });
141
- }
146
+ subscribeMyself();
142
147
 
143
148
  return () => {
144
149
  subscriptions.delete(s);