• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16const navPushPathHelperApi = requireInternal('atomicservice.NavPushPathHelper');
17const hilog = requireNapi('hilog');
18
19const tag = 'NavPushPathHelper::JS::';
20
21export class NavPushPathHelper {
22    static currentID = 0;
23    constructor(navPathStack) {
24        this.navPathStack_ = navPathStack;
25        this.currentHelperId_ = NavPushPathHelper.currentID;
26        NavPushPathHelper.currentID++;
27    }
28
29    async pushPath(moduleName, info, optionParam) {
30        hilog.info(0x3900, tag, `pushPath -> currentID: ${this.currentHelperId_}`);
31        if (navPushPathHelperApi.isHspExist(moduleName, info.name)) {
32            this.navPathStack_?.pushPath(info, optionParam);
33            return;
34        }
35        return new Promise((resolve, reject) => {
36            navPushPathHelperApi.silentInstall(moduleName, () => {
37                navPushPathHelperApi.updateRouteMap();
38                this.navPathStack_?.pushPath(info, optionParam);
39                resolve();
40            },
41            (error) => {
42                const err = new Error(error.message);
43                err.code = error.code;
44                reject(err);
45            });
46        });
47    }
48
49    async pushDestination(moduleName, info, optionParam) {
50        hilog.info(0x3900, tag, `pushDestination -> currentID: ${this.currentHelperId_}`);
51        if (navPushPathHelperApi.isHspExist(moduleName, info.name)) {
52            await this.navPathStack_?.pushDestination(info, optionParam);
53            return;
54        }
55        return new Promise((resolve, reject) => {
56            navPushPathHelperApi.silentInstall(moduleName, () => {
57                navPushPathHelperApi.updateRouteMap();
58                this.navPathStack_?.pushDestination(info, optionParam)
59                .then(resolve).catch(reject);
60            }, (error) => {
61                const err = new Error(error.message);
62                err.code = error.code;
63                reject(err);
64            });
65        });
66    }
67
68    async pushPathByName(moduleName, name, param, onPop, optionParam) {
69        hilog.info(0x3900, tag, `pushPathByName -> currentID: ${this.currentHelperId_}`);
70        if (navPushPathHelperApi.isHspExist(moduleName, name)) {
71            this.navPathStack_?.pushPathByName(name, param, onPop, optionParam);
72            return;
73        }
74        return new Promise((resolve, reject) => {
75            navPushPathHelperApi.silentInstall(moduleName, () => {
76                hilog.info(0x3900, tag, `silentInstall success`);
77                navPushPathHelperApi.updateRouteMap();
78                this.navPathStack_?.pushPathByName(name, param, onPop, optionParam);
79                resolve();
80            }, (error) => {
81                const err = new Error(error.message);
82                err.code = error.code;
83                reject(err);
84            });
85        });
86    }
87
88    async pushDestinationByName(moduleName, name, param, onPop, optionParam) {
89        hilog.info(0x3900, tag, `pushDestinationByName -> currentID: ${this.currentHelperId_}`);
90        if (navPushPathHelperApi.isHspExist(moduleName, name)) {
91            await this.navPathStack_?.pushDestinationByName(name, param, onPop, optionParam);
92            return;
93        }
94        return new Promise((resolve, reject) => {
95            navPushPathHelperApi.silentInstall(moduleName, () => {
96                navPushPathHelperApi.updateRouteMap();
97                this.navPathStack_?.pushDestinationByName(name, param, onPop, optionParam)
98                .then(resolve).catch(reject);
99            }, (error) => {
100                const err = new Error(error.message);
101                err.code = error.code;
102                reject(err);
103            });
104        });
105    }
106
107    async replacePath(moduleName, info, optionParam) {
108        hilog.info(0x3900, tag, `replacePath -> currentID: ${this.currentHelperId_}`);
109        if (navPushPathHelperApi.isHspExist(moduleName, info.name)) {
110            this.navPathStack_?.replacePath(info, optionParam);
111            return;
112        }
113        return new Promise((resolve, reject) => {
114            navPushPathHelperApi.silentInstall(moduleName, () => {
115                navPushPathHelperApi.updateRouteMap();
116                this.navPathStack_?.replacePath(info, optionParam);
117                resolve();
118            }, (error) => {
119                const err = new Error(error.message);
120                err.code = error.code;
121                reject(err);
122            });
123        });
124    }
125
126    async replacePathByName(moduleName, name, param, optionParam) {
127        hilog.info(0x3900, tag, `replacePathByName -> currentID: ${this.currentHelperId_}`);
128        if (navPushPathHelperApi.isHspExist(moduleName, name)) {
129            this.navPathStack_?.replacePathByName(name, param, optionParam);
130            return;
131        }
132        return new Promise((resolve, reject) => {
133            navPushPathHelperApi.silentInstall(moduleName, () => {
134                hilog.info(0x3900, tag, `silentInstall success`);
135                navPushPathHelperApi.updateRouteMap();
136                this.navPathStack_?.replacePathByName(name, param, optionParam);
137                resolve();
138            }, (error) => {
139                const err = new Error(error.message);
140                err.code = error.code;
141                reject(err);
142            });
143        });
144    }
145}
146
147export default { NavPushPathHelper };