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_ci * 2021.01.08 - Add fireEventSync event to eventHandlers and page.destroyed judgment to 'receiveTasks'.
21e484b35bSopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
22e484b35bSopenharmony_ci */
23e484b35bSopenharmony_ci
24e484b35bSopenharmony_ciimport { Log } from '../../../utils/index';
25e484b35bSopenharmony_ciimport { App } from '../../app/App';
26e484b35bSopenharmony_ciimport { PageLinkedMap } from '../../app/map';
27e484b35bSopenharmony_ciimport Page from '../../page';
28e484b35bSopenharmony_ciimport {
29e484b35bSopenharmony_ci  fireEvent,
30e484b35bSopenharmony_ci  callback,
31e484b35bSopenharmony_ci  fireEventSync,
32e484b35bSopenharmony_ci  destroy
33e484b35bSopenharmony_ci} from '../../page/api/index';
34e484b35bSopenharmony_ci
35e484b35bSopenharmony_ciconst pageMap: PageLinkedMap = App.pageMap;
36e484b35bSopenharmony_ci
37e484b35bSopenharmony_ciconst eventHandlers = {
38e484b35bSopenharmony_ci  /**
39e484b35bSopenharmony_ci   * Invoke the fireEvent function.
40e484b35bSopenharmony_ci   * @param {string} id - Page id.
41e484b35bSopenharmony_ci   * @param {*} args - Args.
42e484b35bSopenharmony_ci   */
43e484b35bSopenharmony_ci  fireEvent: (id: string, ...args: any[]) => {
44e484b35bSopenharmony_ci    return fireEvent(pageMap[id], ...args);
45e484b35bSopenharmony_ci  },
46e484b35bSopenharmony_ci
47e484b35bSopenharmony_ci  /**
48e484b35bSopenharmony_ci   * Invoke the callback function.
49e484b35bSopenharmony_ci   * @param {string} id - Page id.
50e484b35bSopenharmony_ci   * @param {*} args - Args
51e484b35bSopenharmony_ci   */
52e484b35bSopenharmony_ci  callback: (id: string, ...args: any[]) => {
53e484b35bSopenharmony_ci    return callback(pageMap[id], ...args);
54e484b35bSopenharmony_ci  },
55e484b35bSopenharmony_ci
56e484b35bSopenharmony_ci  /**
57e484b35bSopenharmony_ci   * Invoke the fireEventSync function.
58e484b35bSopenharmony_ci   * @param {string} id - Page id.
59e484b35bSopenharmony_ci   * @param {*} args - Args.
60e484b35bSopenharmony_ci   */
61e484b35bSopenharmony_ci  fireEventSync: (id: string, ...args: any[]) => {
62e484b35bSopenharmony_ci    return fireEventSync(pageMap[id], ...args);
63e484b35bSopenharmony_ci  }
64e484b35bSopenharmony_ci};
65e484b35bSopenharmony_ci
66e484b35bSopenharmony_ci/**
67e484b35bSopenharmony_ci * Accept calls from native (event or callback).
68e484b35bSopenharmony_ci * @param {string} id - Page id.
69e484b35bSopenharmony_ci * @param {*} tasks list with `method` and `args`.
70e484b35bSopenharmony_ci * @return {*}
71e484b35bSopenharmony_ci */
72e484b35bSopenharmony_ciexport function receiveTasks(id: string, tasks: any[]): any[] | Error {
73e484b35bSopenharmony_ci  id = id.toString();
74e484b35bSopenharmony_ci  Log.debug(`ReceiveTasks id ${id}, tasks: ${JSON.stringify(tasks)}`);
75e484b35bSopenharmony_ci  const page: Page = pageMap[id];
76e484b35bSopenharmony_ci  if (page && Array.isArray(tasks)) {
77e484b35bSopenharmony_ci    const results = [];
78e484b35bSopenharmony_ci    tasks.forEach((task) => {
79e484b35bSopenharmony_ci      const handler = eventHandlers[task.method];
80e484b35bSopenharmony_ci      const args = [...task.args];
81e484b35bSopenharmony_ci      if (typeof handler === 'function') {
82e484b35bSopenharmony_ci        args.unshift(id);
83e484b35bSopenharmony_ci        results.push(handler(...args));
84e484b35bSopenharmony_ci      }
85e484b35bSopenharmony_ci    });
86e484b35bSopenharmony_ci    if (page.destroyed && page.doc.taskCenter.callbackIsEmpty()) {
87e484b35bSopenharmony_ci      page.callTasks([{
88e484b35bSopenharmony_ci        module: 'internal.jsResult',
89e484b35bSopenharmony_ci        method: 'appDestroyFinish',
90e484b35bSopenharmony_ci        args: []
91e484b35bSopenharmony_ci      }]);
92e484b35bSopenharmony_ci      destroy(page);
93e484b35bSopenharmony_ci      pageMap.remove(page);
94e484b35bSopenharmony_ci    }
95e484b35bSopenharmony_ci    return results;
96e484b35bSopenharmony_ci  }
97e484b35bSopenharmony_ci  return new Error(`Invalid page id '${id}' or tasks.`);
98e484b35bSopenharmony_ci}
99