Mooeypoo has uploaded a new change for review.

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

Change subject: [wip^2] Add a SeenTimeModel to handle seenTime in sources
......................................................................

[wip^2] Add a SeenTimeModel to handle seenTime in sources

Change-Id: I629ecfc84999be998b45c9c7adb00ea7e3e51742
---
M Resources.php
M modules/controller/mw.echo.Controller.js
M modules/ext.echo.init.js
M modules/model/mw.echo.dm.ModelManager.js
A modules/model/mw.echo.dm.SeenTimeModel.js
5 files changed, 124 insertions(+), 53 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Echo 
refs/changes/05/298105/1

diff --git a/Resources.php b/Resources.php
index 42a5c72..c4c693b 100644
--- a/Resources.php
+++ b/Resources.php
@@ -177,6 +177,7 @@
                        'model/mw.echo.dm.SourcePagesModel.js',
                        'model/mw.echo.dm.PaginationModel.js',
                        'model/mw.echo.dm.FiltersModel.js',
+                       'model/mw.echo.dm.SeenTimeModel.js',
                        'model/mw.echo.dm.ModelManager.js',
                        'model/mw.echo.dm.SortedList.js',
                        'model/mw.echo.dm.NotificationItem.js',
diff --git a/modules/controller/mw.echo.Controller.js 
b/modules/controller/mw.echo.Controller.js
index f51091b..a74f944 100644
--- a/modules/controller/mw.echo.Controller.js
+++ b/modules/controller/mw.echo.Controller.js
@@ -581,25 +581,7 @@
         *  seenTime was updated for all given types.
         */
        mw.echo.Controller.prototype.updateLocalSeenTime = function () {
-               var controller = this,
-                       promises = [],
-                       types = this.manager.getTypes();
-
-               types.forEach( function ( type ) {
-                       promises.push( controller.api.updateSeenTime( 'local', 
type ) );
-               } );
-
-               return mw.echo.api.NetworkHandler.static.waitForAllPromises( 
promises )
-                       .then( function ( promises ) {
-                               var i;
-                               // Update the seen time object
-                               // The promises are in the same order as the 
types
-                               // so we can use the same iterator for both
-                               for ( i = 0; i < promises.length; i++ ) {
-                                       promises[ i ].done( 
controller.manager.updateSeenTimeObject.bind( controller.manager, types[ i ] ) 
);
-                               }
-                       } )
-                       .then( controller.manager.setLocalModelItemsSeen.bind( 
controller.manager ) );
+               this.manager.getSeenTimeModel().updateSeenTime( 'local' );
        };
 
        /**
diff --git a/modules/ext.echo.init.js b/modules/ext.echo.init.js
index 67422be..28e5a9b 100644
--- a/modules/ext.echo.init.js
+++ b/modules/ext.echo.init.js
@@ -53,6 +53,8 @@
                                        alertModelManager,
                                        unreadMessageCounter,
                                        unreadAlertCounter,
+                                       alertSeenTimeModel,
+                                       messageSeenTimeModel,
                                        maxNotificationCount = mw.config.get( 
'wgEchoMaxNotificationCount' );
 
                                // Overlay
@@ -60,7 +62,8 @@
                                // Load message button and popup if messages 
exist
                                if ( $existingMessageLink.length ) {
                                        unreadMessageCounter = new 
mw.echo.dm.UnreadNotificationCounter( echoApi, 'message', maxNotificationCount 
);
-                                       messageModelManager = new 
mw.echo.dm.ModelManager( unreadMessageCounter, { type: 'message' } );
+                                       messageSeenTimeModel = new 
mw.echo.dm.SeenTimeModel( echoApi, 'message' );
+                                       messageModelManager = new 
mw.echo.dm.ModelManager( unreadMessageCounter, messageSeenTimeModel, { type: 
'message' } );
                                        messageController = new 
mw.echo.Controller(
                                                echoApi,
                                                messageModelManager,
@@ -94,7 +97,8 @@
                                        } );
                                }
                                unreadAlertCounter = new 
mw.echo.dm.UnreadNotificationCounter( echoApi, 'alert', maxNotificationCount );
-                               alertModelManager = new 
mw.echo.dm.ModelManager( unreadAlertCounter, { type: 'alert' } );
+                               alertSeenTimeModel = new 
mw.echo.dm.SeenTimeModel( echoApi, 'alert' );
+                               alertModelManager = new 
mw.echo.dm.ModelManager( unreadAlertCounter, alertSeenTimeModel, { type: 
'alert' } );
                                alertController = new mw.echo.Controller(
                                        echoApi,
                                        alertModelManager,
diff --git a/modules/model/mw.echo.dm.ModelManager.js 
b/modules/model/mw.echo.dm.ModelManager.js
index f119847..cced211 100644
--- a/modules/model/mw.echo.dm.ModelManager.js
+++ b/modules/model/mw.echo.dm.ModelManager.js
@@ -25,13 +25,14 @@
         * @cfg {string|string[]} [type="message"] The type of the 
notifications in
         *  the models that this manager handles.
         */
