Debian Codegraph @

extensions/Echo/modules/model/mw.echo.dm.UnreadNotificationCounter.js

raw symbols SCIP
1 ( function () {
2 /**
3 * Echo notification UnreadNotificationCounter model
4 *
5 * @class
6 * @mixes OO.EventEmitter
7 *
8 * @constructor
9 * @param {Object} api An instance of EchoAPI.
10 * @param {string} type The notification type 'message', 'alert', or 'all'.
11 * @param {number} max Maximum number supported. Above this number there is no precision, we only know it is 'more than max'.
12 * @param {Object} config Configuration object
13 * @param {boolean} [config.localOnly=false] The update only takes into account
14 * local notifications and ignores the number of cross-wiki notifications.
15 * @param {string} [config.source='local'] The source for this counter. Specifically important if the counter
16 * is set to be counting only local notifications
17 */
18 mw.echo.dm.UnreadNotificationCounter = function mwEchoDmUnreadNotificationCounter( api, type, max, config ) {
19 config = config || {};
20
21 // Mixin constructor
22 OO.EventEmitter.call( this );
23
24 this.api = api;
25 this.type = type;
26 this.max = max;
27 this.prioritizer = new mw.echo.api.PromisePrioritizer();
28
29 this.count = 0;
30 this.localOnly = config.localOnly === undefined ? false : !!config.localOnly;
31 this.source = config.source || 'local';
32 };
33
34 /* Inheritance */
35
36 OO.mixinClass( mw.echo.dm.UnreadNotificationCounter, OO.EventEmitter );
37
38 /* Events */
39
40 /**
41 * The number of unread notification represented by this counter has changed.
42 *
43 * @event mw.echo.dm.UnreadNotificationCounter#countChange
44 * @param {number} count Notification count
45 */
46
47 /* Methods */
48
49 /**
50 * Normalizes for a capped count in case the requested count
51 * is higher than the cap.
52 *
53 * This is the client-side version of
54 * NotificationController::getCappedNotificationCount.
55 *
56 * @param {number} count Count before cap is applied
57 * @return {number} Count with cap applied
58 */
59 mw.echo.dm.UnreadNotificationCounter.prototype.getCappedNotificationCount = function ( count ) {
60 if ( count < 0 ) {
61 return 0;
62 } else if ( count <= this.max ) {
63 return count;
64 } else {
65 return this.max + 1;
66 }
67 };
68
69 /**
70 * Get the current count
71 *
72 * @return {number} current count
73 */
74 mw.echo.dm.UnreadNotificationCounter.prototype.getCount = function () {
75 return this.count;
76 };
77
78 /**
79 * Set the current count
80 *
81 * @param {number} count
82 * @param {boolean} isEstimation Whether this number is estimated or accurate
83 * @fires mw.echo.dm.UnreadNotificationCounter#countChange
84 */
85 mw.echo.dm.UnreadNotificationCounter.prototype.setCount = function ( count, isEstimation ) {
86 if ( isEstimation ) {
87 if ( this.count > this.max ) {
88 // this prevents toggling between 90-ish and 99+
89 return;
90 }
91 if ( count < 0 ) {
92 // wrong estimation?
93 return;
94 }
95 }
96
97 // Normalize
98 count = this.getCappedNotificationCount( count );
99
100 if ( count !== this.count ) {
101 this.count = count;
102 this.emit( 'countChange', this.count );
103 }
104 };
105
106 /**
107 * Report an estimated change to this counter
108 *
109 * @param {number} delta
110 */
111 mw.echo.dm.UnreadNotificationCounter.prototype.estimateChange = function ( delta ) {
112 this.setCount( this.count + delta, true );
113 };
114
115 /**
116 * Request that this counter update itself from the API
117 *
118 * @return {jQuery.Promise} Promise that is resolved when the actual unread
119 * count is fetched, with the actual unread notification count.
120 */
121 mw.echo.dm.UnreadNotificationCounter.prototype.update = function () {
122 if ( !this.api ) {
123 return $.Deferred().reject();
124 }
125
126 return this.prioritizer.prioritize( this.api.fetchUnreadCount(
127 this.source,
128 this.type,
129 this.localOnly
130 ) ).then( ( actualCount ) => {
131 this.setCount( actualCount, false );
132
133 return actualCount;
134 } );
135 };
136
137 /**
138 * Set the source for this counter
139 *
140 * @param {string} source Source name
141 */
142 mw.echo.dm.UnreadNotificationCounter.prototype.setSource = function ( source ) {
143 this.source = source;
144 };
145
146 }() );