Skip to content

Commit 9c9994e

Browse files
authored
fix(ssr): callable wire adapter behavior (#5304)
* fix(ssr): callable wire adapter behavior * chore: use optional chaining to avoid opaque errors
1 parent 0748e49 commit 9c9994e

File tree

7 files changed

+49
-2
lines changed

7 files changed

+49
-2
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"entry": "x/wire"
3+
}

packages/@lwc/engine-server/src/__tests__/fixtures/wire/ctor-on-adapter-prop/error.txt

Whitespace-only changes.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<fixture-test>
2+
<template shadowrootmode="open">
3+
Wire adapter invoked: true
4+
</template>
5+
</fixture-test>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
export let isAdapterInvoked = false;
2+
3+
class Adapter {
4+
constructor(dataCallback) {
5+
this.dc = dataCallback;
6+
}
7+
8+
connect() {}
9+
10+
update() {
11+
isAdapterInvoked = true;
12+
this.dc(true);
13+
}
14+
15+
disconnect() {}
16+
}
17+
18+
const arrowFnWithAdapter = () => {};
19+
arrowFnWithAdapter.adapter = Adapter;
20+
21+
export { arrowFnWithAdapter };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<template>
2+
Wire adapter invoked: {isAdapterInvoked}
3+
</template>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { LightningElement, wire } from 'lwc';
2+
3+
import { arrowFnWithAdapter, isAdapterInvoked } from './adapter';
4+
5+
export default class Wire extends LightningElement {
6+
@wire(arrowFnWithAdapter)
7+
wiredProp;
8+
9+
get isAdapterInvoked() {
10+
return isAdapterInvoked;
11+
}
12+
}

packages/@lwc/ssr-compiler/src/compile-js/decorators/wire.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,10 @@ const bCallWiredMethod = esTemplate`
190190
`<ExpressionStatement>;
191191

192192
const bWireAdapterPlumbing = esTemplate`{
193-
const wireInstance = new ${/*wire adapter constructor*/ is.expression}((newValue) => {
193+
// Callable adapters are expressed as a function having an 'adapter' property, which
194+
// is the actual wire constructor.
195+
const AdapterCtor = ${/*wire adapter constructor*/ is.expression}?.adapter ?? ${/*wire adapter constructor*/ 0};
196+
const wireInstance = new AdapterCtor((newValue) => {
194197
${/*update the decorated property or call the decorated method*/ is.expressionStatement};
195198
});
196199
wireInstance.connect?.();
@@ -203,7 +206,7 @@ const bWireAdapterPlumbing = esTemplate`{
203206
// this preserves the behavior of the browser-side wire implementation as well as the
204207
// original SSR implementation.
205208
wireInstance.update(getLiveConfig(), undefined);
206-
__connectContext(${/*wire adapter constructor*/ 0}, instance, (newContextValue) => {
209+
__connectContext(AdapterCtor, instance, (newContextValue) => {
207210
wireInstance.update(getLiveConfig(), newContextValue);
208211
});
209212
}

0 commit comments

Comments
 (0)