-       mw.echo.dm.ModelManager = function MwEchoDmModelManager( counter, 
config ) {
+       mw.echo.dm.ModelManager = function MwEchoDmModelManager( counter, 
seenTimeModel, config ) {
                config = config || {};
 
                // Mixin constructor
                OO.EventEmitter.call( this );
 
                this.counter = counter;
+               this.seenTimeModel = seenTimeModel;
 
                // Keep types in an array
                this.types = config.type || 'message';
@@ -43,9 +44,6 @@
                this.filtersModel = new mw.echo.dm.FiltersModel( {
                        selectedSource: 'local'
                } );
-
-               // Properties
-               this.seenTime = mw.config.get( 'wgEchoSeenTime' ) || {};
        };
 
        OO.initClass( mw.echo.dm.ModelManager );
@@ -268,25 +266,6 @@
        };
 
        /**
-        * Update the local version of seenTime object.
-        *
-        * @private
-        * @param {string|string[]} type Type of notifications; could be a 
single type
-        *  or an array of types.
-        * @param {string} time Seen time
-        */
-       mw.echo.dm.ModelManager.prototype.updateSeenTimeObject = function ( 
type, time ) {
-               var i,
-                       types = Array.isArray( type ) ? type : [ type ];
-
-               for ( i = 0; i < types.length; i++ ) {
-                       if ( this.seenTime.hasOwnProperty( types[ i ] ) ) {
-                               this.seenTime[ types[ i ] ] = time;
-                       }
-               }
-       };
-
-       /**
         * Set items in the local model as seen
         *
         * @private
@@ -302,17 +281,11 @@
        /**
         * Get the system seen time
         *
-        * @param {string|string[]} [type] Notification types
+        * @param {string} [source='local'] Seen time source
         * @return {string} Mediawiki seen timestamp in Mediawiki timestamp 
format
         */
