Skip to content

Commit

Permalink
fix: accepting undefined as an id
Browse files Browse the repository at this point in the history
It allows better usage of createSelector function
  • Loading branch information
satanTime committed Jun 5, 2020
1 parent 7eff890 commit 7978482
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .releaserc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,11 @@ plugins:
- - @semantic-release/github
- assets:
- tmp/*.tgz
successComment: |-
${nextRelease.version} has been released and contains a fix for the issue.
Feel free to reopen the issue or to submit a new one if you meet any problems.
failTitle: false
failComment: false
labels: false
- - @semantic-release/git
- message: "chore(release): ${nextRelease.version}\n\n${nextRelease.notes}"
52 changes: 51 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
* [rootEntities function](#rootentities-function)
* [relationships pipe operator](#relationships-pipe-operator)
* [rootEntityFlags options](#rootentityflags-options)
* [Releasing cache](#releasing-cache)
- [Releasing cache](#releasing-cache)
- [Usage with createSelector](#usage-with-createselector)
- [NGRX integration](#ngrx-store-integration)

## Problem

Expand Down Expand Up @@ -473,6 +475,54 @@ store.select(selectUser, 1).subsribe(user => {
});
```

### Usage with createSelector

Imagine there are a selector that returns id of a current user and a selector with relationships:
```typescript
export const selectCurrentUserId = createSelector(
selectUserFeature,
feature => feature.currentUserId,
);

export const selectUserWithCompany = rootEntity(
selectUserFeature,
relatedEntity(
selectCompanyFeature,
'companyId',
'company',
),
);
```

Then we have 2 options:

* combine them together via `createSelector` function
* usage of `switchMap`

#### combine them together via `createSelector` function

```typescript
export const selectCurrentUser = createSelector(
s => s, // selecting the whole store
selectCurrentUserId, // selecting the id of a current user
selectUserWithCompany, // selecting the user with desired relationships
);

store.select(selectCurrentUser).subscribe(user => {
// profit
});
```

#### usage of `switchMap`

```typescript
store.select(selectCurrentUserId).pipe( // selecting the id of a current user
switchMap(id => store.select(selectUserWithCompany, id)), // selecting the user with desired relationships
).subscribe(user => {
// profit
});
```

### Gathering information of a selector

Besides the `release` function every selector provides information about itself.
Expand Down
2 changes: 1 addition & 1 deletion src/rootEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export function rootEntity<STORE, ENTITY, TRANSFORMED>(
const cacheLevel = '0';
const cache: CACHE<STORE> = new Map();

const callback = (state: STORE, id: ID_TYPES) => {
const callback = (state: STORE, id: undefined | ID_TYPES) => {
if (!id) {
return;
}
Expand Down
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ export type ENTITY_SELECTOR<S = any, E = any> = {
};

export type HANDLER_ROOT_ENTITY<S, E, T, I> = ENTITY_SELECTOR<S, E> & {
(state: S, id: I): undefined | T;
(state: S, id: undefined | I): undefined | T;
release(): void;
};

export type HANDLER_ROOT_ENTITIES<S, E, T, I> = ENTITY_SELECTOR<S, E> & {
(state: S, id: Array<I>): Array<T>;
(state: S, id: undefined | Array<I>): Array<T>;
release(): void;
};

Expand Down

0 comments on commit 7978482

Please sign in to comment.