Skip to content
This repository has been archived by the owner on Apr 24, 2023. It is now read-only.

Commit

Permalink
refactor onDeployManager
Browse files Browse the repository at this point in the history
  • Loading branch information
abz53378 committed Jul 26, 2018
1 parent 668069a commit dfbc7aa
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 41 deletions.
15 changes: 5 additions & 10 deletions src/components/Provider.js
Expand Up @@ -130,15 +130,10 @@ export default class Provider extends React.PureComponent<Props, State> {
});
}

onDeploy = (actions: Array<Action<ActionType>>) => {
return actions.map(action => {
const {key, id, value} = action.payload;
action.payload.value = this.onDeployManager.execute({
key,
id,
value
});
return action;
onDeploy = (key: string, id?: string, value: any) => {
return this.onDeployManager.execute({
key,
value
});
}

Expand All @@ -149,9 +144,9 @@ export default class Provider extends React.PureComponent<Props, State> {
return Promise.resolve();
}
actions = removeIdInCreateArray(actions);
actions = this.onDeploy(actions);
const mutation = objectToQueries(actionToMutation(actions[0]), false);
const variables = actionsToVariables(actions, schema);
variables.payload = this.onDeploy(key, fromJS(variables.payload)).toJS();
return client.mutate({
mutation: gql`${mutation}`,
variables
Expand Down
4 changes: 2 additions & 2 deletions src/hocs/cache.js
Expand Up @@ -230,8 +230,8 @@ export default function withCache(Com: React.ComponentType<*>, options: {
reset={this.reset}
subscribe={this.subscribe}
updateQuery={this.updateQuery}
onDeploy={onDeploy}
removeOnDeploy={removeOnDeploy}
// onDeploy={onDeploy}
// removeOnDeploy={removeOnDeploy}
/>
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/hocs/connectContext.js
Expand Up @@ -38,8 +38,8 @@ export default function connectContext(Com: React.ComponentType<*>) {
request={context.request}
deploy={context.deploy}
updateQuery={context.updateQuery}
onDeploy={context.onDeploy}
removeOnDeploy={context.removeOnDeploy}
// onDeploy={context.onDeploy}
// removeOnDeploy={context.removeOnDeploy}
/>
)}
</HOCContext.Consumer>
Expand Down
4 changes: 2 additions & 2 deletions src/hocs/context.js
Expand Up @@ -8,8 +8,8 @@ export const defaultContext = {
request: () => {throw new Error('there is no request method');},
deploy: () => {throw new Error('there is no deploy method');},
reset: () => {throw new Error('there is not reset method');},
onDeploy: () => {throw new Error('there is not onDeploy method');},
removeOnDeploy: () => {throw new Error('there is not removeOnDeploy method');},
// onDeploy: () => {throw new Error('there is not onDeploy method');},
// removeOnDeploy: () => {throw new Error('there is not removeOnDeploy method');},
updateQuery: () => {}
}

Expand Down
4 changes: 2 additions & 2 deletions src/hocs/onDeploy.js
Expand Up @@ -9,7 +9,7 @@ type Props = {
keyName: string,
routes: Array<string>,
pattern: string,
onDeploy: (key: string, id: ?string, callback: any => any) => void,
onDeploy: (key: string, callback: any => any) => void,
removeOnDeploy: (key: string, id: ?string) => void,
rootValue: any,
};
Expand All @@ -33,7 +33,7 @@ export default function withOndeploy(Com: React.ComponentType<*>) {

onDeploy = (callback: Function) => {
const {onDeploy, refId} = this.props;
onDeploy(this.key, this.id, v => {
onDeploy(this.key, v => {
let restPathArr = refId.getPathArr();
if (this.id) {
restPathArr = restPathArr.slice(2);
Expand Down
4 changes: 2 additions & 2 deletions src/hocs/title.js
Expand Up @@ -57,8 +57,8 @@ export default function withTitleAndDescription(Com: React.ComponentType<*>) {
reset,
query,
updateQuery,
onDeploy,
removeOnDeploy
// onDeploy,
// removeOnDeploy
}}
>
<Context.Provider value={{
Expand Down
33 changes: 19 additions & 14 deletions src/onDeployManager/index.js
Expand Up @@ -3,33 +3,38 @@
import {set, unset, get} from 'lodash';

export class OnDeployManager {
_map: Object = {
_map: {
[string]: {
[string]: Function
}
};

_map = {}
execute = ({
key,
id,
value
}: {
key: string,
id: ?string,
value: any
}): any => {
const callback = this.findCallback(key, id);
return callback(value);
const callbacks = this.findCallback(key);
return callbacks.reduce((result, callback) => callback(result), value);
}

findCallback = (key: string, id: ?string) => {
let callback: any = v => v;
callback = get(this._map, [key, id || 'DEFAULT'], callback);
return callback;
findCallback = (key: string): Array<any> => {
return Object.values(get(this._map, [key], {}));
}

registerCallback = (key: string, id: ?string, callback: Function) => {
set(this._map, [key, id || 'DEFAULT'], callback);
registerCallback = (key: string, callback: Function) => {
const callbackId = randomStr();
set(this._map, [key, callbackId], callback);
return callbackId;
}

unregisterCallback = (key: string, id: ?string) => {
unset(this._map, [key, id || 'DEFAULT']);
unregisterCallback = (key: string, callbackId: ?string) => {
unset(this._map, [key, callbackId]);
}
}

function randomStr() {
return Math.random().toString(36).substr(2, 6);
}
14 changes: 7 additions & 7 deletions test/onDeploy/onDeployManager.test.js
Expand Up @@ -9,20 +9,20 @@ describe('onDeployManager class', () => {

it('registerCallback', () => {
const callback = () => {};
onDeployManager.registerCallback('posts', 'id1', callback);
expect(onDeployManager._map.posts.id1).toBe(callback);
const callbackId = onDeployManager.registerCallback('posts', callback);
expect(onDeployManager._map.posts[callbackId]).toEqual(callback);
});

it('findCallback', () => {
const callback = () => {};
onDeployManager.registerCallback('posts', 'id1', callback);
expect(onDeployManager.findCallback('posts', 'id1')).toBe(callback);
onDeployManager.registerCallback('posts', callback);
expect(onDeployManager.findCallback('posts')).toEqual([callback]);
});

it('unregisterCallback', () => {
const callback = () => {};
onDeployManager.registerCallback('posts', 'id1', callback);
onDeployManager.unregisterCallback('posts', 'id1');
expect(onDeployManager._map.posts.id1).toBeUndefined();
const callbackId = onDeployManager.registerCallback('posts', callback);
onDeployManager.unregisterCallback('posts', callbackId);
expect(onDeployManager._map.posts.id1).toEqual({});
});
});

0 comments on commit dfbc7aa

Please sign in to comment.