• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 #include "n_async_work_promise.h"
17 #include "../../log.h"
18 
19 namespace OHOS {
20 namespace DistributedFS {
21 using namespace std;
22 
NAsyncWorkPromise(napi_env env,NVal thisPtr)23 NAsyncWorkPromise::NAsyncWorkPromise(napi_env env, NVal thisPtr) : NAsyncWorkFactory(env)
24 {
25     ctx_ = new(std::nothrow) NAsyncContextPromise(thisPtr);
26 }
27 
PromiseOnExec(napi_env env,void * data)28 static void PromiseOnExec(napi_env env, void *data)
29 {
30     auto ctx = static_cast<NAsyncContextPromise *>(data);
31     if (ctx != nullptr && ctx->cbExec_ != nullptr) {
32         ctx->err_ = ctx->cbExec_(env);
33     }
34 }
35 
PromiseOnComplete(napi_env env,napi_status status,void * data)36 static void PromiseOnComplete(napi_env env, napi_status status, void *data)
37 {
38     auto ctx = static_cast<NAsyncContextPromise *>(data);
39     if (ctx == nullptr) {
40         return;
41     }
42 
43     if (ctx->cbComplete_ != nullptr) {
44         ctx->res_ = ctx->cbComplete_(env, ctx->err_);
45     }
46 
47     if (!ctx->res_.TypeIsError(true)) {
48         status = napi_resolve_deferred(env, ctx->deferred_, ctx->res_.val_);
49         if (status != napi_ok) {
50             HILOGE("Internal BUG, cannot resolve promise for %{public}d", status);
51         }
52     } else {
53         status = napi_reject_deferred(env, ctx->deferred_, ctx->res_.val_);
54         if (status != napi_ok) {
55             HILOGE("Internal BUG, cannot reject promise for %{public}d", status);
56         }
57     }
58 
59     ctx->deferred_ = nullptr;
60     napi_delete_async_work(env, ctx->awork_);
61     delete ctx;
62 }
63 
Schedule(string procedureName,NContextCBExec cbExec,NContextCBComplete cbComplete)64 NVal NAsyncWorkPromise::Schedule(string procedureName, NContextCBExec cbExec, NContextCBComplete cbComplete)
65 {
66     if (ctx_ == nullptr) {
67         HILOGE("ctx is nullptr");
68         return NVal();
69     }
70     ctx_->cbExec_ = move(cbExec);
71     ctx_->cbComplete_ = move(cbComplete);
72 
73     napi_status status;
74     napi_value result = nullptr;
75     status = napi_create_promise(env_, &ctx_->deferred_, &result);
76     if (status != napi_ok) {
77         HILOGE("INNER BUG. Cannot create promise for %{public}d", status);
78         return NVal();
79     }
80 
81     napi_value resource = NVal::CreateUTF8String(env_, procedureName).val_;
82     status = napi_create_async_work(env_, nullptr, resource, PromiseOnExec, PromiseOnComplete, ctx_, &ctx_->awork_);
83     if (status != napi_ok) {
84         HILOGE("INNER BUG. Failed to create async work for %{public}d", status);
85         return NVal();
86     }
87 
88     status = napi_queue_async_work(env_, ctx_->awork_);
89     if (status != napi_ok) {
90         HILOGE("INNER BUG. Failed to queue async work for %{public}d", status);
91         return NVal();
92     }
93 
94     ctx_ = nullptr; // The ownership of ctx_ has been transferred
95     return { env_, result };
96 }
97 } // namespace DistributedFS
98 } // namespace OHOS
99