Skip to content

Commit 3167f6b

Browse files
fix: added readonly function and tests
1 parent 785479b commit 3167f6b

File tree

8 files changed

+38
-3
lines changed

8 files changed

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

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

Whitespace-only changes.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<fixture-test>
2+
<template shadowrootmode="open">
3+
<div>
4+
Is original the same as proxy? false
5+
</div>
6+
<div>
7+
Is original the same as proxy when stringified? true
8+
</div>
9+
</template>
10+
</fixture-test>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<template>
2+
<div>Is original the same as proxy? {areSame}</div>
3+
<div>Is original the same as proxy when stringified? {areSameStringified}</div>
4+
</template>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { LightningElement, readonly } from 'lwc';
2+
3+
export default class extends LightningElement {
4+
foo = { willSays: 'mobs 4eva' };
5+
readonlyFoo = readonly(this.foo);
6+
// Proxy is the same when stringified but it's a different object
7+
areSameStringified = JSON.stringify(this.foo) === JSON.stringify(this.readonlyFoo);
8+
areSame = this.foo == this.readonlyFoo;
9+
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,14 @@ 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+
* @param value any object
23+
* @returns a readonly proxy of the live object
24+
* @deprecated
25+
*/
26+
export function readonly<T>(value: T): Readonly<T> {
27+
return reactiveMembrane.getReadOnlyProxy(value);
28+
}

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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 0 additions & 3 deletions
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)