refactor: delegate signal effects to DisposableBag
Introduce `addEffect` to `DisposableBag` to encapsulate Preact signal effect creation and disposal. Update `ReactiveScene` to use this method, simplifying effect management and ensuring proper cleanup.
This commit is contained in:
parent
45844ea800
commit
7039938a72
|
|
@ -1,4 +1,4 @@
|
|||
import { effect, type ReadonlySignal } from "@preact/signals-core";
|
||||
import { type ReadonlySignal } from "@preact/signals-core";
|
||||
import { Scene } from "phaser";
|
||||
|
||||
import { DisposableBag, type IDisposable } from "../utils";
|
||||
|
|
@ -85,6 +85,6 @@ export abstract class ReactiveScene<TData = object>
|
|||
|
||||
/** 注册响应式监听(场景关闭时自动清理) */
|
||||
public addEffect(fn: () => CleanupFn): void {
|
||||
this.disposables.add(effect(fn));
|
||||
this.disposables.addEffect(fn);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
import { effect } from "@preact/signals-core";
|
||||
|
||||
export interface IDisposable {
|
||||
dispose(): void;
|
||||
}
|
||||
|
|
@ -8,6 +10,10 @@ export class DisposableBag implements IDisposable {
|
|||
private _disposables = new Set<DisposableItem>();
|
||||
private _isDisposed = false;
|
||||
|
||||
constructor(go?: Phaser.GameObjects.GameObject) {
|
||||
if (go) go.on("shutdown", () => this.dispose());
|
||||
}
|
||||
|
||||
get isDisposed(): boolean {
|
||||
return this._isDisposed;
|
||||
}
|
||||
|
|
@ -20,6 +26,10 @@ export class DisposableBag implements IDisposable {
|
|||
this._disposables.add(item);
|
||||
}
|
||||
|
||||
addEffect(fn: () => void) {
|
||||
this.add(effect(fn));
|
||||
}
|
||||
|
||||
dispose(): void {
|
||||
if (this._isDisposed) return;
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue