This repository has been archived by the owner on Nov 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
freeze state in signalState #2
Open
rainerhahnekamp
wants to merge
5
commits into
markostanimirovic:main
Choose a base branch
from
rainerhahnekamp:freeze-state
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a8cb227
freeze state in signalState
rainerhahnekamp 603d0a5
only freeze state in signal state, if the state has been updated with…
rainerhahnekamp dfa5e7e
- rename util to deep-freeze
rainerhahnekamp baa6b60
fix tests
rainerhahnekamp 491f21c
freeze signalState optionally (only in dev mode). $update function pr…
rainerhahnekamp 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 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
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,40 @@ | ||
export function deepFreeze<T extends object>(target: T): T { | ||
markostanimirovic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Object.freeze(target); | ||
|
||
const targetIsFunction = isFunction(target); | ||
|
||
for (const prop of Object.getOwnPropertyNames(target)) { | ||
// Do not freeze Ivy properties (e.g. router state) | ||
// https://github.com/ngrx/platform/issues/2109#issuecomment-582689060 | ||
if (prop.startsWith('ɵ')) { | ||
continue; | ||
} | ||
|
||
if ( | ||
Object.hasOwn(target, prop) && | ||
(targetIsFunction | ||
? prop !== 'caller' && prop !== 'callee' && prop !== 'arguments' | ||
: true) | ||
) { | ||
// @ts-ignore | ||
const propValue = target[prop]; | ||
markostanimirovic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if ( | ||
(isObjectLike(propValue) || isFunction(propValue)) && | ||
!Object.isFrozen(propValue) | ||
) { | ||
deepFreeze(propValue); | ||
} | ||
} | ||
} | ||
|
||
return target; | ||
} | ||
|
||
export function isFunction(target: unknown): target is () => unknown { | ||
return typeof target === 'function'; | ||
} | ||
|
||
export function isObjectLike(target: unknown): target is object { | ||
return typeof target === 'object' && target !== null; | ||
} |
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,152 @@ | ||
import { effect } from '@angular/core'; | ||
import { withEffect } from './with-effect'; | ||
import { signalState } from '../lib/signal-state'; | ||
import { selectSignal } from '../lib/select-signal'; | ||
|
||
describe('Signal State', () => { | ||
const initialState = { | ||
user: { | ||
firstName: 'John', | ||
lastName: 'Smith', | ||
}, | ||
foo: 'bar', | ||
numbers: [1, 2, 3], | ||
}; | ||
|
||
const setup = () => signalState(initialState); | ||
|
||
it('should support nested signals', () => { | ||
const state = setup(); | ||
|
||
expect(state()).toBe(initialState); | ||
expect(state.user()).toBe(initialState.user); | ||
expect(state.user.firstName()).toBe(initialState.user.firstName); | ||
}); | ||
|
||
it('should allow updates', () => { | ||
const state = setup(); | ||
state.$update((state) => ({ | ||
...state, | ||
user: { firstName: 'Johannes', lastName: 'Schmidt' }, | ||
})); | ||
expect(state()).toEqual({ | ||
...initialState, | ||
user: { firstName: 'Johannes', lastName: 'Schmidt' }, | ||
}); | ||
}); | ||
|
||
it('should update immutably', () => { | ||
const state = setup(); | ||
state.$update((state) => ({ | ||
...state, | ||
foo: 'bar', | ||
numbers: [3, 2, 1], | ||
})); | ||
expect(state.user()).toBe(initialState.user); | ||
expect(state.foo()).toBe(initialState.foo); | ||
expect(state.numbers()).not.toBe(initialState.numbers); | ||
}); | ||
|
||
describe('equal checks', () => { | ||
it( | ||
'should not fire unchanged signals on update', | ||
withEffect((detectChanges) => { | ||
const state = setup(); | ||
|
||
const numberEffect = jest.fn(() => state.numbers()); | ||
effect(numberEffect); | ||
|
||
const userEffect = jest.fn(() => state.user()); | ||
effect(userEffect); | ||
|
||
expect(numberEffect).toHaveBeenCalledTimes(0); | ||
expect(userEffect).toHaveBeenCalledTimes(0); | ||
|
||
// run effects for the first time | ||
detectChanges(); | ||
expect(numberEffect).toHaveBeenCalledTimes(1); | ||
expect(userEffect).toHaveBeenCalledTimes(1); | ||
|
||
// update state with effect run | ||
state.$update((state) => ({ ...state, numbers: [4, 5, 6] })); | ||
detectChanges(); | ||
expect(numberEffect).toHaveBeenCalledTimes(2); | ||
expect(userEffect).toHaveBeenCalledTimes(1); | ||
}) | ||
); | ||
|
||
it( | ||
'should not fire for unchanged derived signals', | ||
withEffect((detectChanges) => { | ||
const state = setup(); | ||
|
||
const numberCount = selectSignal( | ||
state, | ||
(state) => state.numbers.length | ||
); | ||
|
||
const numberEffect = jest.fn(() => numberCount()); | ||
effect(numberEffect); | ||
|
||
// run effects for the first time | ||
detectChanges(); | ||
expect(numberEffect).toHaveBeenCalledTimes(1); | ||
|
||
// update user | ||
state.$update({ | ||
user: { firstName: 'Susanne', lastName: 'Taylor' }, | ||
}); | ||
detectChanges(); | ||
expect(numberEffect).toHaveBeenCalledTimes(1); | ||
|
||
// update numbers | ||
state.$update({ numbers: [1] }); | ||
detectChanges(); | ||
expect(numberEffect).toHaveBeenCalledTimes(2); | ||
expect(numberCount()).toBe(1); | ||
}) | ||
); | ||
}); | ||
|
||
describe('immutability', () => { | ||
it( | ||
'should throw on mutable changes', | ||
withEffect((detectChanges) => { | ||
let numberCounter = 0; | ||
const state = setup(); | ||
const effectFn = jest.fn(() => state.numbers()); | ||
effect(effectFn); | ||
detectChanges(); | ||
expect(effectFn).toHaveBeenCalledTimes(1); | ||
|
||
expect(() => | ||
state.$update((state) => { | ||
const { numbers } = state; | ||
numbers.push(4); | ||
return { ...state }; | ||
}) | ||
).toThrow(); | ||
}) | ||
); | ||
|
||
it( | ||
'should throw on a single mutable change', | ||
withEffect((detectChanges) => { | ||
let numberCounter = 0; | ||
const state = setup(); | ||
const effectFn = jest.fn(() => state.numbers()); | ||
effect(effectFn); | ||
detectChanges(); | ||
expect(effectFn).toHaveBeenCalledTimes(1); | ||
|
||
expect(() => | ||
state.$update({ foo: 'foobar' }, (state) => { | ||
const { numbers } = state; | ||
numbers.push(4); | ||
return { ...state }; | ||
}) | ||
).toThrow(); | ||
}) | ||
); | ||
}); | ||
}); |
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,5 +1,4 @@ | ||
import { | ||
selectSignal, | ||
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.
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. done. I've left testEffects there as it is more a testing file and not a test |
||
signalStore, | ||
signalStoreFeature, | ||
type, | ||
|
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,26 @@ | ||
import { Component } from '@angular/core'; | ||
import { TestBed } from '@angular/core/testing'; | ||
|
||
/** | ||
* Testing function which executes inside of an injectionContext and provides Change Detection trigger. | ||
* | ||
* It looks like, we need to have at least one component to execute effects. | ||
* AppRef::tick() does not work. Only ComponentFixture::detectChanges() seems to do the job. | ||
* withEffects renders a TestComponent and executes the tests inside an injectionContext. | ||
* This allows us to generate effects very easily. | ||
*/ | ||
export const withEffect = | ||
markostanimirovic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
(testFn: (detectChanges: () => void) => void): (() => void) => | ||
() => { | ||
const fixture = TestBed.configureTestingModule({ | ||
imports: [TestComponent], | ||
}).createComponent(TestComponent); | ||
|
||
TestBed.runInInjectionContext(() => testFn(() => fixture.detectChanges())); | ||
}; | ||
|
||
@Component({ | ||
template: '', | ||
standalone: true, | ||
}) | ||
class TestComponent {} |
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.
we should perform
deepFreeze
only in dev mode for better performanceThere 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.
Still working on that