77 int64_t
toInt()
const;
98 other.dispose_ =
nullptr;
99 other.disposed_ =
true;
102 if (
this != &other) {
104 dispose_ = std::move(other.dispose_);
105 disposed_ = other.disposed_;
106 other.dispose_ =
nullptr;
107 other.disposed_ =
true;
115 if (dispose_ && !disposed_) {
117 auto fn = std::move(dispose_);
126 std::function<void()> dispose_;
127 bool disposed_ =
false;
138 using NextFn = std::function<void(
const T&)>;
139 using ErrorFn = std::function<void(
const std::string&)>;
159 void error(
const std::string& e)
const {
160 if (stopped_)
return;
166 if (stopped_)
return;
172 mutable bool stopped_ =
false;
190 obs.
onNext = std::move(next);
198 obs.
onNext = std::move(next);
207 template <
typename R>
226 : fn_(std::move(
fn)) {}
239 in.
onNext = [out, pred](
const T&
v) {
242 in.
onError = [out](
const std::string& e) { out.
error(e); };
244 return self->subscribe(std::move(in));
255 in.
onError = [out](
const std::string& e) { out.
error(e); };
257 return self->subscribe(std::move(in));
265 auto remaining = std::make_shared<int>(
n);
267 in.
onNext = [out, remaining](
const T&
v) {
268 if (*remaining <= 0)
return;
271 if (*remaining == 0) {
276 in.
onError = [out](
const std::string& e) { out.
error(e); };
278 return self->subscribe(std::move(in));
286 auto remaining = std::make_shared<int>(
n);
288 in.
onNext = [out, remaining](
const T&
v) {
289 if (*remaining > 0) {
295 in.
onError = [out](
const std::string& e) { out.
error(e); };
297 return self->subscribe(std::move(in));
305 auto done = std::make_shared<bool>(
false);
307 in.
onNext = [out, done](
const T&
v) {
313 in.
onError = [out](
const std::string& e) { out.
error(e); };
315 return self->subscribe(std::move(in));
325 in.
onError = [out](
const std::string& e) { out.
error(e); };
327 auto src = std::make_shared<Subscription>(self->subscribe(std::move(in)));
330 stopper.
onNext = [out, src](
const T&)
mutable {
338 auto stop = std::make_shared<Subscription>(other->
subscribe(std::move(stopper)));
351 auto last = std::make_shared<T>();
352 auto has = std::make_shared<bool>(
false);
354 in.
onNext = [out, last, has](
const T&
v) {
355 if (*has && *last ==
v)
return;
360 in.
onError = [out](
const std::string& e) { out.
error(e); };
362 return self->subscribe(std::move(in));
378 auto slot = std::make_shared<Slot>();
379 slot->observer = std::move(obs);
381 std::lock_guard<std::mutex> lock(mu_);
389 slots_.push_back(slot);
391 return Subscription([
this, slot]() { removeSlot(slot); });
409 std::lock_guard<std::mutex> lock(mu_);
410 for (
const auto&
s : slots_)
411 if (!
s->removed)
return true;
416 std::lock_guard<std::mutex> lock(mu_);
418 for (
const auto&
s : slots_)
419 if (!
s->removed)
n++;
429 void emit(
const std::function<
void(Observer<T>&)>&
fn) {
430 std::vector<std::shared_ptr<Slot>> copy;
432 std::lock_guard<std::mutex> lock(mu_);
433 for (
auto&
s : slots_)
436 for (
auto&
s : copy) {
437 Observer<T> snapshot =
s->observer;
442 void removeSlot(
const std::shared_ptr<Slot>& slot) {
443 std::lock_guard<std::mutex> lock(mu_);
444 slot->removed =
true;
448 std::lock_guard<std::mutex> lock(mu_);
453 mutable std::mutex mu_;
454 std::vector<std::shared_ptr<Slot>> slots_;
455 bool stopped_ =
false;
474 std::lock_guard<std::mutex> lock(mu_);
480 if (replay) obs.
next(latest);
481 return base_.subscribe(std::move(obs));
486 std::lock_guard<std::mutex> lock(mu_);
493 std::lock_guard<std::mutex> lock(mu_);
494 latest_ = std::move(
v);
497 base_.onNext(emitted);
504 std::lock_guard<std::mutex> lock(mu_);
512 std::lock_guard<std::mutex> lock(mu_);
521 mutable std::mutex mu_;
523 bool completed_ =
false;
540 std::vector<T> replay;
542 std::lock_guard<std::mutex> lock(mu_);
545 for (
const auto&
v : replay)
547 return base_.subscribe(std::move(obs));
554 std::lock_guard<std::mutex> lock(mu_);
555 buffer_.push_back(std::move(
v));
556 if (capacity_ > 0 &&
static_cast<int>(buffer_.size()) > capacity_)
557 buffer_.erase(buffer_.begin());
559 base_.onNext(emitted);
573 mutable std::mutex mu_;
574 std::vector<T> buffer_;
590 T
get()
const {
return subject_.getValue(); }
592 void set(T
v) { subject_.setValue(std::move(
v)); }
SettlementPipeline::Stage fn
Internal: observable built directly from a subscribe function.
Subscription subscribe(Observer< T > obs) override
Subscribes with a full observer; returns a cancel handle.
AnonymousObservable(std::function< Subscription(Observer< T >)> fn)
Subject that replays the latest value to every new subscriber. setValue()/onNext() update the stored ...
void onCompleted()
Stops replay and delivers a terminal completion.
bool hasObservers() const
True while at least one live observer is registered.
T getValue() const
Current stored value.
void onNext(T v)
Alias of setValue().
void onError(const std::string &e)
Stops replay and delivers a terminal error.
void setValue(T v)
Stores a new value and pushes it to observers.
BehaviorSubject(T initial)
Creates a subject with an initial (replayed) value.
Subscription subscribe(Observer< T > obs) override
Replays the latest value, then subscribes the observer.
Push-based stream source. Operators return a new (caller-owned) Observable; subscribe() returns a Sub...
virtual Subscription subscribe(Observer< T > obs)=0
Subscribes with a full observer; returns a cancel handle.
Observable< T > * filter(std::function< bool(const T &)> pred)
Passes values through only when pred(v) is true.
Observable< T > * distinctUntilChanged()
Suppresses consecutive duplicate values (uses operator==).
Observable< T > * takeUntil(Observable< T > *other)
Stops the stream when other emits or completes.
Observable< R > * map(std::function< R(const T &)> fn)
Transforms each value with fn.
virtual ~Observable()=default
Subscription subscribe(typename Observer< T >::NextFn next)
Subscribes with a value callback only.
Observable< T > * skip(int n)
Drops the first n values.
Observable< T > * take(int n)
Emits at most the first n values, then completes.
Observable< T > * first()
Emits only the first value, then completes.
Subscription subscribe(typename Observer< T >::NextFn next, typename Observer< T >::ErrorFn error, typename Observer< T >::CompletedFn completed)
Subscribes with value/error/completed callbacks.
Callback bundle pushed to a subscriber. Once error() or completed() fires the observer is stopped and...
CompletedFn onCompleted
Completion callback (terminal).
void error(const std::string &e) const
Delivers a terminal error and stops.
std::function< void(const T &)> NextFn
void completed() const
Delivers a terminal completion and stops.
NextFn onNext
Value callback.
std::function< void(const std::string &)> ErrorFn
bool isStopped() const
True after error()/completed() (or setStopped()).
ErrorFn onError
Error callback (terminal).
std::function< void()> CompletedFn
void next(const T &v) const
Delivers a value unless stopped.
void setStopped() const
Manually marks the observer stopped (used by operators).
Observable value backed by a BehaviorSubject. get() returns the current value; set() stores and pushe...
ReactiveProperty(T initial=T())
Creates a property with an initial value.
BehaviorSubject< T > * asSubject()
Underlying behavior subject / observable view.
T get() const
Current value.
void set(T v)
Stores a new value and notifies subscribers.
Subscription subscribe(typename Observer< T >::NextFn next)
Subscription subscribe(Observer< T > obs)
Subscribes with a full observer or a value callback.
Observable< T > * asObservable()
Subject that buffers up to capacity values (0 = unlimited) and replays the buffer to every new subscr...
ReplaySubject(int capacity=0)
Creates a replaying subject with the given buffer capacity.
void onNext(T v)
Buffers and pushes a new value to observers.
Subscription subscribe(Observer< T > obs) override
Replays buffered values, then subscribes the observer.
void onCompleted()
Delivers a terminal completion to observers.
void onError(const std::string &e)
Delivers a terminal error to observers.
bool hasObservers() const
True while at least one live observer is registered.
Multicast push-based stream: both an Observable and a push source. Thread-safe: onNext/onError/onComp...
int observerCount() const
Number of live (non-disposed) observers.
~Subject() override=default
Subscription subscribe(Observer< T > obs) override
Registers an observer; returns a Subscription that unregisters it.
bool hasObservers() const
True while at least one live observer is registered.
void onCompleted()
Pushes a terminal completion and stops the subject.
void onNext(const T &v)
Pushes a value to every registered observer.
void onError(const std::string &e)
Pushes a terminal error and stops the subject.
RAII-style dispose handle for an active stream subscription. Disposing unsubscribes from the source; ...
void dispose()
Runs the dispose callback once; idempotent.
Subscription(std::function< void()> dispose)
Wraps a dispose callback (usually unsubscribing from a Subject).
bool isDisposed() const
True once dispose() has run (or the handle was moved from).
Subscription & operator=(Subscription &&other) noexcept
Subscription(const Subscription &)=delete
Subscription & operator=(const Subscription &)=delete
Subscription(Subscription &&other) noexcept
Runtime variant used by the script-facing (Squirrel) streams. The C++ core is templated (Observer<T>/...
bool isNil() const
Type predicates.
static Value makeFloat(double v)
Constructs a floating-point value.
static Value makeNil()
Constructs a nil value.
static Value makePtr(void *v)
Constructs a pointer value (borrowed, not owned).
int64_t toInt() const
Converters (throw eve::Exception on type mismatch).
static Value makeInt(int64_t v)
Constructs an integer value.
bool equals(const Value &o) const
Value equality across the supported types.
static Value makeString(std::string v)
Constructs a string value (takes ownership).
static Value makeBool(bool v)
Constructs a boolean value.
std::string toString() const