• 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 * as framework from '../main';
21import { services } from './service';
22import { Log } from '../utils/utils';
23import { Options } from '../main/app';
24import { I18nInterface } from '../main/extend/i18n/I18n';
25import { DPIInterface } from '../main/extend/dpi/Dpi';
26
27export interface EnvInterface {
28  config: Options;
29  created: number;
30  services: ServiceMapInterface;
31}
32
33export interface pageMapInterface {
34  [key: string]: EnvInterface;
35}
36
37export interface ServicesInterface {
38  I18n?: I18nInterface;
39  dpi?: DPIInterface;
40}
41
42export interface ServiceMapInterface extends ServicesInterface {
43  service?: any;
44}
45
46const pageMap: pageMapInterface = {};
47
48/**
49 * <p>Create instance by framework.</p>
50 * <p>The framework is based on JS Bundle code.</p>
51 * @param {string} id - Id of a page.
52 * @param {string} code - JS Bundle code.
53 * @param {Options} config - Page config
54 * @param {Object} data - Data that needed.
55 * @return {*}
56 */
57function createInstance(id: string, code: string, config: Options, data: object): any | Error {
58  const page = pageMap[id];
59  if (!page) {
60    Log.debug(`Create a page.`);
61    const env: EnvInterface = {
62      config,
63      created: Date.now(),
64      services: createServices(id)
65    };
66    pageMap[id] = env;
67    if (config && config.appCreate && config.appCode && config.packageName) {
68      pageMap[config.appInstanceId] = env;
69    }
70    return framework.createInstance(id, code, config, data, env);
71  }
72  return new Error(`Invalid instance id '${id}'.`);
73}
74
75/**
76 * Get root page by pageId.
77 * @param {*} args - Args.
78 * @return {*} Root page.
79 */
80function getRoot(...args: any[]): any | Error {
81  const pageId = args[0];
82  const page = getPage(pageId);
83  if (page && framework) {
84    return framework['getRoot'](pageId);
85  }
86  return new Error(`Invalid instance id '${pageId}'.`);
87}
88
89/**
90 * Destroy a page.
91 * @param {string} pageId - Id of a page.
92 * @return {*}
93 */
94function destroyInstance(pageId: string): any | Error {
95  const page = getPage(pageId);
96  const result = framework.destroyInstance(pageId);
97  if (page && framework) {
98    services.forEach(service => {
99      const destroy: (pageId: string) => void = service.options.destroy;
100      if (destroy) {
101        destroy(pageId);
102      }
103    });
104    delete pageMap[pageId];
105    return result;
106  }
107  return new Error(`Invalid page id '${pageId}'.`);
108}
109
110/**
111 * <p>When native invokes this method,<br>
112 * the receiveTasks method of the instance corresponding to the pageID is invoked.</p>
113 * @param {string} pageId - Id of a page.
114 * @param {*} tasks - Tasks from native.
115 * @return {*}
116 */
117function callJS(pageId: string, tasks: any[]): any | Error {
118  const page = getPage(pageId);
119  if (page && framework) {
120    return framework.receiveTasks(pageId, tasks);
121  }
122  return new Error(`Invalid page id '${pageId}'.`);
123}
124
125/**
126 * Get page by id.
127 * @param {string} id - Id of a page.
128 * @return {EnvInterface} Page Env.
129 */
130function getPage(id: string): EnvInterface {
131  return pageMap[id];
132}
133
134/**
135 * Init JavaScript services for this instance.
136 * @param {string} id - Create service by id.
137 * @return {ServiceMapInterface} service map.
138 */
139function createServices(id: string): ServiceMapInterface {
140  const serviceMap: ServiceMapInterface = {};
141  services.forEach((service) => {
142    Log.debug(`[JS Runtime] Create service ${service.name}.`);
143    const create: (id: string) => any = service.options.create;
144    if (create) {
145      const result: any = create(id);
146      Object.assign(serviceMap, result.instance);
147    }
148  });
149  return serviceMap;
150}
151
152export default {
153  createInstance: createInstance,
154  getRoot: getRoot,
155  callJS: callJS,
156  destroyInstance: destroyInstance,
157  appError: framework.appError,
158  appDestroy: framework.appDestroy,
159  appHide: framework.appHide,
160  appShow: framework.appShow,
161  registerModules: framework.registerModules
162};
163