11import  {  collections  }  from  '../storage' ; 
2- import  {   config   }  from  './cache' ; 
2+ import  *   as   cache  from  './cache' ; 
33import  {  store  }  from  '../storage' ; 
44import  {  tabs  as  tabUtils ,  logger  }  from  '../utils' ; 
55
@@ -56,14 +56,14 @@ export const initialize = async () => {
5656
5757    // Initial scheduling 
5858    Object . entries ( timestamps ) . forEach ( ( [ tabId ,  timestamp ] )  =>  { 
59-         if  ( tabUtils . shouldWhitelist ( tabs . find ( ( {  id } )  =>  id  ===  + tabId ) ! ,  config . latest ! ) )  { 
59+         if  ( tabUtils . shouldWhitelist ( tabs . find ( ( {  id } )  =>  id  ===  + tabId ) ! ,  cache . config . latest ! ,   cache . tabPageStates . latest ) )  { 
6060            tabWhitelist . add ( + tabId ) ; 
6161            return ; 
6262        } 
6363
6464        if  ( tabs . find ( ( {  id } )  =>  id  ===  + tabId ) ?. discarded )  return ; 
6565
66-         scheduler . schedule ( tabId ,  timestamp ,  config . latest ! . discardTabsAfterMilliseconds ) ; 
66+         scheduler . schedule ( tabId ,  timestamp ,  cache . config . latest ! . discardTabsAfterMilliseconds ) ; 
6767    } ) ; 
6868
6969    log . debug ( 'Initialized' ) ; 
@@ -78,7 +78,7 @@ export const initialize = async () => {
7878            return ; 
7979        } 
8080
81-         scheduler . schedule ( tabId ,  timestamp ,  config . latest ! . discardTabsAfterMilliseconds ) ; 
81+         scheduler . schedule ( tabId ,  timestamp ,  cache . config . latest ! . discardTabsAfterMilliseconds ) ; 
8282    } ; 
8383
8484    collections . tabDeactivationTimestamps . onChange ( handleTabActivationChange ) ; 
@@ -90,7 +90,7 @@ export const initialize = async () => {
9090        const  tab  =  await  chrome . tabs . get ( tabId ) ; 
9191
9292        // If a change was made to the tab that means it should now be whitelisted, add to the whitelist 
93-         if  ( tabUtils . shouldWhitelist ( tab ,  config . latest ! ) )  { 
93+         if  ( tabUtils . shouldWhitelist ( tab ,  cache . config . latest ! ,   cache . tabPageStates . latest ) )  { 
9494            scheduler . unschedule ( tabId ) ; 
9595            tabWhitelist . add ( tabId ) ; 
9696            return ; 
@@ -99,12 +99,39 @@ export const initialize = async () => {
9999        // If the tab was previously marked as should never discard, but that's now changed, 
100100        // schedule the discard 
101101        if  ( tabWhitelist . has ( tabId ) )  { 
102+             // Remove whitelist status 
103+             tabWhitelist . delete ( tabId ) ; 
104+ 
102105            const  timestamp  =  await  collections . tabDeactivationTimestamps . get ( tabId ) ; 
103106
104-             scheduler . schedule ( tabId ,  timestamp ,  config . latest ! . discardTabsAfterMilliseconds ) ; 
107+             scheduler . schedule ( tabId ,  timestamp ,  cache . config . latest ! . discardTabsAfterMilliseconds ) ; 
108+         } 
109+     } ) ; 
110+ 
111+     cache . tabPageStates . onChange ( async  ( tabId ,  state ,  latest )  =>  { 
112+         // Tab should be whitelisted if it's recording audio/video 
113+         if  ( state  &&  ( state . audio  ||  state . video ) )  { 
114+             scheduler . unschedule ( tabId ) ; 
115+             tabWhitelist . add ( + tabId ) ; 
116+             return ; 
105117        } 
106118
107-         tabWhitelist . delete ( tabId ) ; 
119+         // Otherwise, shouldn't be in whitelist 
120+ 
121+         // If it is in the whitelist already though 
122+         if  ( tabWhitelist . has ( + tabId ) )  { 
123+             const  tab  =  await  chrome . tabs . get ( + tabId ) ; 
124+ 
125+             // Check if other conditions say the tab should remain whitelisted, don't remove it 
126+             if  ( tabUtils . shouldWhitelist ( tab ,  cache . config . latest ! ,  latest ) )  return ; 
127+ 
128+             // Otherwise, remove it 
129+             tabWhitelist . delete ( + tabId ) ; 
130+ 
131+             const  timestamp  =  await  collections . tabDeactivationTimestamps . get ( tabId ) ; 
132+ 
133+             scheduler . schedule ( tabId ,  timestamp ,  cache . config . latest ! . discardTabsAfterMilliseconds ) ; 
134+         } 
108135    } ) ; 
109136
110137    store . config . onChange ( async  ( latest ,  previous )  =>  { 
@@ -123,7 +150,7 @@ export const initialize = async () => {
123150                    if  ( tab . discarded  ||  ! tab . id )  return ; 
124151
125152                    const  tabAlreadyInWhitelist  =  tabWhitelist . has ( tab . id ) ; 
126-                     const  shouldAddToWhitelist  =  tabUtils . shouldWhitelist ( tab ,  latest ) ; 
153+                     const  shouldAddToWhitelist  =  tabUtils . shouldWhitelist ( tab ,  latest ,   cache . tabPageStates . latest ) ; 
127154
128155                    // If the tab should be added to the whitelist, but is already in it, do nothing 
129156                    // Improves performance by not iterating through every tab each time 
0 commit comments