//
// Flambe - Rapid game development
// https://github.com/aduros/flambe/blob/master/LICENSE.txt

package flambe.platform;

import flambe.util.Signal1;
import flambe.util.SignalConnection;
import flambe.util.Value;

/** An internal Signal1 with extra frills. */
class HeavySignal1<A> extends Signal1<A>
{
    /**
     * A watchable value, for detecting when the first listener was connected, or the last
     * connection was disposed.
     */
    public var hasListenersValue (default, null) :Value<Bool>;

    public function new (?listener :Listener1<A>)
    {
        super(listener);
        hasListenersValue = new Value<Bool>(hasListeners());
    }

    override public function connect (listener :Listener1<A>, prioritize :Bool = false) :SignalConnection
    {
        var connection = super.connect(listener, prioritize);
        hasListenersValue._ = hasListeners();
        return connection;
    }

    override public function _internal_disconnect (conn :SignalConnection)
    {
        super._internal_disconnect(conn);
        hasListenersValue._ = hasListeners();
    }
}

//
// Flambe - Rapid game development
// https://github.com/aduros/flambe/blob/master/LICENSE.txt

package flambe.platform;

import flambe.util.Signal1;
import flambe.util.SignalConnection;
import flambe.util.Value;

/** An internal Signal1 with extra frills. */
class HeavySignal1<A> extends Signal1<A>
{
    /**
     * A watchable value, for detecting when the first listener was connected, or the last
     * connection was disposed.
     */
    public var hasListenersValue (default, null) :Value<Bool>;

    public function new (?listener :Listener1<A>)
    {
        super(listener);
        hasListenersValue = new Value<Bool>(hasListeners());
    }

    override public function connect (listener :Listener1<A>, prioritize :Bool = false) :SignalConnection
    {
        var connection = super.connect(listener, prioritize);
        hasListenersValue._ = hasListeners();
        return connection;
    }

    override public function _internal_disconnect (conn :SignalConnection)
    {
        super._internal_disconnect(conn);
        hasListenersValue._ = hasListeners();
    }

    override private function didEmit (head :SignalConnection)
    {
        super.didEmit(head);
        hasListenersValue._ = hasListeners();
    }
}

Update hasListenersValue after an emit.
Update hasListenersValue after an emit.

hasListeners() while dispatching may return the wrong value (disconnects
are deferred until the end of the current dispatch).

playedonline/flambe,playedonline/flambe,weilitao/flambe,weilitao/flambe,weilitao/flambe,playedonline/flambe,weilitao/flambe,mikedotalmond/flambe,Disar/flambe,markknol/flambe,aduros/flambe,aduros/flambe,Disar/flambe,Disar/flambe,markknol/flambe,mikedotalmond/flambe,playedonline/flambe,aduros/flambe,aduros/flambe,markknol/flambe,mikedotalmond/flambe,Disar/flambe,mikedotalmond/flambe