5#include <portals/widget.hpp>
15struct ManagedNavigationRecord;
39 std::shared_ptr<const PropertyChange<T>>
snapshot;
62template <std::equality_comparable T>
class Property {
75 static std::expected<std::unique_ptr<Property>,
TreeError>
77 return createImpl(tree, target, initial, kind, options);
93 return createImpl(tree, target, std::move(initial), kind, options);
102 if (state_->owner != std::this_thread::get_id()) {
106 state_->alive =
false;
118 if (state_->owner != std::this_thread::get_id()) {
122 return state_->current;
137 return setImpl(value);
149 return setImpl(std::move(value));
169 template <
class W,
class Callback>
170 std::expected<Subscription, TreeError>
observe(
Node<W> receiver, Callback &&callback) {
173 if (state->owner != std::this_thread::get_id()) {
177 auto allowed = state->tree.withScope(receiver, [](
const UiScope &) {
181 return std::unexpected(allowed.error());
186 if (state->observers.size() >= state->limit) {
191 auto lifetime = std::make_shared<Lifetime>();
192 auto connection = state->tree.template connect<PropertyChange<T>>(
193 receiver, [source = std::weak_ptr<State>(state), lifetime, function = std::forward<Callback>(callback)](
194 W &widget, std::shared_ptr<const PropertyChange<T>> event)
mutable {
195 auto locked = source.lock();
197 if (locked && locked->alive) {
198 std::invoke(function, widget, std::move(event));
203 return std::unexpected(connection.error());
213 if (state->observers.size() >= state->limit) {
217 state->observers.push_back(Observer{std::move(connection->publisher), lifetime});
219 return std::move(connection->subscription);
220 }
catch (
const std::bad_alloc &) {
237 template <
class Value>
238 static std::expected<std::unique_ptr<Property>,
TreeError>
244 return std::unexpected(allowed.error());
253 auto state = std::make_shared<State>(tree, target, kind);
261 state->current = std::make_shared<const PropertyChange<T>>(std::forward<Value>(initial), 0);
263 return std::unique_ptr<Property>(
new Property(std::move(state)));
264 }
catch (
const std::bad_alloc &) {
271 friend struct detail::ManagedNavigationRecord;
277 struct PreparedChange {
278 std::shared_ptr<State> state;
279 std::shared_ptr<const PropertyChange<T>> previous;
280 std::shared_ptr<const PropertyChange<T>> next;
284 template <
class Value> std::expected<PreparedChange, TreeError> prepareChange(Value &&value) {
287 if (state->owner != std::this_thread::get_id()) {
291 auto allowed = state->tree.withScope(state->target, [](
const UiScope &) {
295 return std::unexpected(allowed.error());
298 auto previous = state->current;
301 if (previous->value == value) {
302 if (
auto *recorder = state->tree.diagnostics(); recorder && recorder->enabled()) {
303 const auto id = state->target.id();
306 {
id.session,
id.slot,
id.generation});
309 return PreparedChange{state, previous, previous,
false};
312 if (previous->revision == std::numeric_limits<std::uint64_t>::max()) {
316 auto next = std::make_shared<const PropertyChange<T>>(std::forward<Value>(value), previous->revision + 1);
318 return PreparedChange{state, previous, std::move(next),
true};
319 }
catch (
const std::bad_alloc &) {
326 static std::expected<void, TreeError> validateChange(
const PreparedChange &change) {
327 const auto &state =
change.state;
329 if (state->owner != std::this_thread::get_id()) {
335 if (state->current !=
change.previous) {
339 return state->tree.withScope(state->target, [](
const UiScope &) {
343 static std::expected<void, TreeError> invalidateChange(
const PreparedChange &change) {
345 : std::expected<void, TreeError>{};
348 static void installChange(
const PreparedChange &change)
noexcept {
355 static PropertyUpdate<T> publishChange(
const PreparedChange &change) {
357 return PropertyUpdate<T>{
change.previous};
360 const auto &state =
change.state;
361 const auto &next =
change.next;
363 if (
auto *recorder = state->tree.diagnostics(); recorder && recorder->enabled()) {
364 const auto id = state->target.id();
371 PropertyUpdate<T> result{next,
true};
373 for (
auto &observer : state->observers) {
374 auto published = observer.publisher.publish(next);
379 if (!result.notification_error) {
380 result.notification_error = published.error();
397 template <
class Value> std::expected<PropertyUpdate<T>,
TreeError> setImpl(Value &&value) {
398 auto change = prepareChange(std::forward<Value>(value));
401 return std::unexpected(
change.error());
404 auto invalidated = invalidateChange(*change);
407 return std::unexpected(invalidated.error());
410 installChange(*change);
412 return publishChange(*change);
418 Publisher<PropertyChange<T>> publisher;
419 std::weak_ptr<Lifetime> lifetime;
423 State(WidgetTree &value, Node<Widget> node,
Invalidation invalidation)
424 : tree(value), target(node), kind(invalidation) {
428 std::erase_if(observers, [](
const Observer &observer) {
429 return observer.lifetime.expired();
436 const std::thread::id owner{std::this_thread::get_id()};
439 std::shared_ptr<const PropertyChange<T>> current;
440 std::vector<Observer> observers = std::vector<Observer>(0);
443 explicit Property(std::shared_ptr<State> state) : state_(std::move(state)) {
446 std::shared_ptr<State> state_;
Copyable typed observer; never owns or exposes an unchecked widget pointer.
Definition widget.hpp:92
UI-owned typed value with node-scoped observers and explicit partial delivery. WidgetTree and its Sch...
Definition property.hpp:62
~Property()
Logically close the source; executing callbacks retain their captures until return....
Definition property.hpp:101
static std::expected< std::unique_ptr< Property >, TreeError > create(WidgetTree &tree, Node< Widget > target, const T &initial, Invalidation kind, PropertyOptions options={})
Create a source bound to one checked invalidation target; no initial notification....
Definition property.hpp:76
std::expected< Subscription, TreeError > observe(Node< W > receiver, Callback &&callback)
Register an ordinary Subscription tied to a receiving node in the same tree. The callback owns its ca...
Definition property.hpp:170
std::expected< PropertyUpdate< T >, TreeError > set(const T &value)
Commit a changed value and invalidate its target before attempting notifications. unexpected means no...
Definition property.hpp:136
static std::expected< std::unique_ptr< Property >, TreeError > create(WidgetTree &tree, Node< Widget > target, T &&initial, Invalidation kind, PropertyOptions options={})
Create a source by moving its initial value directly into the snapshot. Validation and lifetime match...
Definition property.hpp:91
std::expected< PropertyUpdate< T >, TreeError > set(T &&value)
Commit a candidate by moving it directly into the immutable snapshot. Commit and notification semanti...
Definition property.hpp:148
std::expected< std::shared_ptr< const PropertyChange< T > >, TreeError > snapshot() const
Obtain an independent immutable owner, even after the target node was removed. Access on another thre...
Definition property.hpp:117
UI-owned scope disconnecting all its subscriptions and timers, in any token order....
Definition scheduler.hpp:154
auto change(T value)
Definition style_static.hpp:1496
Public Portals API.
Definition action.hpp:8
@ CLOSED
Scheduler, scope, registration or endpoint is closed.
Definition scheduler.hpp:24
@ VALUE_UNCHANGED
An observed Property comparison was equal.
Definition diagnostics.hpp:50
@ VALUE_CHANGED
An observed Property commit or tree work change occurred.
Definition diagnostics.hpp:51
@ PROPERTY
Observed equality or committed property change.
Definition diagnostics.hpp:28
Invalidation
Accumulated work; layout always includes painting.
Definition widget_types.hpp:41
@ LAYOUT
Layout and painting are required; old arranged geometry becomes invalid.
Definition widget_types.hpp:44
@ PAINT
Painting is required; arranged geometry remains usable.
Definition widget_types.hpp:43
@ NONE
No new work requested.
Definition widget_types.hpp:42
TreeError
Checked tree failures; rejected operations never start the requested callback.
Definition widget_types.hpp:26
@ CLOSED
The owning tree or source is closed.
Definition widget_types.hpp:31
@ CALLBACK_FAILED
A user extension or callback threw.
Definition widget_types.hpp:37
@ NODE_LIMIT
The configured live node, observer or consumer limit was reached.
Definition widget_types.hpp:35
@ WRONG_THREAD
Operation requires the tree's UI thread.
Definition widget_types.hpp:28
@ WOULD_BLOCK
The current frame/reentry boundary or finite queue/work budget rejects this operation.
Definition widget_types.hpp:32
@ IDENTITY_EXHAUSTED
A session, generation or revision cannot advance without reuse.
Definition widget_types.hpp:34
@ INVALID_ARGUMENT
An option, enum value or operation argument is invalid.
Definition widget_types.hpp:27
@ NO_MEMORY
Allocation failed inside the operation boundary.
Definition widget_types.hpp:33
Immutable owning value/revision pair; revision zero is the initial value. T must own its data transit...
Definition property.hpp:22
T value
Value captured at this revision, independent of the source lifetime.
Definition property.hpp:23
std::uint64_t revision
Monotonic revision within this property, never wrapped.
Definition property.hpp:24
Finite observer endpoint storage, independent of Scheduler queue capacity.
Definition property.hpp:28
std::size_t max_observers
Maximum live or executing observers; must be positive.
Definition property.hpp:29
A successful set result; notification rejection does not undo the committed value....
Definition property.hpp:38
std::size_t cancelled
Endpoints found closed during publication.
Definition property.hpp:43
std::size_t rejected
Live endpoints whose publication was rejected.
Definition property.hpp:42
std::shared_ptr< const PropertyChange< T > > snapshot
Owning snapshot of this operation's revision.
Definition property.hpp:39
bool changed
False means all publication counts are zero.
Definition property.hpp:40
std::size_t accepted
Events accepted by the existing Scheduler.
Definition property.hpp:41
std::optional< SchedulerError > notification_error
First publication failure, including closed.
Definition property.hpp:44