mediawiki-extensions-Popups/resources/ext.popups/changeListener.js
Sam Smith 0d300867ab Make API request after 500 ms
If the user has abandoned the link or dwelled on a different link, then
don't make the API request.

Changes:
* Fix a bug in the linkDwell reducer where the activeLink was always
  being set to undefined.
* Add the private fetch action creator.
* Make the linkDwell action dispatch the result of the fetch action
  creator after 500ms.

Supporting changes:
* Make the link* action creators take DOM elements rather than jQuery
  instances so that:
  1. Testing whether the state tree has changed is an identity
     comparison.
  2. jQuery instances are constructed only when required.
* Document the ext.popups.Gateway type.
* Document the Redux.Store type.
* Rename the "previews-page-title" data attribute to
  "page-previews-title" to avoid confusion.

Change-Id: I0b1cf3337a6f8d6450ad2bd127cb292ebb73af4f
2016-11-17 13:20:11 +00:00

43 lines
1.2 KiB
JavaScript

( function ( mw ) {
/**
* @typedef {Function} ext.popups.ChangeListener
* @param {Object} prevState The previous state
* @param {Object} state The current state
*/
/**
* Registers a change listener, which is bound to the
* [store](http://redux.js.org/docs/api/Store.html).
*
* A change listener is a function that is only invoked when the state in the
* [store](http://redux.js.org/docs/api/Store.html) changes. N.B. that there
* may not be a 1:1 correspondence with actions being dispatched to the store
* and the state in the store changing.
*
* See [Store#subscribe](http://redux.js.org/docs/api/Store.html#subscribe)
* for more information about what change listeners may and may not do.
*
* @param {Redux.Store} store
* @param {ext.popups.ChangeListener} callback
*/
mw.popups.registerChangeListener = function ( store, callback ) {
// This function is based on the example in [the documentation for
// Store#subscribe](http://redux.js.org/docs/api/Store.html#subscribe),
// which was written by Dan Abramov.
var state;
store.subscribe( function () {
var prevState = state;
state = store.getState();
if ( prevState !== state ) {
callback( prevState, state );
}
} );
};
}( mediaWiki ) );