Skip to content
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(entity): add mapOne #2628

Merged
merged 5 commits into from
Jul 24, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ jobs:
- write_master_hash
- run:
name: Run Affected Builds
command: yarn nx affected --target=build --base=$(cat ~/project/master.txt) --head=$CIRCLE_SHA1 --with-deps
command: yarn nx affected --target=build --base=$(cat ~/project/master.txt) --head=$CIRCLE_SHA1 --with-deps --skip-nx-cache

build-bazel:
<<: *run_in_node
Expand Down Expand Up @@ -247,7 +247,7 @@ jobs:
steps:
- add_ssh_keys:
fingerprints:
- 'c9:c2:b4:5e:13:23:b6:6d:d8:29:3e:68:c6:40:9c:ec'
- "c9:c2:b4:5e:13:23:b6:6d:d8:29:3e:68:c6:40:9c:ec"
- checkout:
path: ~/docs
- restore_cache:
Expand Down Expand Up @@ -292,7 +292,7 @@ jobs:
steps:
- add_ssh_keys:
fingerprints:
- 'c9:c2:b4:5e:13:23:b6:6d:d8:29:3e:68:c6:40:9c:ec'
- "c9:c2:b4:5e:13:23:b6:6d:d8:29:3e:68:c6:40:9c:ec"
- checkout
- restore_cache:
keys:
Expand Down Expand Up @@ -340,7 +340,7 @@ jobs:
steps:
- add_ssh_keys:
fingerprints:
- 'c9:c2:b4:5e:13:23:b6:6d:d8:29:3e:68:c6:40:9c:ec'
- "c9:c2:b4:5e:13:23:b6:6d:d8:29:3e:68:c6:40:9c:ec"
- checkout
- restore_cache:
keys:
Expand Down
23 changes: 23 additions & 0 deletions modules/entity/spec/sorted_state_adapter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,29 @@ describe('Sorted State Adapter', () => {
});
});

it('should let you map over one entity by id in the state', () => {
const withMany = adapter.setAll([TheGreatGatsby, AClockworkOrange], state);

const withUpdates = adapter.mapOne(
{
id: TheGreatGatsby.id,
map: (entity) => ({ ...entity, title: 'Updated ' + entity.title }),
},
withMany
);

expect(withUpdates).toEqual({
ids: [AClockworkOrange.id, TheGreatGatsby.id],
entities: {
[TheGreatGatsby.id]: {
...TheGreatGatsby,
title: 'Updated ' + TheGreatGatsby.title,
},
[AClockworkOrange.id]: AClockworkOrange,
},
});
});

