1/* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 */ 19 20import Page from '../page/index'; 21 22/** 23 * This class defines the map for the page instances. 24 */ 25export class PageLinkedMap { 26 public _map: Page[]; 27 28 constructor() { 29 this._map = []; 30 } 31 32 public unshift(instance: Page) { 33 this._map.unshift(instance); 34 Object.defineProperty(this, instance.id, { 35 configurable: true, 36 enumerable: true, 37 get: function proxyGetter() { 38 return proxyGet(this, instance.id); 39 } 40 }); 41 } 42 43 public push(instance: Page) { 44 this._map.push(instance); 45 Object.defineProperty(this, instance.id, { 46 configurable: true, 47 enumerable: true, 48 get: function proxyGetter() { 49 return proxyGet(this, instance.id); 50 } 51 }); 52 } 53 54 public remove(instance: Page) { 55 const index = this._map.indexOf(instance); 56 delete this[instance.id]; 57 // @ts-ignore 58 delete this._map.splice(index, 1); 59 } 60 61 public getTop(packageName: string) { 62 const appMap = this._map.filter(instance => { 63 return instance.packageName === packageName; 64 }); 65 return appMap && appMap[appMap.length - 1]; 66 } 67} 68 69function proxyGet(pageLinkedMap: PageLinkedMap, id: string) { 70 const index = pageLinkedMap._map.map(instance => instance.id).lastIndexOf(id); 71 return pageLinkedMap._map[index]; 72} 73 74export const pageMap: PageLinkedMap = new PageLinkedMap(); 75 76export const appMap = {}; 77