Skip to content

Commit

Permalink
Refactor component start method to call mounted function
Browse files Browse the repository at this point in the history
  • Loading branch information
yysun committed Dec 8, 2023
1 parent d91ba00 commit 5ff9105
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/apprun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ app.start = <T, E = any>(element?: Element | string, model?: T, view?: View<T>,
const opts = { render: true, global_event: true, ...options };
const component = new Component<T, E>(model, view, update);
if (options && options.rendered) component.rendered = options.rendered;
component.mount(element, opts);
if (options && options.mounted) component.mounted = options.mounted;
component.start(element, opts);
return component;
};

Expand Down
8 changes: 6 additions & 2 deletions src/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ export class Component<T = any, E = any> {
}
if (!vdom && html) {
html = directive(html, this);

if (this.options.transition && document && document['startViewTransition']) {
document['startViewTransition'](() => app.render(el, html, this));
} else {
Expand Down Expand Up @@ -135,7 +134,12 @@ export class Component<T = any, E = any> {
}

start = (element = null, options?: MountOptions): Component<T, E> => {
return this.mount(element, { render: true, ...options });
this.mount(element, { render: true, ...options });
if (this.mounted && typeof this.mounted === 'function') {
const new_state = this.mounted({}, [], this.state);
(typeof new_state !== 'undefined') && this.setState(new_state);
}
return this;
}

public mount(element = null, options?: MountOptions): Component<T, E> {
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ export type AppStartOptions<T> = {
transition?: boolean;
route?: string;
rendered?: (state: T) => void
mounted?: (props:any, children:any, state: T) => T
};
2 changes: 1 addition & 1 deletion src/vdom-my.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ function render_component(node, parent, idx) {
let component = parent.__componentCache[key];
if (!component || !(component instanceof tag) || !component.element) {
const element = document.createElement(asTag);
component = parent.__componentCache[key] = new tag({ ...props, children }).start(element);
component = parent.__componentCache[key] = new tag({ ...props, children }).mount(element, { render: true });
} else {
component.renderState(component.state);
}
Expand Down
28 changes: 27 additions & 1 deletion tests/component.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -430,9 +430,35 @@ describe('Component', () => {
// t['state'] = state;
// t.run('.')
// });
t.run('.');
t.run('.');
})


it('should call mounted function with .start', (done) => {
class Test extends Component {
state = 'a'
view = state => <div>{state}</div>
mounted = () => {
expect(this.element.innerHTML).toBe('<div>a</div>');
done();
}
}
const div = document.createElement('div');
const t = new Test().start(div);
});

it('should not call mounted function with .mount', () => {
class Test extends Component {
state = 'a'
view = state => <div>{state}</div>
mounted = () => {
throw new Error('should not call mounted function');
}
}
const div = document.createElement('div');
const t = new Test().mount(div);
});

});


0 comments on commit 5ff9105

Please sign in to comment.