it('should let you add one entity to the state with upsert()', () => {
const withOneEntity = adapter.upsertOne(TheGreatGatsby, state);
expect(withOneEntity).toEqual({
Expand Down
23 changes: 23 additions & 0 deletions modules/entity/spec/unsorted_state_adapter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,29 @@ describe('Unsorted State Adapter', () => {
});
});

it('should let you map over one entity by id in the state', () => {
const withMany = adapter.setAll([TheGreatGatsby, AClockworkOrange], state);

const withUpdates = adapter.mapOne(
{
id: TheGreatGatsby.id,
map: (entity) => ({ ...entity, title: 'Updated ' + entity.title }),
},
withMany
);

expect(withUpdates).toEqual({
ids: [TheGreatGatsby.id, AClockworkOrange.id],
entities: {
[AClockworkOrange.id]: AClockworkOrange,
[TheGreatGatsby.id]: {
...TheGreatGatsby,
title: 'Updated ' + TheGreatGatsby.title,
},
},
});
});

it('should let you add one entity to the state with upsert()', () => {
const withOneEntity = adapter.upsertOne(TheGreatGatsby, state);
expect(withOneEntity).toEqual({
Expand Down
12 changes: 12 additions & 0 deletions modules/entity/src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ export type Predicate<T> = (entity: T) => boolean;

export type EntityMap<T> = (entity: T) => T;

export interface EntityMapOneNum<T> {
id: number;
map: EntityMap<T>;
}

export interface EntityMapOneStr<T> {
id: string;
map: EntityMap<T>;
}

export interface EntityState<T> {
ids: string[] | number[];
entities: Dictionary<T>;
Expand Down Expand Up @@ -64,6 +74,8 @@ export interface EntityStateAdapter<T> {
upsertOne<S extends EntityState<T>>(entity: T, state: S): S;
upsertMany<S extends EntityState<T>>(entities: T[], state: S): S;

mapOne<S extends EntityState<T>>(map: EntityMapOneNum<T>, state: S): S;
mapOne<S extends EntityState<T>>(map: EntityMapOneStr<T>, state: S): S;
map<S extends EntityState<T>>(map: EntityMap<T>, state: S): S;
}

Expand Down
21 changes: 21 additions & 0 deletions modules/entity/src/sorted_state_adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
EntityStateAdapter,
Update,
EntityMap,
EntityMapOneNum,
EntityMapOneStr,
} from './models';
import { createStateOperator, DidMutate } from './state_adapter';
import { createUnsortedStateAdapter } from './unsorted_state_adapter';
Expand Down Expand Up @@ -135,6 +137,24 @@ export function createSortedStateAdapter<T>(selectId: any, sort: any): any {
return updateManyMutably(updates, state);
}

function mapOneMutably(map: EntityMapOneNum<T>, state: R): DidMutate;
function mapOneMutably(map: EntityMapOneStr<T>, state: R): DidMutate;
function mapOneMutably({ map, id }: any, state: any): DidMutate {
const entity = state.entities[id];
if (!entity) {
return DidMutate.None;
}

const updatedEntity = map(entity);
return updateOneMutably(
{
id: id,
changes: updatedEntity,
},
state
);
}

function upsertOneMutably(entity: T, state: R): DidMutate;
function upsertOneMutably(entity: any, state: any): DidMutate {
return upsertManyMutably([entity], state);
Expand Down Expand Up @@ -218,5 +238,6 @@ export function createSortedStateAdapter<T>(selectId: any, sort: any): any {
updateMany: createStateOperator(updateManyMutably),
upsertMany: createStateOperator(upsertManyMutably),
map: createStateOperator(mapMutably),
mapOne: createStateOperator(mapOneMutably),
};
}
21 changes: 21 additions & 0 deletions modules/entity/src/unsorted_state_adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
Update,
Predicate,
EntityMap,
EntityMapOneNum,
EntityMapOneStr,
} from './models';
import { createStateOperator, DidMutate } from './state_adapter';
import { selectIdValue } from './utils';
Expand Down Expand Up @@ -172,6 +174,24 @@ export function createUnsortedStateAdapter<T>(selectId: IdSelector<T>): any {
return updateManyMutably(updates, state);
}

function mapOneMutably(map: EntityMapOneNum<T>, state: R): DidMutate;
function mapOneMutably(map: EntityMapOneStr<T>, state: R): DidMutate;
function mapOneMutably({ map, id }: any, state: any): DidMutate {
const entity = state.entities[id];
if (!entity) {
return DidMutate.None;
}

const updatedEntity = map(entity);
return updateOneMutably(
{
id: id,
changes: updatedEntity,
},
state
);
}

function upsertOneMutably(entity: T, state: R): DidMutate;
function upsertOneMutably(entity: any, state: any): DidMutate {
return upsertManyMutably([entity], state);
Expand Down Expand Up @@ -220,5 +240,6 @@ export function createUnsortedStateAdapter<T>(selectId: IdSelector<T>): any {
removeOne: createStateOperator(removeOneMutably),
removeMany: createStateOperator(removeManyMutably),
map: createStateOperator(mapMutably),
mapOne: createStateOperator(mapOneMutably),
};
}
25 changes: 17 additions & 8 deletions projects/ngrx.io/angular.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"$schema":
"./node_modules/@angular-devkit/core/src/workspace/workspace-schema.json",
"$schema": "./node_modules/@angular-devkit/core/src/workspace/workspace-schema.json",
"version": 1,
"cli": {
"packageManager": "yarn"
"packageManager": "yarn",
"analytics": false
},
"newProjectRoot": "content/examples",
"projects": {
Expand Down Expand Up @@ -47,7 +47,9 @@
"output": "/assets/js"
}
],
"styles": ["src/styles.scss"],
"styles": [
"src/styles.scss"
],
"scripts": []
},
"configurations": {
Expand Down Expand Up @@ -117,7 +119,9 @@
"polyfills": "src/polyfills.ts",
"tsConfig": "src/tsconfig.spec.json",
"scripts": [],
"styles": ["src/styles.scss"],
"styles": [
"src/styles.scss"
],
"assets": [
"src/assets",
"src/generated",
Expand All @@ -140,7 +144,10 @@
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": ["src/tsconfig.app.json", "src/tsconfig.spec.json"],
"tsConfig": [
"src/tsconfig.app.json",
"src/tsconfig.spec.json"
],
"exclude": []
}
}
Expand All @@ -162,7 +169,9 @@
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": ["tests/e2e/tsconfig.e2e.json"],
"tsConfig": [
"tests/e2e/tsconfig.e2e.json"
],
"exclude": []
}
}
Expand All @@ -179,4 +188,4 @@
"prefix": "aio"
}
}
}
}