package com.hypirion.beckon;

import java.util.List;
import java.util.Map;
import java.util.HashMap;

import java.util.concurrent.Callable;

import clojure.lang.Atom;
import clojure.lang.PersistentHashMap;
import clojure.lang.Keyword;
import clojure.lang.AFn;
import clojure.lang.IFn;

public class SignalAtoms {
    private static final Map<String, Atom> atoms = new HashMap<String, Atom>();
    public static final Keyword SIGNAL = Keyword.intern("signal");
    public static final IFn SIGNAL_ATOM_VALIDATOR = new SignalAtomValidator();

    static final synchronized Atom getSignalAtom(String signame) {
        if (!atoms.containsKey(signame)) {
            Object list = SignalRegistererHelper.getHandlerList(signame);
            PersistentHashMap metadata = PersistentHashMap.create(SIGNAL, signame);
            Atom atm = new Atom(list, metadata);
            atm.setValidator(SIGNAL_ATOM_VALIDATOR);
            atoms.put(signame, atm);
        }
        return atoms.get(signame);
    }

    private static class SignalAtomValidator extends AFn {
        @Override
        public Object invoke(Object newVal) {
            if (newVal instanceof List) {
                List lst = (List) newVal;
                for (Object o : lst) {
                    if (!(newVal instanceof Callable)) {
                        return false;
                    }
                }
                return true;
            }
            else {
                return false;
            }
        }
    }
}

@@ -903,16 +903,120 @@
 DATOR);%0A
+            SignalAtomWatch saw = new SignalAtomWatch(signame);%0A            atm.addWatch(signame, saw);%0A
         
@@ -1598,11 +1598,420 @@
  %7D%0A    %7D
+%0A%0A    private static class SignalAtomWatch extends AFn %7B%0A        public final String signame;%0A%0A        public SignalAtomWatch(String signame) %7B%0A            this.signame = signame;%0A        %7D%0A%0A        @Override%0A        public Object invoke(Object key, Object ref, Object oldState, Object newState) %7B%0A            SignalRegistererHelper.register(signame, (List) newState);%0A            return null;%0A        %7D%0A    %7D
 %0A%7D%0A

Add in watch on atom to update actual handler.