Skip to content
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
10 changes: 7 additions & 3 deletions packages/@lwc/engine-core/src/framework/mutation-tracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,13 @@ export function componentValueObserved(vm: VM, key: PropertyKey, target: any = {
lwcRuntimeFlags.ENABLE_EXPERIMENTAL_SIGNALS &&
isObject(target) &&
!isNull(target) &&
safeHasProp(target, 'value') &&
safeHasProp(target, 'subscribe') &&
isFunction(target.subscribe) &&
(lwcRuntimeFlags.DISABLE_TRY_CATCH_FOR_SIGNALS_CHECK
? 'value' in target
: safeHasProp(target, 'value')) &&
(lwcRuntimeFlags.DISABLE_TRY_CATCH_FOR_SIGNALS_CHECK
? 'subscribe' in target
: safeHasProp(target, 'subscribe')) &&
isFunction((target as any).subscribe) &&
isTrustedSignal(target) &&
// Only subscribe if a template is being rendered by the engine
tro.isObserving()
Expand Down
1 change: 1 addition & 0 deletions packages/@lwc/features/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const features: FeatureFlagMap = {
ENABLE_FORCE_SHADOW_MIGRATE_MODE: null,
ENABLE_EXPERIMENTAL_SIGNALS: null,
DISABLE_SYNTHETIC_SHADOW: null,
DISABLE_TRY_CATCH_FOR_SIGNALS_CHECK: null,
};

if (!(globalThis as any).lwcRuntimeFlags) {
Expand Down
5 changes: 5 additions & 0 deletions packages/@lwc/features/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ export interface FeatureFlagMap {
* native shadow mode.
*/
DISABLE_SYNTHETIC_SHADOW: FeatureFlagValue;

/**
* Disable using a try/catch when sniffing for whether an object is a signal or not
*/
DISABLE_TRY_CATCH_FOR_SIGNALS_CHECK: FeatureFlagValue;
}

export type FeatureFlagName = keyof FeatureFlagMap;
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createElement, setFeatureFlagForTest } from 'lwc';
import { catchUnhandledRejectionsAndErrors } from 'test-utils';
import Reactive from 'x/reactive';
import NonReactive from 'x/nonReactive';
import Container from 'x/container';
Expand Down Expand Up @@ -214,13 +215,54 @@ describe('signal protocol', () => {
expect(subscribe).not.toHaveBeenCalled();
});

it('does not throw an error for objects that throw upon "in" checks', async () => {
const elm = createElement('x-throws', { is: Throws });
document.body.appendChild(elm);
describe('try/catch for the signals check', () => {
it('does not throw an error for objects that throw upon "in" checks', async () => {
const elm = createElement('x-throws', { is: Throws });
document.body.appendChild(elm);

await Promise.resolve();
await Promise.resolve();

expect(elm.shadowRoot.querySelector('h1').textContent).toBe('hello');
});

describe('flag on', () => {
beforeAll(() => {
setFeatureFlagForTest('DISABLE_TRY_CATCH_FOR_SIGNALS_CHECK', true);
});

afterAll(() => {
setFeatureFlagForTest('DISABLE_TRY_CATCH_FOR_SIGNALS_CHECK', false);
});

let caughtError;

// handle errors thrown both with and without native custom element lifecycle
catchUnhandledRejectionsAndErrors((error) => {
caughtError = error;
});

afterEach(() => {
caughtError = undefined;
});

it('does throw an error for objects that throw upon "in" checks', async () => {
const elm = createElement('x-throws', { is: Throws });

try {
document.body.appendChild(elm);
} catch (err) {
caughtError = err;
}

expect(elm.shadowRoot.querySelector('h1').textContent).toBe('hello');
await Promise.resolve();

// still renders
expect(elm.shadowRoot.querySelector('h1').textContent).toBe('hello');

// throws error
expect(caughtError).not.toBeUndefined();
});
});
});
});

Expand Down