"use strict";Object.defineProperty(exports,"__esModule",{value:!0});let getOwnerKey=(e,t)=>`${e.id}-${t}`;class ObjectsRegistry{constructor(){this.nextId=0,this.storage={},this.owners={},this.electronIds=new WeakMap}add(e,t,s){let r=this.saveToStorage(s),i=getOwnerKey(e,t),n=this.owners[i];return n||(n=this.owners[i]=new Map,this.registerDeleteListener(e,t)),!n.has(r)&&(n.set(r,0),this.storage[r].count++),n.set(r,n.get(r)+1),r}get(e){let t=this.storage[e];if(null!=t)return t.object}remove(e,t,s){let r=getOwnerKey(e,t),i=this.owners[r];if(i&&i.has(s)){let e=i.get(s)-1;e<=0?(i.delete(s),this.dereference(s)):i.set(s,e)}}clear(e,t){let s=getOwnerKey(e,t),r=this.owners[s];if(r){for(let e of r.keys())this.dereference(e);delete this.owners[s]}}saveToStorage(e){let t=this.electronIds.get(e);return t||(t=++this.nextId,this.storage[t]={count:0,object:e},this.electronIds.set(e,t)),t}dereference(e){let t=this.storage[e];null!=t&&(t.count-=1,0===t.count&&(this.electronIds.delete(t.object),delete this.storage[e]))}registerDeleteListener(e,t){let s=t.split("-")[0],r=(i,n)=>{n&&n.toString()===s&&(e.removeListener("render-view-deleted",r),this.clear(e,t))};e.on("render-view-deleted",r)}}exports.default=new ObjectsRegistry;