reactive

A reactive programming library, including signals, event emitters, and streams observer.

reactive-programming
functional-reactive-programming
frp
streams
observer
signal
event-emiter
events
moon add himeno/reactive@0.1.2
Download zip
Author
Version
0.1.2
License
GPL-3.0-or-later
Last updated
7 months ago
Downloads
18
README

#himeno/reactive

#Overview

himeno/reactive is a reactive programming library that provides tools for handling signals, event emitters, and streams with observers. It facilitates the creation of applications that react to changes in data or events.


#Usage

#Signals

Signals are used to track changes in a value. When the value changes, all dependent components are automatically updated.

Creating Signals:

let condition = state(true)
let count = state(1)

Accessing Signal Values:

let value = condition.get()

Setting Signal Values:

count.set(10)

Listening to Signal Changes:

let off_condition = effect(() => println("Condition changed: \{condition.get()}"))
condition.set(false)
off_condition()

#Computed Values

Computed values are derived from other signals or state. They automatically update when their dependencies change.

Creating Computed Values:

let result = computed(() => {
if condition.get() {
a.get() * 2
} else {
b.get() * 3
}
})

Accessing Computed Values:

let value = result.get()

Listening to Computed Value Changes:

let off_result = result.on(value => println("Result changed: \{value}"))
off_result()

#Effects

Effects are used to perform side effects in response to changes in signals or computed values.

Creating Effects:

effect(() => println("Condition changed: \{condition.get()}"))

#Streams

Streams are used to handle sequences of data that arrive over time. The Observable and Observer components are central to managing streams.

Creating Observables:

let my_observable = Observable::new(observer => {
observer.next(10)
()
})

Subscribing to Observables:

let my_subscription = my_observable.on_from_next(value => println("Received value: \{value}"))
my_subscription.off()

Creating Subjects:

Subjects allow you to manually control the emission of data to subscribers.

let my_subject = Subject::new(observer => {
observer.next(5)
()
})

Emitting Data to Subjects:

my_subject.next(10)
my_subject.complete()


#Event Emitters

Event emitters allow components to listen for specific events and react to them. The Events struct manages event listeners and middlewares.

Creating Event Emitters:

let my_events = Events::new()

Emitting Events:

my_events.emit("user.login", { username: "john_doe" })

Listening to Events:

let off_login = my_events.on("user.login", data => println("User logged in: \{data.username}"))
off_login()

One-Time Event Listeners:

my_events.once("user.login", data => println("User logged in once: \{data.username}"))

#
Computed

pub struct Computed[T] {
// private fields
}

#
Computed::get

fn[T] Computed::get(self : Computed[T]) -> T

Get the current value of the computed value.

#
Computed::new

fn[T] Computed::new(callback : () -> T) -> Computed[T]

Create a new computed value with the given callback.

#
Computed::on

fn[T] Computed::on(self : Computed[T], listener : (T) -> Unit) -> (() -> Unit)

Subscribe to changes of the computed value.

#
Counter

pub struct Counter {
// private fields
}

#
Counter::new

fn Counter::new() -> Counter

Create a new counter with a value of 0.

#
Counter::reset

fn Counter::reset(self : Counter) -> Counter

Reset the counter to 0.

#
Counter::set

fn Counter::set(self : Counter, val : Int) -> Counter

Set the counter to a specific value.

#
Counter::since

fn Counter::since(val : Int) -> Counter

Create a new counter with a specific value.

#
Events

pub struct Events[N, T] {
// private fields
}

#
Events::check

fn[N, T] Events::check(self : Events[N, T], event : N, data : T) -> Bool

Checks if the given event has any listeners.

#
Events::emit

fn[N : Eq + Hash, T] Events::emit(self : Events[N, T], event : N, data : T) -> Unit

Emits the given event with the given data.

#
Events::emitAll

fn[N, T] Events::emitAll(self : Events[N, T], data : T) -> Unit

Emits the given event with the given data to all listeners.

#
Events::middleware

fn[N, T] Events::middleware(self : Events[N, T], middleware : (N, T, () -> Unit) -> Unit) -> (() -> Unit)

Registers a middleware for the given event.

#
Events::new

fn[N : Eq + Hash, T] Events::new() -> Events[N, T]

Creates a new instance of Events.

#
Events::on

fn[N : Eq + Hash, T] Events::on(self : Events[N, T], event : N, listener : (T) -> Unit, once? : Bool) -> (() -> Unit)

Registers a listener for the given event.

#
Events::once

fn[N : Eq + Hash, T] Events::once(self : Events[N, T], event : N, listener : (T) -> Unit) -> (() -> Unit)

Registers a listener for the given event that will be executed only once.

#
Observable

pub struct Observable[T, E] {
// private fields
}

#
Observable::new

fn[T, E] Observable::new(onFn : (Observer[T, E]) -> (() -> Unit)) -> Observable[T, E]

