| 3 | const MapPolyfill: typeof Map = require('es6-map'); |
| 4 | |
| 5 | export class IsolateModule { |
| 6 | private elementsByFullScope: Map<string, Element>; |
| 7 | |
| 8 | /** |
| 9 | * A Map where keys are full scope strings and values are many delegators |
| 10 | * for that scope. The only reason why this data structure is here is to |
| 11 | * be able to update the origin element inside those delegators. |
| 12 | * The delegators are never created in this class. |
| 13 | */ |
| 14 | private delegatorsByFullScope: Map<string, Array<EventDelegator>>; |
| 15 | |
| 16 | /** |
| 17 | * A registry of full scopes representing scopes that are currently |
| 18 | * being updated in delegators or elements. This is useful to avoid |
| 19 | * cleaning up data structures for an element that is being *replaced*, |
| 20 | * not *removed* in the virtual DOM. |
| 21 | */ |
| 22 | private fullScopesBeingUpdated: Array<string>; |
| 23 | |
| 24 | constructor() { |
| 25 | this.elementsByFullScope = new MapPolyfill<string, Element>(); |
| 26 | this.delegatorsByFullScope = new MapPolyfill<string, Array<EventDelegator>>(); |
| 27 | this.fullScopesBeingUpdated = []; |
| 28 | } |
| 29 | |
| 30 | private cleanupVNode({data, elm}: VNode) { |
| 31 | const fullScope: string = (data || {} as any).isolate || ''; |
| 32 | const isCurrentElm = this.elementsByFullScope.get(fullScope) === elm; |
| 33 | const isScopeBeingUpdated = this.fullScopesBeingUpdated.indexOf(fullScope) >= 0; |
| 34 | if (fullScope && isCurrentElm && !isScopeBeingUpdated) { |
| 35 | this.elementsByFullScope.delete(fullScope); |
| 36 | this.delegatorsByFullScope.delete(fullScope); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | public getElement(fullScope: string): Element | undefined { |
| 41 | return this.elementsByFullScope.get(fullScope); |
| 42 | } |
| 43 | |
| 44 | public getFullScope(elm: Element): string { |
| 45 | const iterator = this.elementsByFullScope.entries(); |
| 46 | for (let result = iterator.next(); !!result.value; result = iterator.next()) { |
| 47 | const [fullScope, element] = result.value; |
| 48 | if (elm === element) { |
| 49 | return fullScope; |
| 50 | } |
| 51 | } |
| 52 | return ''; |
| 53 | } |
| 54 | |
| 55 | public addEventDelegator(fullScope: string, eventDelegator: EventDelegator) { |
| 56 | let delegators = this.delegatorsByFullScope.get(fullScope); |
| 57 | if (!delegators) { |
| 58 | delegators = []; |
| 59 | this.delegatorsByFullScope.set(fullScope, delegators); |
| 60 | } |
| 61 | delegators[delegators.length] = eventDelegator; |
| 62 | } |
nothing calls this directly
no outgoing calls
no test coverage detected