Skip to content

Commit

Permalink
Add experimental signal validation.
Browse files Browse the repository at this point in the history
  • Loading branch information
tmadden committed Jun 26, 2020
1 parent dea7386 commit 1290aa5
Show file tree
Hide file tree
Showing 14 changed files with 331 additions and 57 deletions.
9 changes: 6 additions & 3 deletions examples/asm-dom/demos/actions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ init_demo(std::string dom_id)
static dom::system the_dom;

initialize(the_dom, the_system, dom_id, [](dom::context ctx) {
do_ui(ctx, get_state(ctx, 0), get_state(ctx, empty<int>()));
do_ui(
ctx,
enforce_validity(ctx, get_state(ctx, 0)),
enforce_validity(ctx, get_state(ctx, empty<int>())));
});
}

Expand Down Expand Up @@ -55,7 +58,7 @@ init_demo(std::string dom_id)
static dom::system the_dom;

initialize(the_dom, the_system, dom_id, [](dom::context ctx) {
do_ui(ctx, get_state(ctx, 1));
do_ui(ctx, enforce_validity(ctx, get_state(ctx, 1)));
});
}

Expand Down Expand Up @@ -158,7 +161,7 @@ init_demo(std::string dom_id)
static dom::system the_dom;

initialize(the_dom, the_system, dom_id, [](dom::context ctx) {
do_ui(ctx, get_state(ctx, empty<int>()));
do_ui(ctx, enforce_validity(ctx, get_state(ctx, empty<int>())));
});
}

Expand Down
2 changes: 1 addition & 1 deletion examples/asm-dom/demos/adaptors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ init_demo(std::string dom_id)
static dom::system the_dom;

initialize(the_dom, the_system, dom_id, [](dom::context ctx) {
do_ui(ctx, get_state(ctx, 1.));
do_ui(ctx, enforce_validity(ctx, get_state(ctx, 1.)));
});
}

Expand Down
2 changes: 1 addition & 1 deletion examples/asm-dom/demos/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ init_demo(std::string dom_id)
static dom::system the_dom;

initialize(the_dom, the_system, dom_id, [](dom::context ctx) {
do_ui(ctx, get_state(ctx, empty<int>()));
do_ui(ctx, enforce_validity(ctx, get_state(ctx, empty<int>())));
});
}

Expand Down
10 changes: 5 additions & 5 deletions examples/asm-dom/demos/numerical.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ init_demo(std::string dom_id)
initialize(the_dom, the_system, dom_id, [](dom::context ctx) {
do_addition_ui(
ctx,
get_state(ctx, empty<double>()),
get_state(ctx, empty<double>()));
get_state(ctx, enforce_validity(ctx, empty<double>())),
get_state(ctx, enforce_validity(ctx, empty<double>())));
});
}

Expand All @@ -38,10 +38,10 @@ namespace numerical_analysis {
void
do_ui(dom::context ctx)
{
auto n = enforce_validity(ctx, get_state(ctx, empty<double>()));

// clang-format off
/// [analysis]
auto n = get_state(ctx, empty<double>());

dom::do_text(ctx, "Enter a number:");

dom::do_input(ctx, n);
Expand Down Expand Up @@ -197,7 +197,7 @@ init_demo(std::string dom_id)
static dom::system the_dom;

initialize(the_dom, the_system, dom_id, [](dom::context ctx) {
do_factor_tree_demo(ctx, get_state(ctx, 600));
do_factor_tree_demo(ctx, enforce_validity(ctx, get_state(ctx, 600)));
});
}

Expand Down
4 changes: 2 additions & 2 deletions examples/asm-dom/demos/timing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ init_demo(std::string dom_id)
static dom::system the_dom;

initialize(the_dom, the_system, dom_id, [](dom::context ctx) {
do_ui(ctx, get_state(ctx, 42));
do_ui(ctx, enforce_validity(ctx, get_state(ctx, 42)));
});
}

Expand Down Expand Up @@ -157,7 +157,7 @@ init_demo(std::string dom_id)
static dom::system the_dom;

initialize(the_dom, the_system, dom_id, [](dom::context ctx) {
do_ui(ctx, get_state(ctx, 1));
do_ui(ctx, enforce_validity(ctx, get_state(ctx, 1)));
});
}

Expand Down
2 changes: 1 addition & 1 deletion examples/asm-dom/demos/tracking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ init_demo(std::string dom_id)
static dom::system the_dom;

initialize(the_dom, the_system, dom_id, [](dom::context ctx) {
do_ui(ctx, get_state(ctx, empty<int>()));
do_ui(ctx, enforce_validity(ctx, get_state(ctx, empty<int>())));
});
}

