@@ -11,6 +11,7 @@ import {
1111 IReloadOptions ,
1212 ISaveOptions ,
1313 IScene ,
14+ ReloadResult ,
1415} from '../../common' ;
1516import { PrefabEditor , SceneEditor } from './editors' ;
1617import { Rpc } from '../rpc' ;
@@ -234,48 +235,68 @@ export class EditorService extends BaseService<IEditorEvents> implements IEditor
234235 }
235236 }
236237
237- async reload ( params : IReloadOptions ) : Promise < boolean > {
238- if ( this . reloadPromise ) {
238+ async reload ( params : IReloadOptions ) : Promise < ReloadResult > {
239+ if ( this . _isReloading ) {
239240 this . needReloadAgain = params ;
240- return false ;
241- }
242- const urlOrUUID = params . urlOrUUID ?? this . currentEditorUuid ;
243- if ( ! urlOrUUID ) {
244- console . warn ( '当前没有打开任何编辑器' ) ;
245- return false ;
246- }
247-
248- const assetInfo = await Rpc . getInstance ( ) . request ( 'assetManager' , 'queryAssetInfo' , [ urlOrUUID ] ) ;
249- if ( ! assetInfo ) {
250- console . warn ( `通过 ${ urlOrUUID } 请求资源失败` ) ;
251- return false ;
252- }
253-
254- const editor = this . editorMap . get ( assetInfo . uuid ) ;
255- if ( ! editor ) {
256- console . warn ( `当前没有打开任何编辑器` ) ;
257- return false ;
241+ return ReloadResult . QUEUED ;
258242 }
243+ this . _isReloading = true ;
259244
260245 try {
261- await this . waitLocks ( ) ;
262- this . reloadPromise = editor . reload ( ) as Promise < IScene | INode > ;
263- await this . reloadPromise ;
246+ const urlOrUUID = params . urlOrUUID ?? this . currentEditorUuid ;
247+ if ( ! urlOrUUID ) {
248+ console . warn ( '当前没有打开任何编辑器' ) ;
249+ this . _isReloading = false ;
250+ return ReloadResult . NO_EDITOR ;
251+ }
264252
265- if ( this . needReloadAgain ) {
266- this . reload ( this . needReloadAgain ) ;
267- this . needReloadAgain = null ;
253+ const assetInfo = await Rpc . getInstance ( ) . request ( 'assetManager' , 'queryAssetInfo' , [ urlOrUUID ] ) ;
254+ if ( ! assetInfo ) {
255+ console . warn ( `通过 ${ urlOrUUID } 请求资源失败` ) ;
256+ this . _isReloading = false ;
257+ return ReloadResult . ASSET_NOT_FOUND ;
258+ }
259+
260+ const editor = this . editorMap . get ( assetInfo . uuid ) ;
261+ if ( ! editor ) {
262+ console . warn ( `当前没有打开任何编辑器` ) ;
263+ this . _isReloading = false ;
264+ return ReloadResult . EDITOR_NOT_FOUND ;
268265 }
269266
270- this . emit ( 'editor:reload' ) ;
271- this . broadcast ( 'editor:reload' ) ;
272- console . log ( `重载 ${ assetInfo . url } ` ) ;
273- return true ;
267+ this . reloadPromise = ( async ( ) => {
268+ try {
269+ let currentParams : IReloadOptions | null = params ;
270+ while ( currentParams ) {
271+ await this . waitLocks ( ) ;
272+ await editor . reload ( ) ;
273+
274+ if ( this . needReloadAgain ) {
275+ currentParams = this . needReloadAgain ;
276+ this . needReloadAgain = null ;
277+ } else {
278+ currentParams = null ;
279+ }
280+
281+ this . emit ( 'editor:reload' ) ;
282+ this . broadcast ( 'editor:reload' ) ;
283+ console . log ( `重载 ${ assetInfo . url } ` ) ;
284+ }
285+ return ReloadResult . SUCCESS ;
286+ } catch ( error ) {
287+ console . error ( error ) ;
288+ return ReloadResult . FAILED ;
289+ } finally {
290+ this . reloadPromise = null ;
291+ this . _isReloading = false ;
292+ }
293+ } ) ( ) as any ;
294+
295+ return this . reloadPromise as unknown as Promise < ReloadResult > ;
274296 } catch ( error ) {
275297 console . error ( error ) ;
276- return false ;
277- } finally {
278- this . reloadPromise = null ;
298+ this . _isReloading = false ;
299+ return ReloadResult . FAILED ;
279300 }
280301 }
281302
0 commit comments