Skip to content

Commit c09f923

Browse files
committed
centralise
1 parent c0a5869 commit c09f923

File tree

1 file changed

+13
-23
lines changed

1 file changed

+13
-23
lines changed

packages/svelte/src/internal/client/runtime.js

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -875,11 +875,7 @@ export function is_signal(val) {
875875
* @returns {T}
876876
*/
877877
export function getContext(key) {
878-
if (current_component_context === null) {
879-
e.lifecycle_outside_component('getContext');
880-
}
881-
882-
const context_map = get_or_init_context_map(current_component_context);
878+
const context_map = get_or_init_context_map('getContext');
883879
const result = /** @type {T} */ (context_map.get(key));
884880

885881
if (DEV) {
@@ -907,11 +903,7 @@ export function getContext(key) {
907903
* @returns {T}
908904
*/
909905
export function setContext(key, context) {
910-
if (current_component_context === null) {
911-
e.lifecycle_outside_component('setContext');
912-
}
913-
914-
const context_map = get_or_init_context_map(current_component_context);
906+
const context_map = get_or_init_context_map('setContext');
915907
context_map.set(key, context);
916908
return context;
917909
}
@@ -925,11 +917,7 @@ export function setContext(key, context) {
925917
* @returns {boolean}
926918
*/
927919
export function hasContext(key) {
928-
if (current_component_context === null) {
929-
e.lifecycle_outside_component('hasContext');
930-
}
931-
932-
const context_map = get_or_init_context_map(current_component_context);
920+
const context_map = get_or_init_context_map('hasContext');
933921
return context_map.has(key);
934922
}
935923

@@ -943,11 +931,7 @@ export function hasContext(key) {
943931
* @returns {T}
944932
*/
945933
export function getAllContexts() {
946-
if (current_component_context === null) {
947-
e.lifecycle_outside_component('getAllContexts');
948-
}
949-
950-
const context_map = get_or_init_context_map(current_component_context);
934+
const context_map = get_or_init_context_map('getAllContexts');
951935

952936
if (DEV) {
953937
// @ts-expect-error
@@ -963,11 +947,17 @@ export function getAllContexts() {
963947
}
964948

965949
/**
966-
* @param {import('#client').ComponentContext} context
950+
* @param {string} name
967951
* @returns {Map<unknown, unknown>}
968952
*/
969-
function get_or_init_context_map(context) {
970-
return (context.c ??= new Map(get_parent_context(context) || undefined));
953+
function get_or_init_context_map(name) {
954+
if (current_component_context === null) {
955+
e.lifecycle_outside_component(name);
956+
}
957+
958+
return (current_component_context.c ??= new Map(
959+
get_parent_context(current_component_context) || undefined
960+
));
971961
}
972962

973963
/**

0 commit comments

Comments
 (0)