fix: project event listeners will not be invoked sometimes

This commit is contained in:
LeoYuan 袁力皓 2022-07-06 10:51:38 +08:00 committed by 林熠
parent 12f67dcdeb
commit a0c772fb90

View File

@ -136,41 +136,40 @@ export default class Project {
* project document
*/
onChangeDocument(fn: (doc: DocumentModel) => void) {
if (this[projectSymbol].currentDocument) {
fn(DocumentModel.create(this[projectSymbol].currentDocument)!);
return () => {};
}
return this[projectSymbol].onCurrentDocumentChange((originalDoc) => {
const offFn = this[projectSymbol].onCurrentDocumentChange((originalDoc) => {
fn(DocumentModel.create(originalDoc)!);
});
if (this[projectSymbol].currentDocument) {
fn(DocumentModel.create(this[projectSymbol].currentDocument)!);
}
return offFn;
}
/**
* project ready
*/
onSimulatorHostReady(fn: (host: SimulatorHost) => void) {
if (this[simulatorHostSymbol]) {
fn(SimulatorHost.create(this[simulatorHostSymbol])!);
return () => {};
}
return this[projectSymbol].onSimulatorReady((simulator: BuiltinSimulatorHost) => {
const offFn = this[projectSymbol].onSimulatorReady((simulator: BuiltinSimulatorHost) => {
this[simulatorHostSymbol] = simulator;
fn(SimulatorHost.create(simulator)!);
});
if (this[simulatorHostSymbol]) {
fn(SimulatorHost.create(this[simulatorHostSymbol])!);
}
return offFn;
}
/**
* project ready
*/
onSimulatorRendererReady(fn: () => void) {
if (this[simulatorRendererSymbol]) {
fn();
return () => {};
}
// TODO: 补充 renderer 实例
return this[projectSymbol].onRendererReady((renderer: any) => {
const offFn = this[projectSymbol].onRendererReady((renderer: any) => {
this[simulatorRendererSymbol] = renderer;
fn();
});
if (this[simulatorRendererSymbol]) {
fn();
}
return offFn;
}
}