-
Notifications
You must be signed in to change notification settings - Fork 5
/
component-helper.js
40 lines (35 loc) · 1.27 KB
/
component-helper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
* @typedef {import("xstate").AnyStateNodeDefinition} StateNode
* @typedef {() => object | object} Props
* @typedef {{ component: Function, props: Props }} ComponentAndProps
*/
/**
* @param {Function | ComponentAndProps} child - Component instance, or component loader
* @param {StateNode} node - Xstate node data
* @returns {StateNode} an xstate node containing component information in its meta
*/
export default (child, node = {}) => {
/**
* Handle both bare component & { component, props } via destructuring & default values
* 1. state : component(UIComponent, {})
* 2. state : component{ component : UIComponent, props : () => { ... } | { ... }}
*/
const {
component = child,
props = false,
...meta
} = child;
// meta will always be an object since it's a rest param, fine to assign to
meta.load = (...args) => [
// Run arrow functions (function w/o a prototype), anything else is assumed to be
// already a component
component && typeof component === "function" && !component.prototype ?
component(...args) :
component,
typeof props === "function" ?
props(...args) :
props,
];
node.meta = meta;
return node;
};