(b) is easy if you're willing to drop down to Java, or use gen-class. You
can define a finalize method.

On the other hand, you might also want to look into WeakReference and
ReferenceQueue, which can be used from Clojure with interop. You can
discover when objects become eligible for GC with ReferenceQueue, if they
are held by WeakReference (or SoftReference). So you could create a
channel, a ReferenceQueue, a WeakReference on the channel set up to use
that ReferenceQueue, and a map with mappings from WeakReference to some
sort of identifiers, as well as mappings from identifiers to functions to
call. When a reference shows up on the queue the mapping is used to look up
the id and then the function and invoke it. The modified channel can
directly call the same function when closed. The function can take the id
as a parameter and use a map in the other direction, from id to
WeakReference, to remove all of the mappings for a given id when called
with that id, before doing whatever else it does. It won't have access to
the channel object, at least not if that's been GC'd, but it wouldn't be
able to do much with it anyway in the case where the channel was closed,
save use the channel as a lookup key itself, and the id can serve that
purpose.


On Sun, Jan 26, 2014 at 4:42 AM, t x <txrev...@gmail.com> wrote:

> ## Context:
>
>   I'm doing some communication across cljs/clj with core.async
>
> ## Problem:
>
>   I want a certain function called whenever:
>   (a) the first time the channel is closed
>   (b) when the channel is gc-ed (if not previously closed)
>
>   (a) is rather easy: I define my own channel, wrapped around
> ManyToManyChannel, and define my own
> https://github.com/clojure/core.async/blob/master/src/main/clojure/clojure/core/async/impl/protocols.clj#L22
>
>   (b) I have no idea how to do -- is there someway to register some
> function that gets called when a channel is gc-ed ? (perferably in both
> cljs + clj -- although, if it just works in cljs, it suffices)
>
> Thanks!
>
> --
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clojure@googlegroups.com
> Note that posts from new members are moderated - please be patient with
> your first post.
> To unsubscribe from this group, send email to
> clojure+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
> ---
> You received this message because you are subscribed to the Google Groups
> "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to clojure+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>

-- 
-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to