• 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 
16 #ifndef INTERFACES_KITS_JS_ZIP_NAPI_COMMON_NAPI_ASYNC_CONTEXT_H
17 #define INTERFACES_KITS_JS_ZIP_NAPI_COMMON_NAPI_ASYNC_CONTEXT_H
18 
19 #include "napi_business_error.h"
20 #include "napi_value.h"
21 #include "napi_reference.h"
22 
23 namespace OHOS {
24 namespace AppExecFwk {
25 namespace LIBZIP {
26 using NContextCBExec = std::function<NapiBusinessError(napi_env)>;
27 using NContextCBComplete = std::function<NapiValue(napi_env, NapiBusinessError)>;
28 
29 class NapiAsyncContext {
30 public:
31     NapiBusinessError err_;
32     NapiValue res_;
33     NContextCBExec cbExec_;
34     NContextCBComplete cbComplete_;
35     napi_async_work awork_;
36     NapiReference thisPtr_;
37 
NapiAsyncContext(NapiValue thisPtr)38     explicit NapiAsyncContext(NapiValue thisPtr)
39         : err_(0), res_(NapiValue()), cbExec_(nullptr), cbComplete_(nullptr), awork_(nullptr), thisPtr_(thisPtr)
40     {}
41     virtual ~NapiAsyncContext() = default;
42 };
43 
44 class NAsyncContextPromise : public NapiAsyncContext {
45 public:
46     napi_deferred deferred_ = nullptr;
NAsyncContextPromise(NapiValue thisPtr)47     explicit NAsyncContextPromise(NapiValue thisPtr) : NapiAsyncContext(thisPtr)
48     {}
49     ~NAsyncContextPromise() override = default;
50 };
51 
52 class NAsyncContextCallback : public NapiAsyncContext {
53 public:
54     NapiReference cb_;
NAsyncContextCallback(NapiValue thisPtr,NapiValue cb)55     NAsyncContextCallback(NapiValue thisPtr, NapiValue cb) : NapiAsyncContext(thisPtr), cb_(cb)
56     {}
57     ~NAsyncContextCallback() override = default;
58 };
59 
60 class NAsyncContextLegacy : public NapiAsyncContext {
61 public:
62     NapiReference cbSucc_;
63     NapiReference cbFail_;
64     NapiReference cbFinal_;
NAsyncContextLegacy(NapiValue thisPtr,NapiValue cbSucc,NapiValue cbFail,NapiValue cbFinal)65     NAsyncContextLegacy(NapiValue thisPtr, NapiValue cbSucc, NapiValue cbFail, NapiValue cbFinal)
66         : NapiAsyncContext(thisPtr), cbSucc_(cbSucc), cbFail_(cbFail), cbFinal_(cbFinal)
67     {}
68     ~NAsyncContextLegacy() override = default;
69 };
70 }  // namespace LIBZIP
71 }  // namespace AppExecFwk
72 }  // namespace OHOS
73 #endif  // INTERFACES_KITS_JS_ZIP_NAPI_COMMON_NAPI_ASYNC_CONTEXT_H