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 { destroy } from '../../page/api/index'; 21import { App } from '../../app/App'; 22import { PageLinkedMap } from '../../app/map'; 23import { resetTarget } from '../../reactivity/dep'; 24import Page from '../../page'; 25import { init as initApp } from '../../page/api/index'; 26import { appCreate, Options } from '../../app/index'; 27 28const pageMap: PageLinkedMap = App.pageMap; 29 30/** 31 * Create a page. 32 * @param {string} id - Page id. 33 * @param {string} code - JS Bundle code. 34 * @param {Object} options - Options of a page instance. 35 * @param {Object} data - Data that needed. 36 * @param {*} env - Such as: { created, ... services }. 37 * @return {Object | Error} 38 */ 39export function createInstance(id: string, code: string, options: Options, data: object, env: any): object | Error { 40 const { services } = env; 41 const { I18n, dpi } = services; 42 resetTarget(); 43 44 let page: Page = pageMap[id]; 45 let result: object; 46 if (!page) { 47 page = new Page(id, options, options.packageName, data); 48 page.i18nService = I18n; 49 page.dpiService = dpi; 50 appCreate(page, options, data, services); 51 pageMap.push(page); 52 result = initApp(page, code, data, services); 53 } else { 54 result = new Error(`invalid page id "${id}"`); 55 } 56 return result; 57} 58 59/** 60 * Destroy a page by id. 61 * @param {string} id - Page id. 62 * @return {Object} The reset pageMap. 63 */ 64export function destroyInstance(id: string): object { 65 if (typeof markupState === 'function') { 66 markupState(); 67 } 68 resetTarget(); 69 const page: Page = pageMap[id]; 70 if (!page) { 71 return new Error(`invalid page id '${id}'.`); 72 } 73 pageMap.remove(page); 74 destroy(page); 75 76 // Tell v8 to do a full Garbage Collection every eighteen times. 77 const remainder: number = Math.round(Number(id)) % 18; 78 if (!remainder) { 79 if (typeof notifyTrimMemory === 'function') { 80 notifyTrimMemory(); 81 } 82 } 83 return pageMap; 84} 85