Skip to content

Commit

Permalink
feat(core/utils): traverse passes trees to callback
Browse files Browse the repository at this point in the history
  • Loading branch information
rafamel committed Oct 14, 2019
1 parent b826046 commit e112fa9
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 24 deletions.
6 changes: 4 additions & 2 deletions packages/core/src/create/application/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
ErrorType
} from '~/types';
import clone from 'lodash.clonedeep';
import { traverse, isElementType } from '~/utils';
import { traverse, isElementType, isElementService } from '~/utils';
import camelcase from 'camelcase';
import serviceIntercepts from './service-intercepts';
import { mergeServiceTypes } from './merge';
Expand All @@ -19,6 +19,8 @@ import { error } from '../types';

// TODO: validate collection object (ajv) + check schemas are valid
// TODO: adapters rely on resolve() existing on all services. Separate normalization from application?
// TODO: check scope names don't collide with services
// TODO: check no service or type has empty name

/**
* Returns a new object instance of a collection; prepares a collection to be used by an adapter:
Expand Down Expand Up @@ -107,7 +109,7 @@ export default function application(
serviceIntercepts(fullName, service, types.source);
mergeServiceTypes(fullName, service, types, opts);
}
} else {
} else if (isElementService(element)) {
const fullName =
opts.prefixScope && path[path.length - 3]
? opts.transform(path[path.length - 3], false) + name
Expand Down
21 changes: 16 additions & 5 deletions packages/core/src/utils/is.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,10 @@ import {
ErrorType,
RequestType,
ResponseType,
TypeImplementation
TypeImplementation,
ScopeTree
} from '~/types';

export function isTreeCollection(tree: Tree): tree is CollectionTree {
return Object.hasOwnProperty.call(tree, 'types');
}

export function isElement(item: any): item is Element {
return (
typeof item === 'object' &&
Expand All @@ -26,6 +23,20 @@ export function isElement(item: any): item is Element {
);
}

export function isElementTree(element: Element): element is Tree {
return ['collection', 'scope'].includes(element.kind);
}

export function isTreeCollection(tree: Tree): tree is CollectionTree {
return (
tree.kind === 'collection' && Object.hasOwnProperty.call(tree, 'types')
);
}

export function isTreeScope(tree: Tree): tree is ScopeTree {
return tree.kind === 'scope';
}

export function isElementService(element: Element): element is Service {
return ['query', 'mutation', 'subscription'].includes(element.kind);
}
Expand Down
29 changes: 12 additions & 17 deletions packages/core/src/utils/traverse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {
Type,
QueryService,
MutationService,
SubscriptionService
SubscriptionService,
Element
} from '~/types';
import { isTreeCollection } from './is';

Expand All @@ -16,11 +17,7 @@ function traverse<
S extends SubscriptionService
>(
tree: Tree<Q, M, S>,
cb: (
element: Service<Q, M, S> | Type<Q, S>,
path: string[],
route: string[]
) => void
cb: (element: Element<Q, M, S>, path: string[], route: string[]) => void
): void;
function traverse<
Q extends QueryService,
Expand All @@ -29,17 +26,13 @@ function traverse<
>(
tree: Tree<Q, M, S>,
options: { deep?: boolean; children?: boolean; inline?: boolean },
cb: (
element: Service<Q, M, S> | Type<Q, S>,
path: string[],
route: string[]
) => void
cb: (element: Element<Q, M, S>, path: string[], route: string[]) => void
): void;
function traverse(tree: Tree, b: any, c?: any): void {
const options = c ? b : {};
const cb = c || b;

return traverseEach(
return traverseTree(
[],
[],
tree,
Expand All @@ -48,13 +41,15 @@ function traverse(tree: Tree, b: any, c?: any): void {
);
}

export function traverseEach(
export function traverseTree(
path: string[],
route: string[],
tree: Tree,
options: { deep: boolean; children: boolean; inline: boolean },
cb: (element: Service | Type, path: string[], route: string[]) => void
cb: (element: Element, path: string[], route: string[]) => void
): void {
cb(tree, path, route);

if (isTreeCollection(tree)) {
const typeKeys = Object.keys(tree.types);
for (const key of typeKeys) {
Expand Down Expand Up @@ -82,7 +77,7 @@ export function traverseEach(
if (options.deep && Object.hasOwnProperty.call(tree, 'scopes')) {
const scopeNames = Object.keys(tree.scopes);
for (const scopeName of scopeNames) {
traverseEach(
traverseTree(
path.concat(['scopes', scopeName]),
route.concat([scopeName]),
tree.scopes[scopeName],
Expand All @@ -98,7 +93,7 @@ export function traverseType(
route: string[],
type: Type,
options: { deep: boolean; children: boolean; inline: boolean },
cb: (element: Service | Type, path: string[], route: string[]) => void
cb: (element: Element, path: string[], route: string[]) => void
): void {
cb(type, path, route);

Expand Down Expand Up @@ -127,7 +122,7 @@ export function traverseService(
route: string[],
service: Service,
options: { deep: boolean; children: boolean; inline: boolean },
cb: (element: Service | Type, path: string[], route: string[]) => void
cb: (element: Element, path: string[], route: string[]) => void
): void {
cb(service, path, route);

Expand Down

0 comments on commit e112fa9

Please sign in to comment.