-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: expose previousValueStore for reactive bind
- Loading branch information
Showing
7 changed files
with
135 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
import type { Invalidator, Readable, Unsubscriber } from "svelte/store"; | ||
|
||
import { SubscriberWithPrevious } from "./SubscriberWithPrevious"; | ||
import { type SubscriberWithPrevious } from "./SubscriberWithPrevious"; | ||
|
||
export interface ReadableWithPrevious<T> extends Readable<T> { | ||
subscribe: ( | ||
this: void, | ||
run: SubscriberWithPrevious<T>, | ||
invalidate?: Invalidator<T> | undefined, | ||
) => Unsubscriber; | ||
previousValueStore: Readable<T | undefined>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,13 @@ | ||
import type { Invalidator, Unsubscriber, Writable } from "svelte/store"; | ||
import type { Invalidator, Readable, Unsubscriber, Writable } from "svelte/store"; | ||
|
||
import { ReadableWithPrevious } from "./ReadableWithPrevious"; | ||
import { SubscriberWithPrevious } from "./SubscriberWithPrevious"; | ||
import { type ReadableWithPrevious } from "./ReadableWithPrevious"; | ||
import { type SubscriberWithPrevious } from "./SubscriberWithPrevious"; | ||
|
||
export interface WritableWithPrevious<T> extends Writable<T>, ReadableWithPrevious<T> { | ||
subscribe: ( | ||
this: void, | ||
run: SubscriberWithPrevious<T>, | ||
invalidate?: Invalidator<T> | undefined, | ||
) => Unsubscriber; | ||
previousValueStore: Readable<T | undefined>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,44 @@ | ||
import type { Readable, Writable } from "svelte/store"; | ||
import { derived, type Readable, type Writable } from "svelte/store"; | ||
|
||
import { ReadableWithPrevious } from "./ReadableWithPrevious"; | ||
import { SubscriberWithPrevious } from "./SubscriberWithPrevious"; | ||
import { WritableWithPrevious } from "./WritableWithPrevious"; | ||
import { type ReadableWithPrevious } from "./ReadableWithPrevious"; | ||
import { type SubscriberWithPrevious } from "./SubscriberWithPrevious"; | ||
import { type WritableWithPrevious } from "./WritableWithPrevious"; | ||
|
||
export function giveSvelteStorePreviousBehaviour<T>(store: Writable<T>): WritableWithPrevious<T>; | ||
export function giveSvelteStorePreviousBehaviour<T>(store: Readable<T>): ReadableWithPrevious<T>; | ||
|
||
export function giveSvelteStorePreviousBehaviour<T>( | ||
store: Readable<T> | Writable<T>, | ||
): ReadableWithPrevious<T> | WritableWithPrevious<T> { | ||
const { subscribe: originalSubscribe, ...rest } = store; | ||
const { subscribe, ...rest } = store; | ||
|
||
const storeValues: { current: undefined | T; previous: undefined | T } = { | ||
current: undefined, | ||
previous: undefined, | ||
}; | ||
|
||
originalSubscribe((x) => { | ||
subscribe((x) => { | ||
storeValues.previous = storeValues.current; | ||
storeValues.current = x; | ||
}); | ||
|
||
const forReturn = { | ||
let _previousValueStore: Readable<T | undefined>; | ||
|
||
return { | ||
...rest, | ||
|
||
subscribe(run: SubscriberWithPrevious<T>) { | ||
return originalSubscribe((value: T) => { | ||
return subscribe((value: T) => { | ||
run(value, storeValues.previous); | ||
}); | ||
}, | ||
}; | ||
|
||
return forReturn; | ||
get previousValueStore() { | ||
if (!_previousValueStore) { | ||
_previousValueStore = derived(store, () => storeValues.previous); | ||
} | ||
|
||
return _previousValueStore; | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
export { giveSvelteStorePreviousBehaviour } from "./giveSvelteStorePreviousBehaviour"; | ||
export { getPrevious } from "./getPrevious"; | ||
export { ReadableWithPrevious } from "./ReadableWithPrevious"; | ||
export { WritableWithPrevious } from "./WritableWithPrevious"; | ||
export { SubscriberWithPrevious } from "./SubscriberWithPrevious"; | ||
export { type ReadableWithPrevious } from "./ReadableWithPrevious"; | ||
export { type WritableWithPrevious } from "./WritableWithPrevious"; | ||
export { type SubscriberWithPrevious } from "./SubscriberWithPrevious"; |