File tree Expand file tree Collapse file tree 3 files changed +41
-7
lines changed
packages-exp/auth-exp/src
platform_browser/messagechannel Expand file tree Collapse file tree 3 files changed +41
-7
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * @license
3
+ * Copyright 2021 Google LLC
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+
18
+ import { expect } from 'chai' ;
19
+ import { _generateEventId } from './event_id' ;
20
+
21
+ describe ( 'core/util/event_id' , ( ) => {
22
+ it ( 'sub-15 digit id' , ( ) => {
23
+ expect ( _generateEventId ( '' , 10 ) ) . to . have . length ( 10 ) ;
24
+ } ) ;
25
+
26
+ it ( '15 digit id' , ( ) => {
27
+ expect ( _generateEventId ( '' , 15 ) ) . to . have . length ( 15 ) ;
28
+ } ) ;
29
+
30
+ it ( 'above-15 digit id' , ( ) => {
31
+ expect ( _generateEventId ( '' , 20 ) ) . to . have . length ( 20 ) ;
32
+ } ) ;
33
+ } ) ;
Original file line number Diff line number Diff line change 15
15
* limitations under the License.
16
16
*/
17
17
18
- export function _generateEventId ( prefix ?: string ) : string {
19
- return `${ prefix ? prefix : '' } ${ Math . floor ( Math . random ( ) * 1000000000 ) } ` ;
18
+ export function _generateEventId ( prefix = '' , digits = 10 ) : string {
19
+ let random = '' ;
20
+ for ( let i = 0 ; i < digits ; i ++ ) {
21
+ random += Math . floor ( Math . random ( ) * 10 ) ;
22
+ }
23
+ return prefix + random ;
20
24
}
Original file line number Diff line number Diff line change 15
15
* limitations under the License.
16
16
*/
17
17
18
+ import { _generateEventId } from '../../core/util/event_id' ;
18
19
import {
19
20
_SenderRequest ,
20
21
_EventType ,
@@ -32,10 +33,6 @@ interface MessageHandler {
32
33
onMessage : EventListenerOrEventListenerObject ;
33
34
}
34
35
35
- function generateEventId ( prefix = '' , digits = 20 ) : string {
36
- return `${ prefix } ${ Math . floor ( Math . random ( ) * Math . pow ( 10 , digits ) ) } ` ;
37
- }
38
-
39
36
/**
40
37
* Interface for sending messages and waiting for a completion response.
41
38
*
@@ -91,7 +88,7 @@ export class Sender {
91
88
let completionTimer : any ;
92
89
let handler : MessageHandler ;
93
90
return new Promise < _ReceiverMessageResponse < T > > ( ( resolve , reject ) => {
94
- const eventId = generateEventId ( ) ;
91
+ const eventId = _generateEventId ( '' , 20 ) ;
95
92
messageChannel . port1 . start ( ) ;
96
93
const ackTimer = setTimeout ( ( ) => {
97
94
reject ( new Error ( _MessageError . UNSUPPORTED_EVENT ) ) ;
You can’t perform that action at this time.
0 commit comments