From dfbc7aa3cb2d4d69c792df5f0a9f9c802d15a79d Mon Sep 17 00:00:00 2001 From: Siou Date: Thu, 26 Jul 2018 15:35:42 +0800 Subject: [PATCH] refactor onDeployManager --- src/components/Provider.js | 15 ++++-------- src/hocs/cache.js | 4 ++-- src/hocs/connectContext.js | 4 ++-- src/hocs/context.js | 4 ++-- src/hocs/onDeploy.js | 4 ++-- src/hocs/title.js | 4 ++-- src/onDeployManager/index.js | 33 +++++++++++++++------------ test/onDeploy/onDeployManager.test.js | 14 ++++++------ 8 files changed, 41 insertions(+), 41 deletions(-) diff --git a/src/components/Provider.js b/src/components/Provider.js index 266de9ec..a03c190a 100644 --- a/src/components/Provider.js +++ b/src/components/Provider.js @@ -130,15 +130,10 @@ export default class Provider extends React.PureComponent { }); } - onDeploy = (actions: Array>) => { - 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 }); } @@ -149,9 +144,9 @@ export default class Provider extends React.PureComponent { 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 diff --git a/src/hocs/cache.js b/src/hocs/cache.js index 10d998a4..a716806f 100644 --- a/src/hocs/cache.js +++ b/src/hocs/cache.js @@ -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} /> ); } diff --git a/src/hocs/connectContext.js b/src/hocs/connectContext.js index f941efd1..2c68fea4 100644 --- a/src/hocs/connectContext.js +++ b/src/hocs/connectContext.js @@ -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} /> )} diff --git a/src/hocs/context.js b/src/hocs/context.js index ff2cb0be..f790f53c 100644 --- a/src/hocs/context.js +++ b/src/hocs/context.js @@ -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: () => {} } diff --git a/src/hocs/onDeploy.js b/src/hocs/onDeploy.js index ce262b74..7ac0cc68 100644 --- a/src/hocs/onDeploy.js +++ b/src/hocs/onDeploy.js @@ -9,7 +9,7 @@ type Props = { keyName: string, routes: Array, 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, }; @@ -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); diff --git a/src/hocs/title.js b/src/hocs/title.js index 6270505a..d8b696c5 100644 --- a/src/hocs/title.js +++ b/src/hocs/title.js @@ -57,8 +57,8 @@ export default function withTitleAndDescription(Com: React.ComponentType<*>) { reset, query, updateQuery, - onDeploy, - removeOnDeploy + // onDeploy, + // removeOnDeploy }} > { - 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 => { + 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); } \ No newline at end of file diff --git a/test/onDeploy/onDeployManager.test.js b/test/onDeploy/onDeployManager.test.js index 16deadcd..e24c8c15 100644 --- a/test/onDeploy/onDeployManager.test.js +++ b/test/onDeploy/onDeployManager.test.js @@ -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({}); }); });