Skip to content

Commit

Permalink
feat: useSignal() support an initializer fn (#2077)
Browse files Browse the repository at this point in the history
  • Loading branch information
wmertens authored Nov 11, 2022
1 parent 619f531 commit db03ac4
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/qwik/src/core/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,7 @@ export interface UseSignal {
// (undocumented)
<T>(): Signal<T | undefined>;
// (undocumented)
<T>(value: T): Signal<T>;
<T>(value: T | (() => T)): Signal<T>;
}

// @alpha (undocumented)
Expand Down
10 changes: 7 additions & 3 deletions packages/qwik/src/core/container/store.unit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ storeSuite('should serialize content', async () => {
<body q:version="dev" q:container="paused" q:render="dom-dev">
<div>
<!--qv q:key=sX: q:id=0-->
<div q:id="1" on:click="/runtimeQRL#_[0 1 2 3 4 5 6 7 8 9 10 11]">
<div q:id="1" on:click="/runtimeQRL#_[0 1 2 3 4 5 6 7 8 9 10 11 12]">
<!--t=2-->
0
<!---->
Expand All @@ -62,7 +62,7 @@ storeSuite('should serialize content', async () => {

equal(JSON.parse(script.textContent!), {
refs: {
'1': '1 2 f m 8 i 7 6 k! m l 0',
'1': '1 2 f o 8 i 7 6 k! o l 0 n',
},
ctx: {},
objs: [
Expand Down Expand Up @@ -92,7 +92,7 @@ storeSuite('should serialize content', async () => {
d: '6',
e: '7',
f: '8',
g: 'm',
g: 'o',
h: 'd',
i: 'e',
},
Expand All @@ -104,6 +104,8 @@ storeSuite('should serialize content', async () => {
count: 'j',
},
'\u0002/runtimeQRL#_',
'ok',
'\u0012m',
'\u0001',
],
subs: [['2 #0 0 #2 data']],
Expand Down Expand Up @@ -133,6 +135,7 @@ export const LexicalScope = component$(() => {
count: 0,
});
const signal = useSignal(0);
const signalFromFn = useSignal(() => 'ok');
const nu = 1;
const str = 'hola';
const obj = {
Expand Down Expand Up @@ -166,6 +169,7 @@ export const LexicalScope = component$(() => {
noserialize,
qrl,
signal,
signalFromFn,
]);
return <div onClick$={thing}>{signal as any}</div>;
});
Expand Down
6 changes: 4 additions & 2 deletions packages/qwik/src/core/use/use-signal.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { createSignal, Signal } from '../state/signal';
import { isFunction } from '../util/types';
import { useSequentialScope } from './use-sequential-scope';

/**
* @alpha
*/
export interface UseSignal {
<T>(): Signal<T | undefined>;
<T>(value: T): Signal<T>;
<T>(value: T | (() => T)): Signal<T>;
}

/**
Expand All @@ -19,7 +20,8 @@ export const useSignal: UseSignal = <STATE>(initialState?: STATE): Signal<STATE>
}

const containerState = ctx.$renderCtx$.$static$.$containerState$;
const signal = createSignal(initialState, containerState, undefined) as Signal<STATE>;
const value = isFunction(initialState) ? (initialState as Function)() : initialState;
const signal = createSignal(value, containerState, undefined) as Signal<STATE>;
set(signal);
return signal;
};

0 comments on commit db03ac4

Please sign in to comment.