From 01ead95cbe5c8c8c384149c9c11d16b2f959e5ca Mon Sep 17 00:00:00 2001 From: pushkin Date: Mon, 23 Nov 2020 17:29:01 +0100 Subject: [PATCH] add hasContext lifecycle function (#5690) --- site/content/docs/03-run-time.md | 20 ++++++++++++++++++++ src/runtime/internal/lifecycle.ts | 4 ++++ 2 files changed, 24 insertions(+) diff --git a/site/content/docs/03-run-time.md b/site/content/docs/03-run-time.md index b35af93838..386c18d5fd 100644 --- a/site/content/docs/03-run-time.md +++ b/site/content/docs/03-run-time.md @@ -178,6 +178,26 @@ Retrieves the context that belongs to the closest parent component with the spec ``` +#### `hasContext` + +```js +hasContext: boolean = hasContext(key: any) +``` + +--- + +Checks whether a given `key` has been set in the context of a parent component. Must be called during component initialisation. + +```sv + +``` + #### `createEventDispatcher` ```js diff --git a/src/runtime/internal/lifecycle.ts b/src/runtime/internal/lifecycle.ts index 4a7616bdea..002bd78d24 100644 --- a/src/runtime/internal/lifecycle.ts +++ b/src/runtime/internal/lifecycle.ts @@ -54,6 +54,10 @@ export function getContext(key): T { return get_current_component().$$.context.get(key); } +export function hasContext(key): boolean { + return get_current_component().$$.context.has(key); +} + // TODO figure out if we still want to support // shorthand events, or if we want to implement // a real bubbling mechanism