Skip to content
This repository was archived by the owner on Jun 4, 2024. It is now read-only.

Commit 8edbc0d

Browse files
committed
🐎 clientside hook
1 parent e2455d2 commit 8edbc0d

File tree

1 file changed

+49
-1
lines changed

1 file changed

+49
-1
lines changed

src/actions/index.js

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
has,
1414
intersection,
1515
isEmpty,
16+
isNil,
1617
keys,
1718
lensPath,
1819
merge,
@@ -425,7 +426,7 @@ function updateOutput(
425426
changedPropIds,
426427
};
427428

428-
const {inputs, state} = dependenciesRequest.content.find(
429+
const {inputs, state, client_function} = dependenciesRequest.content.find(
429430
dependency => dependency.output === outputIdAndProp
430431
);
431432
const validKeys = keys(getState().paths);
@@ -496,6 +497,53 @@ function updateOutput(
496497
});
497498
}
498499

500+
// Clientside hook
501+
if (!isNil(client_function) && !isEmpty(client_function)) {
502+
503+
const returnValue = (
504+
window[client_function.namespace][client_function.function_name](
505+
...(has('inputs', payload) ? pluck('value', payload.inputs) : []),
506+
...(has('state', payload) ? pluck('value', payload.state) : [])
507+
)
508+
);
509+
510+
const [outputId, outputProp] = payload.output.split('.');
511+
const updatedProps = {
512+
[outputProp]: returnValue
513+
};
514+
515+
/*
516+
* Update the request queue by treating a successful clientside
517+
* like a succesful serverside response (200 status code)
518+
*/
519+
updateRequestQueue(false, STATUS.OK);
520+
521+
// Update the layout with the new result
522+
dispatch(updateProps({
523+
itempath: getState().paths[outputId],
524+
props: updatedProps,
525+
source: 'response'
526+
}));
527+
528+
/*
529+
* This output could itself be a serverside or clientside input
530+
* to another function
531+
*/
532+
dispatch(notifyObservers({
533+
id: outputId,
534+
props: updatedProps
535+
}));
536+
537+
/*
538+
* Note that unlike serverside updates, we're not handling
539+
* children as components right now, so we don't need to
540+
* crawl the computed result to check for nested components
541+
* or properties that might trigger other inputs.
542+
* In the future, we could handle this case.
543+
*/
544+
return;
545+
}
546+
499547
if (hooks.request_pre !== null) {
500548
hooks.request_pre(payload);
501549
}

0 commit comments

Comments
 (0)