Skip to content

Commit

Permalink
[fix] Mount & Update timing of Class components
Browse files Browse the repository at this point in the history
  • Loading branch information
TechQuery committed Feb 8, 2024
1 parent b7efb95 commit 642ac04
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "web-cell",
"version": "3.0.0-rc.10",
"version": "3.0.0-rc.14",
"description": "Web Components engine based on VDOM, JSX, MobX & TypeScript",
"keywords": [
"web",
Expand Down
31 changes: 19 additions & 12 deletions source/WebCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import {
stringifyDOM
} from 'web-utility';

import { Defer } from './utility';

export interface ComponentMeta
extends ElementDefinitionOptions,
Partial<ShadowRootInit> {
Expand All @@ -24,7 +22,7 @@ export interface WebCell<P = {}> extends CustomElement {
internals: ElementInternals;
renderer: DOMRenderer;
root: ParentNode;
mounted: Defer;
mounted: boolean;
update: () => void;
/**
* Called at DOM tree updated
Expand Down Expand Up @@ -64,7 +62,7 @@ export function component(meta: ComponentMeta) {
get root(): ParentNode {
return this.internals.shadowRoot || this;
}
mounted = new Defer();
mounted = false;
declare mountedCallback?: () => any;

constructor() {
Expand All @@ -75,8 +73,6 @@ export function component(meta: ComponentMeta) {
}

connectedCallback() {
this.update();

const { mode } = meta;
const renderChildren = !(mode != null);

Expand All @@ -93,8 +89,12 @@ export function component(meta: ComponentMeta) {

super['connectedCallback']?.();

this.mounted.promise.then(this.mountedCallback);
this.mounted.resolve();
if (this.mounted) return;

this.update();

this.mounted = true;
this.mountedCallback?.();
}

declare render?: () => VNode;
Expand All @@ -103,11 +103,18 @@ export function component(meta: ComponentMeta) {
update() {
const vNode = this.render?.();

this.renderer.render(
isEmpty(vNode) ? meta.mode ? <slot /> : <></> : vNode,
this.root
const content = isEmpty(vNode) ? (
meta.mode ? (
<slot />
) : null
) : (
vNode
);
this.updatedCallback?.();

if (content != null) {
this.renderer.render(content, this.root);
this.updatedCallback?.();
}
}

disconnectedCallback() {
Expand Down

1 comment on commit 642ac04

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deploy preview for web-cell ready!

✅ Preview
https://web-cell-phx3sqm28-techquery.vercel.app

Built with commit 642ac04.
This pull request is being automatically deployed with vercel-action

Please sign in to comment.