-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add unwrapFunctionStore utility * chore(release): v3.6.0 🎉
- Loading branch information
1 parent
113301e
commit ebca372
Showing
21 changed files
with
144 additions
and
16 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
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import type { Readable } from 'svelte/store'; | ||
|
||
type UnwrapStore<T> = T extends Readable<infer U> ? U : T; | ||
|
||
/** | ||
* Unwraps a function from a store and make it function calleable easily outside of a Svelte component. | ||
* | ||
* It works by creating a subscription to the store and getting local reference to the store value. | ||
* Then when the returned function is called, it will execute the function by using the local reference. | ||
* | ||
* The returned function has a 'freeze' method that will stop listening to the store. | ||
* | ||
* @example | ||
* // some-js-file.js | ||
* import { format } from 'svelte-i18n'; | ||
* | ||
* const $format = unwrapFunctionStore(format); | ||
* | ||
* console.log($format('hello', { name: 'John' })); | ||
* | ||
*/ | ||
export function unwrapFunctionStore< | ||
S extends Readable<(...args: any[]) => any>, | ||
Fn extends UnwrapStore<S>, | ||
>( | ||
store: S, | ||
): Fn & { | ||
/** | ||
* Stops listening to the store. | ||
*/ | ||
freeze: () => void; | ||
} { | ||
let localReference: Fn; | ||
|
||
const cancel = store.subscribe((value) => (localReference = value as Fn)); | ||
|
||
const fn = (...args: Parameters<Fn>) => localReference(...args); | ||
|
||
fn.freeze = cancel; | ||
|
||
return fn as Fn & { freeze: () => void }; | ||
} |
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
File renamed without changes.
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
2 changes: 1 addition & 1 deletion
2
test/runtime/includes/lookup.test.ts → test/runtime/modules/lookup.test.ts
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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { writable, derived, get } from 'svelte/store'; | ||
|
||
import { unwrapFunctionStore } from '../../../src/runtime'; | ||
|
||
import type { Readable } from 'svelte/store'; | ||
|
||
test('unwraps the function from a store', () => { | ||
const store = writable(0); | ||
const functionStore = derived(store, ($store) => () => $store) as Readable< | ||
() => number | ||
>; | ||
|
||
const unwrapped = unwrapFunctionStore(functionStore); | ||
|
||
expect(get(functionStore)()).toBe(0); | ||
expect(unwrapped()).toBe(0); | ||
|
||
store.set(1); | ||
|
||
expect(get(functionStore)()).toBe(1); | ||
expect(unwrapped()).toBe(1); | ||
}); | ||
|
||
test('stops listening to store changes when .freeze is called', () => { | ||
const store = writable(0); | ||
const functionStore = derived(store, ($store) => () => $store) as Readable< | ||
() => number | ||
>; | ||
|
||
const unwrapped = unwrapFunctionStore(functionStore); | ||
|
||
expect(get(functionStore)()).toBe(0); | ||
expect(unwrapped()).toBe(0); | ||
|
||
store.set(1); | ||
|
||
expect(get(functionStore)()).toBe(1); | ||
expect(unwrapped()).toBe(1); | ||
|
||
unwrapped.freeze(); | ||
|
||
store.set(2); | ||
|
||
expect(get(functionStore)()).toBe(2); | ||
expect(unwrapped()).toBe(1); | ||
}); |
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