Closer - laforge49/aatree GitHub Wiki
Closer is a component that manages a stack of close functions. There are two functions:
(aatree.closer-trait.open-component this name f)
(aatree.closer.close-components this)
The open-component function pushes f and the name onto the close function stack, adds the stack to opts as needed, logs the open and returns the updated options map. Here is the signature of a close function passed to open-component:
(close-me this)
For each item on the stack, the close-components function logs the close and calls the close function. This is done in the reverse of the order they were added to the stack. Close functions will only be called once for each occurrence on the close stack. And if a close function throws an exception it will be caught and logged but otherwise ignored.
See also Closer Options.
Test
(ns aatree.closer-trait-test
(:require [aatree.closer-trait :refer :all]))
(set! *warn-on-reflection* true)
(close-components {})
(defn close-a [this] (println " close a"))
(defn close-b [this] (println " close b"))
(defn close-c [this] (println " close c"))
(let [this (open-component {} "a" close-a)
this (open-component this "b" close-b)
this (open-component this "c" close-c)]
(println "first close")
(close-components this)
(println "second close")
(close-components this))