|
7 | 7 |
|
8 | 8 | #include "JSTask.hpp" |
9 | 9 |
|
| 10 | +#include "TaskScheduler.hpp" |
10 | 11 | #include "constants.hpp" |
| 12 | +#include "helpers/FetchService.hpp" |
11 | 13 | #include "helpers/fetch.h" |
12 | 14 | #include "helpers/helpers.h" |
13 | | -#include "TaskScheduler.hpp" |
14 | | - |
15 | 15 |
|
16 | 16 | using namespace facebook; |
17 | 17 |
|
18 | 18 | constexpr std::hash<std::string> hasher; |
19 | 19 |
|
20 | 20 | jsi::Function createJSTaskCreator( |
21 | | - jsi::Runtime &rt, |
22 | | - std::shared_ptr<react::CallInvoker> callInvoker) { |
23 | | - jsi::Function createTask = jsi::Function::createFromHostFunction( |
24 | | - rt, jsi::PropNameID::forAscii(rt, CREATE_TASK_KEY), 1, |
25 | | - [callInvoker = std::move(callInvoker)]( |
26 | | - jsi::Runtime &rt, const jsi::Value &thisVal, const jsi::Value *args, |
27 | | - size_t count) { |
28 | | - jsi::Object taskJS(rt); |
29 | | - |
30 | | - if (!args[0].isObject()) { |
31 | | - throw jsi::JSError( |
32 | | - rt, |
33 | | - "[SyncTasksManager]: createTask -> argument must be an object"); |
34 | | - } |
35 | | - |
36 | | - jsi::Object props = args[0].asObject(rt); |
37 | | - |
38 | | - jsi::Object config = props.getPropertyAsObject(rt, CONFIG_KEY); |
39 | | - |
40 | | - int interval = config.getProperty(rt, INTERVAL_KEY).asNumber(); |
41 | | - std::string url = config.getProperty(rt, URL_KEY).asString(rt).utf8(rt); |
42 | | - |
43 | | - if (!checkJSType<jsi::Function>(rt, |
44 | | - props.getProperty(rt, ON_DATA_KEY))) { |
45 | | - throw jsi::JSError(rt, |
46 | | - "[SyncTasksManager]: onData must be a function"); |
47 | | - } |
| 21 | + jsi::Runtime& rt, |
| 22 | + std::shared_ptr<react::CallInvoker> callInvoker) { |
| 23 | + jsi::Function createTask = jsi::Function::createFromHostFunction( |
| 24 | + rt, jsi::PropNameID::forAscii(rt, CREATE_TASK_KEY), 1, |
| 25 | + [callInvoker = std::move(callInvoker)]( |
| 26 | + jsi::Runtime& rt, const jsi::Value& thisVal, const jsi::Value* args, |
| 27 | + size_t count) { |
| 28 | + jsi::Object taskJS(rt); |
48 | 29 |
|
49 | | - auto onData = std::make_shared<jsi::Function>( |
50 | | - props.getPropertyAsFunction(rt, ON_DATA_KEY)); |
| 30 | + if (!args[0].isObject()) { |
| 31 | + throw jsi::JSError( |
| 32 | + rt, |
| 33 | + "[SyncTasksManager]: createTask -> argument must be an object"); |
| 34 | + } |
51 | 35 |
|
52 | | - jsi::Value onErrorValue = props.getProperty(rt, ON_ERROR_KEY); |
| 36 | + jsi::Object props = args[0].asObject(rt); |
53 | 37 |
|
54 | | - std::shared_ptr<jsi::Function> onError; |
| 38 | + jsi::Object config = props.getPropertyAsObject(rt, CONFIG_KEY); |
55 | 39 |
|
56 | | - if (checkJSType<jsi::Function>(rt, onErrorValue)) { |
57 | | - onError = std::make_shared<jsi::Function>( |
58 | | - props.getPropertyAsFunction(rt, ON_ERROR_KEY)); |
59 | | - } |
| 40 | + int interval = config.getProperty(rt, INTERVAL_KEY).asNumber(); |
| 41 | + std::string url = config.getProperty(rt, URL_KEY).asString(rt).utf8(rt); |
60 | 42 |
|
61 | | - std::unordered_map<std::string, std::string> c_headers; |
| 43 | + if (!checkJSType<jsi::Function>(rt, |
| 44 | + props.getProperty(rt, ON_DATA_KEY))) { |
| 45 | + throw jsi::JSError(rt, |
| 46 | + "[SyncTasksManager]: onData must be a function"); |
| 47 | + } |
62 | 48 |
|
63 | | - jsi::Value headers = config.getProperty(rt, HEADERS_KEY); |
| 49 | + auto onData = std::make_shared<jsi::Function>( |
| 50 | + props.getPropertyAsFunction(rt, ON_DATA_KEY)); |
64 | 51 |
|
65 | | - if (headers.isObject()) { |
66 | | - jsi::Object h = headers.asObject(rt); |
| 52 | + jsi::Value onErrorValue = props.getProperty(rt, ON_ERROR_KEY); |
67 | 53 |
|
68 | | - jsi::Array names = h.getPropertyNames(rt); |
| 54 | + std::shared_ptr<jsi::Function> onError; |
69 | 55 |
|
70 | | - for (int i = 0; i < names.size(rt); ++i) { |
71 | | - jsi::Value keyValue = names.getValueAtIndex(rt, i); |
| 56 | + auto fetcher = rt.global().getNativeState<FetchService>(rt); |
72 | 57 |
|
73 | | - if (!keyValue.isString()) [[unlikely]] { |
74 | | - throw jsi::JSError( |
75 | | - rt, |
76 | | - "[SyncTasksManager] Invalid Header, key must be a string"); |
77 | | - } |
| 58 | + if (checkJSType<jsi::Function>(rt, onErrorValue)) { |
| 59 | + onError = std::make_shared<jsi::Function>( |
| 60 | + props.getPropertyAsFunction(rt, ON_ERROR_KEY)); |
| 61 | + } |
78 | 62 |
|
79 | | - jsi::String key = keyValue.asString(rt); |
| 63 | + FetchHeaders headers = getFetchHeadersFromJSObject(rt, config); |
80 | 64 |
|
81 | | - jsi::Value valValue = h.getProperty(rt, key); |
| 65 | + auto task = std::make_shared<Task>( |
| 66 | + url, interval, |
| 67 | + [url, fetcher, callInvoker, onData, onError, &rt, |
| 68 | + h = std::move(headers)](Task& self) mutable { |
| 69 | + auto response = fetcher->fetch(url, std::move(h)); |
82 | 70 |
|
83 | | - if (!valValue.isString()) [[unlikely]] { |
84 | | - throw jsi::JSError( |
85 | | - rt, |
86 | | - "[SyncTasksManager] Invalid Header, value must be a string"); |
87 | | - } |
| 71 | + if (std::holds_alternative<FetchBody>(response)) { |
| 72 | + auto& data = std::get<FetchBody>(response); |
88 | 73 |
|
89 | | - jsi::String value = valValue.asString(rt); |
| 74 | + size_t bodyHash = hasher(data.body); |
90 | 75 |
|
91 | | - c_headers[key.utf8(rt)] = value.utf8(rt); |
92 | | - } |
| 76 | + if (self.hasSameBodyHash(bodyHash)) { |
| 77 | + return; |
93 | 78 | } |
94 | 79 |
|
95 | | - auto task = std::make_shared<Task>( |
96 | | - url, interval, |
97 | | - [url, callInvoker, onData, onError, &rt, |
98 | | - h = std::move(c_headers)](Task &self) mutable { |
99 | | - auto response = fetch(url.c_str(), std::move(h)); |
100 | | - |
101 | | - if (std::holds_alternative<FetchResult>(response)) { |
102 | | - auto &data = std::get<FetchResult>(response); |
103 | | - |
104 | | - size_t bodyHash = hasher(data.body); |
105 | | - |
106 | | - if (self.hasSameBodyHash(bodyHash)) { |
107 | | - return; |
108 | | - } |
109 | | - |
110 | | - self.setLastBodyHash(bodyHash); |
| 80 | + self.setLastBodyHash(bodyHash); |
111 | 81 |
|
112 | | - callInvoker->invokeAsync( |
113 | | - [&rt, onData, body = std::move(data.body)] { |
114 | | - onData->call(rt, jsi::String::createFromUtf8(rt, body)); |
115 | | - }); |
| 82 | + callInvoker->invokeAsync( |
| 83 | + [&rt, onData, body = std::move(data.body)] { |
| 84 | + onData->call(rt, jsi::String::createFromUtf8(rt, body)); |
| 85 | + }); |
116 | 86 |
|
117 | | - } else { |
118 | | - auto &err = std::get<FetchError>(response); |
| 87 | + } else { |
| 88 | + auto& err = std::get<FetchError>(response); |
119 | 89 |
|
120 | | - callInvoker->invokeAsync([onError, &rt, err = std::move(err)] { |
121 | | - if (onError) { |
122 | | - jsi::Object error(rt); |
| 90 | + callInvoker->invokeAsync([onError, &rt, err = std::move(err)] { |
| 91 | + if (onError) { |
| 92 | + jsi::Object error(rt); |
123 | 93 |
|
124 | | - error.setProperty( |
125 | | - rt, "error", |
126 | | - jsi::String::createFromUtf8(rt, err.message)); |
127 | | - error.setProperty( |
128 | | - rt, "body", jsi::String::createFromUtf8(rt, err.body)); |
129 | | - error.setProperty(rt, "status_code", |
130 | | - jsi::Value((int) err.httpCode)); |
| 94 | + error.setProperty( |
| 95 | + rt, "error", |
| 96 | + jsi::String::createFromUtf8(rt, err.message)); |
| 97 | + error.setProperty( |
| 98 | + rt, "body", jsi::String::createFromUtf8(rt, err.body)); |
| 99 | + error.setProperty(rt, "status_code", |
| 100 | + jsi::Value((int)err.httpCode)); |
131 | 101 |
|
132 | | - onError->call(rt, error); |
133 | | - } |
134 | | - }); |
135 | | - } |
136 | | - }); |
| 102 | + onError->call(rt, error); |
| 103 | + } |
| 104 | + }); |
| 105 | + } |
| 106 | + }); |
137 | 107 |
|
138 | | - jsi::Function stopTask = jsi::Function::createFromHostFunction( |
139 | | - rt, jsi::PropNameID::forAscii(rt, "stopTask"), 1, |
140 | | - [task](jsi::Runtime &rt, const jsi::Value &thisVal, |
141 | | - const jsi::Value *args, size_t count) { |
142 | | - task->stop(); |
| 108 | + jsi::Function stopTask = jsi::Function::createFromHostFunction( |
| 109 | + rt, jsi::PropNameID::forAscii(rt, "stopTask"), 1, |
| 110 | + [task](jsi::Runtime& rt, const jsi::Value& thisVal, |
| 111 | + const jsi::Value* args, size_t count) { |
| 112 | + task->stop(); |
143 | 113 |
|
144 | | - return jsi::Value(true); |
145 | | - }); |
| 114 | + return jsi::Value(true); |
| 115 | + }); |
146 | 116 |
|
147 | | - jsi::Function startTask = jsi::Function::createFromHostFunction( |
148 | | - rt, jsi::PropNameID::forAscii(rt, "startTask"), 1, |
149 | | - [task](jsi::Runtime &rt, const jsi::Value &thisVal, |
150 | | - const jsi::Value *args, size_t count) { |
151 | | - task->start(); |
| 117 | + jsi::Function startTask = jsi::Function::createFromHostFunction( |
| 118 | + rt, jsi::PropNameID::forAscii(rt, "startTask"), 1, |
| 119 | + [task](jsi::Runtime& rt, const jsi::Value& thisVal, |
| 120 | + const jsi::Value* args, size_t count) { |
| 121 | + task->start(); |
152 | 122 |
|
153 | | - return jsi::Value(true); |
154 | | - }); |
| 123 | + return jsi::Value(true); |
| 124 | + }); |
155 | 125 |
|
156 | | - jsi::Function isRunning = jsi::Function::createFromHostFunction( |
157 | | - rt, jsi::PropNameID::forAscii(rt, "isRunning"), 1, |
158 | | - [task](jsi::Runtime &rt, const jsi::Value &thisVal, |
159 | | - const jsi::Value *args, |
160 | | - size_t count) { |
161 | | - return jsi::Value(!task->isStopped()); |
162 | | - }); |
| 126 | + jsi::Function isRunning = jsi::Function::createFromHostFunction( |
| 127 | + rt, jsi::PropNameID::forAscii(rt, "isRunning"), 1, |
| 128 | + [task](jsi::Runtime& rt, const jsi::Value& thisVal, |
| 129 | + const jsi::Value* args, |
| 130 | + size_t count) { return jsi::Value(!task->isStopped()); }); |
163 | 131 |
|
164 | | - taskJS.setNativeState(rt, task); |
| 132 | + taskJS.setNativeState(rt, task); |
165 | 133 |
|
166 | | - taskJS.setProperty(rt, "stop", std::move(stopTask)); |
167 | | - taskJS.setProperty(rt, "start", std::move(startTask)); |
168 | | - taskJS.setProperty(rt, "isRunning", std::move(isRunning)); |
| 134 | + taskJS.setProperty(rt, "stop", std::move(stopTask)); |
| 135 | + taskJS.setProperty(rt, "start", std::move(startTask)); |
| 136 | + taskJS.setProperty(rt, "isRunning", std::move(isRunning)); |
169 | 137 |
|
170 | | - taskJS.setExternalMemoryPressure(rt, sizeof(Task)); |
| 138 | + taskJS.setExternalMemoryPressure(rt, sizeof(Task)); |
171 | 139 |
|
172 | | - return taskJS; |
173 | | - }); |
| 140 | + return taskJS; |
| 141 | + }); |
174 | 142 |
|
175 | | - return createTask; |
| 143 | + return createTask; |
176 | 144 | } |
0 commit comments