7#include <simplesquirrel/simplesquirrel.hpp>
11#include <unordered_map>
25 return static_cast<int64_t
>(std::stoll(
s));
55 default:
return false;
100 sq_pushstring(
vm,
v.s.c_str(),
static_cast<SQInteger
>(
v.s.size()));
108 switch (sq_gettype(
vm,
index)) {
126 const SQChar*
s =
nullptr;
130 case OT_USERPOINTER: {
131 SQUserPointer
p =
nullptr;
156Value callScript(
const ssq::Function&
fn,
const std::vector<Value>& args,
bool wantResult) {
159 SQInteger
top = sq_gettop(
raw);
160 sq_pushobject(
raw,
fn.getRaw());
161 sq_pushroottable(
raw);
170 if (SQ_SUCCEEDED(sq_call(
raw,
params, wantResult ? SQTrue : SQFalse, SQTrue))) {
171 if (wantResult && sq_gettop(
raw) >
top)
result = ssq::detail::popValue<Value>(
raw, -1);
179std::function<void()> scriptCompleted(ssq::Function
fn) {
180 return [
fn]() { callScript(
fn, {},
false); };
182std::function<void(
const Value&)> scriptNext(ssq::Function
fn) {
183 return [
fn](
const Value&
v) { callScript(
fn, {
v},
false); };
185std::function<void(
const std::string&)> scriptError(ssq::Function
fn) {
189bool isClosure(
const ssq::Object& obj) {
190 if (obj.isEmpty())
return false;
191 auto t = obj.getType();
192 return t == ssq::Type::CLOSURE ||
t == ssq::Type::NATIVECLOSURE;
196ssq::Object asClosure(
const ssq::Object& obj) {
197 if (isClosure(obj))
return obj;
198 return ssq::Object();
203 ssq::Object
n = asClosure(next);
204 ssq::Object e = asClosure(
error);
205 ssq::Object
c = asClosure(
done);
206 if (!
n.isEmpty()) o.
onNext = scriptNext(
n.toFunction());
207 if (!e.isEmpty()) o.
onError = scriptError(e.toFunction());
208 if (!
c.isEmpty()) o.
onCompleted = scriptCompleted(
c.toFunction());
223 in.
onError = detail::ForwardError<Value>{
out};
234 if (callScript(pred, {
v},
true).toBool())
out.next(
v);
236 in.
onError = detail::ForwardError<Value>{
out};
245 auto remaining = std::make_shared<int>(
n);
248 if (*remaining <= 0)
return;
251 if (*remaining == 0)
out.completed();
253 in.
onError = detail::ForwardError<Value>{
out};
262 auto remaining = std::make_shared<int>(
n);
265 if (*remaining > 0) {
271 in.
onError = detail::ForwardError<Value>{
out};
280 auto done = std::make_shared<bool>(
false);
288 in.
onError = detail::ForwardError<Value>{
out};
295 if (!self)
throw eve::Exception(
"Rx.distinctUntilChanged: null observable");
297 auto last = std::make_shared<Value>();
298 auto has = std::make_shared<bool>(
false);
301 if (*has && last->equals(
v))
return;
306 in.
onError = detail::ForwardError<Value>{
out};
329 auto subject = std::make_shared<SubjectV>();
331 std::lock_guard<std::mutex> lock(mu_);
345 std::string eventName = msg->name;
347 for (
const auto&
a : msg->args) {
353 std::shared_ptr<SubjectV>
subject;
355 std::lock_guard<std::mutex> lock(mu_);
356 auto it = bridges_.find(eventName);
357 if (it != bridges_.end())
subject = it->second;
365 std::unordered_map<std::string, std::shared_ptr<SubjectV>> bridges_;
377void bindCommon(ssq::Class&
cls) {
378 cls.addFunc(
"subscribe",
380 [](
ObservableV* self, ssq::Object
n) {
return doSubscribe(self,
n, {}, {}); }));
381 cls.addFunc(
"subscribe3",
382 std::function<Subscription*(
ObservableV*, ssq::Object, ssq::Object, ssq::Object)>(
383 [](
ObservableV* self, ssq::Object
n, ssq::Object e, ssq::Object
d) {
384 return doSubscribe(self,
n, e,
d);
396void Rx::expose(ssq::Table& table) {
401 "Observable", std::function<ObservableV*()>([]() ->
ObservableV* {
return nullptr; }),
true);
405 "Subject", std::function<SubjectV*()>([]() ->
SubjectV* {
return nullptr; }),
true);
413 "BehaviorSubject", std::function<BehaviorSubjectV*()>([]() ->
BehaviorSubjectV* {
return nullptr; }),
true);
420 bindCommon(behavior);
423 "ReplaySubject", std::function<ReplaySubjectV*()>([]() ->
ReplaySubjectV* {
return nullptr; }),
true);
431 "ReactiveProperty", std::function<ReactivePropertyV*()>([]() ->
ReactivePropertyV* {
return nullptr; }),
true);
434 prop.addFunc(
"subscribe",
438 return doSubscribe(self->asObservable(),
n, {}, {});
440 prop.addFunc(
"subscribe3",
441 std::function<Subscription*(
ReactivePropertyV*, ssq::Object, ssq::Object, ssq::Object)>(
444 return doSubscribe(self->asObservable(),
n, e,
d);
448 "Subscription", std::function<Subscription*()>([]() -> Subscription* {
return nullptr; }),
true);
453void Rx::expose(ssq::Class&
cls) {
struct SQVM * HSQUIRRELVM
#define Module_IMPL(ModuleName, newExpr)
The single Squirrel projection for common Result, Status and Value.
EVENGINE_API_FOUNDATION public API.
EVENGINE_API_FOUNDATION public API.
virtual std::string getName() const =0
Returns the name.
Internal: observable built directly from a subscribe function.
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.
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.
Callback bundle pushed to a subscriber. Once error() or completed() fires the observer is stopped and...
CompletedFn onCompleted
Completion callback (terminal).
NextFn onNext
Value callback.
ErrorFn onError
Error callback (terminal).
Observable value backed by a BehaviorSubject. get() returns the current value; set() stores and pushe...
T get() const
Current value.
void set(T v)
Stores a new value and notifies subscribers.
Subject that buffers up to capacity values (0 = unlimited) and replays the buffer to every new subscr...
void onNext(T v)
Buffers and pushes a new value to observers.
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.
ReactivePropertyV * newProperty(const Value &initial)
ReplaySubjectV * newReplaySubject(int capacity=0)
void pump(platform_event::PlatformEvent *ev)
BehaviorSubjectV * newBehaviorSubject(const Value &initial)
ObservableV * fromEvent(const std::string &eventName)
Multicast push-based stream: both an Observable and a push source. Thread-safe: onNext/onError/onComp...
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.
bool isDisposed() const
True once dispose() has run (or the handle was moved from).
Runtime variant used by the script-facing (Squirrel) streams. The C++ core is templated (Observer<T>/...
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
Result< void > replay(const Trace &trace, IEnvironment &environment, double tolerance)
Reset and replay actual actions, checking all observations and failure evidence.
eve::Subscription Subscription
Compatibility alias for the common observer lifetime token.
std::unordered_map< std::string, SkillDefinition > & table()
ReactiveProperty< Value > ReactivePropertyV
Observable< Value > ObservableV
Subject< Value > SubjectV
BehaviorSubject< Value > BehaviorSubjectV
ReplaySubject< Value > ReplaySubjectV
void bindPointerMethod(ssq::Class &cls, const char *name, Pointer pointer, bool isStatic=false)
Binds a free/member function directly, without std::function or a heap wrapper.
std::string reportScriptCallFailure(HSQUIRRELVM vm, const char *what)
Report a failed engine-to-script call, consuming the VM's script error.
void pushValue(HSQUIRRELVM vm, const eve::rx::Value &v)
eve::rx::Value popValue(HSQUIRRELVM vm, SQInteger index)
EVEngine ECS 集成层:底层实现使用 sunxfancy/ECS.hpp https://github.com/sunxfancy/ECS.hpp
SettlementPipeline::Stage fn