mirror of
https://github.com/unanmed/HumanBreak.git
synced 2026-11-12 00:24:30 +08:00
加载资源初步
This commit is contained in:
1 parent
7cc7fe375b
commit
5cbd9950b9
6 files changed
+163
-16
No files matched your search
@@ -3,21 +3,33 @@ import { EmitableEvent, EventEmitter } from './eventEmitter';
|
||||
interface DisposableEvent<T> extends EmitableEvent {
|
||||
active: (value: T) => void;
|
||||
dispose: (value: T) => void;
|
||||
destroy: () => void;
|
||||
}
|
||||
|
||||
export class Disposable<T> extends EventEmitter<DisposableEvent<T>> {
|
||||
protected _data: T;
|
||||
protected _data?: T;
|
||||
set data(value: T | null) {
|
||||
if (this.destroyed) {
|
||||
throw new Error(
|
||||
`Cannot set value of destroyed disposable variable.`
|
||||
);
|
||||
}
|
||||
if (value !== null) this._data = value;
|
||||
}
|
||||
get data(): T | null {
|
||||
if (this.destroyed) {
|
||||
throw new Error(
|
||||
`Cannot get value of destroyed disposable variable.`
|
||||
);
|
||||
}
|
||||
if (!this.activated) {
|
||||
return null;
|
||||
}
|
||||
return this._data;
|
||||
return this._data!;
|
||||
}
|
||||
|
||||
protected activated: boolean = false;
|
||||
protected destroyed: boolean = false;
|
||||
|
||||
constructor(data: T) {
|
||||
super();
|
||||
@@ -26,11 +38,17 @@ export class Disposable<T> extends EventEmitter<DisposableEvent<T>> {
|
||||
|
||||
active() {
|
||||
this.activated = true;
|
||||
this.emit('active', this._data);
|
||||
this.emit('active', this._data!);
|
||||
}
|
||||
|
||||
dispose() {
|
||||
this.activated = false;
|
||||
this.emit('dispose', this._data);
|
||||
this.emit('dispose', this._data!);
|
||||
}
|
||||
|
||||
destroy() {
|
||||
this.destroyed = true;
|
||||
this.emit('destroy');
|
||||
delete this._data;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user