Jdlrobson has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/236062

Change subject: Hygiene: Goodbye custom event emitter code.
......................................................................

Hygiene: Goodbye custom event emitter code.

Changes:
* Removes EventEmitter code
* All classes previously inheriting from EventEmitter now inherit
from class.
* ModuleLoader in mobile.modules now depends on OO. OOJS now loads
in head.

Change-Id: I5f06b876ba346170c9707eb7ea10e28eafa9df77
---
M includes/Resources.php
M resources/mobile.infiniteScroll/InfiniteScroll.js
M resources/mobile.modules/modules.js
M resources/mobile.oo/Class.js
D resources/mobile.oo/eventemitter.js
M resources/mobile.startup/Router.js
M resources/mobile.startup/api.js
M resources/mobile.swipe/Swipe.js
M resources/mobile.view/View.js
D tests/qunit/mobile.oo/test_eventemitter.js
M tests/qunit/mobile.startup/test_OverlayManager.js
11 files changed, 38 insertions(+), 110 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/MobileFrontend 
refs/changes/62/236062/1

diff --git a/includes/Resources.php b/includes/Resources.php
index 1ad7acf..5e13832 100644
--- a/includes/Resources.php
+++ b/includes/Resources.php
@@ -230,6 +230,9 @@
 
 $wgResourceModules = array_merge( $wgResourceModules, array(
        'mobile.modules' => $wgMFResourceFileModuleBoilerplate + array(
+               'dependencies' => array(
+                       'oojs',
+               ),
                'scripts' => array(
                        'resources/mobile.modules/modules.js',
                ),
@@ -241,7 +244,6 @@
                ),
                'scripts' => array(
                        'resources/mobile.oo/Class.js',
-                       'resources/mobile.oo/eventemitter.js',
                ),
        ),
        'mobile.view' => $wgMFResourceFileModuleBoilerplate + array(
diff --git a/resources/mobile.infiniteScroll/InfiniteScroll.js 
b/resources/mobile.infiniteScroll/InfiniteScroll.js
index 08e930d..d19ba75 100644
--- a/resources/mobile.infiniteScroll/InfiniteScroll.js
+++ b/resources/mobile.infiniteScroll/InfiniteScroll.js
@@ -1,6 +1,6 @@
 ( function ( M, $ ) {
 
-       var EventEmitter = M.require( 'eventemitter' ),
+       var Class = M.require( 'Class' ),
                InfiniteScroll;
 
        /**
@@ -8,6 +8,7 @@
         * element.
         *
         * @class InfiniteScroll
+        * @extends Class
         *
         * Use this class in a view to help it do infinite scrolling.
         *
@@ -53,14 +54,14 @@
         *       } );
         *     </code>
         */
-       InfiniteScroll = EventEmitter.extend( {
+       InfiniteScroll = Class.extend( {
                /**
                 * Constructor.
                 * @param {Number} threshold distance in pixels used to 
calculate if scroll
                 * position is near the end of the $el
                 */
                initialize: function ( threshold ) {
-                       EventEmitter.prototype.initialize.apply( this, 
arguments );
+                       Class.prototype.initialize.apply( this, arguments );
                        this.threshold = threshold || 100;
                        this.enabled = true;
                        this._bindScroll();
diff --git a/resources/mobile.modules/modules.js 
b/resources/mobile.modules/modules.js
index 13dec79..73065d0 100644
--- a/resources/mobile.modules/modules.js
+++ b/resources/mobile.modules/modules.js
@@ -1,6 +1,4 @@
 ( function () {
-       var loader;
-
        /**
         * Class for managing modules
         *
@@ -8,6 +6,7 @@
         * ResourceLoader modules).
         *
         * @class ModuleLoader
+        * @extends OO.EventEmitter
         */
        function ModuleLoader() {
                /**
@@ -15,6 +14,7 @@
                 * @private
                 */
                this._register = {};
+               OO.EventEmitter.call( this );
        }
 
        ModuleLoader.prototype = {
@@ -75,8 +75,7 @@
                        mw.log.deprecate( this._register, id, obj, depreacteMsg 
);
                }
        };
-
-       loader = new ModuleLoader();
+       OO.mixinClass( ModuleLoader, OO.EventEmitter );
 
        /**
         *
@@ -85,27 +84,7 @@
         * @class mw.mobileFrontend
         * @singleton
         */
-       mw.mobileFrontend = {
-               /**
-                * @see ModuleLoader#define
-                * @return {Object}
-                */
-               define: function () {
-                       return loader.define.apply( loader, arguments );
-               },
-               /**
-                * @see ModuleLoader#require
-                */
-               require: function () {
-                       return loader.require.apply( loader, arguments );
-               },
-               /**
-                * @see ModuleLoader#deprecate
-                */
-               deprecate: function () {
-                       return loader.deprecate.apply( loader, arguments );
-               }
-       };
+       mw.mobileFrontend = new ModuleLoader();
 
        // inception to support testing (!!)
        mw.mobileFrontend.define( 'ModuleLoader', ModuleLoader );
diff --git a/resources/mobile.oo/Class.js b/resources/mobile.oo/Class.js
index f21fc2e..1156dc6 100644
--- a/resources/mobile.oo/Class.js
+++ b/resources/mobile.oo/Class.js
@@ -36,8 +36,11 @@
         * @class Class
         */
        function Class() {
+               OO.EventEmitter.call( this );
                this.initialize.apply( this, arguments );
        }
+       OO.mixinClass( Class, OO.EventEmitter );
+
        /**
         * Constructor, if you override it, use _super().
         * @method
diff --git a/resources/mobile.oo/eventemitter.js 
b/resources/mobile.oo/eventemitter.js
deleted file mode 100644
index 948ee9e..0000000
--- a/resources/mobile.oo/eventemitter.js
+++ /dev/null
@@ -1,25 +0,0 @@
-( function ( M, $, OO ) {
-
-       var EventEmitter,
-               Class = M.require( 'Class' );
-
-       // HACK: wrap around oojs's EventEmitter
-       // This needs some hackery to make oojs's
-       // and MobileFrontend's different OO models get along,
-       // and we need to alias one() to once().
-       /**
-        * A base class with support for event emitting.
-        * @class EventEmitter
-        * @extends Class
-        * @uses OO.EventEmitter
-       **/
-       EventEmitter = Class.extend( $.extend( {
-               initialize: OO.EventEmitter
-       }, OO.EventEmitter.prototype ) );
-
-       M.define( 'eventemitter', EventEmitter );
-       // FIXME: if we want more of M's functionality in loaded in <head>,
-       // move this to a separate file
-       $.extend( mw.mobileFrontend, new EventEmitter() );
-
-}( mw.mobileFrontend, jQuery, OO ) );
diff --git a/resources/mobile.startup/Router.js 
b/resources/mobile.startup/Router.js
index 99574ed..df0d20e 100644
--- a/resources/mobile.startup/Router.js
+++ b/resources/mobile.startup/Router.js
@@ -1,8 +1,8 @@
 // FIXME: Merge this code with OverlayManager
 ( function ( M, $ ) {
 
-       var key, router,
-               EventEmitter = M.require( 'eventemitter' );
+       var Router,
+               Class = M.require( 'Class' );
 
        /**
         * Does hash match entry.path?
@@ -25,11 +25,13 @@
        /**
         * Provides navigation routing and location information
         * @class Router
-        * @uses EventEmitter
+        * @extends Class
         */
-       function Router() {
+       Router = Class.extend( {} );
+
+       Router.prototype.initialize = function () {
                var self = this;
-               EventEmitter.prototype.initialize.apply( this, arguments );
+               Class.prototype.initialize.apply( this, arguments );
                // use an object instead of an array for routes so that we don't
                // duplicate entries that already exist
                this.routes = {};
@@ -64,13 +66,7 @@
 
                        self._oldHash = self.getPath();
                } );
-       }
-
-       for ( key in EventEmitter.prototype ) {
-               if ( EventEmitter.prototype.hasOwnProperty( key ) ) {
-                       Router.prototype[ key ] = EventEmitter.prototype[ key ];
-               }
-       }
+       };
 
        /**
         * Check the current route and run appropriate callback if it matches.
@@ -166,9 +162,7 @@
                return 'onhashchange' in window;
        };
 
-       router = new Router();
-
-       M.define( 'router', router );
+       M.define( 'router', new Router() );
        M.define( 'Router', Router );
 
 }( mw.mobileFrontend, jQuery ) );
diff --git a/resources/mobile.startup/api.js b/resources/mobile.startup/api.js
index 3c7c671..ea010d5 100644
--- a/resources/mobile.startup/api.js
+++ b/resources/mobile.startup/api.js
@@ -1,14 +1,13 @@
 ( function ( M, $ ) {
-       var api,
-               Api = mw.Api,
-               EventEmitter = M.require( 'eventemitter' );
+       var Api, api,
+               Class = M.require( 'Class' );
 
        /**
         * JavaScript wrapper for a horrible API. Use to retrieve things.
         * @class Api
-        * @extends EventEmitter
+        * @extends Class
         */
-       Api = EventEmitter.extend( mw.Api.prototype ).extend( {
+       Api = Class.extend( mw.Api.prototype ).extend( {
                /**
                 * @property {String} apiUrl
                 * URL to the api endpoint (api.php)
@@ -25,7 +24,7 @@
                                options.ajax.url = this.apiUrl;
                        }
                        mw.Api.call( this, options );
-                       EventEmitter.prototype.initialize.apply( this, 
arguments );
+                       Class.prototype.initialize.apply( this, arguments );
                }
        } );
        api = new Api();
diff --git a/resources/mobile.swipe/Swipe.js b/resources/mobile.swipe/Swipe.js
index 5577dc3..cfeadf3 100644
--- a/resources/mobile.swipe/Swipe.js
+++ b/resources/mobile.swipe/Swipe.js
@@ -1,12 +1,13 @@
 ( function ( M, $ ) {
 
-       var EventEmitter = M.require( 'eventemitter' ),
+       var Class = M.require( 'Class' ),
                Swipe;
 
        /**
         * Class to assist a view in implementing swipe gestures on a specific 
element
         *
         * @class Swipe
+        * @extends Class
         *
         * Use this class in a view to help it do things on swipe gestures.
         *
@@ -49,14 +50,14 @@
         *       } );
         *     </code>
         */
-       Swipe = EventEmitter.extend( {
+       Swipe = Class.extend( {
                /**
                 * Constructor.
                 * @param {Number} minDistance minimal distance in pixel 
between touchstart and touchend
                 * to be recognized as a swipe event. Default: 200
                 */
                initialize: function ( minDistance ) {
-                       EventEmitter.prototype.initialize.apply( this, 
arguments );
+                       Class.prototype.initialize.apply( this, arguments );
                        this.minDistance = minDistance || 200;
                },
                /**
diff --git a/resources/mobile.view/View.js b/resources/mobile.view/View.js
index 4529a2c..7aff3ef 100644
--- a/resources/mobile.view/View.js
+++ b/resources/mobile.view/View.js
@@ -1,6 +1,6 @@
 ( function ( M, $ ) {
 
-       var EventEmitter = M.require( 'eventemitter' ),
+       var Class = M.require( 'Class' ),
                View,
                // Cached regex to split keys for `delegate`.
                delegateEventSplitter = /^(\S+)\s*(.*)$/,
@@ -71,7 +71,7 @@
         *     </code>
         *
         * @class View
-        * @extends EventEmitter
+        * @extends Class
         * @param {Object} options Options for the view, containing the el or
         * template data or any other information you want to use in the view.
         * Example:
@@ -86,7 +86,7 @@
         *     section.appendTo( 'body' );
         *     </pre>
         */
-       View = EventEmitter.extend( {
+       View = Class.extend( {
                /**
                 * A css class to apply to the containing element of the View.
                 * @property {String} className
@@ -158,7 +158,7 @@
                initialize: function ( options ) {
                        var self = this;
 
-                       EventEmitter.prototype.initialize.apply( this, 
arguments );
+                       Class.prototype.initialize.apply( this, arguments );
                        this.defaults = $.extend( {}, this._parent.defaults, 
this.defaults );
                        this.templatePartials = $.extend( {}, 
this._parent.templatePartials, this.templatePartials );
                        options = $.extend( {}, this.defaults, options );
diff --git a/tests/qunit/mobile.oo/test_eventemitter.js 
b/tests/qunit/mobile.oo/test_eventemitter.js
deleted file mode 100644
index f6c2fe1..0000000
--- a/tests/qunit/mobile.oo/test_eventemitter.js
+++ /dev/null
@@ -1,25 +0,0 @@
-( function ( M ) {
-
-       var EventEmitter = M.require( 'eventemitter' );
-
-       QUnit.module( 'MobileFrontend EventEmitter' );
-
-       QUnit.test( '#on', 1, function ( assert ) {
-               var e = new EventEmitter(),
-                       spy = this.sandbox.spy();
-               e.on( 'testEvent', spy );
-               e.emit( 'testEvent', 'first', 2 );
-               assert.ok( spy.calledWith( 'first', 2 ), 'run callback when 
event runs' );
-       } );
-
-       QUnit.test( '#one', 2, function ( assert ) {
-               var e = new EventEmitter(),
-                       spy = this.sandbox.spy();
-               e.once( 'testEvent', spy );
-               e.emit( 'testEvent', 'first', 2 );
-               e.emit( 'testEvent', 'second', 2 );
-               assert.ok( spy.calledWith( 'first', 2 ), 'run callback when 
event runs' );
-               assert.ok( spy.calledOnce, 'run callback once' );
-       } );
-
-}( mw.mobileFrontend ) );
diff --git a/tests/qunit/mobile.startup/test_OverlayManager.js 
b/tests/qunit/mobile.startup/test_OverlayManager.js
index d519e5e..d223202 100644
--- a/tests/qunit/mobile.startup/test_OverlayManager.js
+++ b/tests/qunit/mobile.startup/test_OverlayManager.js
@@ -1,13 +1,12 @@
 ( function ( M, $ ) {
        var
                OverlayManager = M.require( 'OverlayManager' ),
-               EventEmitter = M.require( 'eventemitter' ),
                fakeRouter, overlayManager;
 
        QUnit.module( 'MobileFrontend OverlayManager', {
                setup: function () {
                        this.createFakeOverlay = function ( options ) {
-                               var fakeOverlay = new EventEmitter();
+                               var fakeOverlay = new OO.EventEmitter();
                                fakeOverlay.show = this.sandbox.spy();
                                fakeOverlay.hide = function () {
                                        this.emit( 'hide' );
@@ -18,7 +17,7 @@
                                return fakeOverlay;
                        };
 
-                       fakeRouter = new EventEmitter();
+                       fakeRouter = new OO.EventEmitter();
                        fakeRouter.getPath = this.sandbox.stub().returns( '' );
                        fakeRouter.back = this.sandbox.spy();
                        overlayManager = new OverlayManager( fakeRouter );

-- 
To view, visit https://gerrit.wikimedia.org/r/236062
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I5f06b876ba346170c9707eb7ea10e28eafa9df77
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/MobileFrontend
Gerrit-Branch: master
Gerrit-Owner: Jdlrobson <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to