|
| 1 | +import {Injectable, Inject} from "angular2/di"; |
| 2 | +import {RenderProtoViewRef} from "angular2/src/render/api"; |
| 3 | +import {ON_WEBWORKER} from "angular2/src/web-workers/shared/api"; |
| 4 | + |
| 5 | +@Injectable() |
| 6 | +export class RenderProtoViewRefStore { |
| 7 | + private _lookupByIndex: Map<number, RenderProtoViewRef> = new Map<number, RenderProtoViewRef>(); |
| 8 | + private _lookupByProtoView: Map<RenderProtoViewRef, number> = |
| 9 | + new Map<RenderProtoViewRef, number>(); |
| 10 | + private _nextIndex: number = 0; |
| 11 | + private _onWebworker: boolean; |
| 12 | + |
| 13 | + constructor(@Inject(ON_WEBWORKER) onWebworker) { this._onWebworker = onWebworker; } |
| 14 | + |
| 15 | + storeRenderProtoViewRef(ref: RenderProtoViewRef): number { |
| 16 | + if (this._lookupByProtoView.has(ref)) { |
| 17 | + return this._lookupByProtoView.get(ref); |
| 18 | + } else { |
| 19 | + this._lookupByIndex.set(this._nextIndex, ref); |
| 20 | + this._lookupByProtoView.set(ref, this._nextIndex); |
| 21 | + return this._nextIndex++; |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + retreiveRenderProtoViewRef(index: number): RenderProtoViewRef { |
| 26 | + return this._lookupByIndex.get(index); |
| 27 | + } |
| 28 | + |
| 29 | + deserialize(index: number): RenderProtoViewRef { |
| 30 | + if (index == null) { |
| 31 | + return null; |
| 32 | + } |
| 33 | + |
| 34 | + if (this._onWebworker) { |
| 35 | + return new WebworkerRenderProtoViewRef(index); |
| 36 | + } else { |
| 37 | + return this.retreiveRenderProtoViewRef(index); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + serialize(ref: RenderProtoViewRef): number { |
| 42 | + if (ref == null) { |
| 43 | + return null; |
| 44 | + } |
| 45 | + |
| 46 | + if (this._onWebworker) { |
| 47 | + return (<WebworkerRenderProtoViewRef>ref).refNumber; |
| 48 | + } else { |
| 49 | + return this.storeRenderProtoViewRef(ref); |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +export class WebworkerRenderProtoViewRef extends RenderProtoViewRef { |
| 55 | + constructor(public refNumber: number) { super(); } |
| 56 | +} |
0 commit comments