-       mw.echo.dm.ModelManager.prototype.getSeenTime = function ( type ) {
-               var types = type || this.getTypes();
-               types = Array.isArray( types ) ? types : [ types ];
-
-               // If the type that is set is an array [ 'alert', 'message' ]
-               // then the seen time will be the same to both, and we can
-               // return the value of the arbitrary first one.
-               return this.seenTime[ types[ 0 ] ];
+       mw.echo.dm.ModelManager.prototype.getSeenTime = function ( source ) {
+               return this.getSeenTimeModel().getSeenTime( source );
        };
 
        /**
@@ -354,4 +327,8 @@
                return notifications;
        };
 
+       mw.echo.dm.ModelManager.prototype.getSeenTimeModel = function () {
+               return this.seenTimeModel;
+       };
+
 } )( mediaWiki, jQuery );
diff --git a/modules/model/mw.echo.dm.SeenTimeModel.js 
b/modules/model/mw.echo.dm.SeenTimeModel.js
new file mode 100644
index 0000000..8a2271d
--- /dev/null
+++ b/modules/model/mw.echo.dm.SeenTimeModel.js
@@ -0,0 +1,107 @@
+( function ( mw, $ ) {
+       /**
+        * SeenTime model for Echo notifications
+        *
+        * @param {mw.echo.api.EchoApi} echoApi Echo API
+        * @param {mw.echo.dm.ModelManager} manager Model manager
+        * @param {Object} [config] Configuration
+        */
+       mw.echo.dm.SeenTimeModel = function MwEchoSeenTimeModel( echoApi, 
types, config ) {
+               config = config || {};
+
+               // Mixin constructor
+               OO.EventEmitter.call( this );
+
+               this.api = echoApi;
+               this.types = Array.isArray( types ) ? types : [ types ];
+
+               this.prioritizer = new mw.echo.api.PromisePrioritizer();
+
+               this.seenTime = {
+                       local: mw.config.get( 'wgEchoSeenTime' ) || {}
+               };
+       };
+
+       /* Initialization */
+
+       OO.initClass( mw.echo.dm.SeenTimeModel );
+       OO.mixinClass( mw.echo.dm.SeenTimeModel, OO.EventEmitter );
+
+       /* Events */
+
+       /**
+        * @event update
+        * @param {string} source The source that updated its seenTime
+        * @param {number} time Seen time
+        *
+        * Seen time has been updated for the given source
+        */
+
+       /* Methods */
+
+       /**
+        * Update seen time value from the API, for the given source
+        *
+        * @param {string} [source='local'] Given source
+        * @return {jQuery.Promise} Promise that is resolved when the seen time 
is
+        *  updated to the new value
+        */
+       mw.echo.dm.SeenTimeModel.prototype.updateSeenTime = function ( source ) 
{
+               source = source || 'local';
+
+               return this.prioritizer.prioritize(
+                       this.api.updateSeenTime( source, this.getTypes() )
+               )
+                       .then( this.setSeenTimeForSource.bind( this, source ) );
+       };
+
+       /**
+        * Get the seenTime value for the source
+        *
+        * @param {string} source Source name
+        * @return {number} Seen time
+        */
+       mw.echo.dm.SeenTimeModel.prototype.getSeenTime = function ( source ) {
+               source = source || 'local';
+
+               return ( this.seenTime[ source ] && this.seenTime[ source ][ 
this.getTypes()[ 0 ] ] ) || 0;
+       };
+
+       /**
+        * Set the seen time value for the source
+        *
+        * @private
+        * @param {string} [source='local'] Given source
+        * @fires update
+        */
+       mw.echo.dm.SeenTimeModel.prototype.setSeenTimeForSource = function ( 
source, time ) {
+               var model = this,
+                       hasChanged = false;
+
+               source = source || 'local';
+
+               this.seenTime[ source ] = this.seenTime[ source ] || {};
+
+               this.getTypes().forEach( function ( type ) {
+                       if ( model.seenTime[ source ][ type ] !== time ) {
+                               model.seenTime[ source ][ type ] = time;
+                               hasChanged = true;
+                       }
+               } );
+
+               if ( hasChanged ) {
+                       this.emit( 'update', source, time );
+               }
+       };
+
+       /**
+        * Get the types associated with this model
+        *
+        * @private
+        * @return {string[]} Types for this model; an array of 'alert', 
'message' or both.
+        */
+       mw.echo.dm.SeenTimeModel.prototype.getTypes = function () {
+               return this.types;
+       };
+
+} )( mediaWiki, jQuery );

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I629ecfc84999be998b45c9c7adb00ea7e3e51742
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Echo
Gerrit-Branch: master
Gerrit-Owner: Mooeypoo <mor...@gmail.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to