@@ -4,10 +4,33 @@ import type {
44} from '@reduxjs/toolkit'
55import { createAction } from './rtkImports'
66
7- export const onFocus = /* @__PURE__ */ createAction ( '__rtkq/focused' )
8- export const onFocusLost = /* @__PURE__ */ createAction ( '__rtkq/unfocused' )
9- export const onOnline = /* @__PURE__ */ createAction ( '__rtkq/online' )
10- export const onOffline = /* @__PURE__ */ createAction ( '__rtkq/offline' )
7+ export const INTERNAL_PREFIX = '__rtkq/'
8+
9+ const ONLINE = 'online'
10+ const OFFLINE = 'offline'
11+ const FOCUS = 'focus'
12+ const FOCUSED = 'focused'
13+ const VISIBILITYCHANGE = 'visibilitychange'
14+
15+ export const onFocus = /* @__PURE__ */ createAction (
16+ `${ INTERNAL_PREFIX } ${ FOCUSED } ` ,
17+ )
18+ export const onFocusLost = /* @__PURE__ */ createAction (
19+ `${ INTERNAL_PREFIX } un${ FOCUSED } ` ,
20+ )
21+ export const onOnline = /* @__PURE__ */ createAction (
22+ `${ INTERNAL_PREFIX } ${ ONLINE } ` ,
23+ )
24+ export const onOffline = /* @__PURE__ */ createAction (
25+ `${ INTERNAL_PREFIX } ${ OFFLINE } ` ,
26+ )
27+
28+ const actions = {
29+ onFocus,
30+ onFocusLost,
31+ onOnline,
32+ onOffline,
33+ }
1134
1235let initialized = false
1336
@@ -40,10 +63,13 @@ export function setupListeners(
4063 ) => ( ) => void ,
4164) {
4265 function defaultHandler ( ) {
43- const handleFocus = ( ) => dispatch ( onFocus ( ) )
44- const handleFocusLost = ( ) => dispatch ( onFocusLost ( ) )
45- const handleOnline = ( ) => dispatch ( onOnline ( ) )
46- const handleOffline = ( ) => dispatch ( onOffline ( ) )
66+ const [ handleFocus , handleFocusLost , handleOnline , handleOffline ] = [
67+ onFocus ,
68+ onFocusLost ,
69+ onOnline ,
70+ onOffline ,
71+ ] . map ( ( action ) => ( ) => dispatch ( action ( ) ) )
72+
4773 const handleVisibilityChange = ( ) => {
4874 if ( window . document . visibilityState === 'visible' ) {
4975 handleFocus ( )
@@ -52,33 +78,42 @@ export function setupListeners(
5278 }
5379 }
5480
81+ let unsubscribe = ( ) => {
82+ initialized = false
83+ }
84+
5585 if ( ! initialized ) {
56- if ( typeof window !== 'undefined' && window . addEventListener ) {
57- // Handle focus events
58- window . addEventListener (
59- 'visibilitychange' ,
60- handleVisibilityChange ,
61- false ,
62- )
63- window . addEventListener ( 'focus' , handleFocus , false )
86+ const WINDOW = window
87+ if ( typeof WINDOW !== 'undefined' && ! ! WINDOW . addEventListener ) {
88+ const handlers = {
89+ [ FOCUS ] : handleFocus ,
90+ [ VISIBILITYCHANGE ] : handleVisibilityChange ,
91+ [ ONLINE ] : handleOnline ,
92+ [ OFFLINE ] : handleOffline ,
93+ }
6494
65- // Handle connection events
66- window . addEventListener ( 'online' , handleOnline , false )
67- window . addEventListener ( 'offline' , handleOffline , false )
95+ function updateListeners ( add : boolean ) {
96+ Object . entries ( handlers ) . forEach ( ( [ event , handler ] ) => {
97+ if ( add ) {
98+ WINDOW . addEventListener ( event , handler , false )
99+ } else {
100+ WINDOW . removeEventListener ( event , handler )
101+ }
102+ } )
103+ }
104+ // Handle focus events
105+ updateListeners ( true )
68106 initialized = true
107+
108+ unsubscribe = ( ) => {
109+ updateListeners ( false )
110+ initialized = false
111+ }
69112 }
70113 }
71- const unsubscribe = ( ) => {
72- window . removeEventListener ( 'focus' , handleFocus )
73- window . removeEventListener ( 'visibilitychange' , handleVisibilityChange )
74- window . removeEventListener ( 'online' , handleOnline )
75- window . removeEventListener ( 'offline' , handleOffline )
76- initialized = false
77- }
114+
78115 return unsubscribe
79116 }
80117
81- return customHandler
82- ? customHandler ( dispatch , { onFocus, onFocusLost, onOffline, onOnline } )
83- : defaultHandler ( )
118+ return customHandler ? customHandler ( dispatch , actions ) : defaultHandler ( )
84119}
0 commit comments