use super::*;
impl<ValueType> Clone for Signal<ValueType>
where
ValueType: 'static,
{
fn clone(&self) -> Self {
*self
}
}
impl<ValueType> Copy for Signal<ValueType> where ValueType: 'static {}
impl<ValueType> Default for Signal<ValueType>
where
ValueType: 'static,
{
fn default() -> Self {
Self {
id: SignalId::default(),
marker: PhantomData,
}
}
}
impl<ValueType> PartialEq for Signal<ValueType>
where
ValueType: 'static,
{
fn eq(&self, other: &Self) -> bool {
self.id() == other.id()
}
}
impl<ValueType> Eq for Signal<ValueType> where ValueType: 'static {}
impl<ValueType> Hash for Signal<ValueType>
where
ValueType: 'static,
{
fn hash<HasherType>(&self, state: &mut HasherType)
where
HasherType: Hasher,
{
self.id().hash(state);
}
}
impl<ValueType> Signal<ValueType>
where
ValueType: 'static,
{
#[must_use]
pub fn new(initial_value: ValueType) -> Self {
let id: SignalId = with_runtime(
|runtime_ref: &mut ReactiveRuntime| runtime_ref.create_signal(initial_value),
SignalId::default(),
);
Self {
id,
marker: PhantomData,
}
}
#[must_use]
pub fn new_detached(initial_value: ValueType) -> Self {
let id: SignalId = with_runtime(
|runtime_ref: &mut ReactiveRuntime| runtime_ref.create_detached_signal(initial_value),
SignalId::default(),
);
Self {
id,
marker: PhantomData,
}
}
#[must_use]
pub(crate) fn from_raw_id(id: SignalId) -> Self {
Self {
id,
marker: PhantomData,
}
}
#[must_use]
#[inline(always)]
pub fn id(&self) -> SignalId {
let Self { id, .. } = *self;
id
}
#[must_use]
#[inline(always)]
pub fn has_subscribers(&self) -> bool {
let id: SignalId = self.id();
with_runtime_ref(
|runtime_ref: &ReactiveRuntime| runtime_ref.signal_has_subscribers(id),
false,
)
}
#[must_use]
#[inline(always)]
pub fn subscriber_count(&self) -> usize {
let id: SignalId = self.id();
with_runtime_ref(
|runtime_ref: &ReactiveRuntime| runtime_ref.signal_subscriber_count(id),
0,
)
}
#[must_use]
#[inline(always)]
pub fn is_inline(&self) -> bool {
let id: SignalId = self.id();
with_runtime_ref(
|runtime_ref: &ReactiveRuntime| runtime_ref.signal_is_inline(id),
false,
)
}
#[inline(always)]
pub fn track(&self) {
track_or_defer_dependency(DependencyId::Signal(self.id()));
}
#[must_use]
#[inline(always)]
pub fn try_get(&self) -> Option<ValueType>
where
ValueType: Clone,
{
let id: SignalId = self.id();
if ref_borrow_depth() > 0 || !is_tracking_active() {
return read_signal_with_ref::<ValueType, _, ValueType>(id, true, |val: &ValueType| {
val.clone()
});
}
with_runtime(
|runtime_ref: &mut ReactiveRuntime| runtime_ref.read_signal_cloned::<ValueType>(id),
None,
)
}
#[must_use]
#[inline(always)]
pub fn get(&self) -> ValueType
where
ValueType: Clone + Default,
{
self.try_get().unwrap_or_default()
}
#[must_use]
#[inline(always)]
pub fn try_get_untracked(&self) -> Option<ValueType>
where
ValueType: Clone,
{
let id: SignalId = self.id();
with_runtime_ref(
|runtime_ref: &ReactiveRuntime| runtime_ref.read_signal_untracked::<ValueType>(id),
None,
)
}
#[must_use]
#[inline(always)]
pub fn get_untracked(&self) -> ValueType
where
ValueType: Clone + Default,
{
self.try_get_untracked().unwrap_or_default()
}
#[inline]
pub fn try_with_ref<ReaderFn, ReturnValue>(&self, reader: ReaderFn) -> Option<ReturnValue>
where
ReaderFn: FnOnce(&ValueType) -> ReturnValue,
{
read_signal_with_ref::<ValueType, ReaderFn, ReturnValue>(self.id(), true, reader)
}
#[inline]
pub fn try_with_ref_untracked<ReaderFn, ReturnValue>(
&self,
reader: ReaderFn,
) -> Option<ReturnValue>
where
ReaderFn: FnOnce(&ValueType) -> ReturnValue,
{
read_signal_with_ref::<ValueType, ReaderFn, ReturnValue>(self.id(), false, reader)
}
#[inline]
pub fn with_ref<ReaderFn, ReturnValue>(&self, reader: ReaderFn) -> ReturnValue
where
ReaderFn: FnOnce(&ValueType) -> ReturnValue,
{
self.try_with_ref(reader).unwrap()
}
#[inline]
pub fn with<CallbackFn, ReturnValue>(&self, reader: CallbackFn) -> Option<ReturnValue>
where
ValueType: Clone,
CallbackFn: FnOnce(&ValueType) -> ReturnValue,
{
let snapshot: ValueType = self.try_get()?;
Some(reader(&snapshot))
}
#[inline(always)]
pub fn set(&self, value: ValueType) -> bool {
let id: SignalId = self.id();
let (updated, should_flush): (bool, bool) = with_runtime(
|runtime_ref: &mut ReactiveRuntime| runtime_ref.write_signal(id, value),
(false, false),
);
if should_flush {
flush_effects();
}
updated
}
#[inline(always)]
pub fn set_if_changed(&self, value: ValueType) -> bool
where
ValueType: PartialEq,
{
let id: SignalId = self.id();
let (updated, should_flush): (bool, bool) = with_runtime(
|runtime_ref: &mut ReactiveRuntime| runtime_ref.write_signal_if_changed(id, value),
(false, false),
);
if should_flush {
flush_effects();
}
updated
}
pub fn dispose(&self) {
let id: SignalId = self.id();
let dropped: SignalSlot = with_runtime(
|runtime_ref: &mut ReactiveRuntime| runtime_ref.dispose_signal(id),
SignalSlot::empty(),
);
drop(dropped);
}
}