Expand Down
47 changes: 20 additions & 27 deletions examples/asm-dom/dom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ struct input_data
component_identity identity;
captured_id external_id;
string value;
bool invalid = false;
signal_validation_data validation;
element_data element;
unsigned version = 0;
};
Expand All @@ -74,35 +74,38 @@ struct value_update_event : targeted_event
};

void
do_input_(dom::context ctx, duplex<string> value)
do_input_(dom::context ctx, duplex<string> value_)
{
input_data* data;
get_cached_data(ctx, &data);

auto value = enforce_validity(ctx, value_, data->validation);

on_refresh(ctx, [&](auto ctx) {
refresh_component_identity(ctx, data->identity);

refresh_signal_shadow(
data->external_id,
value,
[&](string new_value) {
data->value = std::move(new_value);
data->invalid = false;
++data->version;
},
[&]() {
data->value.clear();
data->invalid = false;
++data->version;
});
if (!value.is_invalidated())
{
refresh_signal_shadow(
data->external_id,
value,
[&](string new_value) {
data->value = std::move(new_value);
++data->version;
},
[&]() {
data->value.clear();
++data->version;
});
}
});

auto* system = &ctx.get<system_tag>();
auto external_id = externalize(&data->identity);

add_element(ctx, data->element, make_id(data->version), [&]() {
asmdom::Attrs attrs;
if (data->invalid)
if (value.is_invalidated())
attrs["class"] = "invalid-input";
return asmdom::h(
"input",
Expand All @@ -119,17 +122,7 @@ do_input_(dom::context ctx, duplex<string> value)
});
on_targeted_event<value_update_event>(
ctx, &data->identity, [=](auto ctx, auto& e) {
if (signal_ready_to_write(value))
{
try
{
write_signal(value, e.value);
}
catch (validation_error&)
{
data->invalid = true;
}
}
write_signal(value, e.value);
data->value = e.value;
++data->version;
});
Expand Down
70 changes: 70 additions & 0 deletions src/alia/signals/adaptors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ struct readability_faker : signal<
{
return wrapped_.write(value);
}
bool
invalidate(std::exception_ptr error) const
{
return wrapped_.invalidate(error);
}
bool
is_invalidated() const
{
return wrapped_.is_invalidated();
}

private:
Wrapped wrapped_;
Expand Down Expand Up @@ -168,6 +178,16 @@ struct casting_signal : regular_signal<
{
return wrapped_.write(static_cast<typename Wrapped::value_type>(value));
}
bool
invalidate(std::exception_ptr error) const
{
return wrapped_.invalidate(error);
}
bool
is_invalidated() const
{
return wrapped_.is_invalidated();
}

private:
Wrapped wrapped_;
Expand Down Expand Up @@ -311,6 +331,16 @@ struct fallback_signal : signal<
{
primary_.write(value);
}
bool
invalidate(std::exception_ptr error) const
{
return primary_.invalidate(error);
}
bool
is_invalidated() const
{
return primary_.is_invalidated();
}

private:
mutable id_pair<simple_id<bool>, id_ref> id_;
Expand Down Expand Up @@ -368,6 +398,16 @@ struct simplified_id_wrapper : regular_signal<
{
return wrapped_.write(value);
}
bool
invalidate(std::exception_ptr error) const
{
return wrapped_.invalidate(error);
}
bool
is_invalidated() const
{
return wrapped_.is_invalidated();
}

private:
Wrapped wrapped_;
Expand Down Expand Up @@ -423,6 +463,16 @@ struct masking_signal : signal<
{
primary_.write(value);
}
bool
invalidate(std::exception_ptr error) const
{
return primary_.invalidate(error);
}
bool
is_invalidated() const
{
return primary_.is_invalidated();
}

private:
Primary primary_;
Expand Down Expand Up @@ -490,6 +540,16 @@ struct write_masking_signal : signal<
{
primary_.write(value);
}
bool
invalidate(std::exception_ptr error) const
{
return primary_.invalidate(error);
}
bool
is_invalidated() const
{
return primary_.is_invalidated();
}

private:
Primary primary_;
Expand Down Expand Up @@ -560,6 +620,16 @@ struct unwrapper_signal : signal<
{
wrapped_.write(value);
}
bool
invalidate(std::exception_ptr error) const
{
return wrapped_.invalidate(error);
}
bool
is_invalidated() const
{
return wrapped_.is_invalidated();
}

private:
Wrapped wrapped_;
Expand Down

0 comments on commit 1290aa5

Please sign in to comment.