Skip to content

Instantly share code, notes, and snippets.

@bluwy
Last active September 22, 2021 18:01
Show Gist options
  • Save bluwy/e8f47dbc5f0b0c574190915eab617479 to your computer and use it in GitHub Desktop.
Save bluwy/e8f47dbc5f0b0c574190915eab617479 to your computer and use it in GitHub Desktop.
Utility function to create Svelte contexts
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()
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