fix: 简化 onPageReady 实现逻辑

This commit is contained in:
mario.gk 2020-06-22 19:25:19 +08:00
parent 935ffad2e6
commit a36e5f25c4
6 changed files with 11 additions and 39 deletions

View File

@ -1,5 +1,4 @@
import { obx, autorun, computed, getPublicPath, hotkey, focusTracker, globalContext, Editor } from '@ali/lowcode-editor-core'; import { obx, autorun, computed, getPublicPath, hotkey, focusTracker, globalContext, Editor } from '@ali/lowcode-editor-core';
import { EventEmitter } from 'events';
import { ISimulatorHost, Component, NodeInstance, ComponentInstance } from '../simulator'; import { ISimulatorHost, Component, NodeInstance, ComponentInstance } from '../simulator';
import Viewport from './viewport'; import Viewport from './viewport';
import { createSimulator } from './create-simulator'; import { createSimulator } from './create-simulator';
@ -73,12 +72,10 @@ const defaultEnvironment = [
export class BuiltinSimulatorHost implements ISimulatorHost<BuiltinSimulatorProps> { export class BuiltinSimulatorHost implements ISimulatorHost<BuiltinSimulatorProps> {
readonly isSimulator = true; readonly isSimulator = true;
readonly designer = this.document.designer;
private emitter: EventEmitter;
constructor(readonly document: DocumentModel) { constructor(readonly document: DocumentModel) {}
this.emitter = new EventEmitter();
} readonly designer = this.document.designer;
@computed get device(): string { @computed get device(): string {
return this.get('device') || 'default'; return this.get('device') || 'default';
@ -129,7 +126,6 @@ export class BuiltinSimulatorHost implements ISimulatorHost<BuiltinSimulatorProp
*/ */
connect(renderer: BuiltinSimulatorRenderer, fn: (context: { dispose: () => void; firstRun: boolean }) => void) { connect(renderer: BuiltinSimulatorRenderer, fn: (context: { dispose: () => void; firstRun: boolean }) => void) {
this._renderer = renderer; this._renderer = renderer;
this.emitter.emit('lowcode_engine_renderer_connect', renderer);
return autorun(fn as any, true); return autorun(fn as any, true);
} }
@ -1134,13 +1130,6 @@ export class BuiltinSimulatorHost implements ISimulatorHost<BuiltinSimulatorProp
*/ */
} }
// #endregion // #endregion
onRendererConnect(fn: (renderer: BuiltinSimulatorRenderer) => void): () => void {
this.emitter.on('lowcode_engine_renderer_connect', fn);
return () => {
this.emitter.removeListener('lowcode_engine_renderer_connect', fn);
};
}
} }
function isHTMLTag(name: string) { function isHTMLTag(name: string) {

View File

@ -14,7 +14,6 @@ export interface BuiltinSimulatorRenderer {
setCopyState(state: boolean): void; setCopyState(state: boolean): void;
clearState(): void; clearState(): void;
run(): void; run(): void;
onRendered(fn: () => void): () => void;
} }
export function isSimulatorRenderer(obj: any): obj is BuiltinSimulatorRenderer { export function isSimulatorRenderer(obj: any): obj is BuiltinSimulatorRenderer {

View File

@ -345,11 +345,6 @@ export class DocumentModel {
// TODO: 多设备 simulator 支持 // TODO: 多设备 simulator 支持
this._simulator = simulator; this._simulator = simulator;
// TODO: emit simulator mounted // TODO: emit simulator mounted
this._simulator?.onRendererConnect((renderer) => {
this.emitter.emit('lowcode_engine_renderer_ready', {
renderer,
});
})
} }
// FIXME: does needed? // FIXME: does needed?
@ -514,6 +509,10 @@ export class DocumentModel {
this.emitter.removeListener('lowcode_engine_renderer_ready', fn); this.emitter.removeListener('lowcode_engine_renderer_ready', fn);
}; };
} }
setRendererReady(renderer) {
this.emitter.emit('lowcode_engine_renderer_ready', renderer);
}
} }
export function isDocumentModel(obj: any): obj is DocumentModel { export function isDocumentModel(obj: any): obj is DocumentModel {

View File

@ -143,8 +143,6 @@ export interface ISimulatorHost<P = object> extends ISensor {
findDOMNodes(instance: ComponentInstance, selector?: string): Array<Element | Text> | null; findDOMNodes(instance: ComponentInstance, selector?: string): Array<Element | Text> | null;
onRendererConnect(fn: (renderer: any) => void): () => void;
/** /**
* *
*/ */

View File

@ -26,12 +26,9 @@ editor.set(Designer, designer);
editor.set('designer', designer); editor.set('designer', designer);
designer.project.onCurrentDocumentChange((doc) => { designer.project.onCurrentDocumentChange((doc) => {
doc.onRendererReady((args) => { doc.onRendererReady(() => {
const { renderer } = args;
renderer.onRendered(() => {
bus.emit(VE_EVENTS.VE_PAGE_PAGE_READY); bus.emit(VE_EVENTS.VE_PAGE_PAGE_READY);
}); });
})
}); });
// 节点 props 初始化 // 节点 props 初始化

View File

@ -1,6 +1,5 @@
import React, { createElement, ReactInstance, ComponentType, ReactElement } from 'react'; import React, { createElement, ReactInstance, ComponentType, ReactElement } from 'react';
import { render as reactRender } from 'react-dom'; import { render as reactRender } from 'react-dom';
import { EventEmitter } from 'events';
import { host } from './host'; import { host } from './host';
import SimulatorRendererView from './renderer-view'; import SimulatorRendererView from './renderer-view';
import { computed, obx } from '@recore/obx'; import { computed, obx } from '@recore/obx';
@ -18,14 +17,12 @@ import Leaf from './builtin-components/leaf';
export class SimulatorRenderer implements BuiltinSimulatorRenderer { export class SimulatorRenderer implements BuiltinSimulatorRenderer {
readonly isSimulatorRenderer = true; readonly isSimulatorRenderer = true;
private dispose?: () => void; private dispose?: () => void;
private emitter: EventEmitter;
constructor() { constructor() {
if (!host) { if (!host) {
return; return;
} }
this.emitter = new EventEmitter();
this.dispose = host.connect(this, () => { this.dispose = host.connect(this, () => {
// sync layout config // sync layout config
@ -291,14 +288,7 @@ export class SimulatorRenderer implements BuiltinSimulatorRenderer {
document.body.classList.add('engine-document'); // important! Stylesheet.invoke depends document.body.classList.add('engine-document'); // important! Stylesheet.invoke depends
reactRender(createElement(SimulatorRendererView, { renderer: this }), container); reactRender(createElement(SimulatorRendererView, { renderer: this }), container);
this.emitter.emit('lowcode_engine_render_run'); host.document.setRendererReady(this);
}
onRendered(fn: () => void): () => void {
this.emitter.on('lowcode_engine_render_run', fn);
return () => {
this.emitter.removeListener('lowcode_engine_render_run', fn);
};
} }
} }