Skip to content

Commit e232c07

Browse files
committed
Updated createApp to accept only RootComponent and props
1 parent 75d5733 commit e232c07

2 files changed

Lines changed: 21 additions & 45 deletions

File tree

src/app.js

Lines changed: 20 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,36 @@
1-
import { destroyDOM } from './destroy-dom';
2-
import { mountDOM } from './mount-dom';
3-
import { Dispatcher } from './dispatcher';
4-
import { patchDOM } from './patch-dom';
1+
import { mountDOM } from "./mount-dom";
2+
import { destroyDOM } from './destroy-dom'
3+
import { hElement } from "./h";
54

6-
export function createApp({ state, view, reducers = {} }) {
5+
export function createApp(RootComponent, props = {}) {
76
let parentEl = null;
8-
let vdom = null;
9-
let isMounted = null;
10-
11-
const dispatcher = new Dispatcher();
12-
// Subscribed the renderApp() function to be an after-command handler
13-
// so that the application is re-rendered after every command is handled.
14-
const subscriptions = [dispatcher.afterEveryCommand(renderApp)]
15-
16-
function dispatchEvent(eventName, payload) {
17-
dispatcher.dispatch(eventName, payload);
18-
}
19-
20-
for (const actionName in reducers) {
21-
const reducer = reducers[actionName];
22-
const subs = dispatcher.subscribe(actionName, (payload) => {
23-
state = reducer(state, payload);
24-
})
25-
subscriptions.push(subs);
26-
}
27-
// Draw UI.
28-
function renderApp() {
29-
const newVdom = view(state, emit);
30-
vdom = patchDOM(vdom, newVdom, parentEl);
7+
let isMounted = false;
8+
let vdom = null;
9+
10+
// Reset internal properties of the app.
11+
function reset() {
12+
parentEl = null;
13+
isMounted = false;
14+
vdom = null;
3115
}
16+
3217
return {
33-
mount(_parentEl) { // Mount to the DOM.
18+
mount(_parentEl) {
3419
if (isMounted) {
3520
throw new Error('The application is already mounted');
3621
}
37-
3822
parentEl = _parentEl;
39-
vdom = view(state, emit);
40-
mountDOM(vdom, parentEl); // Only once.
23+
vdom = hElement(RootComponent, props);
24+
mountDOM(vdom, parentEl);
4125

4226
isMounted = true;
4327
},
4428
unmount() {
29+
if (!isMounted) {
30+
throw new Error('The application is not mounted');
31+
}
4532
destroyDOM(vdom);
46-
vdom = null;
47-
subscriptions.forEach((unsubscribe) => unsubscribe());
48-
49-
isMounted = false;
33+
reset();
5034
}
5135
}
5236
}
53-
54-
// createApp({
55-
// state: { count: 0 },
56-
// view: (state, dispatch) => `<div>${state.count}</div>`,
57-
// reducers: {
58-
// increment: (state) => ({ count: state.count + 1 })
59-
// }
60-
// });
61-

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export { createApp } from './app';
22
export { hString, hElement, hFragment } from './h';
3+
export { defineComponent } from './components/component';

0 commit comments

Comments
 (0)