1 /* 2 * Copyright (c) 2023 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 16 #ifndef BASE_PROMISE_SESSION_H 17 #define BASE_PROMISE_SESSION_H 18 19 #include "napi/native_api.h" 20 #include "node_api.h" 21 22 #include "napi_session.h" 23 24 namespace OHOS::UpdateEngine { 25 template <typename RESULT> class BasePromiseSession : public NapiSession { 26 public: 27 BasePromiseSession(BaseClient *client, SessionParams &sessionParams, size_t argc, size_t callbackNumber = 0) NapiSession(client,sessionParams,argc,callbackNumber)28 : NapiSession(client, sessionParams, argc, callbackNumber) {} 29 30 ~BasePromiseSession() override = default; 31 StartWork(napi_env env,size_t startIndex,const napi_value * args)32 napi_value StartWork(napi_env env, size_t startIndex, const napi_value *args) override 33 { 34 ENGINE_LOGI("BasePromiseSession::StartWork"); 35 PARAM_CHECK_NAPI_CALL(env, args != nullptr, return nullptr, "Invalid para"); 36 napi_value workName = CreateWorkerName(env); 37 PARAM_CHECK_NAPI_CALL(env, workName != nullptr, return nullptr, "Failed to worker name"); 38 39 napi_value promise; 40 napi_status status = napi_create_promise(env, &deferred_, &promise); 41 PARAM_CHECK_NAPI_CALL(env, status == napi_ok, return nullptr, "Failed to napi_create_promise"); 42 43 // Create an asynchronous call. 44 status = napi_create_async_work(env, nullptr, workName, NapiSession::ExecuteWork, NapiSession::CompleteWork, 45 this, &(worker_)); 46 PARAM_CHECK_NAPI_CALL(env, status == napi_ok, return nullptr, "Failed to napi_create_async_work"); 47 // Put the thread in the task execution queue. 48 status = napi_queue_async_work_with_qos(env, worker_, napi_qos_default); 49 PARAM_CHECK_NAPI_CALL(env, status == napi_ok, return nullptr, "Failed to napi_queue_async_work"); 50 return promise; 51 } 52 NotifyJS(napi_env env,napi_value thisVar,const RESULT & result)53 void NotifyJS(napi_env env, napi_value thisVar, const RESULT &result) 54 { 55 int32_t errorNum = static_cast<int32_t>(result.businessError.errorNum); 56 ENGINE_LOGI("BasePromiseSession NotifyJS errorNum:%{public}d", errorNum); 57 58 // Get the return result. 59 napi_value processResult = nullptr; 60 BusinessError businessError; 61 GetBusinessError(businessError, result); 62 if (businessError.errorNum == CallResult::SUCCESS) { 63 result.buildJSObject(env, processResult, result); 64 napi_resolve_deferred(env, deferred_, processResult); 65 } else { 66 NapiCommonUtils::BuildBusinessError(env, processResult, businessError); 67 napi_reject_deferred(env, deferred_, processResult); 68 } 69 napi_delete_async_work(env, worker_); 70 worker_ = nullptr; 71 } 72 CompleteWork(napi_env env,napi_status status)73 virtual void CompleteWork(napi_env env, napi_status status) override {} 74 75 protected: 76 napi_async_work worker_ = nullptr; 77 napi_deferred deferred_ = nullptr; 78 }; 79 } // namespace OHOS::UpdateEngine 80 #endif // BASE_PROMISE_SESSION_H