Skip to content

feat: implement standalone fromContext #51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<template>
<h3>Child</h3>
<p class='child-content-detached'>nameProvidedByParent: {nameProvidedByParent}</p>
</template>
12 changes: 12 additions & 0 deletions example/src/modules/x/contextChildDetached/contextChildDetached.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { ContextfulLightningElement } from '@lwc/state/context';
import { fromContext } from '@lwc/state';
import childStateFactory from 'x/childState';
import parentStateFactory from 'x/parentState';

export default class ContextChild extends ContextfulLightningElement {
parentState = fromContext(parentStateFactory);

get nameProvidedByParent() {
return this.parentState.value?.name ?? 'not available';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<template>
<x-context-child-detached></x-context-child-detached>
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { api } from 'lwc';
import { ContextfulLightningElement } from '@lwc/state/context';
import parentStateFactory from 'x/parentState';

export default class ContextParent extends ContextfulLightningElement {
@api
parentState = parentStateFactory('parentFoo');
}
6 changes: 6 additions & 0 deletions example/src/modules/x/contextRoot/context.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,10 @@ describe('context', () => {

expect(contextParent.parentState.subscribers.size).toBe(1);
});

it('children can access context directly with detached fromContext', async () => {
const el = await clientSideRender(parentEl, componentPath, {});
const childWithDetachedFromContext = querySelectorDeep('.child-content-detached', el);
expect(childWithDetachedFromContext.innerText).to.include('parentFoo');
});
});
1 change: 1 addition & 0 deletions example/src/modules/x/contextRoot/contextRoot.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<template>
<x-context-parent></x-context-parent>
<x-context-lonely-child></x-context-lonely-child>
<x-context-parent-detached></x-context-parent-detached>
</template>
1 change: 1 addition & 0 deletions packages/@lwc/state/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type {
UnwrapSignal,
} from './types.ts';
export { setTrustedSignalSet } from '@lwc/signals';
export { fromContext } from './standalone-context.js';

const atomSetter = Symbol('atomSetter');
const contextID = Symbol('contextID');
Expand Down
56 changes: 56 additions & 0 deletions packages/@lwc/state/src/standalone-context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { connectContext } from './shared.js';
import { SignalBaseClass, type Signal } from '@lwc/signals';
import type { ExposedUpdater } from './types.js';
import type { ContextRuntimeAdapter } from './runtime-interface.js';
import type { ContextManager } from './index.js';

type ValidStateShape = Record<string, Signal<unknown> | ExposedUpdater>;
type ValidStateDef<StateShape extends ValidStateShape> = () => Signal<StateShape>;

class ConsumedContextSignal<StateShape extends ValidStateShape>
extends SignalBaseClass<ValidStateShape>
implements ContextManager
{
private desiredStateDef: ValidStateDef<StateShape>;
private _value: StateShape | null = null;
// Currently unused. Should be called once `disconnectContext` is implemented.
private unsubscribe: () => void = () => {};

constructor(stateDef: ValidStateDef<StateShape>) {
super();
this.desiredStateDef = stateDef;
}

get value(): StateShape | null {
return this._value;
}

[connectContext](runtimeAdapter: ContextRuntimeAdapter<object>) {
if (!runtimeAdapter) {
throw new Error(
'Implementation error: runtimeAdapter must be present at the time of connect.',
);
}

runtimeAdapter.consumeContext(
this.desiredStateDef,
(providedContextSignal: Signal<StateShape>) => {
this._value = providedContextSignal.value;
this.notify();
this.unsubscribe = providedContextSignal.subscribe(() => {
this._value = providedContextSignal.value;
this.notify();
});
},
);
}
}

export const fromContext = <
StateShape extends ValidStateShape,
StateDef extends ValidStateDef<StateShape>,
>(
stateDef: StateDef,
) => {
return new ConsumedContextSignal<StateShape>(stateDef);
};
Loading