@animech-public/playcanvas
Version:
PlayCanvas WebGL game engine
2 lines (1 loc) • 2.34 kB
JavaScript
class t{constructor(t,s,i,e,h){this._evtLoadById=null,this._evtUnloadById=null,this._evtAddById=null,this._evtRemoveById=null,this._evtLoadByUrl=null,this._evtAddByUrl=null,this._evtRemoveByUrl=null,this.propertyName=t,this.parent=s,this._scope=h,this._registry=i,this.id=null,this.url=null,this.asset=null,this._onAssetLoad=e.load,this._onAssetAdd=e.add,this._onAssetRemove=e.remove,this._onAssetUnload=e.unload}set id(t){if(this.url)throw Error("Can't set id and url");this._unbind(),this._id=t,this.asset=this._registry.get(this._id),this._bind()}get id(){return this._id}set url(t){if(this.id)throw Error("Can't set id and url");this._unbind(),this._url=t,this.asset=this._registry.getByUrl(this._url),this._bind()}get url(){return this._url}_bind(){this.id&&(this._onAssetLoad&&(this._evtLoadById=this._registry.on(`load:${this.id}`,this._onLoad,this)),this._onAssetAdd&&(this._evtAddById=this._registry.once(`add:${this.id}`,this._onAdd,this)),this._onAssetRemove&&(this._evtRemoveById=this._registry.on(`remove:${this.id}`,this._onRemove,this)),this._onAssetUnload&&(this._evtUnloadById=this._registry.on(`unload:${this.id}`,this._onUnload,this))),this.url&&(this._onAssetLoad&&(this._evtLoadByUrl=this._registry.on(`load:url:${this.url}`,this._onLoad,this)),this._onAssetAdd&&(this._evtAddByUrl=this._registry.once(`add:url:${this.url}`,this._onAdd,this)),this._onAssetRemove&&(this._evtRemoveByUrl=this._registry.on(`remove:url:${this.url}`,this._onRemove,this)))}_unbind(){var t,s,i,e,h,d,o;this.id&&(null==(t=this._evtLoadById)||t.off(),this._evtLoadById=null,null==(s=this._evtAddById)||s.off(),this._evtAddById=null,null==(i=this._evtRemoveById)||i.off(),this._evtRemoveById=null,null==(e=this._evtUnloadById)||e.off(),this._evtUnloadById=null);this.url&&(null==(h=this._evtLoadByUrl)||h.off(),this._evtLoadByUrl=null,null==(d=this._evtAddByUrl)||d.off(),this._evtAddByUrl=null,null==(o=this._evtRemoveByUrl)||o.off(),this._evtRemoveByUrl=null)}_onLoad(t){this._onAssetLoad.call(this._scope,this.propertyName,this.parent,t)}_onAdd(t){this.asset=t,this._onAssetAdd.call(this._scope,this.propertyName,this.parent,t)}_onRemove(t){this._onAssetRemove.call(this._scope,this.propertyName,this.parent,t),this.asset=null}_onUnload(t){this._onAssetUnload.call(this._scope,this.propertyName,this.parent,t)}}export{t as AssetReference};