INIT
This commit is contained in:
+42
@@ -0,0 +1,42 @@
|
||||
export declare class Context<TContext> {
|
||||
#private;
|
||||
/**
|
||||
* @param name The name of the context.
|
||||
* This is used for generating the context key and error messages.
|
||||
*/
|
||||
constructor(name: string);
|
||||
/**
|
||||
* The key used to get and set the context.
|
||||
*
|
||||
* It is not recommended to use this value directly.
|
||||
* Instead, use the methods provided by this class.
|
||||
*/
|
||||
get key(): symbol;
|
||||
/**
|
||||
* Checks whether this has been set in the context of a parent component.
|
||||
*
|
||||
* Must be called during component initialisation.
|
||||
*/
|
||||
exists(): boolean;
|
||||
/**
|
||||
* Retrieves the context that belongs to the closest parent component.
|
||||
*
|
||||
* Must be called during component initialisation.
|
||||
*
|
||||
* @throws An error if the context does not exist.
|
||||
*/
|
||||
get(): TContext;
|
||||
/**
|
||||
* Retrieves the context that belongs to the closest parent component,
|
||||
* or the given fallback value if the context does not exist.
|
||||
*
|
||||
* Must be called during component initialisation.
|
||||
*/
|
||||
getOr<TFallback>(fallback: TFallback): TContext | TFallback;
|
||||
/**
|
||||
* Associates the given value with the current component and returns it.
|
||||
*
|
||||
* Must be called during component initialisation.
|
||||
*/
|
||||
set(context: TContext): TContext;
|
||||
}
|
||||
+65
@@ -0,0 +1,65 @@
|
||||
import { getContext, hasContext, setContext } from "svelte";
|
||||
export class Context {
|
||||
#name;
|
||||
#key;
|
||||
/**
|
||||
* @param name The name of the context.
|
||||
* This is used for generating the context key and error messages.
|
||||
*/
|
||||
constructor(name) {
|
||||
this.#name = name;
|
||||
this.#key = Symbol(name);
|
||||
}
|
||||
/**
|
||||
* The key used to get and set the context.
|
||||
*
|
||||
* It is not recommended to use this value directly.
|
||||
* Instead, use the methods provided by this class.
|
||||
*/
|
||||
get key() {
|
||||
return this.#key;
|
||||
}
|
||||
/**
|
||||
* Checks whether this has been set in the context of a parent component.
|
||||
*
|
||||
* Must be called during component initialisation.
|
||||
*/
|
||||
exists() {
|
||||
return hasContext(this.#key);
|
||||
}
|
||||
/**
|
||||
* Retrieves the context that belongs to the closest parent component.
|
||||
*
|
||||
* Must be called during component initialisation.
|
||||
*
|
||||
* @throws An error if the context does not exist.
|
||||
*/
|
||||
get() {
|
||||
const context = getContext(this.#key);
|
||||
if (context === undefined) {
|
||||
throw new Error(`Context "${this.#name}" not found`);
|
||||
}
|
||||
return context;
|
||||
}
|
||||
/**
|
||||
* Retrieves the context that belongs to the closest parent component,
|
||||
* or the given fallback value if the context does not exist.
|
||||
*
|
||||
* Must be called during component initialisation.
|
||||
*/
|
||||
getOr(fallback) {
|
||||
const context = getContext(this.#key);
|
||||
if (context === undefined) {
|
||||
return fallback;
|
||||
}
|
||||
return context;
|
||||
}
|
||||
/**
|
||||
* Associates the given value with the current component and returns it.
|
||||
*
|
||||
* Must be called during component initialisation.
|
||||
*/
|
||||
set(context) {
|
||||
return setContext(this.#key, context);
|
||||
}
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
export { Context } from "./context.js";
|
||||
+1
@@ -0,0 +1 @@
|
||||
export { Context } from "./context.js";
|
||||
Reference in New Issue
Block a user