Skip to content

Commit

Permalink
Reconsider APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
RanolP committed Feb 22, 2020
1 parent effa3b7 commit b6f0446
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 22 deletions.
22 changes: 10 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,23 @@ Read like *spike*.
auto all = prop_set(is_dark);
}
namespace state::app {
auto [count, set_count] = use_state(0);
State<int> count;

auto all = state_set(count);
}
namespace msg {
auto increment = app_msg<int>();
auto decrement = app_msg<int>();
Message<int> increment;
Message<int> decrement;
}
ShouldRender update(Msg&& msg) {
if (increment == msg) {
set_count([](previous) {
return previous + increment[0];
})
ShouldRender update() {
using namespace state::app;
using namespace msg;
if (increment) {
count += increment.get<0>();
return true;
}
if (decrement == msg) {
set_count([](previous) {
return previous - decrement[0];
})
if (decrement) {
count -= decrement.get<0>();
return true;
}
return false;
Expand Down
28 changes: 21 additions & 7 deletions examples/Counter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,31 @@ namespace props::counter
{
auto is_dark_theme = fallback<bool>(false);
}
class Counter
namespace state::counter
{
private:
bool test;
State<int> count;
}
namespace msg
{
Message<int> increment;
Message<int> decrement;
} // namespace msg

public:
template <typename... T>
Counter(T... values)
ShouldRender update()
{
using namespace state::counter;
using namespace msg;
if (increment)
{
count += increment.get<0>();
return true;
}
};
if (decrement)
{
count -= decrement.get<0>();
}
return true;
}

int main()
{
Expand Down
4 changes: 2 additions & 2 deletions include/spaic/Component.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <functional>

namespace spaic
namespace spaic::comp
{
using ShouldRender = bool;
template <typename State, typename Msg>
Expand All @@ -13,5 +13,5 @@ class Component
{
};
template <typename Props, typename State>
Component<Props, State> create_component();
Component<Props, State> create_component(Props props, State state);
} // namespace spaic
17 changes: 17 additions & 0 deletions include/spaic/Message.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

namespace spaic::msg
{
template <typename... T>
class Message
{
public:
Message() {}

operator bool();

template <size_t I>
std::tuple_element<I, T...> get();
};

} // namespace spaic::msg
8 changes: 7 additions & 1 deletion include/spaic/Prelude.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
#pragma once

#include <spaic/Property.hpp>
#include <spaic/Message.hpp>
#include <spaic/Component.hpp>
#include <spaic/State.hpp>

using namespace spaic::props;
using namespace spaic::props;
using namespace spaic::msg;
using namespace spaic::state;
using namespace spaic::comp;
11 changes: 11 additions & 0 deletions include/spaic/State.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

namespace spaic::state
{
template <typename T>
class State
{
public:
State() {}
};
} // namespace spaic::state

0 comments on commit b6f0446

Please sign in to comment.