-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstance.js
43 lines (36 loc) · 1.27 KB
/
instance.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
const mitt = require('mitt');
const applyHook = require('./hook');
const createRenderer = require('../renderer');
module.exports = function instanceComponent(patch, container, factory, userProps = {}) {
const render = createRenderer(patch, container);
let props = userProps;
let children = [];
let userView;
let store;
const emitter = mitt();
emitter.on('render', () => {
render({ usePatch: true, view, state, props, children });
});
const instance = factory({ emitter, props });
if (typeof instance === 'function') {
userView = instance;
} else {
userView = instance.view;
store = instance.store;
}
const view = ({ state, props, children }) => {
return applyHook(userView({ state, props, children }));
};
const state = typeof store === 'function' ? store() : {};
if (typeof state !== 'object') throw new Error('Store function in your components should return an state object');
return {
render({ usePatch = false, props: userProps = {}, children: userChildren = [] }) {
props = userProps;
children = userChildren;
return render({ usePatch, view, state, props, children });
},
state,
props,
emitter
};
};