-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(signals): add signalStore and signalStoreFeature #4049
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { Signal, signal } from '@angular/core'; | ||
import { | ||
selectSignal, | ||
signalStore, | ||
signalStoreFeature, | ||
type, | ||
withMethods, | ||
withSignals, | ||
withState, | ||
} from '../src'; | ||
import { STATE_SIGNAL } from '../src/signal-state'; | ||
|
||
describe('signalStoreFeature', () => { | ||
function withCustomFeature1() { | ||
return signalStoreFeature( | ||
withState({ foo: 'foo' }), | ||
withSignals(({ foo }) => ({ bar: selectSignal(() => foo() + 1) })), | ||
withMethods(({ foo, bar }) => ({ | ||
baz: () => foo() + bar() + 2, | ||
})) | ||
); | ||
} | ||
|
||
function withCustomFeature2() { | ||
return signalStoreFeature( | ||
withCustomFeature1(), | ||
withMethods(({ foo, baz }) => ({ | ||
bar: (value: number) => value, | ||
m: () => foo() + baz() + 3, | ||
})) | ||
); | ||
} | ||
|
||
function withCustomFeatureWithInput<_>() { | ||
return signalStoreFeature( | ||
{ | ||
state: type<{ foo: string }>(), | ||
signals: type<{ s: Signal<number> }>(), | ||
Comment on lines
+37
to
+38
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I got a question about the usage of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Exactly! |
||
}, | ||
withState({ foo1: 1 }), | ||
withState({ foo2: 2 }) | ||
); | ||
} | ||
|
||
it('creates a custom feature by combining base features', () => { | ||
const Store = signalStore( | ||
withCustomFeature1(), | ||
withSignals(({ bar }) => ({ | ||
s: selectSignal(() => bar() + 's'), | ||
})) | ||
); | ||
|
||
const store = new Store(); | ||
|
||
expect(store[STATE_SIGNAL]()).toEqual({ foo: 'foo' }); | ||
expect(store.foo()).toBe('foo'); | ||
expect(store.bar()).toBe('foo1'); | ||
expect(store.baz()).toBe('foofoo12'); | ||
expect(store.s()).toBe('foo1s'); | ||
}); | ||
|
||
it('creates a custom feature by combining base and custom features', () => { | ||
const Store = signalStore( | ||
withCustomFeature2(), | ||
withMethods(({ foo }) => ({ m1: () => foo() + 10 })) | ||
); | ||
|
||
const store = new Store(); | ||
|
||
expect(store[STATE_SIGNAL]()).toEqual({ foo: 'foo' }); | ||
expect(store.foo()).toBe('foo'); | ||
expect(store.bar(10)).toBe(10); | ||
expect(store.m()).toBe('foofoofoo123'); | ||
expect(store.m1()).toBe('foo10'); | ||
}); | ||
|
||
it('creates a custom feature with input', () => { | ||
const Store = signalStore( | ||
withCustomFeature1(), | ||
withSignals(() => ({ s: signal(1).asReadonly() })), | ||
withCustomFeatureWithInput() | ||
); | ||
|
||
const store = new Store(); | ||
|
||
expect(store[STATE_SIGNAL]()).toEqual({ foo: 'foo', foo1: 1, foo2: 2 }); | ||
expect(store.foo()).toBe('foo'); | ||
expect(store.bar()).toBe('foo1'); | ||
expect(store.baz()).toBe('foofoo12'); | ||
expect(store.s()).toBe(1); | ||
expect(store.foo1()).toBe(1); | ||
expect(store.foo2()).toBe(2); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this generic has a meaning?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I noticed a weird typing bug when custom features with input are used in complex use cases.
Here is the reproduction. If we remove
<_>
fromwithY
, the compilation error will be thrown in SignalStores that usewithY
feature although the generic isn't used anywhere. 👀This is a rare case, but I'll document this behavior and there will be a recommendation in the docs.