<commit_before>package flambe.platform.html;

import flambe.util.Signal0;
import flambe.util.Signal1;
import flambe.util.SignalConnection;
import flambe.input.Accelerometer;
import flambe.input.AccelerometerMotion;
import flambe.input.AccelerometerOrientation;
import flambe.platform.EventGroup;
import Type;

import js.Lib;

 /**
 * Basic idea is to conform differences in accelerometer support for each
 * device/platform as much as possible.
 */
class HtmlAccelerometer implements Accelerometer
{
    /**
     * 
     */
    //public var motionSupported (default, null) :Bool;//TODO
    /**
     * 
     */
    public var orientationSupported (default, null) :Bool;
    /** 
    * <code>null</code> if not supported.
    */
    //public var motionChange(default, null): Signal1<AccelerometerMotion>;//TODO
    /** 
    * <code>null</code> if not supported.
    */
    public var orientationUpdate(default, null): Signal1<AccelerometerOrientation>;
    /**
     * 
     */
    public var disposed(default, null):Signal0;

    /**
     * 
     */
    public function new()
    {   
        disposed = new Signal0();

        _win = (untyped Lib.window);

        //motionSupported = _win.DeviceMotionEvent != null; 
        orientationSupported = _win.DeviceOrientationEvent != null; 

        // if (motionSupported)
        // {
        //     _motion = new AccelerometerMotion();

        //     motionChange = _motionChange = new NotifyingSignal1();

        //     _motionChange.addedFirst.connect(function()
        //         {
        //             _motionEventGroup = new EventGroup();
        //             _motionEventGroup.addListener(_win, "devicemotion", handleAccelerometerMotion);
        //         }
        //     );

        //     _motionChange.disposedLast.connect(function()
        //         {
        //             _motionEventGroup.dispose();
        //             _motionEventGroup = null;
        //         }
        //     );
        // }

        if (orientationSupported)
        {
            _orientation = new AccelerometerOrientation();

            orientationUpdate = _orientationUpdate = new NotifyingSignal1();

            _orientationUpdate.addedFirst.connect(function()
                {

                    _orientationEventGroup = new EventGroup();
                    _orientationEventGroup.addListener(_win, "deviceorientation", handleAccelerometerOrientation);
                }
            );

            _orientationUpdate.disposedLast.connect(function()
                {
                    _motionEventGroup.dispose();
                    _motionEventGroup = null;
                }
            );
        }
    }

    /**
     * 
     */
    private function handleAccelerometerOrientation(event):Void
    {
        _windowOrientation = _win.orientation;

        //alpha = z = azimuth, beta = x = pitch, gamma = y = roll
        if (_windowOrientation == -90)
        {
            _orientation._internal_update(event.gamma, -event.beta, event.alpha);
        }
        else if (_windowOrientation == 0)
        {
            _orientation._internal_update(event.beta, event.gamma, event.alpha);
        }
        else if (_windowOrientation == 90)
        {
            _orientation._internal_update(-event.gamma, event.beta, event.alpha);
        }
        else if (_windowOrientation == 180)
        {
            _orientation._internal_update(-event.beta, -event.gamma, event.alpha);
        }
        // else
        // {
        //     trace("Window orientation " + _windowOrientation + " not valid.");
        // }

        orientationUpdate.emit(_orientation);

    }

    //private var _motionChange:NotifyingSignal1<AccelerometerOrientation>;
    private var _orientationUpdate:NotifyingSignal1<AccelerometerOrientation>;
    private var _windowOrientation:Float;
    private var _win:Dynamic;
    private var _orientationEventGroup:EventGroup;
    private var _motionEventGroup:EventGroup;
    //private var _motion:AccelerometerMotion;
    private var _orientation:AccelerometerOrientation;

}

private class NotifyingSignal1<A> extends Signal1<A>
{
    public var disposedLast(default, null):Signal0;
    public var addedFirst(default, null):Signal0;

    public function new (?listener :Listener1<A>)
    {
        super(listener);

        disposedLast = new Signal0();
        addedFirst = new Signal0();
    }

    override public function connect (listener :Listener1<A>, prioritize :Bool = false) :SignalConnection
    {
        if (!hasListeners())
        {
            // Added the first listener.
            addedFirst.emit();
        }

        return super.connect(listener, prioritize);
    }

    override public function _internal_disconnect (conn :SignalConnection)
    {
        super._internal_disconnect(conn);

        if (!hasListeners()) {
            // Disposed the last listener.
            disposedLast.emit();
        }

    }

}







<commit_msg>Bugfix<commit_after>package flambe.platform.html;

