Skip to content

Commit b69c6af

Browse files
committed
Refactor dispatching
1 parent 3dd3760 commit b69c6af

3 files changed

Lines changed: 94 additions & 54 deletions

File tree

src/framework/store/actions.js

Lines changed: 93 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ function transformFirstArgument (elementKey, transformScript, firstArgument) {
4949
return firstArgumentResult;
5050
}
5151

52-
function dispatchToComponent (taskEventName, props, payload, dispatch, helpers) {
52+
function dispatchToComponent (props, payload, dispatch, helpers) {
5353
if (props) {
5454
const {
5555
componentName, componentInstance, propertyName, forwardPath, componentKey, transformScript
@@ -138,30 +138,57 @@ function findEventTargets (events, type) {
138138
}
139139

140140
function executeUserFunctionDispatch (
141-
events, innerTasks, dispatchType, payload, dispatch, getState, helpers
141+
events, innerTasks, functionKey, functionName, dispatchPayloads, dispatch, getState, helpers
142142
) {
143-
let targetsCount = 0;
144-
// check if the user function dispatches any event
145-
const eventTargets = findEventTargets(events, dispatchType);
146-
if (eventTargets && eventTargets.length > 0) {
147-
targetsCount = eventTargets.length;
148-
eventTargets.forEach(eventTarget => {
149-
const { type: eventTargetType, props: eventTargetProps } = eventTarget;
150-
if (eventTargetType === COMPONENT_TYPE) {
151-
dispatchToComponent(dispatchType, eventTargetProps, payload, dispatch, helpers);
143+
const dispatchTypeKeys = dispatchPayloads ? Object.keys(dispatchPayloads) : null;
144+
let dispatchType;
145+
let payload;
146+
if (dispatchTypeKeys && dispatchTypeKeys.length > 0) {
147+
for (let i = 0; i < dispatchTypeKeys.length; i++) {
148+
dispatchType = dispatchTypeKeys[i];
149+
// check if the user function dispatches any event
150+
const eventTargets = findEventTargets(events, dispatchType);
151+
if (eventTargets && eventTargets.length > 0) {
152+
eventTargets.forEach(eventTarget => {
153+
const { type: eventTargetType, props: eventTargetProps } = eventTarget;
154+
if (eventTargetType === COMPONENT_TYPE) {
155+
payload = dispatchPayloads[dispatchType];
156+
if (process.env.NODE_ENV !== 'production') {
157+
sendDebugMessage({
158+
key: functionKey,
159+
eventType: constants.DEBUG_MSG_FUNCTION_FIRE_EVENT,
160+
eventName: dispatchType,
161+
outputData: payload,
162+
functionName: functionName,
163+
timestamp: Date.now(),
164+
});
165+
}
166+
dispatchToComponent(eventTargetProps, payload, dispatch, helpers);
167+
}
168+
});
169+
if (innerTasks[dispatchType] && innerTasks[dispatchType].length > 0) {
170+
payload = dispatchPayloads[dispatchType];
171+
if (process.env.NODE_ENV !== 'production') {
172+
sendDebugMessage({
173+
key: functionKey,
174+
eventType: constants.DEBUG_MSG_FUNCTION_FIRE_EVENT,
175+
eventName: dispatchType,
176+
outputData: payload,
177+
functionName: functionName,
178+
timestamp: Date.now(),
179+
});
180+
}
181+
innerTasks[dispatchType].forEach(task => {
182+
const { func } = task;
183+
func.apply(null, [payload])(dispatch, getState, helpers);
184+
});
185+
}
152186
}
153-
});
154-
if (innerTasks[dispatchType] && innerTasks[dispatchType].length > 0) {
155-
innerTasks[dispatchType].forEach(task => {
156-
const { func } = task;
157-
func.apply(null, [payload])(dispatch, getState, helpers);
158-
});
159187
}
160188
}
161-
return targetsCount;
162189
}
163190

164-
function createTasks (targets, taskEventName) {
191+
function createTasks (targets) {
165192
const tasks = [];
166193
if (targets && targets.length > 0) {
167194
targets.forEach(target => {
@@ -181,34 +208,31 @@ function createTasks (targets, taskEventName) {
181208
innerTasks[innerEvent.name] = innerTasks[innerEvent.name] || [];
182209
innerTasks[innerEvent.name] = [
183210
...innerTasks[innerEvent.name],
184-
...createTasks(userFunctionTargets, innerEvent.name)
211+
...createTasks(userFunctionTargets)
185212
];
186213
}
187214
}
188215
});
189216
}
190217
// create dispatchFunction in order to reuse its instance in the action function body
191-
const dispatchFunction = (dispatchType, payload, dispatch, getState, helpers) => {
218+
const dispatchFunction = (functionKey, functionName, dispatchPayloads, dispatch, getState, helpers) => {
192219
executeUserFunctionDispatch(
193-
events, innerTasks, dispatchType, payload, dispatch, getState, helpers
220+
events, innerTasks, functionKey, functionName, dispatchPayloads, dispatch, getState, helpers
194221
);
195222
};
196223
// this function is used to pass the error object caught by the exception caching
197224
// the function is called with null error object before each user function invocation
198225
// this will let user to do not worry about the clearing of the error object
199-
const caughtExceptionFunction = (error, dispatch, getState, helpers) => {
200-
if (process.env.NODE_ENV !== 'production' && error) {
201-
sendDebugMessage({
202-
key: props.functionKey,
203-
eventType: constants.DEBUG_MSG_FUNCTION_FIRE_EVENT,
204-
eventName: DISPATCH_ERROR_TYPE,
205-
outputData: error && error.message,
206-
functionName: props.functionName,
207-
timestamp: Date.now(),
208-
});
209-
}
226+
const caughtExceptionFunction = (functionKey, functionName, error, dispatch, getState, helpers) => {
210227
executeUserFunctionDispatch(
211-
events, innerTasks, DISPATCH_ERROR_TYPE, error, dispatch, getState, helpers
228+
events,
229+
innerTasks,
230+
functionKey,
231+
functionName,
232+
{[DISPATCH_ERROR_TYPE]: error ? error.message : null},
233+
dispatch,
234+
getState,
235+
helpers
212236
);
213237
if (error) {
214238
console.error(`[Framework] In "${props.functionName}" function ${error.message}.`);
@@ -240,30 +264,48 @@ function createTasks (targets, taskEventName) {
240264
const userFunctionInstance = func.apply(null, [firstArgument]);
241265
try {
242266
// dispatch caughtException as null to the assigned targets
243-
caughtExceptionFunction(null, dispatch, getState, helpers);
267+
caughtExceptionFunction(
268+
props.functionKey,
269+
props.functionName,
270+
null,
271+
dispatch,
272+
getState,
273+
helpers
274+
);
244275
// now execute dispatching of the events objects to the targets
245-
const userFunctionResult = userFunctionInstance((dispatchType, payload) => {
276+
const userFunctionResult = userFunctionInstance((dispatchPayloads) => {
246277
// user function is invoked now
247-
if (process.env.NODE_ENV !== 'production') {
248-
sendDebugMessage({
249-
key: props.functionKey,
250-
eventType: constants.DEBUG_MSG_FUNCTION_FIRE_EVENT,
251-
eventName: dispatchType,
252-
outputData: payload,
253-
functionName: props.functionName,
254-
timestamp: Date.now(),
255-
});
256-
}
257-
dispatchFunction(dispatchType, payload, dispatch, getState, helpers);
278+
dispatchFunction(
279+
props.functionKey,
280+
props.functionName,
281+
dispatchPayloads,
282+
dispatch,
283+
getState,
284+
helpers
285+
);
258286
});
259287
// here user returns a Promise and there may be the error
260288
if (userFunctionResult && userFunctionResult.then) {
261289
userFunctionResult.catch(error => {
262-
caughtExceptionFunction(error, dispatch, getState, helpers);
290+
caughtExceptionFunction(
291+
props.functionKey,
292+
props.functionName,
293+
error,
294+
dispatch,
295+
getState,
296+
helpers
297+
);
263298
});
264299
}
265300
} catch (error) {
266-
caughtExceptionFunction(error, dispatch, getState, helpers);
301+
caughtExceptionFunction(
302+
props.functionKey,
303+
props.functionName,
304+
error,
305+
dispatch,
306+
getState,
307+
helpers
308+
);
267309
}
268310
} catch (e) {
269311
if (e && e.message !== TRANSFORMATION_ERROR_SKIP_DATA_TRANSFERRING) {
@@ -287,7 +329,7 @@ function createTasks (targets, taskEventName) {
287329
payload = args[0];
288330
}
289331
return (dispatch, getState, helpers) => {
290-
dispatchToComponent(taskEventName, props, payload, dispatch, helpers);
332+
dispatchToComponent(props, payload, dispatch, helpers);
291333
};
292334
}
293335
});
@@ -302,7 +344,7 @@ function createActions (eventHandlers) {
302344
if (eventHandlers && eventHandlers.length > 0) {
303345
eventHandlers.forEach(eventHandler => {
304346
const { name, targets } = eventHandler;
305-
const tasks = createTasks(targets, name);
347+
const tasks = createTasks(targets);
306348
actions[name] = function () {
307349
const args = arguments;
308350
return (dispatch, getState, helpers) => {

src/framework/store/constants.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
export const COMPONENT_TYPE = 'component';
22
export const USER_FUNCTION_TYPE = 'userFunction';
33
export const DISPATCH_ERROR_TYPE = 'caughtException';
4-
export const STORE_ITEM_ACTION_SET = 'setItem';
5-
export const STORE_ITEM_ACTION_GET = 'getItem';

src/framework/store/selectors.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const select = (componentName, componentInstance, propertyName) => (state, props
44
const instanceState = state[`${componentName}_${componentInstance}`];
55
if (instanceState) {
66
if (props) {
7-
if (instanceState[propertyName] !== undefined) {
7+
if (typeof instanceState[propertyName] !== 'undefined') {
88
return instanceState[propertyName];
99
}
1010
return props.wrappedProps[propertyName];

0 commit comments

Comments
 (0)