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.initRouteMap(); 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.initRouteMap(); 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 navPushPathHelperApi.initRouteMap(); 77 this.navPathStack_?.pushPathByName(name, param, onPop, optionParam); 78 resolve(); 79 }, (error) => { 80 const err = new Error(error.message); 81 err.code = error.code; 82 reject(err); 83 }); 84 }); 85 } 86 87 async pushDestinationByName(moduleName, name, param, onPop, optionParam) { 88 hilog.info(0x3900, tag, `pushDestinationByName -> currentID: ${this.currentHelperId_}`); 89 if (navPushPathHelperApi.isHspExist(moduleName, name)) { 90 await this.navPathStack_?.pushDestinationByName(name, param, onPop, optionParam); 91 return; 92 } 93 return new Promise((resolve, reject) => { 94 navPushPathHelperApi.silentInstall(moduleName, () => { 95 navPushPathHelperApi.initRouteMap(); 96 this.navPathStack_?.pushDestinationByName(name, param, onPop, optionParam) 97 .then(resolve).catch(reject); 98 }, (error) => { 99 const err = new Error(error.message); 100 err.code = error.code; 101 reject(err); 102 }); 103 }); 104 } 105 106 async replacePath(moduleName, info, optionParam) { 107 hilog.info(0x3900, tag, `replacePath -> currentID: ${this.currentHelperId_}`); 108 if (navPushPathHelperApi.isHspExist(moduleName, info.name)) { 109 this.navPathStack_?.replacePath(info, optionParam); 110 return; 111 } 112 return new Promise((resolve, reject) => { 113 navPushPathHelperApi.silentInstall(moduleName, () => { 114 navPushPathHelperApi.initRouteMap(); 115 this.navPathStack_?.replacePath(info, optionParam); 116 resolve(); 117 }, (error) => { 118 const err = new Error(error.message); 119 err.code = error.code; 120 reject(err); 121 }); 122 }); 123 } 124 125 async replacePathByName(moduleName, name, param, optionParam) { 126 hilog.info(0x3900, tag, `replacePathByName -> currentID: ${this.currentHelperId_}`); 127 if (navPushPathHelperApi.isHspExist(moduleName, name)) { 128 this.navPathStack_?.replacePathByName(name, param, optionParam); 129 return; 130 } 131 return new Promise((resolve, reject) => { 132 navPushPathHelperApi.silentInstall(moduleName, () => { 133 hilog.info(0x3900, tag, `silentInstall success`); 134 navPushPathHelperApi.initRouteMap(); 135 this.navPathStack_?.replacePathByName(name, param, optionParam); 136 resolve(); 137 }, (error) => { 138 const err = new Error(error.message); 139 err.code = error.code; 140 reject(err); 141 }); 142 }); 143 } 144} 145 146export default { NavPushPathHelper };