Skip to content

Commit

Permalink
default listen is changed to strong and listen_weak added for the wea…
Browse files Browse the repository at this point in the history
…k version of listen
  • Loading branch information
clinuxrulz committed Nov 17, 2018
1 parent c555811 commit 7c8b1de
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 7 deletions.
7 changes: 7 additions & 0 deletions src/sodium/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ impl<A: Clone + Trace + Finalize + 'static> Cell<A> {
) -> Listener {
self.impl_.listen(callback)
}

pub fn listen_weak<CALLBACK:FnMut(&A)+'static>(
&self,
callback: CALLBACK
) -> Listener {
self.impl_.listen_weak(callback)
}
}

impl<A: Clone + Trace + Finalize + 'static> Clone for Cell<A> {
Expand Down
17 changes: 16 additions & 1 deletion src/sodium/impl_/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,21 @@ impl<A: Clone + Trace + Finalize + 'static> Cell<A> {
pub fn listen<CALLBACK:FnMut(&A)+'static>(
&self,
callback: CALLBACK
) -> Listener {
self._listen(callback, false)
}

pub fn listen_weak<CALLBACK:FnMut(&A)+'static>(
&self,
callback: CALLBACK
) -> Listener {
self._listen(callback, true)
}

pub fn _listen<CALLBACK:FnMut(&A)+'static>(
&self,
callback: CALLBACK,
weak: bool
) -> Listener {
let sodium_ctx = self._node().sodium_ctx();
let sodium_ctx = &sodium_ctx;
Expand Down Expand Up @@ -435,7 +450,7 @@ impl<A: Clone + Trace + Finalize + 'static> Cell<A> {
vec![self._node().clone()],
|| {},
String::from("Cell::listen_node")
))
), weak)
}
}

Expand Down
16 changes: 13 additions & 3 deletions src/sodium/impl_/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,35 @@ use sodium::gc::Trace;
use std::cell::UnsafeCell;

pub struct Listener {
node_op: Gc<UnsafeCell<Option<Node>>>
node_op: Gc<UnsafeCell<Option<Node>>>,
weak: bool
}

impl Listener {
pub fn debug(&self) {
self.node_op.debug();
}

pub fn new(node: Node) -> Listener {
pub fn new(node: Node, weak: bool) -> Listener {
let sodium_ctx = node.sodium_ctx();
let mut gc_ctx = sodium_ctx.gc_ctx();
if !weak {
sodium_ctx.add_keep_alive(node.clone());
}
Listener {
node_op: gc_ctx.new_gc_with_desc(UnsafeCell::new(Some(node)), String::from("Listener::new"))
node_op: gc_ctx.new_gc_with_desc(UnsafeCell::new(Some(node)), String::from("Listener::new")),
weak
}
}

pub fn unlisten(&self) {
let weak = self.weak;
let node_op = unsafe { &mut *(*self.node_op).get() };
if let &mut Some(ref mut node) = node_op {
let sodium_ctx = node.sodium_ctx();
if !weak {
sodium_ctx.remove_keep_alive(node);
}
node.remove_all_dependencies();
}
*node_op = None;
Expand Down
17 changes: 15 additions & 2 deletions src/sodium/impl_/sodium_ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use sodium::gc::Finalize;
use sodium::gc::GcCtx;
use sodium::gc::Trace;
use sodium::impl_::IsLambda0;
use sodium::impl_::Listener;
use sodium::impl_::MemoLazy;
use sodium::impl_::Node;
use std::cell::UnsafeCell;
Expand All @@ -28,7 +29,8 @@ pub struct SodiumCtxData {
pub resort_required: bool,
pub pre_trans: Vec<Box<FnMut()>>,
pub post_trans: Vec<Box<FnMut()>>,
pub node_count: u32
pub node_count: u32,
pub keep_alive: HashSet<Node>
}

impl SodiumCtx {
Expand All @@ -43,7 +45,8 @@ impl SodiumCtx {
resort_required: false,
pre_trans: Vec::new(),
post_trans: Vec::new(),
node_count: 0
node_count: 0,
keep_alive: HashSet::new()
}))
}
}
Expand Down Expand Up @@ -72,6 +75,16 @@ impl SodiumCtx {
id
}

pub fn add_keep_alive(&self, node: Node) {
let self_ = unsafe { &mut *(*self.data).get() };
self_.keep_alive.insert(node);
}

pub fn remove_keep_alive(&self, node: &Node) {
let self_ = unsafe { &mut *(*self.data).get() };
self_.keep_alive.remove(node);
}

pub fn inc_node_count(&self) {
let self_ = unsafe { &mut *(*self.data).get() };
self_.node_count = self_.node_count + 1;
Expand Down
17 changes: 16 additions & 1 deletion src/sodium/impl_/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,21 @@ impl<A: Clone + Trace + Finalize + 'static> Stream<A> {
pub fn listen<CALLBACK:FnMut(&A)+'static>(
&self,
callback: CALLBACK
) -> Listener {
self._listen(callback, false)
}

pub fn listen_weak<CALLBACK:FnMut(&A)+'static>(
&self,
callback: CALLBACK
) -> Listener {
self._listen(callback, true)
}

pub fn _listen<CALLBACK:FnMut(&A)+'static>(
&self,
callback: CALLBACK,
weak: bool
) -> Listener {
let sodium_ctx = self._node().sodium_ctx();
let sodium_ctx = &sodium_ctx;
Expand Down Expand Up @@ -482,7 +497,7 @@ impl<A: Clone + Trace + Finalize + 'static> Stream<A> {
vec![self._node().clone()],
|| {},
String::from("Stream::listen_node")
))
), weak)
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/sodium/is_cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ pub trait IsCell<A: Finalize + Trace + Clone + 'static>: Sized {
) -> Listener {
self.to_cell().listen(callback)
}

fn listen_weak<CALLBACK:FnMut(&A)+'static>(
&self,
callback: CALLBACK
) -> Listener {
self.to_cell().listen(callback)
}
}

impl<A: Finalize + Trace + Clone + 'static> IsCell<A> for Cell<A> {
Expand Down
7 changes: 7 additions & 0 deletions src/sodium/is_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,13 @@ pub trait IsStream<A: Finalize + Trace + Clone + 'static> {
) -> Listener {
self.to_stream().listen(callback)
}

fn listen_weak<CALLBACK:FnMut(&A)+'static>(
&self,
callback: CALLBACK
) -> Listener {
self.to_stream().listen_weak(callback)
}
}

impl<A: Finalize + Trace + Clone + 'static> IsStream<A> for Stream<A> {
Expand Down
7 changes: 7 additions & 0 deletions src/sodium/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,13 @@ impl<A: Clone + Trace + Finalize + 'static> Stream<A> {
) -> Listener {
self.impl_.listen(callback)
}

pub fn listen_weak<CALLBACK:FnMut(&A)+'static>(
&self,
callback: CALLBACK
) -> Listener {
self.impl_.listen_weak(callback)
}
}

impl<A: Clone + Trace + Finalize + 'static> Clone for Stream<A> {
Expand Down

0 comments on commit 7c8b1de

Please sign in to comment.