This repository has been archived by the owner on Jan 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactored selectors module; added selectors docs section
- Loading branch information
1 parent
6ad87ab
commit 19a8abf
Showing
7 changed files
with
109 additions
and
88 deletions.
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
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,10 +1,10 @@ | ||
{ | ||
"name": "juliette-react", | ||
"version": "0.12.0", | ||
"version": "0.14.0", | ||
"main": "public-api.js", | ||
"types": "public-api.d.ts", | ||
"peerDependencies": { | ||
"juliette": "0.12.0", | ||
"juliette": "0.14.0", | ||
"react": "^16.13.1" | ||
} | ||
} |
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,95 +1,92 @@ | ||
import { Selector } from './models'; | ||
|
||
export function createFeatureSelector<T, R extends T[keyof T]>( | ||
featureKey: keyof T, | ||
): Selector<T, R> { | ||
return state => state[featureKey] as R; | ||
} | ||
|
||
export function createSelector<T, R, S>( | ||
s: Selector<T, S>, | ||
export function composeSelectors<T, R, S>( | ||
selectors: [Selector<T, S>], | ||
composer: Selector<S, R>, | ||
): Selector<T, R>; | ||
|
||
export function createSelector<T, R, S1, S2>( | ||
s1: Selector<T, S1>, | ||
s2: Selector<T, S2>, | ||
export function composeSelectors<T, R, S1, S2>( | ||
selectors: [Selector<T, S1>, Selector<T, S2>], | ||
composer: (s1: S1, s2: S2) => R, | ||
): Selector<T, R>; | ||
|
||
export function createSelector<T, R, S1, S2, S3>( | ||
s1: Selector<T, S1>, | ||
s2: Selector<T, S2>, | ||
s3: Selector<T, S3>, | ||
export function composeSelectors<T, R, S1, S2, S3>( | ||
selectors: [Selector<T, S1>, Selector<T, S2>, Selector<T, S3>], | ||
composer: (s1: S1, s2: S2, s3: S3) => R, | ||
): Selector<T, R>; | ||
|
||
export function createSelector<T, R, S1, S2, S3, S4>( | ||
s1: Selector<T, S1>, | ||
s2: Selector<T, S2>, | ||
s3: Selector<T, S3>, | ||
s4: Selector<T, S4>, | ||
export function composeSelectors<T, R, S1, S2, S3, S4>( | ||
selectors: [Selector<T, S1>, Selector<T, S2>, Selector<T, S3>, Selector<T, S4>], | ||
composer: (s1: S1, s2: S2, s3: S3, s4: S4) => R, | ||
): Selector<T, R>; | ||
|
||
export function createSelector<T, R, S1, S2, S3, S4, S5>( | ||
s1: Selector<T, S1>, | ||
s2: Selector<T, S2>, | ||
s3: Selector<T, S3>, | ||
s4: Selector<T, S4>, | ||
s5: Selector<T, S5>, | ||
export function composeSelectors<T, R, S1, S2, S3, S4, S5>( | ||
selectors: [Selector<T, S1>, Selector<T, S2>, Selector<T, S3>, Selector<T, S4>, Selector<T, S5>], | ||
composer: (s1: S1, s2: S2, s3: S3, s4: S4, s5: S5) => R, | ||
): Selector<T, R>; | ||
|
||
export function createSelector<T, R, S1, S2, S3, S4, S5, S6>( | ||
s1: Selector<T, S1>, | ||
s2: Selector<T, S2>, | ||
s3: Selector<T, S3>, | ||
s4: Selector<T, S4>, | ||
s5: Selector<T, S5>, | ||
s6: Selector<T, S6>, | ||
export function composeSelectors<T, R, S1, S2, S3, S4, S5, S6>( | ||
selectors: [ | ||
Selector<T, S1>, | ||
Selector<T, S2>, | ||
Selector<T, S3>, | ||
Selector<T, S4>, | ||
Selector<T, S5>, | ||
Selector<T, S6>, | ||
], | ||
composer: (s1: S1, s2: S2, s3: S3, s4: S4, s5: S5, s6: S6) => R, | ||
): Selector<T, R>; | ||
|
||
export function createSelector<T, R, S1, S2, S3, S4, S5, S6, S7>( | ||
s1: Selector<T, S1>, | ||
s2: Selector<T, S2>, | ||
s3: Selector<T, S3>, | ||
s4: Selector<T, S4>, | ||
s5: Selector<T, S5>, | ||
s6: Selector<T, S6>, | ||
s7: Selector<T, S7>, | ||
export function composeSelectors<T, R, S1, S2, S3, S4, S5, S6, S7>( | ||
selectors: [ | ||
Selector<T, S1>, | ||
Selector<T, S2>, | ||
Selector<T, S3>, | ||
Selector<T, S4>, | ||
Selector<T, S5>, | ||
Selector<T, S6>, | ||
Selector<T, S7>, | ||
], | ||
composer: (s1: S1, s2: S2, s3: S3, s4: S4, s5: S5, s6: S6, s7: S7) => R, | ||
): Selector<T, R>; | ||
|
||
export function createSelector<T, R, S1, S2, S3, S4, S5, S6, S7, S8>( | ||
s1: Selector<T, S1>, | ||
s2: Selector<T, S2>, | ||
s3: Selector<T, S3>, | ||
s4: Selector<T, S4>, | ||
s5: Selector<T, S5>, | ||
s6: Selector<T, S6>, | ||
s7: Selector<T, S7>, | ||
s8: Selector<T, S8>, | ||
export function composeSelectors<T, R, S1, S2, S3, S4, S5, S6, S7, S8>( | ||
selectors: [ | ||
Selector<T, S1>, | ||
Selector<T, S2>, | ||
Selector<T, S3>, | ||
Selector<T, S4>, | ||
Selector<T, S5>, | ||
Selector<T, S6>, | ||
Selector<T, S7>, | ||
Selector<T, S8>, | ||
], | ||
composer: (s1: S1, s2: S2, s3: S3, s4: S4, s5: S5, s6: S6, s7: S7, s8: S8) => R, | ||
): Selector<T, R>; | ||
|
||
export function createSelector<T, R, S1, S2, S3, S4, S5, S6, S7, S8, S9>( | ||
s1: Selector<T, S1>, | ||
s2: Selector<T, S2>, | ||
s3: Selector<T, S3>, | ||
s4: Selector<T, S4>, | ||
s5: Selector<T, S5>, | ||
s6: Selector<T, S6>, | ||
s7: Selector<T, S7>, | ||
s8: Selector<T, S8>, | ||
s9: Selector<T, S9>, | ||
export function composeSelectors<T, R, S1, S2, S3, S4, S5, S6, S7, S8, S9>( | ||
selectors: [ | ||
Selector<T, S1>, | ||
Selector<T, S2>, | ||
Selector<T, S3>, | ||
Selector<T, S4>, | ||
Selector<T, S5>, | ||
Selector<T, S6>, | ||
Selector<T, S7>, | ||
Selector<T, S8>, | ||
Selector<T, S9>, | ||
], | ||
composer: (s1: S1, s2: S2, s3: S3, s4: S4, s5: S5, s6: S6, s7: S7, s8: S8, s9: S9) => R, | ||
): Selector<T, R>; | ||
|
||
export function createSelector<T, R>(...functions: ((...args: any) => any)[]): Selector<T, R> { | ||
const composer = functions.pop() as (...states: any[]) => R; | ||
const selectors = functions as Selector<T, any>[]; | ||
export function composeSelectors<T, R>( | ||
selectors: Selector<T, any>[], | ||
composer: (...states: any[]) => R, | ||
): Selector<T, R>; | ||
|
||
export function composeSelectors<T, R>( | ||
selectors: Selector<T, any>[], | ||
composer: (...states: any[]) => R, | ||
): Selector<T, R> { | ||
return state => composer(...selectors.map(selector => selector(state))); | ||
} |
12 changes: 6 additions & 6 deletions
12
projects/playground-ng/src/app/feature1/store/feature1.selectors.ts
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,12 +1,12 @@ | ||
import { createFeatureSelector, createSelector } from 'juliette'; | ||
import { composeSelectors } from 'juliette'; | ||
import { Feature1AppState, fromFeature1 } from './index'; | ||
import { selectUsers } from '../../store/selectors'; | ||
import { Selector } from '../../../../../juliette/src/lib/models'; | ||
|
||
export const selectFeature1State = createFeatureSelector<Feature1AppState, fromFeature1.State>( | ||
fromFeature1.featureKey, | ||
); | ||
export const selectFoo = createSelector(selectFeature1State, state => state.foo); | ||
export const selectFooWithUsers = createSelector(selectFoo, selectUsers, (foo, users) => ({ | ||
export const selectFeature1State: Selector<Feature1AppState, fromFeature1.State> = state => | ||
state[fromFeature1.featureKey]; | ||
export const selectFoo = composeSelectors([selectFeature1State], state => state.foo); | ||
export const selectFooWithUsers = composeSelectors([selectFoo, selectUsers], (foo, users) => ({ | ||
foo, | ||
users, | ||
})); |
9 changes: 4 additions & 5 deletions
9
projects/playground-ng/src/app/store/selectors/users.selectors.ts
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,8 +1,7 @@ | ||
import { createFeatureSelector, createSelector } from 'juliette'; | ||
import { Selector, composeSelectors } from 'juliette'; | ||
import { fromUsers } from '../handlers'; | ||
import { AppState } from '../index'; | ||
|
||
export const selectUsersState = createFeatureSelector<AppState, fromUsers.State>( | ||
fromUsers.featureKey, | ||
); | ||
export const selectUsers = createSelector(selectUsersState, state => state.users); | ||
export const selectUsersState: Selector<AppState, fromUsers.State> = state => | ||
state[fromUsers.featureKey]; | ||
export const selectUsers = composeSelectors([selectUsersState], state => state.users); |