µReact
µReact is an open-source minimalistic single-header reactive programming library for C++17.
However, it works perfectly fine and can be already used with small future changes in mind.
Feedback is strongly required and appreciated to stabilize the API and achieve the first major release.
Features
- Update minimality: nothing is re-calculated or processed unnecessarily
- Glitch freedom: no transiently inconsistent data sets
- Externally synchronized (not thread-safe) by design: it allows not to pay for what you don't use and to have very determined consistent behaviour
- Ease of use: small self-contained single header code base, no external dependencies, permissive Boost Software License license
- Really easy to get started: it's just 1 header file - see the tutorial
- Reliability: the library has an extensive set of tests
- Portability: continuously tested under Windows, MacOS and Ubuntu using MSVC/GCC/Clang
- Clean warning-free codebase even on the most aggressive warning levels for MSVC/GCC/Clang
- Doesn't pollute the global namespace (everything is in namespace
ureact
) and doesn't drag any headers with it
Examples
Basic usage (run)
ureact::context ctx;
// Declaring reactive variables. We can reassign their values later
ureact::value<int> b = ctx.make_value(1);
ureact::value<int> c = ctx.make_value(2);
// Declaring reactive function using overloaded operator
// Its value will be updated each time its dependencies are changed
ureact::function<int> a = b + c;
std::cout << "a (init): " << a.get() << "\n"; // 3
// Assign a new value to 'b'. Value of 'a' is recalculated automatically
b <<= 10;
std::cout << "a (new): " << a.get() << "\n"; // 12
Complex signals (run)
ureact::context ctx;
ureact::value<int> base = ctx.make_value(1);
ureact::value<int> exp = ctx.make_value(3);
// Declaring reactive function with formula
// Its value will be recalculated according to the given function
ureact::function<double> result = make_function( with(base, exp) , std::pow<int, int> );
// Alternative form of make_function using operator |
ureact::function<std::string> expression = with(base, exp, result) |
[]( int base, int exp, int result ){
return std::to_string(base) + "^" + std::to_string(exp)
+ " == " + std::to_string(result);
};
std::cout << expression.get() << "\n"; // 1^3 == 1
base <<= 2;
std::cout << expression.get() << "\n"; // 2^3 == 8
exp <<= 0;
std::cout << expression.get() << "\n"; // 2^0 == 1
Observers (run)
ureact::context ctx;
ureact::value<int> a = ctx.make_value(1);
ureact::function<int> abs_a = with(a) | [](int a){ return std::abs(a); };
ureact::function<int> abs_a_x2 = abs_a * 2;
// Declaring reactive observers
// They execute given functor only when the observed value is changed
observe(a, []( int a ){ std::cout << " a -> " << a << "\n"; });
observe(abs_a, []( int abs_a ){ std::cout << " abs(a) -> " << abs_a << "\n"; });
observe(abs_a_x2, []( int abs_a_x2 ){ std::cout << " abs(a)*2 -> " << abs_a_x2 << "\n"; });
// All values change their values so all of them is notified
std::cout << "a <<= 3\n";
a <<= 3;
// Only change of 'a' is notified
// Value of 'abs_a' is not changed after recalculation, so it is not notified
// Value 'abs_a_x2' is not even recalculated because the value of 'abs_a' is not changed
std::cout << "a <<= -3\n";
a <<= -3;
Output:
a <<= 3
a -> 3
abs(a) -> 3
abs(a)*2 -> 6
a <<= -3
a -> -3
Transaction (run)
ureact::context ctx;
ureact::value<int> b = ctx.make_value(1);
ureact::value<int> c = ctx.make_value(1);
ureact::function<int> a = b + c;
observe(a, []( int a ){ std::cout << " a -> " << a << "\n"; });
// Normally values are recalculated each change of their dependencies
std::cout << "without transaction\n";
std::cout << "b <<= 2\n";
b <<= 2;
std::cout << "c <<= 2\n";
c <<= 2;
// To perform several changes atomically they should be grouped into a transaction
std::cout << "\nwith transaction\n";
ctx.do_transaction([&]()
{
std::cout << "b <<= 3\n";
b <<= 3;
std::cout << "c <<= 3\n";
c <<= 3;
});
Output:
without transaction
b <<= 2
a -> 3
c <<= 2
a -> 4
with transaction
b <<= 3
c <<= 3
a -> 6
See other examples at the examples folder.
License
This software is licensed under the Boost Software License 1.0:
Copyright © 2014-2017 Sebastian Jeckel
Copyright © 2020-2021 Krylov Yaroslav
Permission is hereby granted, free of charge, to any person or organization obtaining a copy of the software and accompanying documentation covered by this license (the "Software") to use, reproduce, display, distribute, execute, and transmit the Software, and to prepare derivative works of the Software, and to permit third-parties to whom the Software is furnished to do so, all subject to the following:
The copyright notices in the Software and this entire statement, including the above license grant, this restriction and the following disclaimer, must be included in all copies of the Software, in whole or in part, and all derivative works of the Software, unless such copies or derivative works are solely in the form of machine-executable object code generated by a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
This software started out as a fork of the cpp.react by Sebastian Jeckel ([email protected]), BSL 1.0 licensed.
Used third-party tools
The library itself consists of a single header file licensed under the Boost Software License license. However, it is built, tested, documented, and whatnot using a lot of third-party tools and services. Thanks a lot!
- CMake for build automation
- ClangFormat for automatic source code indentation
- gersemi for automatic CMake code indentation
- doctest for the unit tests
- GitHub Changelog Generator to generate the ChangeLog
Contact
If you have questions regarding the library, I would like to invite you to open an issue at GitHub. Please describe your request, problem, or question as detailed as possible, and also mention the version of the library you are using as well as the version of your compiler and operating system. Opening an issue at GitHub allows other users and contributors to this library to collaborate.