-
-
Notifications
You must be signed in to change notification settings - Fork 204
WIP Rx state/connect partial obs and signals #1644
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
Closed
michaelbe812
wants to merge
10
commits into
rx-angular:main
from
michaelbe812:rx-state/connect-partial-obs-and-signals
Closed
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
3e95988
restore tutorial
michaelbe812 a795d53
Merge branch 'rx-angular:main' into main
michaelbe812 a17546f
feat(state): allow to connect mixed partial of observables and signals
michaelbe812 b9b9d46
feat(state): cleanup
michaelbe812 11a8536
feat(state): cleanup
michaelbe812 f5b9d25
feat(state): use correct durationSelector for coalescing
michaelbe812 518ae5f
docs(state): revert change in docs
michaelbe812 9b0abe5
refactor(state): improve convert-partials-to-observable helper
michaelbe812 a398aa5
Merge remote-tracking branch 'origin' into rx-state/connect-partial-o…
michaelbe812 2a57f56
feat(state): update jsdoc
michaelbe812 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
26 changes: 26 additions & 0 deletions
26
libs/state/selections/spec/is-partial-of-signals-or-observables-guard.spec.ts
This file contains hidden or 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 { isPartialOfSignalsOrObservablesGuard } from '@rx-angular/state/selections'; | ||
import { of } from 'rxjs'; | ||
import { signal } from '@angular/core'; | ||
|
||
describe('isPartialOfSignalsOrObservablesGuard', () => { | ||
it('should return false if arg is no object', () => { | ||
expect(isPartialOfSignalsOrObservablesGuard('some')).toEqual(false); | ||
}); | ||
it('should return true if all props are either observables or signals', () => { | ||
expect( | ||
isPartialOfSignalsOrObservablesGuard({ | ||
a: of(true), | ||
b: signal(10), | ||
}) | ||
).toEqual(true); | ||
}); | ||
it('should return false if one prop is neither a observable or signal', () => { | ||
expect( | ||
isPartialOfSignalsOrObservablesGuard({ | ||
a: of(true), | ||
b: signal(10), | ||
c: true, | ||
}) | ||
).toEqual(false); | ||
}); | ||
}); |
This file contains hidden or 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 hidden or 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 hidden or 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,28 @@ | ||
import { convertPartialsToObservable } from '../src/lib/convert-partials-to.observable'; | ||
import { EMPTY, NEVER, of } from 'rxjs'; | ||
import { signal } from '@angular/core'; | ||
import { TestBed } from '@angular/core/testing'; | ||
|
||
describe('convertPartialsToObservable', () => { | ||
it('should emit once initially and combine all partials', (done) => { | ||
TestBed.runInInjectionContext(() => { | ||
const result = convertPartialsToObservable<TestModel, keyof TestModel>({ | ||
a: of('Hi'), | ||
b: signal(10), | ||
// c: NEVER, | ||
// d: EMPTY | ||
}); | ||
|
||
result.subscribe((value) => { | ||
expect(value).toEqual({ a: 'Hi', b: 10, c: undefined, d: undefined }); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
interface TestModel { | ||
a: string; | ||
b: number; | ||
c: boolean; | ||
d: string; | ||
} |
This file contains hidden or 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 hidden or 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 @@ | ||
import { combineLatest, from, isObservable, Observable, startWith } from 'rxjs'; | ||
import { Injector, isSignal, Signal } from '@angular/core'; | ||
import { toObservable } from '@angular/core/rxjs-interop'; | ||
import { map } from 'rxjs/operators'; | ||
import { coalesceWith } from '@rx-angular/cdk/coalescing'; | ||
import { Promise } from '@rx-angular/cdk/zone-less/browser'; | ||
|
||
export function convertPartialsToObservable< | ||
T extends object, | ||
K extends keyof T | ||
>( | ||
input: Partial<{ [K: string]: Observable<T[K]> | Signal<T[K]> }>, | ||
injector?: Injector | ||
): Observable<Partial<T>> { | ||
const keys = Object.keys(input); | ||
|
||
const observables = keys.map((key) => { | ||
const obsOrSignal = input[key]; | ||
|
||
if (isObservable(obsOrSignal)) { | ||
return obsOrSignal?.pipe(startWith(undefined)); | ||
} | ||
if (isSignal(obsOrSignal)) { | ||
return toObservable(obsOrSignal, { injector }); | ||
} | ||
|
||
throw new TypeError(`Expected type Observable or Signal for key "${key}"`); | ||
}); | ||
|
||
return combineLatest(observables).pipe( | ||
coalesceWith(from(Promise.resolve())), | ||
map((values) => { | ||
const result: Partial<T> = {} as Partial<T>; | ||
for (let i = 0; i < keys.length; i++) { | ||
result[keys[i]] = values[i]; | ||
} | ||
return result; | ||
}) | ||
); | ||
} |
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.