-
Notifications
You must be signed in to change notification settings - Fork 7
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: add immerPatchState
#24
Merged
timdeschryver
merged 10 commits into
timdeschryver:main
from
rainerhahnekamp:feat/immer-patch-state
Mar 13, 2024
Merged
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
27f6bff
feat: immerPatchState
rainerhahnekamp 38bd782
feat: add `immerPatchState`
rainerhahnekamp aeea16b
Update README.md
rainerhahnekamp df8a2f0
Update src/signals/index.ts
rainerhahnekamp a0b7883
Update README.md
rainerhahnekamp 6d68366
Update README.md
rainerhahnekamp f7a2b51
refactor: add improvements according to review.
rainerhahnekamp 172c901
ci: run tests before build
timdeschryver 9383dca
test: should work with chained patch functions
timdeschryver d220193
test: reword tests descriptions (no should)
timdeschryver File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/** @type {import('jest').Config} */ | ||
const config = { | ||
preset: 'jest-preset-angular', | ||
setupFilesAfterEnv: ['<rootDir>/setup-jest.ts'], | ||
testMatch: ['**/*.jest.ts'], | ||
globalSetup: 'jest-preset-angular/global-setup', | ||
modulePathIgnorePatterns: ['<rootDir>/src/package.json'], | ||
moduleNameMapper: { | ||
'ngrx-immer/signals': '<rootDir>/src/signals', | ||
'ngrx-immer': '<rootDir>/src', | ||
}, | ||
}; | ||
|
||
module.exports = config; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import 'jest-preset-angular/setup-jest'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,24 @@ | ||
export function placeholder() { | ||
return 'placeholder'; | ||
} | ||
import { PartialStateUpdater, patchState, StateSignal } from '@ngrx/signals'; | ||
import { immerReducer } from 'ngrx-immer'; | ||
|
||
export type ImmerStateUpdater<State extends object> = (state: State) => void; | ||
|
||
function toFullStateUpdater<State extends object>(updater: PartialStateUpdater<State & {}> | ImmerStateUpdater<State & {}>): (state: State) => State | void { | ||
return (state: State) => { | ||
const patchedState = updater(state); | ||
if (patchedState) { | ||
return ({ ...state, ...patchedState }); | ||
} | ||
return; | ||
}; | ||
} | ||
|
||
export function immerPatchState<State extends object>(state: StateSignal<State>, ...updaters: Array<Partial<State & {}> | PartialStateUpdater<State & {}> | ImmerStateUpdater<State & {}>>) { | ||
const immerUpdaters = updaters.map(updater => { | ||
if (typeof updater === 'function') { | ||
return immerReducer(toFullStateUpdater(updater)) as unknown as PartialStateUpdater<State & {}>; | ||
} | ||
return updater; | ||
}); | ||
patchState(state, ...immerUpdaters); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
import { signalStore, withComputed, withState } from '@ngrx/signals'; | ||
import { immerPatchState } from 'ngrx-immer/signals'; | ||
import { computed, effect } from '@angular/core'; | ||
import { TestBed } from '@angular/core/testing'; | ||
|
||
const UserState = signalStore(withState({ | ||
id: 1, | ||
name: { firstname: 'Konrad', lastname: 'Schultz' }, | ||
address: { city: 'Vienna', zip: '1010' }, | ||
}), withComputed(({ name }) => ({ prettyName: computed(() => `${name.firstname()} ${name.lastname()}`) }))); | ||
|
||
|
||
describe('immerPatchState', () => { | ||
const setup = () => { | ||
return new UserState; | ||
}; | ||
|
||
it('should do a sanity check', () => { | ||
const userState = setup(); | ||
expect(userState.id()).toBe(1); | ||
}); | ||
|
||
it('should be type-safe', () => { | ||
const userState = setup(); | ||
|
||
//@ts-expect-error number is not a property | ||
immerPatchState(userState, { number: 1 }); | ||
|
||
//@ts-expect-error number is not a property | ||
immerPatchState(userState, state => ({ number: 1 })); | ||
}); | ||
|
||
it('should allow patching with object literal', () => { | ||
const userState = setup(); | ||
immerPatchState(userState, { name: { firstname: 'Lucy', lastname: 'Sanders' } }); | ||
expect(userState.prettyName()).toBe('Lucy Sanders'); | ||
}); | ||
|
||
describe('update with return value', () => { | ||
it('should work with the default patch function', () => { | ||
const userState = setup(); | ||
immerPatchState(userState, ({ name }) => ({ name: { firstname: name.firstname, lastname: 'Sanders' } })); | ||
expect(userState.prettyName()).toBe('Konrad Sanders'); | ||
}); | ||
|
||
it('should not emit other signals', () => { | ||
TestBed.runInInjectionContext(() => { | ||
let effectCounter = 0; | ||
const userState = setup(); | ||
effect(() => { | ||
userState.id(); | ||
effectCounter++; | ||
}); | ||
TestBed.flushEffects(); | ||
|
||
expect(effectCounter).toBe(1); | ||
immerPatchState(userState, ({ name }) => ({ name: { firstname: name.firstname, lastname: 'Sanders' } })); | ||
|
||
TestBed.flushEffects(); | ||
expect(effectCounter).toBe(1); | ||
}); | ||
}); | ||
|
||
it('should throw if a mutated patched state is returned', () => { | ||
const userState = setup(); | ||
|
||
expect(() => | ||
immerPatchState(userState, (state) => { | ||
state.name.lastname = 'Sanders'; | ||
return state; | ||
})).toThrow('[Immer] An immer producer returned a new value *and* modified its draft.'); | ||
}); | ||
}); | ||
|
||
describe('update without returning a value', () => { | ||
it('should allow a mutable update', () => { | ||
const userState = setup(); | ||
immerPatchState(userState, (state => { | ||
state.name = { firstname: 'Lucy', lastname: 'Sanders' }; | ||
})); | ||
expect(userState.prettyName()).toBe('Lucy Sanders'); | ||
}); | ||
|
||
it('should not emit other signals', () => { | ||
TestBed.runInInjectionContext(() => { | ||
let effectCounter = 0; | ||
const userState = setup(); | ||
effect(() => { | ||
userState.id(); | ||
effectCounter++; | ||
}); | ||
TestBed.flushEffects(); | ||
|
||
expect(effectCounter).toBe(1); | ||
immerPatchState(userState, (state => { | ||
state.name = { firstname: 'Lucy', lastname: 'Sanders' }; | ||
})); | ||
|
||
TestBed.flushEffects(); | ||
expect(effectCounter).toBe(1); | ||
}); | ||
}); | ||
}); | ||
|
||
it('should check the Signal notification on multiple updates', () => { | ||
TestBed.runInInjectionContext(() => { | ||
// setup effects | ||
let addressEffectCounter = 0; | ||
let nameEffectCounter = 0; | ||
let idEffectCounter = 0; | ||
const userState = setup(); | ||
effect(() => { | ||
userState.id(); | ||
idEffectCounter++; | ||
}); | ||
effect(() => { | ||
userState.address(); | ||
addressEffectCounter++ | ||
}); | ||
effect(() => { | ||
userState.name(); | ||
nameEffectCounter++ | ||
}); | ||
|
||
|
||
// first run | ||
TestBed.flushEffects(); | ||
expect(idEffectCounter).toBe(1); | ||
expect(addressEffectCounter).toBe(1); | ||
expect(nameEffectCounter).toBe(1); | ||
|
||
// change | ||
immerPatchState(userState, (state => { | ||
state.name = { firstname: 'Lucy', lastname: 'Sanders' }; | ||
state.address.zip = '1020' | ||
})); | ||
|
||
// second run | ||
TestBed.flushEffects(); | ||
expect(idEffectCounter).toBe(1); | ||
expect(addressEffectCounter).toBe(2); | ||
expect(nameEffectCounter).toBe(2); | ||
}); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/* To learn more about this file see: https://angular.io/config/tsconfig. */ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "./out-tsc/spec", | ||
"types": ["jest"] | ||
}, | ||
"include": ["**/tests/*.jest.ts"] | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 wonder if we can update the generics to be able to do the following:
setAllEntities
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.
Something like the following seems to work:
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.
Hi Tim, I can't currently work on it, but please update my branch as you wish. I'll continue tomorrow.
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.
You can ignore this.
In WebStorm this was working, after setting the TS version of Visual Studio Code to the installed version it does work.
Sorry for the confusion.
Let's ship this! 🚀