From f514b4d2d4f43410e4567edadcfbea33014c6170 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Marche?= <35939574+Marr11317@users.noreply.github.com> Date: Tue, 6 Apr 2021 01:57:55 -0400 Subject: [PATCH] Update index.ts --- src/runtime/store/index.ts | 44 +++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/src/runtime/store/index.ts b/src/runtime/store/index.ts index 6abb014553..433712c16d 100644 --- a/src/runtime/store/index.ts +++ b/src/runtime/store/index.ts @@ -57,32 +57,12 @@ const subscriber_queue = []; */ export function readable(value: T, start: StartStopNotifier): Readable { - function subscribe(run: Subscriber, invalidate: Invalidator = noop): Unsubscriber { - const subscriber: SubscribeInvalidateTuple = [run, invalidate]; - subscribers.push(subscriber); - if (subscribers.length === 1) { - stop = start(set) || noop; - } - run(value); - - return () => { - const index = subscribers.indexOf(subscriber); - if (index !== -1) { - subscribers.splice(index, 1); - } - if (subscribers.length === 0) { - stop(); - stop = null; - } - }; - } - function get(): T { return value; } return { - subscribe + subscribe: writable(value, start).subscribe }; } @@ -118,8 +98,28 @@ export function writable(value: T, start: StartStopNotifier = noop): Writa function update(fn: Updater): void { set(fn(value)); } + + function subscribe(run: Subscriber, invalidate: Invalidator = noop): Unsubscriber { + const subscriber: SubscribeInvalidateTuple = [run, invalidate]; + subscribers.push(subscriber); + if (subscribers.length === 1) { + stop = start(set) || noop; + } + run(value); + + return () => { + const index = subscribers.indexOf(subscriber); + if (index !== -1) { + subscribers.splice(index, 1); + } + if (subscribers.length === 0) { + stop(); + stop = null; + } + }; + } - return { set, update, subscribe: readable(value, start).subscribe }; + return { set, update, subscribe }; } /** One or more `Readable`s. */