Last active
September 22, 2021 18:01
-
-
Save bluwy/e8f47dbc5f0b0c574190915eab617479 to your computer and use it in GitHub Desktop.
Utility function to create Svelte contexts
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
import { getContext, setContext } from 'svelte' | |
/** | |
* @template T | |
* @param {any} [k] | |
* @returns {[() => T, (v: T) => void]} | |
*/ | |
export function createContext(k = {}) { | |
return [() => getContext(k), (v) => setContext(k, v)] | |
} | |
/** | |
* @template T | |
* @typedef {[() => T, (v: T) => void]} ContextTuple | |
*/ | |
/** @type {ContextTuple<number>} */ | |
export const [getAppContext, setAppContext] = createContext() |
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
import { getContext, setContext } from 'svelte' | |
export function createContext<T>(k: any = {}): [() => T, (v: T) => void] { | |
return [() => getContext(k), (v) => setContext(k, v)] | |
} | |
export const [getAppContext, setAppContext] = createContext<number>() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment