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 { EventEmitter } from 'events';
import { ISimulatorHost, Component, NodeInstance, ComponentInstance } from '../simulator';
import Viewport from './viewport';
import { createSimulator } from './create-simulator';
@ -73,12 +72,10 @@ const defaultEnvironment = [
export class BuiltinSimulatorHost implements ISimulatorHost<BuiltinSimulatorProps> {
readonly isSimulator = true;
readonly designer = this.document.designer;
private emitter: EventEmitter;
constructor(readonly document: DocumentModel) {
this.emitter = new EventEmitter();
}
constructor(readonly document: DocumentModel) {}
readonly designer = this.document.designer;
@computed get device(): string {
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) {
this._renderer = renderer;
this.emitter.emit('lowcode_engine_renderer_connect', renderer);
return autorun(fn as any, true);
}
@ -1134,13 +1130,6 @@ export class BuiltinSimulatorHost implements ISimulatorHost<BuiltinSimulatorProp
*/
}
// #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) {

View File

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

View File

@ -345,11 +345,6 @@ export class DocumentModel {
// TODO: 多设备 simulator 支持
this._simulator = simulator;
// TODO: emit simulator mounted
this._simulator?.onRendererConnect((renderer) => {
this.emitter.emit('lowcode_engine_renderer_ready', {
renderer,
});
})
}
// FIXME: does needed?
@ -514,6 +509,10 @@ export class DocumentModel {
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 {

View File

@ -142,8 +142,6 @@ export interface ISimulatorHost<P = object> extends ISensor {
computeComponentInstanceRect(instance: ComponentInstance, selector?: string): DOMRect | 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);
designer.project.onCurrentDocumentChange((doc) => {
doc.onRendererReady((args) => {
const { renderer } = args;
renderer.onRendered(() => {
bus.emit(VE_EVENTS.VE_PAGE_PAGE_READY);
});
})
doc.onRendererReady(() => {
bus.emit(VE_EVENTS.VE_PAGE_PAGE_READY);
});
});
// 节点 props 初始化

View File

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