-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample.js
176 lines (149 loc) · 3.83 KB
/
sample.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
function Hero(props, context, emit) {
return {
markup: (
<header>
<h1>{props.title}</h1>
<p>{props.subtitle}</p>
{!!context.content.cta && (
<button onClick={emit('cta-click')}>
{context.content.cta}
</button>
)}
</header>
),
bind: ['content.cta'],
}
}
async function initializeContent({ setContext, singletons }) {
try {
const content = await singletons('fetch')('...', { ... });
setContext('content', content);
} catch (error) {
singletons('reportError')(error);
setContext('content.hasFailedToLoad', true);
}
}
async function handleCtaClick({ consume, emit }) {
consume('cta-click', (event) => {
event.preventDefault();
emit('@package-modal/trigger', { type: 'signup', ... });
});
}
/* --------------------------- */
async function formSubmit({ consume }) {
// ...
}
async function formOnChange({ consume, setContext }) {
// ...
}
function Form(props, context, emit) {
const { formName } = props;
const { email, password } = context.forms || {};
function onChangeGenerator(fieldName) {
function onChange(event) {
emit('form-input-onChange', { fieldName, event });
}
return onChange;
}
return {
markup: (
<form onSubmit={(event) => emit(`form-submit`, { event, formName })}>
<input
type="email"
value={email || ''}
onChange={onChangeGenerator('email')}
/>
<input
type="password"
value={password || ''}
onChange={onChangeGenerator('password')}
/>
<button type="submit">submit</button>
</form>
),
bind: [
`forms.${formName}`,
],
systems: [
formSubmit,
formOnChange,
],
}
}
/* --------------------------- */
export function useStateLib({
bind,
register,
}) {
const core = useContext(StateLibContext);
const [localState, setLocalState] = useState({});
useEffect(() => {
const hasBinds = Array.isArray(bind) && !!bind.length;
if (hasBinds) {
bind.forEach((key) => core.on(key, (value) => {
setLocalState...
}));
return () => {
bind.forEach((key) => core.removeListener('bind', key, setLocalState));
};
}
}, [
bind,
]);
useEffect(() => {
const hasRegisters = Array.isArray(register) && !!register.length;
if (hasRegisters) {
const systems = register.map((system) => {
system.__id = `${Date.now()}${Math.round(Math.random())}`;
return system;
});
systems.forEach((system) => core.addSystem(system));
return () => {
systems.forEach((system) => core.removeSystem(system));
};
}
}, [
register,
]);
return {
context: localState,
...core.publicApi,
};
}
function Hero(props) {
const { context, emit } = useStateLib({
bind: ['content.cta'],
});
return (
<header>
<h1>{props.title}</h1>
<p>{props.subtitle}</p>
{!!context.content.cta && (
<button onClick={emit('cta-click')}>
{context.content.cta}
</button>
)}
</header>
);
}
async function initializeContent({ setState, singletons }) {
try {
const content = await singletons('fetch')('...', { ... });
setState('content', content);
} catch (error) {
singletons('reportError')(error);
setState('content.hasFailedToLoad', true);
}
}
async function handleCtaClick({ consume, emit }) {
consume('cta-click', (event) => {
event.preventDefault();
emit('@package-modal/trigger', { type: 'signup', ... });
});
}
// Something else to think about...
// "dependsOn" --> key in the hook argument
// Can have a system that listens for a `dependsOn` event
// Does the data fetch and sets the context
//
// Also need to work through the setup api, systems client vs ssr, systems threads (webworkers)?