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