Skip to content

Commit 38c24c1

Browse files
fix: added readonly function and tests (#5286)
1 parent 05df182 commit 38c24c1

File tree

10 files changed

+71
-3
lines changed

10 files changed

+71
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"entry": "x/component",
3+
"ssrFiles": {
4+
"error": "error-ssr.txt",
5+
"expected": "expected-ssr.html"
6+
}
7+
}

packages/@lwc/engine-server/src/__tests__/fixtures/read-only-props/function/error-ssr.txt

Whitespace-only changes.

packages/@lwc/engine-server/src/__tests__/fixtures/read-only-props/function/error.txt

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<fixture-test>
2+
<template shadowrootmode="open">
3+
<div>
4+
Was foo mutated: true
5+
</div>
6+
<div>
7+
Was proxy updated: true
8+
</div>
9+
<div>
10+
Was proxy mutated: true
11+
</div>
12+
</template>
13+
</fixture-test>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<fixture-test>
2+
<template shadowrootmode="open">
3+
<div>
4+
Was foo mutated: true
5+
</div>
6+
<div>
7+
Was proxy updated: true
8+
</div>
9+
<div>
10+
Was proxy mutated: false
11+
</div>
12+
</template>
13+
</fixture-test>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<template>
2+
<div>Was foo mutated: {foo.mutated}</div>
3+
<div>Was proxy updated: {readonlyFoo.mutated}</div>
4+
<div>Was proxy mutated: {readonlyFoo.readonlyMutated}</div>
5+
</template>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { LightningElement, readonly } from 'lwc';
2+
3+
export default class extends LightningElement {
4+
foo = {
5+
mutated: false,
6+
readonlyMutated: false,
7+
};
8+
readonlyFoo = readonly(this.foo);
9+
10+
connectedCallback() {
11+
this.foo.mutated = true;
12+
13+
// Mutating the proxy should not work in V1 but will work in V2.
14+
// The V2 implementation returns the original object as readonly invokation
15+
// is deprecated and will be removed.
16+
try {
17+
this.readonlyFoo.readonlyMutated = true;
18+
} catch (e) {}
19+
}
20+
}

packages/@lwc/ssr-runtime/src/get-read-only-proxy.ts

+12
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,15 @@ const reactiveMembrane = new ObservableMembrane();
1515
export function getReadOnlyProxy<T>(value: T): Readonly<T> {
1616
return reactiveMembrane.getReadOnlyProxy(value);
1717
}
18+
19+
/**
20+
* DEPRECATED: This function allows you to create a reactive readonly
21+
* membrane around any object value.
22+
* WARNING: This function does *NOT* make the object read-only.
23+
* @param value any object
24+
* @returns the provided value
25+
* @deprecated
26+
*/
27+
export function readonly<T>(value: T): T {
28+
return value;
29+
}

packages/@lwc/ssr-runtime/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,4 @@ export { hasScopedStaticStylesheets, renderStylesheets } from './styles';
4040
export { toIteratorDirective } from './to-iterator-directive';
4141
export { validateStyleTextContents } from './validate-style-text-contents';
4242
export { createContextProvider, establishContextfulRelationship, connectContext } from './wire';
43+
export { readonly } from './get-read-only-proxy';

packages/@lwc/ssr-runtime/src/stubs.ts

-3
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@ export function parseFragment(..._: unknown[]): never {
3030
export function parseSVGFragment(..._: unknown[]): never {
3131
throw new Error('parseSVGFragment cannot be used in SSR context.');
3232
}
33-
export function readonly(..._: unknown[]): never {
34-
throw new Error('readonly cannot be used in SSR context.');
35-
}
3633
export function registerComponent(..._: unknown[]): never {
3734
throw new Error('registerComponent cannot be used in SSR context.');
3835
}

0 commit comments

Comments
 (0)