1e484b35bSopenharmony_ci/*
2e484b35bSopenharmony_ci * Licensed to the Apache Software Foundation (ASF) under one
3e484b35bSopenharmony_ci * or more contributor license agreements.  See the NOTICE file
4e484b35bSopenharmony_ci * distributed with this work for additional information
5e484b35bSopenharmony_ci * regarding copyright ownership.  The ASF licenses this file
6e484b35bSopenharmony_ci * to you under the Apache License, Version 2.0 (the
7e484b35bSopenharmony_ci * "License"); you may not use this file except in compliance
8e484b35bSopenharmony_ci * with the License.  You may obtain a copy of the License at
9e484b35bSopenharmony_ci *
10e484b35bSopenharmony_ci *   http://www.apache.org/licenses/LICENSE-2.0
11e484b35bSopenharmony_ci *
12e484b35bSopenharmony_ci * Unless required by applicable law or agreed to in writing,
13e484b35bSopenharmony_ci * software distributed under the License is distributed on an
14e484b35bSopenharmony_ci * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15e484b35bSopenharmony_ci * KIND, either express or implied.  See the License for the
16e484b35bSopenharmony_ci * specific language governing permissions and limitations
17e484b35bSopenharmony_ci * under the License.
18e484b35bSopenharmony_ci */
19e484b35bSopenharmony_ci
20e484b35bSopenharmony_ciimport * as framework from '../main';
21e484b35bSopenharmony_ciimport { services } from './service';
22e484b35bSopenharmony_ciimport { Log } from '../utils/utils';
23e484b35bSopenharmony_ciimport { Options } from '../main/app';
24e484b35bSopenharmony_ciimport { I18nInterface } from '../main/extend/i18n/I18n';
25e484b35bSopenharmony_ciimport { DPIInterface } from '../main/extend/dpi/Dpi';
26e484b35bSopenharmony_ci
27e484b35bSopenharmony_ciexport interface EnvInterface {
28e484b35bSopenharmony_ci  config: Options;
29e484b35bSopenharmony_ci  created: number;
30e484b35bSopenharmony_ci  services: ServiceMapInterface;
31e484b35bSopenharmony_ci}
32e484b35bSopenharmony_ci
33e484b35bSopenharmony_ciexport interface pageMapInterface {
34e484b35bSopenharmony_ci  [key: string]: EnvInterface;
35e484b35bSopenharmony_ci}
36e484b35bSopenharmony_ci
37e484b35bSopenharmony_ciexport interface ServicesInterface {
38e484b35bSopenharmony_ci  I18n?: I18nInterface;
39e484b35bSopenharmony_ci  dpi?: DPIInterface;
40e484b35bSopenharmony_ci}
41e484b35bSopenharmony_ci
42e484b35bSopenharmony_ciexport interface ServiceMapInterface extends ServicesInterface {
43e484b35bSopenharmony_ci  service?: any;
44e484b35bSopenharmony_ci}
45e484b35bSopenharmony_ci
46e484b35bSopenharmony_ciconst pageMap: pageMapInterface = {};
47e484b35bSopenharmony_ci
48e484b35bSopenharmony_ci/**
49e484b35bSopenharmony_ci * <p>Create instance by framework.</p>
50e484b35bSopenharmony_ci * <p>The framework is based on JS Bundle code.</p>
51e484b35bSopenharmony_ci * @param {string} id - Id of a page.
52e484b35bSopenharmony_ci * @param {string} code - JS Bundle code.
53e484b35bSopenharmony_ci * @param {Options} config - Page config
54e484b35bSopenharmony_ci * @param {Object} data - Data that needed.
55e484b35bSopenharmony_ci * @return {*}
56e484b35bSopenharmony_ci */
57e484b35bSopenharmony_cifunction createInstance(id: string, code: string, config: Options, data: object): any | Error {
58e484b35bSopenharmony_ci  const page = pageMap[id];
59e484b35bSopenharmony_ci  if (!page) {
60e484b35bSopenharmony_ci    Log.debug(`Create a page.`);
61e484b35bSopenharmony_ci    const env: EnvInterface = {
62e484b35bSopenharmony_ci      config,
63e484b35bSopenharmony_ci      created: Date.now(),
64e484b35bSopenharmony_ci      services: createServices(id)
65e484b35bSopenharmony_ci    };
66e484b35bSopenharmony_ci    pageMap[id] = env;
67e484b35bSopenharmony_ci    if (config && config.appCreate && config.appCode && config.packageName) {
68e484b35bSopenharmony_ci      pageMap[config.appInstanceId] = env;
69e484b35bSopenharmony_ci    }
70e484b35bSopenharmony_ci    return framework.createInstance(id, code, config, data, env);
71e484b35bSopenharmony_ci  }
72e484b35bSopenharmony_ci  return new Error(`Invalid instance id '${id}'.`);
73e484b35bSopenharmony_ci}
74e484b35bSopenharmony_ci
75e484b35bSopenharmony_ci/**
76e484b35bSopenharmony_ci * Get root page by pageId.
77e484b35bSopenharmony_ci * @param {*} args - Args.
78e484b35bSopenharmony_ci * @return {*} Root page.
79e484b35bSopenharmony_ci */
80e484b35bSopenharmony_cifunction getRoot(...args: any[]): any | Error {
81e484b35bSopenharmony_ci  const pageId = args[0];
82e484b35bSopenharmony_ci  const page = getPage(pageId);
83e484b35bSopenharmony_ci  if (page && framework) {
84e484b35bSopenharmony_ci    return framework['getRoot'](pageId);
85e484b35bSopenharmony_ci  }
86e484b35bSopenharmony_ci  return new Error(`Invalid instance id '${pageId}'.`);
87e484b35bSopenharmony_ci}
88e484b35bSopenharmony_ci
89e484b35bSopenharmony_ci/**
90e484b35bSopenharmony_ci * Destroy a page.
91e484b35bSopenharmony_ci * @param {string} pageId - Id of a page.
92e484b35bSopenharmony_ci * @return {*}
93e484b35bSopenharmony_ci */
94e484b35bSopenharmony_cifunction destroyInstance(pageId: string): any | Error {
95e484b35bSopenharmony_ci  const page = getPage(pageId);
96e484b35bSopenharmony_ci  const result = framework.destroyInstance(pageId);
97e484b35bSopenharmony_ci  if (page && framework) {
98e484b35bSopenharmony_ci    services.forEach(service => {
99e484b35bSopenharmony_ci      const destroy: (pageId: string) => void = service.options.destroy;
100e484b35bSopenharmony_ci      if (destroy) {
101e484b35bSopenharmony_ci        destroy(pageId);
102e484b35bSopenharmony_ci      }
103e484b35bSopenharmony_ci    });
104e484b35bSopenharmony_ci    delete pageMap[pageId];
105e484b35bSopenharmony_ci    return result;
106e484b35bSopenharmony_ci  }
107e484b35bSopenharmony_ci  return new Error(`Invalid page id '${pageId}'.`);
108e484b35bSopenharmony_ci}
109e484b35bSopenharmony_ci
110e484b35bSopenharmony_ci/**
111e484b35bSopenharmony_ci * <p>When native invokes this method,<br>
112e484b35bSopenharmony_ci * the receiveTasks method of the instance corresponding to the pageID is invoked.</p>
113e484b35bSopenharmony_ci * @param {string} pageId - Id of a page.
114e484b35bSopenharmony_ci * @param {*} tasks - Tasks from native.
115e484b35bSopenharmony_ci * @return {*}
116e484b35bSopenharmony_ci */
117e484b35bSopenharmony_cifunction callJS(pageId: string, tasks: any[]): any | Error {
118e484b35bSopenharmony_ci  const page = getPage(pageId);
119e484b35bSopenharmony_ci  if (page && framework) {
120e484b35bSopenharmony_ci    return framework.receiveTasks(pageId, tasks);
121e484b35bSopenharmony_ci  }
122e484b35bSopenharmony_ci  return new Error(`Invalid page id '${pageId}'.`);
123e484b35bSopenharmony_ci}
124e484b35bSopenharmony_ci
125e484b35bSopenharmony_ci/**
126e484b35bSopenharmony_ci * Get page by id.
127e484b35bSopenharmony_ci * @param {string} id - Id of a page.
128e484b35bSopenharmony_ci * @return {EnvInterface} Page Env.
129e484b35bSopenharmony_ci */
130e484b35bSopenharmony_cifunction getPage(id: string): EnvInterface {
131e484b35bSopenharmony_ci  return pageMap[id];
132e484b35bSopenharmony_ci}
133e484b35bSopenharmony_ci
134e484b35bSopenharmony_ci/**
135e484b35bSopenharmony_ci * Init JavaScript services for this instance.
136e484b35bSopenharmony_ci * @param {string} id - Create service by id.
137e484b35bSopenharmony_ci * @return {ServiceMapInterface} service map.
138e484b35bSopenharmony_ci */
139e484b35bSopenharmony_cifunction createServices(id: string): ServiceMapInterface {
140e484b35bSopenharmony_ci  const serviceMap: ServiceMapInterface = {};
141e484b35bSopenharmony_ci  services.forEach((service) => {
142e484b35bSopenharmony_ci    Log.debug(`[JS Runtime] Create service ${service.name}.`);
143e484b35bSopenharmony_ci    const create: (id: string) => any = service.options.create;
144e484b35bSopenharmony_ci    if (create) {
145e484b35bSopenharmony_ci      const result: any = create(id);
146e484b35bSopenharmony_ci      Object.assign(serviceMap, result.instance);
147e484b35bSopenharmony_ci    }
148e484b35bSopenharmony_ci  });
149e484b35bSopenharmony_ci  return serviceMap;
150e484b35bSopenharmony_ci}
151e484b35bSopenharmony_ci
152e484b35bSopenharmony_ciexport default {
153e484b35bSopenharmony_ci  createInstance: createInstance,
154e484b35bSopenharmony_ci  getRoot: getRoot,
155e484b35bSopenharmony_ci  callJS: callJS,
156e484b35bSopenharmony_ci  destroyInstance: destroyInstance,
157e484b35bSopenharmony_ci  appError: framework.appError,
158e484b35bSopenharmony_ci  appDestroy: framework.appDestroy,
159e484b35bSopenharmony_ci  appHide: framework.appHide,
160e484b35bSopenharmony_ci  appShow: framework.appShow,
161e484b35bSopenharmony_ci  registerModules: framework.registerModules
162e484b35bSopenharmony_ci};
163