Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 4x 4x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 6x 6x 6x 6x 6x 6x 6x 22x 19x 19x 19x 19x 7x 7x 7x 22x 22x 7x 7x 7x 7x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 3x 3x 3x 6x 6x 5x 5x 20x 5x 5x 1x 1x 5x 5x 1x 1x 1x 2x 2x 1x 1x | /** @import { Readable, Writable } from './public.js' */
import { noop } from '../internal/shared/utils.js';
import {
effect_root,
effect_tracking,
render_effect
} from '../internal/client/reactivity/effects.js';
import { source } from '../internal/client/reactivity/sources.js';
import { get as get_source, tick } from '../internal/client/runtime.js';
import { increment } from '../reactivity/utils.js';
import { get, writable } from './shared/index.js';
export { derived, get, readable, readonly, writable } from './shared/index.js';
/**
* @template V
* @overload
* @param {() => V} get
* @param {(v: V) => void} set
* @returns {Writable<V>}
*/
/**
* @template V
* @overload
* @param {() => V} get
* @returns {Readable<V>}
*/
/**
* Create a store from a function that returns state, and (to make a writable store), an
* optional second function that sets state.
*
* ```ts
* import { toStore } from 'svelte/store';
*
* let count = $state(0);
*
* const store = toStore(() => count, (v) => (count = v));
* ```
* @template V
* @param {() => V} get
* @param {(v: V) => void} [set]
* @returns {Writable<V> | Readable<V>}
*/
export function toStore(get, set) {
const store = writable(get(), (set) => {
let ran = false;
// TODO do we need a different implementation on the server?
const teardown = effect_root(() => {
render_effect(() => {
const value = get();
if (ran) set(value);
});
});
ran = true;
return teardown;
});
if (set) {
return {
set,
update: (fn) => set(fn(get())),
subscribe: store.subscribe
};
}
return {
subscribe: store.subscribe
};
}
/**
* @template V
* @overload
* @param {Writable<V>} store
* @returns {{ current: V }}
*/
/**
* @template V
* @overload
* @param {Readable<V>} store
* @returns {{ readonly current: V }}
*/
/**
* Convert a store to an object with a reactive `current` property. If `store`
* is a readable store, `current` will be a readonly property.
*
* ```ts
* import { fromStore, get, writable } from 'svelte/store';
*
* const store = writable(0);
*
* const count = fromStore(store);
*
* count.current; // 0;
* store.set(1);
* count.current; // 1
*
* count.current += 1;
* get(store); // 2
* ```
* @template V
* @param {Writable<V> | Readable<V>} store
*/
export function fromStore(store) {
let value = /** @type {V} */ (undefined);
let version = source(0);
let subscribers = 0;
let unsubscribe = noop;
function current() {
if (effect_tracking()) {
get_source(version);
render_effect(() => {
if (subscribers === 0) {
let ran = false;
unsubscribe = store.subscribe((v) => {
value = v;
if (ran) increment(version);
});
ran = true;
}
subscribers += 1;
return () => {
subscribers -= 1;
tick().then(() => {
if (subscribers === 0) {
unsubscribe();
}
});
};
});
return value;
}
return get(store);
}
if ('set' in store) {
return {
get current() {
return current();
},
set current(v) {
store.set(v);
}
};
}
return {
get current() {
return current();
}
};
}
|