Skip to content

Commit

Permalink
Revert "feat: remove controller mounted callback"
Browse files Browse the repository at this point in the history
This reverts commit 7bf8ac6.
  • Loading branch information
nieyt committed Apr 7, 2024
1 parent 7bf8ac6 commit 68a4501
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@ export class QuarkElement extends HTMLElement implements ReactiveControllerHost
* 初始值重写后首次渲染
*/
this._render();
this._controllers?.forEach((c) => c.hostMounted?.());
if (isFunction(this.componentDidMount)) {
this.componentDidMount();
}
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/reactiveController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,9 @@ export interface ReactiveController {
*
*/
hostUpdated?(): void;

/**
* Called when the host is mounted, just after the host calls first render.
*/
hostMounted?():void;
}
8 changes: 6 additions & 2 deletions packages/router/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ export class Router extends Routes {

override hostConnected() {
super.hostConnected();
eventBus.emit("router-host-mounted", this);
eventBus.on("link-mounted", this._busRouterLinstner);
window.addEventListener('popstate', this._onPopState);
// Kick off routed rendering by going to the current URL
this.goto(this._resolvePath(window.location));
Expand All @@ -67,6 +65,12 @@ export class Router extends Routes {
this.host.removeEventListener(RouterJumpMethodEnum.replace, this._rootHostListener!);
}

override hostMounted() {
super.hostMounted();
eventBus.emit("router-host-mounted", this);
eventBus.on("link-mounted", this._busRouterLinstner);
}

protected override addListeners() {
super.addListeners();
this._rootHostListener = (e: CustomEvent<RouterJumpDetail>) => {
Expand Down
7 changes: 5 additions & 2 deletions packages/router/src/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,6 @@ export class Routes implements ReactiveController {
const event = new RoutesConnectedEvent(this);
this._host.dispatchEvent(event);
this._onDisconnect = event.onDisconnect;
eventBus.emit("routes-host-mounted", this);
eventBus.on("link-mounted", this._busRoutesListener);
}

hostDisconnected() {
Expand All @@ -325,6 +323,11 @@ export class Routes implements ReactiveController {
this._host.removeEventListener(RouteMethodEnum.replace, this._hostListener!);
}

hostMounted() {
eventBus.emit("routes-host-mounted", this);
eventBus.on("link-mounted", this._busRoutesListener);
}

private _onRoutesConnected = (e: RoutesConnectedEvent) => {
// Don't handle the event fired by this routes controller, which we get
// because we do this.dispatchEvent(...)
Expand Down

0 comments on commit 68a4501

Please sign in to comment.