Skip to content

Commit

Permalink
fix: lifecycle event handling in plugin holder (#2244)
Browse files Browse the repository at this point in the history
* fix: lifecycle event handling in plugin holder

* chore: make code cleaner

* fix(sheets-ui): fix skeleton not disposed
  • Loading branch information
wzhudev committed May 15, 2024
1 parent ba853df commit 85af642
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
8 changes: 7 additions & 1 deletion packages/core/src/services/lifecycle/lifecycle.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,13 @@ export class LifecycleService extends Disposable {
subscriber.next(LifecycleStages.Rendered);
}

return this._lifecycle$.subscribe(subscriber);
this._lifecycle$.subscribe((stage) => {
subscriber.next(stage);

if (stage === LifecycleStages.Steady) {
subscriber.complete();
}
});
});
}

Expand Down
15 changes: 8 additions & 7 deletions packages/core/src/services/plugin/plugin-holder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@

/* eslint-disable ts/no-explicit-any */

import { type Ctor, Inject, Injector } from '@wendellhu/redi';
import type { Ctor } from '@wendellhu/redi';
import { Inject, Injector } from '@wendellhu/redi';

import { finalize } from 'rxjs';
import { LifecycleStages } from '../lifecycle/lifecycle';
import { LifecycleInitializerService, LifecycleService } from '../lifecycle/lifecycle.service';
import { Disposable } from '../../shared/lifecycle';
Expand Down Expand Up @@ -74,12 +76,11 @@ export class PluginHolder extends Disposable {
const plugins = this._pluginRegistry.getRegisterPlugins().map(({ plugin, options }) => this._initPlugin(plugin, options));
this._pluginRegistry.removePlugins();

const lifecycleSubscription = this.disposeWithMe(this._lifecycleService.subscribeWithPrevious().subscribe((stage) => {
this._pluginsRunLifecycle(plugins, stage);
if (stage === LifecycleStages.Steady) {
lifecycleSubscription.dispose();
}
}));
const subscription = this.disposeWithMe(this._lifecycleService.subscribeWithPrevious()
// It has to been async because the finalize may execute synchronously after we
// make the subscription. For example, the lifecycle service is already in stage "steady".
.pipe(finalize(() => { Promise.resolve().then(() => subscription.dispose()); }))
.subscribe((stage) => { this._pluginsRunLifecycle(plugins, stage); }));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ export class SheetSkeletonManagerService implements IDisposable {
removeSkeleton(searchParam: Pick<ISheetSkeletonManagerSearch, 'unitId'>) {
const index = this._sheetSkeletonParam.findIndex((param) => param.unitId === searchParam.unitId);
if (index !== -1) {
const skeletonParam = this._sheetSkeletonParam[index];
skeletonParam.skeleton.dispose();

this._sheetSkeletonParam.splice(index, 1);
this._currentSkeletonBefore$.next(null);
this._currentSkeleton$.next(null);
Expand Down

0 comments on commit 85af642

Please sign in to comment.