Files
react-native/ReactCommon/utils/ThreadStorage.h
T
Samuel Susla 36b586ada1 Font size in Text now respects preferredContentSizeCategory
Summary:
Changelog: [Internal]

Add support for dynamic font size.

New class `ThreadStorage` is introduced, which is used to pass LayoutContext to `YogaLayoutableShadowNode::yogaNodeMeasureCallbackConnector`.

## Shortcoming
This implementation doesn't cause re-render, if user changes font size and comes to the app without restarting it, it will show old font size. I believe this is fine for now as most people set their font size before they use the app and keep the same setting for a long time.

Reviewed By: shergin

Differential Revision: D22043728

fbshipit-source-id: 7453d165c280a2f4bcb73f4ee6daf9e64b637ded
2020-06-17 10:22:32 -07:00

58 lines
1.3 KiB
C++

/*
* 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.
*/
#pragma once
#include <better/map.h>
#include <better/optional.h>
#include <mutex>
#include <thread>
namespace facebook {
namespace react {
/*
* ThreadStorage is a class designed to store data for specific thread.
* When data is inserted from thread 1, it can only be retrieved from thread 1.
*/
template <typename DataT>
class ThreadStorage {
/*
* Private default constructor. This class has to be used as a singleton.
*/
ThreadStorage() = default;
public:
static ThreadStorage<DataT> &getInstance() {
static ThreadStorage<DataT> threadStorage;
return threadStorage;
}
better::optional<DataT> get() const {
std::lock_guard<std::mutex> lock(mutex_);
auto iterator = storage_.find(std::this_thread::get_id());
if (iterator != storage_.end()) {
return iterator->second;
} else {
return {};
}
}
void set(DataT data) {
std::lock_guard<std::mutex> lock(mutex_);
storage_[std::this_thread::get_id()] = data;
}
private:
mutable std::mutex mutex_;
better::map<std::thread::id, DataT> storage_;
};
} // namespace react
} // namespace facebook