import flambe.util.Signal0;
import flambe.util.Signal1;
import flambe.util.SignalConnection;
import flambe.input.Accelerometer;
import flambe.input.AccelerometerMotion;
import flambe.input.AccelerometerOrientation;
import flambe.platform.EventGroup;
import Type;

import js.Lib;

 /**
 * Basic idea is to conform differences in accelerometer support for each
 * device/platform as much as possible.
 */
class HtmlAccelerometer implements Accelerometer
{
    /**
     * 
     */
    //public var motionSupported (default, null) :Bool;//TODO
    /**
     * 
     */
    public var orientationSupported (default, null) :Bool;
    /** 
    * <code>null</code> if not supported.
    */
    //public var motionChange(default, null): Signal1<AccelerometerMotion>;//TODO
    /** 
    * <code>null</code> if not supported.
    */
    public var orientationUpdate(default, null): Signal1<AccelerometerOrientation>;
    /**
     * 
     */
    public var disposed(default, null):Signal0;

    /**
     * 
     */
    public function new()
    {   
        disposed = new Signal0();

        _win = (untyped Lib.window);

        //motionSupported = _win.DeviceMotionEvent != null; 
        orientationSupported = _win.DeviceOrientationEvent != null; 

        // if (motionSupported)
        // {
        //     _motion = new AccelerometerMotion();

        //     motionChange = _motionChange = new NotifyingSignal1();

        //     _motionChange.addedFirst.connect(function()
        //         {
        //             _motionEventGroup = new EventGroup();
        //             _motionEventGroup.addListener(_win, "devicemotion", handleAccelerometerMotion);
        //         }
        //     );

        //     _motionChange.disposedLast.connect(function()
        //         {
        //             _motionEventGroup.dispose();
        //             _motionEventGroup = null;
        //         }
        //     );
        // }

        if (orientationSupported)
        {
            _orientation = new AccelerometerOrientation();

            orientationUpdate = _orientationUpdate = new NotifyingSignal1();

            _orientationUpdate.addedFirst.connect(function()
                {

                    _orientationEventGroup = new EventGroup();
                    _orientationEventGroup.addListener(_win, "deviceorientation", handleAccelerometerOrientation);
                }
            );

            _orientationUpdate.disposedLast.connect(function()
                {
                    _orientationEventGroup.dispose();
                    _orientationEventGroup = null;
                }
            );
        }
    }

    /**
     * 
     */
    private function handleAccelerometerOrientation(event):Void
    {
        _windowOrientation = _win.orientation;

        //alpha = z = azimuth, beta = x = pitch, gamma = y = roll
        if (_windowOrientation == -90)
        {
            _orientation._internal_update(event.gamma, -event.beta, event.alpha);
        }
        else if (_windowOrientation == 0)
        {
            _orientation._internal_update(event.beta, event.gamma, event.alpha);
        }
        else if (_windowOrientation == 90)
        {
            _orientation._internal_update(-event.gamma, event.beta, event.alpha);
        }
        else if (_windowOrientation == 180)
        {
            _orientation._internal_update(-event.beta, -event.gamma, event.alpha);
        }
        // else
        // {
        //     trace("Window orientation " + _windowOrientation + " not valid.");
        // }

        orientationUpdate.emit(_orientation);

    }

    //private var _motionChange:NotifyingSignal1<AccelerometerOrientation>;
    private var _orientationUpdate:NotifyingSignal1<AccelerometerOrientation>;
    private var _windowOrientation:Float;
    private var _win:Dynamic;
    private var _orientationEventGroup:EventGroup;
    private var _motionEventGroup:EventGroup;
    //private var _motion:AccelerometerMotion;
    private var _orientation:AccelerometerOrientation;

}

private class NotifyingSignal1<A> extends Signal1<A>
{
    public var disposedLast(default, null):Signal0;
    public var addedFirst(default, null):Signal0;

    public function new (?listener :Listener1<A>)
    {
        super(listener);

        disposedLast = new Signal0();
        addedFirst = new Signal0();
    }

    override public function connect (listener :Listener1<A>, prioritize :Bool = false) :SignalConnection
    {
        if (!hasListeners())
        {
            // Added the first listener.
            addedFirst.emit();
        }

        return super.connect(listener, prioritize);
    }

    override public function _internal_disconnect (conn :SignalConnection)
    {
        super._internal_disconnect(conn);

        if (!hasListeners()) {
            // Disposed the last listener.
            disposedLast.emit();
        }

    }

}








{'fraction_non_alphanumeric': 0.1025330507619336, 'fraction_numerical': 0.005247754566555657, 'mean_word_length': 2.014907210222087, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3391803', 'n_tokens_mistral': 2781, 'n_tokens_neox': 2542, 'n_words': 520}