-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This hook enables you to provide values to one or multiple contexts from the same component. Instead of: ```html <app-state-provider .value=${appState}> <settings-provider .value=${settings}> <main-app></main-app> </settings-provider> </app-state-provider> ``` you can do: ```js useProvideContext(AppStateContext, appState, [appState]); useProvideContext(SettingsContext, settings, [settings]); ```
- Loading branch information
1 parent
7ac506f
commit 4069d4f
Showing
4 changed files
with
101 additions
and
1 deletion.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"haunted": minor | ||
--- | ||
|
||
New hook: useProvideContext |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { Context, ContextDetail } from "./create-context"; | ||
import { Hook, hook } from "./hook"; | ||
import { State } from "./state"; | ||
import { contextEvent } from "./symbols"; | ||
|
||
/** | ||
* @function | ||
* @template T | ||
* @param {Context} Context Context to provide a value for | ||
* @param {T} value the current value | ||
* @param {unknown[]} values dependencies to the value update | ||
* @return void | ||
*/ | ||
export const useProvideContext = hook( | ||
class<T> extends Hook<[Context<T>, T, unknown[]], void, Element> { | ||
listeners: Set<(value: T) => void>; | ||
|
||
constructor( | ||
id: number, | ||
state: State<Element>, | ||
private context: Context<T>, | ||
private value: T, | ||
private values?: unknown[] | ||
) { | ||
super(id, state); | ||
this.context = context; | ||
this.value = value; | ||
this.values = values; | ||
|
||
this.listeners = new Set(); | ||
this.state.host.addEventListener(contextEvent, this); | ||
} | ||
|
||
disconnectedCallback() { | ||
this.state.host.removeEventListener(contextEvent, this); | ||
} | ||
|
||
handleEvent(event: CustomEvent<ContextDetail<T>>): void { | ||
const { detail } = event; | ||
|
||
if (detail.Context === this.context) { | ||
detail.value = this.value; | ||
detail.unsubscribe = this.unsubscribe.bind(this, detail.callback); | ||
|
||
this.listeners.add(detail.callback); | ||
|
||
event.stopPropagation(); | ||
} | ||
} | ||
|
||
unsubscribe(callback: (value: T) => void): void { | ||
this.listeners.delete(callback); | ||
} | ||
|
||
update(context: Context<T>, value: T, values?: unknown[]): void { | ||
if (this.hasChanged(values)) { | ||
this.values = values; | ||
this.value = value; | ||
for (const callback of this.listeners) { | ||
callback(value); | ||
} | ||
} | ||
} | ||
|
||
hasChanged(values?: unknown[]) { | ||
const lastValues = this.values; | ||
|
||
if (lastValues == null || values == null) { | ||
return true; | ||
} | ||
|
||
return values.some((value, i) => lastValues[i] !== value); | ||
} | ||
} | ||
); |
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