加载资源初步

This commit is contained in:
AncTe committed 2023-06-07 18:17:45 +08:00
1 parent 7cc7fe375b
commit 5cbd9950b9
6 files changed
+163 -16

No files matched your search

+22 -4
View File
@@ -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;
}
}