Skip to content

Commit

Permalink
feat: add String and JSON helpers to Signal (QwikDev#1645)
Browse files Browse the repository at this point in the history
  • Loading branch information
wmertens authored Nov 7, 2022
1 parent 2c61ad1 commit a95865f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
11 changes: 11 additions & 0 deletions packages/qwik/src/core/state/signal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ export class SignalImpl<T> implements Signal<T> {
this[QObjectManagerSymbol] = manager;
}

// prevent accidental use as value
valueOf() {
throw new TypeError('Cannot coerce a Signal, use `.value` instead');
}
toString() {
return `[Signal ${String(this.value)}]`;
}
toJSON() {
return { value: this.value };
}

get value() {
const sub = tryGetInvokeContext()?.$subscriber$;
if (sub) {
Expand Down
4 changes: 2 additions & 2 deletions starters/e2e/e2e.lexical-scope.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ test.describe('lexical-scope', () => {

test('should rerender without changes', async ({ page }) => {
const SNAPSHOT =
'<p>1</p><p>"&lt;/script&gt;"</p><p>{"a":{"thing":12},"b":"hola","c":123,"d":false,"e":true,"f":null,"h":[1,"string",false,{"hola":1},["hello"]],"promise":{}}</p><p>undefined</p><p>null</p><p>[1,2,"hola",null,{}]</p><p>true</p><p>false</p><p>()=&gt;console.error()</p><p><!--t=2-->mutable message<!----></p><p>{"signal":{"untrackedValue":0},"signalValue":0,"store":{"count":0,"signal":{"untrackedValue":0}},"storeCount":0,"storeSignal":{"untrackedValue":0}}</p><p>from a promise</p><p>message, message2, signal, signalValue, store, storeCount, storeSignal</p>';
'<p>1</p><p>"&lt;/script&gt;"</p><p>{"a":{"thing":12},"b":"hola","c":123,"d":false,"e":true,"f":null,"h":[1,"string",false,{"hola":1},["hello"]],"promise":{}}</p><p>undefined</p><p>null</p><p>[1,2,"hola",null,{}]</p><p>true</p><p>false</p><p>()=&gt;console.error()</p><p><!--t=2-->mutable message<!----></p><p>{"signal":{"value":0},"signalValue":0,"store":{"count":0,"signal":{"value":0}},"storeCount":0,"storeSignal":{"value":0}}</p><p>from a promise</p><p>message, message2, signal, signalValue, store, storeCount, storeSignal</p>';
const RESULT =
'[1,"</script>",{"a":{"thing":12},"b":"hola","c":123,"d":false,"e":true,"f":null,"h":[1,"string",false,{"hola":1},["hello"]],"promise":{}},"undefined","null",[1,2,"hola",null,{}],true,false,null,"mutable message",null,{"untrackedValue":0},0,{"count":0,"signal":{"untrackedValue":0}},0,{"untrackedValue":0},"from a promise","http://qwik.builder.com/docs?query=true","2022-07-26T17:40:30.255Z","hola()\\\\/ gi",12,"failed message",["\\b: backspace","\\f: form feed","\\n: line feed","\\r: carriage return","\\t: horizontal tab","\\u000b: vertical tab","\\u0000: null character","\': single quote","\\\\: backslash"],"Infinity","-Infinity","NaN"]';
'[1,"</script>",{"a":{"thing":12},"b":"hola","c":123,"d":false,"e":true,"f":null,"h":[1,"string",false,{"hola":1},["hello"]],"promise":{}},"undefined","null",[1,2,"hola",null,{}],true,false,null,"mutable message",null,{"value":0},0,{"count":0,"signal":{"value":0}},0,{"value":0},"from a promise","http://qwik.builder.com/docs?query=true","2022-07-26T17:40:30.255Z","hola()\\\\/ gi",12,"failed message",["\\b: backspace","\\f: form feed","\\n: line feed","\\r: carriage return","\\t: horizontal tab","\\u000b: vertical tab","\\u0000: null character","\': single quote","\\\\: backslash"],"Infinity","-Infinity","NaN"]';

function normalizeSnapshot(str: string) {
return str.replace(' =&gt; ', '=&gt;');
Expand Down

0 comments on commit a95865f

Please sign in to comment.