Creates a new observable with the given onFn function.

#
Observable::on

fn[T, E] Observable::on(self : Observable[T, E], observer : Observer[T, E]) -> Subscription

Subscribes to the observable with the given observer and returns a

#
Observable::on_from_next

fn[T, E] Observable::on_from_next(self : Observable[T, E], next : (T) -> Unit) -> Subscription

Converts the observable to an observer and subscribes to it with the given

#
Observer

pub struct Observer[T, E] {
next : (T) -> Unit?
error : (E) -> Unit?
complete : () -> Unit?
// private fields
}

#
Observer::new

fn[T, E] Observer::new(next? : (T) -> Unit, error? : (E) -> Unit, complete? : () -> Unit) -> Observer[T, E]

Creates a new observer with the given next, error, and complete

#
State

pub struct State[T] {
// private fields
}

#
State::get

fn[T] State::get(self : State[T]) -> T

Get the current value of the state.

#
State::new

fn[T] State::new(val : T) -> State[T]

Create a new state with the given initial value.

#
State::on

fn[T] State::on(self : State[T], listener : (T) -> Unit) -> (() -> Unit)

Get the current value of the state and subscribe to changes.

#
State::set

fn[T : Eq] State::set(self : State[T], val : T) -> Unit

Set the current value of the state.

#
State::update

fn[T : Eq] State::update(self : State[T], f : (T) -> T) -> Unit

Update the current value of the state using a function.

#
Subject

pub struct Subject[T, E] {
// private fields
}

#
Subject::as_observable

fn[T, E] Subject::as_observable(self : Subject[T, E]) -> Observable[T, E]

Converts the subject to an observable and returns it.

#
Subject::close

fn[T, E] Subject::close(self : Subject[T, E]) -> Unit

Closes the subject and unsubscribes all observers.

#
Subject::complete

fn[T, E] Subject::complete(self : Subject[T, E]) -> Unit

Completes the subject.

#
Subject::error

fn[T, E] Subject::error(self : Subject[T, E], error : E) -> Unit

Errors the subject with the given error.

#
Subject::get_error

fn[T, E] Subject::get_error(self : Subject[T, E]) -> E?

Returns the error of the subject, if any.

#
Subject::has_error

fn[T, E] Subject::has_error(self : Subject[T, E]) -> Bool

Returns true if the subject has an error, false otherwise.

#
Subject::is_closed

fn[T, E] Subject::is_closed(self : Subject[T, E]) -> Bool

Returns true if the subject is closed, false otherwise.

#
Subject::is_stopped

fn[T, E] Subject::is_stopped(self : Subject[T, E]) -> Bool

Returns true if the subject is stopped, false otherwise.

#
Subject::new

fn[T, E] Subject::new(onFn : (Observer[T, E]) -> (() -> Unit)) -> Subject[T, E]

Creates a new subject with the given onFn function.

#
Subject::next

fn[T, E] Subject::next(self : Subject[T, E], value : T) -> Unit

Nexts the subject with the given value.

#
Subject::observers_count

fn[T, E] Subject::observers_count(self : Subject[T, E]) -> Int

Returns the number of observers of the subject.

#
Subject::on

fn[T, E] Subject::on(self : Subject[T, E], observer : Observer[T, E]) -> Subscription

Subscribes to the subject with the given observer and returns a

#
Subject::on_from_next

fn[T, E] Subject::on_from_next(self : Subject[T, E], next : (T) -> Unit) -> Subscription

Converts the subject to an observable and subscribes to it with the given

#
Subscription

pub struct Subscription {
// private fields
}

#
Subscription::add

fn Subscription::add(self : Subscription, subscription : Subscription) -> Unit

Adds the given subscription to the list of subscriptions.

#
Subscription::add_from_func

fn Subscription::add_from_func(self : Subscription, off : () -> Unit) -> Unit

Adds the given off function to the list of subscriptions.

#
Subscription::is_closed

fn Subscription::is_closed(self : Subscription) -> Bool

Returns true if the subscription is closed, false otherwise.

#
Subscription::new

fn Subscription::new(off? : () -> Unit) -> Subscription

Creates a new subscription with the given off function.

#
Subscription::off

fn Subscription::off(self : Subscription) -> Unit

Unsubscribes the observer from the subscription.

#
Subscription::remove

fn Subscription::remove(self : Subscription, subscription : Subscription) -> Unit

Removes the given subscription from the list of subscriptions.

#
computed

fn[T] computed(callback : () -> T) -> Computed[T]

Create a new computed value with the given callback.

#
effect

fn effect(callback : () -> Unit) -> (() -> Unit)

Update the current value of the computed value using a function.

#
state

fn[T] state(val : T) -> State[T]

Create a new state with the given initial value.

Powered by MoonBit

Site sourceReport issuePackagesBuild queueSkillsStatistics

© 2026 mooncakes.io