| 127 | * @returns {Readable<T>} |
| 128 | */ |
| 129 | export function derived(stores, fn, initial_value) { |
| 130 | const single = !Array.isArray(stores); |
| 131 | /** @type {Array<Readable<any>>} */ |
| 132 | const stores_array = single ? [stores] : stores; |
| 133 | if (!stores_array.every(Boolean)) { |
| 134 | throw new Error('derived() expects stores as input, got a falsy value'); |
| 135 | } |
| 136 | const auto = fn.length < 2; |
| 137 | return readable(initial_value, (set, update) => { |
| 138 | let started = false; |
| 139 | /** @type {T[]} */ |
| 140 | const values = []; |
| 141 | let pending = 0; |
| 142 | let cleanup = noop; |
| 143 | const sync = () => { |
| 144 | if (pending) { |
| 145 | return; |
| 146 | } |
| 147 | cleanup(); |
| 148 | const result = fn(single ? values[0] : values, set, update); |
| 149 | if (auto) { |
| 150 | set(result); |
| 151 | } else { |
| 152 | cleanup = typeof result === 'function' ? result : noop; |
| 153 | } |
| 154 | }; |
| 155 | const unsubscribers = stores_array.map((store, i) => |
| 156 | subscribe_to_store( |
| 157 | store, |
| 158 | (value) => { |
| 159 | values[i] = value; |
| 160 | pending &= ~(1 << i); |
| 161 | if (started) { |
| 162 | sync(); |
| 163 | } |
| 164 | }, |
| 165 | () => { |
| 166 | pending |= 1 << i; |
| 167 | } |
| 168 | ) |
| 169 | ); |
| 170 | started = true; |
| 171 | sync(); |
| 172 | return function stop() { |
| 173 | run_all(unsubscribers); |
| 174 | cleanup(); |
| 175 | // We need to set this to false because callbacks can still happen despite having unsubscribed: |
| 176 | // Callbacks might already be placed in the queue which doesn't know it should no longer |
| 177 | // invoke this derived store. |
| 178 | started = false; |
| 179 | }; |
| 180 | }); |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Takes a store and returns a new one derived from the old one that is readable. |