File tree Expand file tree Collapse file tree 3 files changed +38
-1
lines changed
modules/credentialsPolicy Expand file tree Collapse file tree 3 files changed +38
-1
lines changed Original file line number Diff line number Diff line change 2121import { USER_INTERACTION } from 'shared/modules/userInteraction/userInteractionDuck'
2222import { credentialsTimeout } from 'shared/modules/dbMeta/dbMetaDuck'
2323import { disconnectAction , getActiveConnection } from 'shared/modules/connections/connectionsDuck'
24+ import { parseTimeMillis } from 'services/utils'
2425
2526// Local variables (used in epics)
2627let timer = null
@@ -29,7 +30,7 @@ let timer = null
2930export const credentialsTimeoutEpic = ( action$ , store ) =>
3031 action$ . ofType ( USER_INTERACTION )
3132 . do ( ( action ) => {
32- const cTimeout = credentialsTimeout ( store . getState ( ) )
33+ const cTimeout = parseTimeMillis ( credentialsTimeout ( store . getState ( ) ) )
3334 if ( ! cTimeout ) return clearTimeout ( timer )
3435 clearTimeout ( timer )
3536 timer = setTimeout ( ( ) => {
Original file line number Diff line number Diff line change @@ -179,6 +179,25 @@ export const canUseDOM = () => !!(
179179
180180export const ecsapeCypherMetaItem = ( str ) => / ^ [ A - Z a - z ] [ A - Z a - z 0 - 9 _ ] * $ / . test ( str ) ? str : '`' + str . replace ( / ` / g, '``' ) + '`'
181181
182+ export const parseTimeMillis = ( timeWithOrWithoutUnit ) => {
183+ timeWithOrWithoutUnit += '' // cast to string
184+ const readUnit = timeWithOrWithoutUnit . match ( / \D + / )
185+ const value = parseInt ( timeWithOrWithoutUnit )
186+
187+ const unit = ( readUnit === undefined || readUnit === null ) ? 's' : readUnit [ 0 ] // Assume seconds
188+
189+ switch ( unit ) {
190+ case 'ms' :
191+ return value
192+ case 's' :
193+ return value * 1000
194+ case 'm' :
195+ return value * 60 * 1000
196+ default :
197+ return 0
198+ }
199+ }
200+
182201export const stringifyMod = ( ) => {
183202 const toString = Object . prototype . toString
184203 const isArray = Array . isArray || function ( a ) { return toString . call ( a ) === '[object Array]' }
Original file line number Diff line number Diff line change @@ -317,6 +317,23 @@ describe('utils', () => {
317317 expect ( utils . stringifyMod ( ) ( t , modFn ) ) . toEqual ( expects [ index ] )
318318 } )
319319 } )
320+ test ( 'parseTimeMillis correctly parses human readable units correctly' , ( ) => {
321+ // Given
322+ const times = [
323+ { test : '100' , expect : 100 * 1000 } ,
324+ { test : 100 , expect : 100 * 1000 } ,
325+ { test : '100ms' , expect : 100 } ,
326+ { test : '100s' , expect : 100 * 1000 } ,
327+ { test : '100m' , expect : 100 * 60 * 1000 } ,
328+ { test : '100x' , expect : 0 } ,
329+ { test : 'x' , expect : 0 }
330+ ]
331+
332+ // When & Then
333+ times . forEach ( ( time ) => {
334+ expect ( utils . parseTimeMillis ( time . test ) ) . toEqual ( time . expect )
335+ } )
336+ } )
320337 describe ( 'ecsapeCypherMetaItem' , ( ) => {
321338 // Given
322339 const items = [
You can’t perform that action at this time.
0 commit comments