mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-07 09:27:29 +08:00
Summary: Makes the delta bundle data structures more consistent. The changes are as follows: * There are now two types of JSON bundles that can be downloaded from the delta endpoint. Base bundles (`Bundle` type), and Delta bundles (`DeltaBundle` type). * The `reset` boolean is renamed to `base`. * `pre` and `post` properties are now strings. * Only `Bundle` can define `pre` and `post` properties. * The `delta` property is renamed to `modules`. * Deleted modules are now listed inside of the `deleted` property, which is only defined by `DeltaBundle`. Reviewed By: mjesun Differential Revision: D10446831 fbshipit-source-id: 40e229a2811d48950f0bad8dd341ece189089e9b
73 lines
1.7 KiB
C++
73 lines
1.7 KiB
C++
#include "JSDeltaBundleClient.h"
|
|
|
|
#include <sstream>
|
|
|
|
#include <folly/Memory.h>
|
|
|
|
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::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);
|
|
} else {
|
|
const folly::dynamic *deleted = delta.get_ptr("deleted");
|
|
if (deleted != nullptr) {
|
|
for (const folly::dynamic id : *deleted) {
|
|
modules_.erase(id.getInt());
|
|
}
|
|
}
|
|
}
|
|
|
|
const folly::dynamic *modules = delta.get_ptr("modules");
|
|
if (modules != nullptr) {
|
|
for (const folly::dynamic pair : *modules) {
|
|
auto id = pair[0].getInt();
|
|
auto module = pair[1];
|
|
modules_.emplace(id, module.getString());
|
|
}
|
|
}
|
|
}
|
|
|
|
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 folly::make_unique<JSBigStdString>(startupCode_);
|
|
}
|
|
|
|
void JSDeltaBundleClient::clear() {
|
|
modules_.clear();
|
|
startupCode_.clear();
|
|
}
|
|
|
|
} // namespace react
|
|
} // namespace facebook
|