Skip to content

Commit

Permalink
Merge pull request #14 from guillotinaweb/resolve-many
Browse files Browse the repository at this point in the history
Resolve many
  • Loading branch information
Mathilde Pellerin authored Feb 7, 2020
2 parents 5085fe0 + e9d541c commit 26abdc0
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 1.1.1 (2020-02-05)

- Allow to select children
- Allow to resolve several contexts at once

# 1.1.0 (2020-01-28)

- Tile support
Expand Down
2 changes: 1 addition & 1 deletion projects/ngx-state-traverser/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@guillotinaweb/ngx-state-traverser",
"version": "1.1.0",
"version": "1.1.1",
"license": "MIT",
"author": {
"name": "Eric Brehault",
Expand Down
7 changes: 7 additions & 0 deletions projects/ngx-state-traverser/src/lib/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export namespace TraverserActions {
Traverse = '[Traversal] Traverse',
ResolveContext = '[Traversal] Resolve context',
Resolve = '[Traversal] Resolve',
ResolveMany = '[Traversal] Resolve many',
CleanTraverserResources = '[Traversal] Clean resources',
UpdateTraverserResource = '[Traversal] Update resources',
LoadTile = '[Traversal] Load tile',
Expand All @@ -28,6 +29,11 @@ export namespace TraverserActions {
constructor(readonly payload: {path: string, object: any}) {}
}

export class ResolveMany implements Action {
readonly type = Types.ResolveMany;
constructor(readonly payload: {path: string, object: any}[]) {}
}

export class CleanTraverserResources implements Action {
readonly type = Types.CleanTraverserResources;
constructor(readonly payload: string[]) {}
Expand All @@ -52,6 +58,7 @@ export namespace TraverserActions {
| Traverse
| ResolveContext
| Resolve
| ResolveMany
| CleanTraverserResources
| UpdateTraverserResource
| LoadTile
Expand Down
17 changes: 17 additions & 0 deletions projects/ngx-state-traverser/src/lib/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,23 @@ export function reducer(state = initialState, action: TraverserActions.Actions):
},
};
}
case TraverserActions.Types.ResolveMany: {
const collection = action.payload.reduce((all, current) => {
let path = current.path;
if (!!path && path.endsWith('/')) {
path = path.slice(0, -1);
}
all[path] = current.object;
return all;
}, {});
return {
...state,
collection: {
...state.collection,
...collection,
},
};
}
case TraverserActions.Types.CleanTraverserResources: {
const exactPathes = action.payload.filter(path => !path.endsWith('*'));
const startPathes = action.payload.filter(path => path.endsWith('*')).map(path => path.slice(0, -1));
Expand Down
15 changes: 15 additions & 0 deletions projects/ngx-state-traverser/src/lib/selector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,21 @@ export namespace TraverserSelectors {
}
);

export const getContextChildren = createSelector(
traversalSelector,
getContextPath,
(
state: TraversingState,
contextPath: string,
): { [key: string]: any }[] => Object.entries(state.collection).reduce((children, [id, obj]) => {
const parentPath = id.substring(0, id.lastIndexOf('/'));
if (parentPath === contextPath) {
children.push(obj);
}
return children;
}, [] as { [key: string]: any }[])
);

export const getAncestors = createSelector(
traversalSelector,
(state: TraversingState, path: string): ContextOrMissing[] => {
Expand Down

0 comments on commit 26abdc0

Please sign in to comment.