@animech-public/playcanvas
Version:
PlayCanvas WebGL game engine
94 lines (92 loc) • 3.36 kB
JavaScript
class AssetReference {
constructor(propertyName, parent, registry, callbacks, scope) {
this._evtLoadById = null;
this._evtUnloadById = null;
this._evtAddById = null;
this._evtRemoveById = null;
this._evtLoadByUrl = null;
this._evtAddByUrl = null;
this._evtRemoveByUrl = null;
this.propertyName = propertyName;
this.parent = parent;
this._scope = scope;
this._registry = registry;
this.id = null;
this.url = null;
this.asset = null;
this._onAssetLoad = callbacks.load;
this._onAssetAdd = callbacks.add;
this._onAssetRemove = callbacks.remove;
this._onAssetUnload = callbacks.unload;
}
set id(value) {
if (this.url) throw Error('Can\'t set id and url');
this._unbind();
this._id = value;
this.asset = this._registry.get(this._id);
this._bind();
}
get id() {
return this._id;
}
set url(value) {
if (this.id) throw Error('Can\'t set id and url');
this._unbind();
this._url = value;
this.asset = this._registry.getByUrl(this._url);
this._bind();
}
get url() {
return this._url;
}
_bind() {
if (this.id) {
if (this._onAssetLoad) this._evtLoadById = this._registry.on(`load:${this.id}`, this._onLoad, this);
if (this._onAssetAdd) this._evtAddById = this._registry.once(`add:${this.id}`, this._onAdd, this);
if (this._onAssetRemove) this._evtRemoveById = this._registry.on(`remove:${this.id}`, this._onRemove, this);
if (this._onAssetUnload) this._evtUnloadById = this._registry.on(`unload:${this.id}`, this._onUnload, this);
}
if (this.url) {
if (this._onAssetLoad) this._evtLoadByUrl = this._registry.on(`load:url:${this.url}`, this._onLoad, this);
if (this._onAssetAdd) this._evtAddByUrl = this._registry.once(`add:url:${this.url}`, this._onAdd, this);
if (this._onAssetRemove) this._evtRemoveByUrl = this._registry.on(`remove:url:${this.url}`, this._onRemove, this);
}
}
_unbind() {
if (this.id) {
var _this$_evtLoadById, _this$_evtAddById, _this$_evtRemoveById, _this$_evtUnloadById;
(_this$_evtLoadById = this._evtLoadById) == null || _this$_evtLoadById.off();
this._evtLoadById = null;
(_this$_evtAddById = this._evtAddById) == null || _this$_evtAddById.off();
this._evtAddById = null;
(_this$_evtRemoveById = this._evtRemoveById) == null || _this$_evtRemoveById.off();
this._evtRemoveById = null;
(_this$_evtUnloadById = this._evtUnloadById) == null || _this$_evtUnloadById.off();
this._evtUnloadById = null;
}
if (this.url) {
var _this$_evtLoadByUrl, _this$_evtAddByUrl, _this$_evtRemoveByUrl;
(_this$_evtLoadByUrl = this._evtLoadByUrl) == null || _this$_evtLoadByUrl.off();
this._evtLoadByUrl = null;
(_this$_evtAddByUrl = this._evtAddByUrl) == null || _this$_evtAddByUrl.off();
this._evtAddByUrl = null;
(_this$_evtRemoveByUrl = this._evtRemoveByUrl) == null || _this$_evtRemoveByUrl.off();
this._evtRemoveByUrl = null;
}
}
_onLoad(asset) {
this._onAssetLoad.call(this._scope, this.propertyName, this.parent, asset);
}
_onAdd(asset) {
this.asset = asset;
this._onAssetAdd.call(this._scope, this.propertyName, this.parent, asset);
}
_onRemove(asset) {
this._onAssetRemove.call(this._scope, this.propertyName, this.parent, asset);
this.asset = null;
}
_onUnload(asset) {
this._onAssetUnload.call(this._scope, this.propertyName, this.parent, asset);
}
}
export { AssetReference };