Skip to content
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

fix(standalone-context): unsub on disconnect #53

Merged
merged 1 commit into from
Nov 15, 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
@@ -1,3 +1,5 @@
<template>
<x-context-child-detached></x-context-child-detached>
<template lwc:if={showChild}>
<x-context-child-detached></x-context-child-detached>
</template>
</template>
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,11 @@ import parentStateFactory from 'x/parentState';
export default class ContextParent extends ContextfulLightningElement {
@api
parentState = parentStateFactory('parentFoo');

@api
hideChild = false;

get showChild() {
return !this.hideChild;
}
}
16 changes: 14 additions & 2 deletions example/src/modules/x/contextRoot/context.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,13 @@ describe('context', () => {
const contextParent = querySelectorDeep('x-context-parent');

// Current active subscriptions
// 1. <x-context-parent>
// 1. <x-context-child-sibling>
// 2. <x-context-child>
// 3. <x-context-grand-child>
expect(contextParent.parentState.subscribers.size).toBe(3);

// Only active subscription to parent State will be
// LWC framework subscribing to component <x-context-parent>
// from component: <x-context-child-sibling>
// subscription of context child and grand child
// should be removed after unsubscribing
contextParent.hideChild = true;
Expand Down Expand Up @@ -125,4 +125,16 @@ describe('context', () => {
const childWithDetachedFromContext = querySelectorDeep('.child-content-detached', el);
expect(childWithDetachedFromContext.innerText).to.include('parentFoo');
});

it('standalone context is unsubscribed once the child disconnects', async () => {
const el = await clientSideRender(parentEl, componentPath, {});
const contextParentDetached = querySelectorDeep('x-context-parent-detached', el);

expect(contextParentDetached.parentState.subscribers.size).toBe(1);

contextParentDetached.hideChild = true;
await freshRender();

expect(contextParentDetached.parentState.subscribers.size).toBe(0);
});
});
12 changes: 10 additions & 2 deletions packages/@lwc/state/src/standalone-context.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { connectContext } from './shared.js';
import { connectContext, disconnectContext } from './shared.js';
import { SignalBaseClass, type Signal } from '@lwc/signals';
import type { ExposedUpdater } from './types.js';
import type { ContextRuntimeAdapter } from './runtime-interface.js';
Expand All @@ -13,7 +13,6 @@ class ConsumedContextSignal<StateShape extends ValidStateShape>
{
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>) {
Expand Down Expand Up @@ -44,6 +43,15 @@ class ConsumedContextSignal<StateShape extends ValidStateShape>
},
);
}

[disconnectContext](_componentId: ContextRuntimeAdapter<object>['component']) {
// Unlike the state manager's fromContext which can subscribe to multiple
// ancestor contexts simultaneously, this standalone version only subscribes
// to a single context at a time. Therefore, we don't need to use componentId
// to track subscriptions.
this.unsubscribe();
this.unsubscribe = () => {};
}
}

export const fromContext = <
Expand Down
Loading