Skip to content

Commit

Permalink
Rename 'lambda_action' to 'callback'.
Browse files Browse the repository at this point in the history
  • Loading branch information
tmadden committed Oct 6, 2020
1 parent 2584f29 commit 3ee4a4e
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 49 deletions.
18 changes: 10 additions & 8 deletions docs/actions.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Actions

<script>
init_alia_demos(['unready-copier', 'action-operators', 'action-combining',
'action-latching', 'action-binding', 'lambda-action-demo']);
'action-latching', 'action-binding', 'callback-demo']);
</script>

Actions are the preferred way for application code to respond to events in alia.
Expand Down Expand Up @@ -143,18 +143,20 @@ function objects). There are two options for defining lambda actions:

<dl>

<dt>lambda_action(perform)</dt><dd>
<dt>callback(perform)</dt><dd>

Creates an action that calls `perform`.
Creates an action that's equivalent to a simple C++ callback.

`perform` is any callable C++ object.

The action is always considered ready and simply calls `perform` when invoked.

`perform` can take any number/type of arguments and defines the signature of the
action.

The action is always ready to be performed.

</dd>

<dt>lambda_action(is_ready, perform)</dt><dd>
<dt>callback(is_ready, perform)</dt><dd>

Creates an action whose behavior is defined by `is_ready` and `perform`.

Expand All @@ -171,10 +173,10 @@ action.
Actions that are defined this way can have parameters, and of course those
parameters can be bound to signals using the `<<` operator:

[source](actions.cpp ':include :fragment=lambda-action-demo')
[source](actions.cpp ':include :fragment=callback-demo')

<div class="demo-panel">
<div id="lambda-action-demo"></div>
<div id="callback-demo"></div>
</div>

If you want to do something more interesting, you may want to implement the
Expand Down
7 changes: 5 additions & 2 deletions docs/time-as-a-signal.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ smoothed view will change smoothly according to `transition`.

</dl>

See `alia/timing/smoothing.hpp` for details on specifying custom transitions, abruptly changing a smoothed signal, and the interfaces required of a value that needs to be smoothed.
See `alia/timing/smoothing.hpp` for details on specifying custom transitions,
abruptly changing a smoothed signal, and the interfaces required of a value
that needs to be smoothed.

Here's an example of basic number smoothing:

Expand Down Expand Up @@ -113,7 +115,8 @@ then the deflickered view will also lose its value.)

</dl>

To see `deflicker()` in action, let's first create a signal that flickers. We can do this artificially using `alia::async` and a simple delayed callback...
To see `deflicker()` in action, let's first create a signal that flickers. We
can do this artificially using `alia::async` and a simple delayed callback...

[source](timing.cpp ':include :fragment=flickering')

Expand Down
8 changes: 4 additions & 4 deletions examples/asm-dom/demos/actions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,18 +175,18 @@ void
demo_ui(dom::context ctx, duplex<std::string> message)
{
// clang-format off
/// [lambda-action-demo]
/// [callback-demo]
// Define a UI for inputting a message.
dom::text(ctx, "Enter a message for your browser's console:");
dom::input(ctx, message);

// Create an action that takes a message as a parameter.
auto sender = lambda_action(
auto sender = callback(
[](std::string message) { std::cout << message << std::endl; });

// Bind the message to the action and hook them up to a button.
dom::button(ctx, "Send", sender << message);
/// [lambda-action-demo]
/// [callback-demo]
// clang-format on
}

Expand All @@ -201,6 +201,6 @@ init_demo(std::string dom_id)
});
}

static demo the_demo("lambda-action-demo", init_demo);
static demo the_demo("callback-demo", init_demo);

} // namespace action_demo
4 changes: 2 additions & 2 deletions examples/asm-dom/demos/tracking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ records_ui(dom::context ctx, std::vector<my_record>& records)
dom::input(ctx, new_label);

// Create an action that adds the new record.
auto add_record = lambda_action(
auto add_record = callback(
[&](std::string label) { records.push_back({label}); });

// Present a button that adds the new record.
Expand Down Expand Up @@ -267,7 +267,7 @@ records_ui(dom::context ctx, std::vector<my_record>& records)
}

dom::button(ctx, "Shuffle!",
lambda_action(
callback(
[&]() { std::shuffle(records.begin(), records.end(), rng); }));

}
Expand Down
28 changes: 14 additions & 14 deletions src/alia/flow/actions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -374,8 +374,8 @@ push_back(Container container)
typename Container::value_type::value_type>(std::move(container));
}

// lambda_action(is_ready, perform) creates an action whose behavior is
// defined by two function objects.
// callback(is_ready, perform) creates an action whose behavior is defined by
// two function objects.
//
// :is_ready takes no arguments and simply returns true or false to indicate if
// the action is ready to be performed.
Expand All @@ -395,19 +395,19 @@ struct call_operator_action_signature<R (T::*)(Args...) const>
};

template<class Lambda>
struct lambda_action_signature
struct callback_action_signature
: call_operator_action_signature<decltype(&Lambda::operator())>
{
};

template<class IsReady, class Perform, class Interface>
struct lambda_action_object;
struct callback_action;

