File size: 931 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import debugFactory from 'debug';
import { NOTIFICATIONS_PANEL_TOGGLE } from '../../state/action-types';
import { NOTIFY_DESKTOP_NOTIFICATIONS_UNSEEN_COUNT_RESET } from '../../state/desktop/window-events';

/**
 * Module variables
 */
const debug = debugFactory( 'desktop:middleware' );

/**
 * WP Desktop Middleware
 *
 * WP Desktop cannot subscribe to redux actions directly, so this middleware is
 * used to relay redux actions to the Electron renderer process via window events.
 */
export const desktopMiddleware = () => {
	return ( next ) => ( action ) => {
		switch ( action.type ) {
			case NOTIFICATIONS_PANEL_TOGGLE: {
				debug( 'Dispatching window event for action type: ', action.type );
				window.dispatchEvent(
					new window.CustomEvent( NOTIFY_DESKTOP_NOTIFICATIONS_UNSEEN_COUNT_RESET )
				);
				return next( action );
			}

			default:
				return next( action );
		}
	};
};

export default desktopMiddleware;