1 /* 2 * Copyright (c) 2022 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 FILEMGMT_LIBN_N_ASYNC_CONTEXT_H 17 #define FILEMGMT_LIBN_N_ASYNC_CONTEXT_H 18 19 #include <functional> 20 21 #include "n_error.h" 22 #include "n_ref.h" 23 #include "n_val.h" 24 25 namespace OHOS { 26 namespace FileManagement { 27 namespace LibN { 28 using NContextCBExec = std::function<NError()>; 29 using NContextCBComplete = std::function<NVal(napi_env, NError)>; 30 31 class NAsyncContext { 32 public: 33 NError err_; 34 NVal res_; 35 NContextCBExec cbExec_; 36 NContextCBComplete cbComplete_; 37 napi_async_work awork_; 38 NRef thisPtr_; 39 NAsyncContext(NVal thisPtr)40 NAsyncContext(NVal thisPtr) 41 : err_(0), res_(NVal()), cbExec_(nullptr), cbComplete_(nullptr), awork_(nullptr), thisPtr_(thisPtr) 42 { 43 } 44 virtual ~NAsyncContext() = default; 45 }; 46 47 class NAsyncContextPromise : public NAsyncContext { 48 public: 49 napi_deferred deferred_; NAsyncContextPromise(NVal thisPtr)50 explicit NAsyncContextPromise(NVal thisPtr) : NAsyncContext(thisPtr) {} 51 ~NAsyncContextPromise() = default; 52 }; 53 54 class NAsyncContextCallback : public NAsyncContext { 55 public: 56 NRef cb_; NAsyncContextCallback(NVal thisPtr,NVal cb)57 NAsyncContextCallback(NVal thisPtr, NVal cb) : NAsyncContext(thisPtr), cb_(cb) {} 58 ~NAsyncContextCallback() = default; 59 }; 60 61 class NAsyncContextLegacy : public NAsyncContext { 62 public: 63 NRef cbSucc_; 64 NRef cbFail_; 65 NRef cbFinal_; NAsyncContextLegacy(NVal thisPtr,NVal cbSucc,NVal cbFail,NVal cbFinal)66 NAsyncContextLegacy(NVal thisPtr, NVal cbSucc, NVal cbFail, NVal cbFinal) 67 : NAsyncContext(thisPtr), cbSucc_(cbSucc), cbFail_(cbFail), cbFinal_(cbFinal) 68 { 69 } 70 ~NAsyncContextLegacy() = default; 71 }; 72 } // namespace LibN 73 } // namespace FileManagement 74 } // namespace OHOS 75 #endif // FILEMGMT_LIBN_N_ASYNC_CONTEXT_H