template<class IsReady, class Perform, class... Args>
struct lambda_action_object<IsReady, Perform, action_interface<Args...>>
struct callback_action<IsReady, Perform, action_interface<Args...>>
: action_interface<Args...>
{
lambda_action_object(IsReady is_ready, Perform perform)
callback_action(IsReady is_ready, Perform perform)
: is_ready_(is_ready), perform_(perform)
{
}
Expand All @@ -432,25 +432,25 @@ struct lambda_action_object<IsReady, Perform, action_interface<Args...>>

template<class IsReady, class Perform>
auto
lambda_action(IsReady is_ready, Perform perform)
callback(IsReady is_ready, Perform perform)
{
return lambda_action_object<
return callback_action<
IsReady,
Perform,
typename lambda_action_signature<Perform>::type>(is_ready, perform);
typename callback_action_signature<Perform>::type>(is_ready, perform);
}

// The single-argument version of lambda_action creates an action that's always
// The single-argument version of callback() creates an action that's always
// ready to perform.
template<class Perform>
auto
lambda_action(Perform perform)
callback(Perform perform)
{
return lambda_action([]() { return true; }, perform);
return callback([]() { return true; }, perform);
}

// actionize(x) returns the action form of x (if it isn't already one).
// Specifically, if x is a callable object, this returns lambda_action(x).
// Specifically, if x is a callable object, this returns callback(x).
// If x is an action, this returns x itself.
template<class Action>
std::enable_if_t<is_action_type<Action>::value, Action>
Expand All @@ -464,7 +464,7 @@ template<
auto
actionize(Callable x)
{
return lambda_action(std::move(x));
return callback(std::move(x));
}

// add_write_action(signal, on_write) wraps signal in a similar signal that
Expand Down
18 changes: 9 additions & 9 deletions src/alia/flow/conditionals.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@

namespace alia {

template<class Context, class ComponentCallback>
std::enable_if_t<is_invocable<ComponentCallback>::value>
invoke_component_callback(Context, ComponentCallback&& callback)
template<class Context, class Function>
std::enable_if_t<is_invocable<Function>::value>
invoke_component_function(Context, Function&& function)
{
callback();
std::forward<Function>(function)();
}

template<class Context, class ComponentCallback>
std::enable_if_t<is_invocable<ComponentCallback, Context>::value>
invoke_component_callback(Context ctx, ComponentCallback&& callback)
template<class Context, class Function>
std::enable_if_t<is_invocable<Function, Context>::value>
invoke_component_function(Context ctx, Function&& function)
{
callback(ctx);
std::forward<Function>(function)(ctx);
}

namespace detail {
Expand Down Expand Up @@ -45,7 +45,7 @@ if_(Context ctx, bool inherited_condition, Condition condition, Body&& body)

ALIA_IF(inherited_condition && condition)
{
invoke_component_callback(ctx, body);
invoke_component_function(ctx, body);
}
// Hacking the macros...
// clang-format off
Expand Down
2 changes: 1 addition & 1 deletion src/alia/timing/ticks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ struct animation_timer
auto
start()
{
return lambda_action(
return callback(
[&](millisecond_count duration) { raw_.start(duration); });
}

Expand Down
16 changes: 8 additions & 8 deletions unit_tests/flow/actions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ TEST_CASE("latch-like action", "[flow][actions]")
TEST_CASE("parameterized combined action", "[flow][actions]")
{
int x = 0, y = 0;
auto a = lambda_action([&](int n) { x += n; });
auto b = lambda_action([&](int n) { y -= n; });
auto a = callback([&](int n) { x += n; });
auto b = callback([&](int n) { y -= n; });
perform_action((a, b), 4);
REQUIRE(x == 4);
REQUIRE(y == -4);
Expand Down Expand Up @@ -235,12 +235,12 @@ TEST_CASE("push_back movable", "[flow][actions]")
TEST_CASE("lambda actions", "[flow][actions]")
{
int x = 0;
auto a = lambda_action([&](int y, int z) { x = y + z; });
auto a = callback([&](int y, int z) { x = y + z; });
perform_action(a, 1, 2);
REQUIRE(x == 3);

bool ready = false;
auto b = lambda_action([&]() { return ready; }, [&](int y) { x += y; });
auto b = callback([&]() { return ready; }, [&](int y) { x += y; });
REQUIRE(!b.is_ready());
ready = true;
REQUIRE(b.is_ready());
Expand All @@ -251,7 +251,7 @@ TEST_CASE("lambda actions", "[flow][actions]")
TEST_CASE("action binding", "[flow][actions]")
{
int x = 0;
auto a = lambda_action([&](int y, int z) { x = y - z; });
auto a = callback([&](int y, int z) { x = y - z; });

REQUIRE(!(a << empty<int>()).is_ready());

Expand All @@ -272,7 +272,7 @@ TEST_CASE("action binding", "[flow][actions]")
perform_action(e);
REQUIRE(x == 1);

REQUIRE(!(lambda_action([]() { return false; }, [&](int) {}) << value(0))
REQUIRE(!(callback([]() { return false; }, [&](int) {}) << value(0))
.is_ready());
}

Expand All @@ -298,8 +298,8 @@ TEST_CASE("add_write_action", "[flow][actions]")
{
int x = 0;
bool written = false;
auto s = add_write_action(
direct(x), lambda_action([&](int) { written = true; }));
auto s
= add_write_action(direct(x), callback([&](int) { written = true; }));

typedef decltype(s) signal_t;
REQUIRE(signal_is_readable<signal_t>::value);
Expand Down
2 changes: 1 addition & 1 deletion unit_tests/flow/events.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ TEST_CASE("error isolation", "[flow][events]")

alia::system sys;
initialize_system(sys, [&](context ctx) {
on_value_change(ctx, value(n), lambda_action([&] {
on_value_change(ctx, value(n), callback([&] {
static_cast<void>(std::string("abc").at(n));
}));
});
Expand Down

0 comments on commit 3ee4a4e

Please sign in to comment.