forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSDeltaBundleClient.cpp
More file actions
100 lines (80 loc) · 2.48 KB
/
JSDeltaBundleClient.cpp
File metadata and controls
100 lines (80 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "JSDeltaBundleClient.h"
#include <memory>
#include <sstream>
namespace facebook {
namespace react {
namespace {
std::string startupCode(const folly::dynamic *pre, const folly::dynamic *post) {
std::ostringstream startupCode;
for (auto section : {pre, post}) {
if (section != nullptr) {
startupCode << section->getString() << '\n';
}
}
return startupCode.str();
}
} // namespace
void JSDeltaBundleClient::patchModules(const folly::dynamic *modules) {
for (const folly::dynamic &pair : *modules) {
auto id = pair[0].getInt();
auto module = pair[1];
modules_[id] = std::move(module.getString());
}
}
void JSDeltaBundleClient::patch(const folly::dynamic &delta) {
auto const base = delta.get_ptr("base");
if (base != nullptr && base->asBool()) {
clear();
auto const pre = delta.get_ptr("pre");
auto const post = delta.get_ptr("post");
startupCode_ = startupCode(pre, post);
const folly::dynamic *modules = delta.get_ptr("modules");
if (modules != nullptr) {
patchModules(modules);
}
} else {
const folly::dynamic *deleted = delta.get_ptr("deleted");
if (deleted != nullptr) {
for (const folly::dynamic &id : *deleted) {
modules_.erase(id.getInt());
}
}
// TODO T37123645 This is deprecated but necessary in order to support older
// versions of the Metro server.
const folly::dynamic *modules = delta.get_ptr("modules");
if (modules != nullptr) {
patchModules(modules);
}
const folly::dynamic *added = delta.get_ptr("added");
if (added != nullptr) {
patchModules(added);
}
const folly::dynamic *modified = delta.get_ptr("modified");
if (modified != nullptr) {
patchModules(modified);
}
}
}
JSModulesUnbundle::Module JSDeltaBundleClient::getModule(
uint32_t moduleId) const {
auto search = modules_.find(moduleId);
if (search != modules_.end()) {
return {folly::to<std::string>(search->first, ".js"), search->second};
}
throw JSModulesUnbundle::ModuleNotFound(moduleId);
}
std::unique_ptr<const JSBigString> JSDeltaBundleClient::getStartupCode() const {
return std::make_unique<JSBigStdString>(startupCode_);
}
void JSDeltaBundleClient::clear() {
modules_.clear();
startupCode_.clear();
}
} // namespace react
} // namespace facebook