1
- /*
2
- * Copyright 2020 Leanplum Inc. All rights reserved.
3
- *
4
- * Licensed under the Apache License, Version 2.0 (the "License");
5
- * you may not use this file except in compliance with the License.
6
- * You may obtain a copy of the License at:
7
- *
8
- * https://www.apache.org/licenses/LICENSE-2.0
9
- *
10
- * Unless required by applicable law or agreed to in writing, software
11
- * distributed under the License is distributed on an "AS IS" BASIS,
12
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- * See the License for the specific language governing permissions and
14
- * limitations under the License.
15
- */
16
-
17
1
import ArgsBuilder from './ArgsBuilder'
18
2
import BrowserDetector from './BrowserDetector'
19
3
import Constants from './Constants'
@@ -25,6 +9,7 @@ import StorageManager from './StorageManager'
25
9
import PushManager from './PushManager'
26
10
import Messages from './Messages'
27
11
import EventEmitter from './EventEmitter'
12
+ import MigrationManager from './MigrationManager'
28
13
import {
29
14
Action ,
30
15
EventType ,
@@ -36,6 +21,7 @@ import {
36
21
WebPushOptions ,
37
22
UserAttributes ,
38
23
} from './types/public'
24
+ import { BatchResponse , MigrationState } from './types/internal'
39
25
import VarCache from './VarCache'
40
26
41
27
/* eslint-disable @typescript-eslint/ban-types */
@@ -44,6 +30,9 @@ import VarCache from './VarCache'
44
30
const SESSION_KEY = Constants . DEFAULT_KEYS . SESSION
45
31
46
32
export default class LeanplumInternal {
33
+ private _migration = new MigrationManager (
34
+ this . createRequest . bind ( this )
35
+ )
47
36
private _events : EventEmitter = new EventEmitter ( ) ;
48
37
private _browserDetector : BrowserDetector
49
38
private _internalState : InternalState = new InternalState ( )
@@ -59,7 +48,10 @@ export default class LeanplumInternal {
59
48
this . _lpRequest . getLastResponse . bind ( this . _lpRequest ) ,
60
49
this . _events
61
50
)
62
- private _pushManager : PushManager = new PushManager ( this . createRequest . bind ( this ) )
51
+ private _pushManager : PushManager = new PushManager (
52
+ this . _events ,
53
+ this . createRequest . bind ( this )
54
+ )
63
55
private _webPushOptions : WebPushOptions
64
56
private _messages : Messages = new Messages (
65
57
this . _events ,
@@ -74,6 +66,8 @@ export default class LeanplumInternal {
74
66
private _systemVersion : string
75
67
private _sessionLength : number
76
68
69
+ private _ct : any
70
+
77
71
constructor ( private wnd : Window ) {
78
72
this . _browserDetector = new BrowserDetector ( wnd )
79
73
this . _events . on ( 'navigationChange' , ( url : string ) => {
@@ -90,6 +84,18 @@ export default class LeanplumInternal {
90
84
this . _events . on ( 'registerForPush' , ( ) => this . registerForWebPush ( ) )
91
85
this . _events . on ( 'updateDevServerHost' ,
92
86
( host : string ) => this . setSocketHost ( host ) )
87
+ this . _events . on ( 'migrateStateReceived' ,
88
+ ( sha : string ) => this . _migration . verifyState ( sha ) )
89
+
90
+ this . _events . on ( 'webPushSubscribed' ,
91
+ ( ) => {
92
+ this . _ct && this . _ct . notifications . push ( {
93
+ titleText : '' ,
94
+ bodyText : '' ,
95
+ okButtonText : '' ,
96
+ rejectButtonText : '' ,
97
+ } )
98
+ } )
93
99
}
94
100
95
101
setApiPath ( apiPath : string ) : void {
@@ -245,7 +251,7 @@ export default class LeanplumInternal {
245
251
this . createRequest ( Constants . METHODS . GET_VARS , args , {
246
252
queued : false ,
247
253
sendNow : true ,
248
- response : ( response : Array < Object > ) => {
254
+ response : ( response : BatchResponse ) => {
249
255
const getVarsResponse = this . _lpRequest . getLastResponse ( response )
250
256
const isSuccess = this . _lpRequest . isResponseSuccess ( getVarsResponse )
251
257
if ( isSuccess ) {
@@ -290,79 +296,102 @@ export default class LeanplumInternal {
290
296
return this . startFromCache ( userId , userAttributes , callback )
291
297
}
292
298
293
- this . _lpRequest . userId = userId
294
-
295
- if ( callback ) {
296
- this . addStartResponseHandler ( callback )
297
- }
298
-
299
- this . _varCache . onUpdate = ( ) => {
300
- this . _varCache . triggerVariablesChangedHandlers ( )
301
- }
299
+ this . _migration . getState ( ( state : MigrationState ) => {
300
+ if ( state === MigrationState . DUPLICATE ) {
301
+ this . _ct = this . _migration . initCleverTap ( )
302
302
303
- const args = new ArgsBuilder ( )
304
- . add ( Constants . PARAMS . USER_ATTRIBUTES , JSON . stringify ( userAttributes ) )
305
- . add ( Constants . PARAMS . COUNTRY , Constants . VALUES . DETECT )
306
- . add ( Constants . PARAMS . REGION , Constants . VALUES . DETECT )
307
- . add ( Constants . PARAMS . CITY , Constants . VALUES . DETECT )
308
- . add ( Constants . PARAMS . LOCATION , Constants . VALUES . DETECT )
309
- . add ( Constants . PARAMS . SYSTEM_NAME , this . _systemName || this . _browserDetector . OS )
310
- . add ( Constants . PARAMS . SYSTEM_VERSION , ( this . _systemVersion || '' ) . toString ( ) )
311
- . add ( Constants . PARAMS . BROWSER_NAME , this . _browserDetector . browser )
312
- . add ( Constants . PARAMS . BROWSER_VERSION , this . _browserDetector . version . toString ( ) )
313
- . add ( Constants . PARAMS . LOCALE , this . _locale || Constants . VALUES . DETECT )
314
- . add ( Constants . PARAMS . DEVICE_NAME , this . _deviceName ||
315
- ` ${ this . _browserDetector . browser } ${ this . _browserDetector . version } ` )
316
- . add ( Constants . PARAMS . DEVICE_MODEL , this . _deviceModel || 'Web Browser' )
317
- . add ( Constants . PARAMS . NEWSFEED_MESSAGES , this . _lpInbox . messageIds ( ) )
318
- . add ( Constants . PARAMS . INCLUDE_DEFAULTS , false )
319
- . add ( Constants . PARAMS . INCLUDE_VARIANT_DEBUG_INFO , this . _internalState . variantDebugInfoEnabled )
303
+ // silently register subscription in CT
304
+ this . isWebPushSubscribed ( ) . then ( ( isSubscribed ) => {
305
+ if ( isSubscribed ) {
306
+ this . _events . emit ( 'webPushSubscribed' )
307
+ }
308
+ } )
309
+ } else if ( state === MigrationState . CLEVERTAP ) {
310
+ this . _ct = this . _migration . initCleverTap ( )
311
+
312
+ Object . values ( Constants . DEFAULT_KEYS )
313
+ . filter ( key => ! [
314
+ Constants . DEFAULT_KEYS . USER_ID ,
315
+ Constants . DEFAULT_KEYS . DEVICE_ID ,
316
+ Constants . DEFAULT_KEYS . TOKEN ,
317
+ ] . includes ( key ) )
318
+ . forEach ( key => StorageManager . remove ( key ) )
319
+ }
320
320
321
- this . createRequest ( Constants . METHODS . START , args , {
322
- queued : true ,
323
- sendNow : true ,
324
- response : ( response : Array < Object > ) => {
325
- this . _internalState . hasStarted = true
326
- const startResponse = this . _lpRequest . getLastResponse ( response )
327
- const isSuccess = this . _lpRequest . isResponseSuccess ( startResponse )
328
- this . _internalState . startSuccessful = isSuccess
321
+ this . _lpRequest . userId = userId
329
322
330
- if ( isSuccess ) {
323
+ if ( callback ) {
324
+ this . addStartResponseHandler ( callback )
325
+ }
331
326
332
- this . updateSession ( )
327
+ this . _varCache . onUpdate = ( ) => {
328
+ this . _varCache . triggerVariablesChangedHandlers ( )
329
+ }
333
330
334
- const messages = startResponse [ Constants . KEYS . MESSAGES ]
335
- if ( startResponse . actionDefinitions ) {
336
- messages . actionDefinitions = startResponse . actionDefinitions
337
- }
338
- this . _events . emit ( 'messagesReceived' , messages )
331
+ const args = new ArgsBuilder ( )
332
+ . add ( Constants . PARAMS . USER_ATTRIBUTES , JSON . stringify ( userAttributes ) )
333
+ . add ( Constants . PARAMS . COUNTRY , Constants . VALUES . DETECT )
334
+ . add ( Constants . PARAMS . REGION , Constants . VALUES . DETECT )
335
+ . add ( Constants . PARAMS . CITY , Constants . VALUES . DETECT )
336
+ . add ( Constants . PARAMS . LOCATION , Constants . VALUES . DETECT )
337
+ . add ( Constants . PARAMS . SYSTEM_NAME , this . _systemName || this . _browserDetector . OS )
338
+ . add ( Constants . PARAMS . SYSTEM_VERSION , ( this . _systemVersion || '' ) . toString ( ) )
339
+ . add ( Constants . PARAMS . BROWSER_NAME , this . _browserDetector . browser )
340
+ . add ( Constants . PARAMS . BROWSER_VERSION , this . _browserDetector . version . toString ( ) )
341
+ . add ( Constants . PARAMS . LOCALE , this . _locale || Constants . VALUES . DETECT )
342
+ . add ( Constants . PARAMS . DEVICE_NAME , this . _deviceName ||
343
+ `${ this . _browserDetector . browser } ${ this . _browserDetector . version } ` )
344
+ . add ( Constants . PARAMS . DEVICE_MODEL , this . _deviceModel || 'Web Browser' )
345
+ . add ( Constants . PARAMS . NEWSFEED_MESSAGES , this . _lpInbox . messageIds ( ) )
346
+ . add ( Constants . PARAMS . INCLUDE_DEFAULTS , false )
347
+ . add ( Constants . PARAMS . INCLUDE_VARIANT_DEBUG_INFO , this . _internalState . variantDebugInfoEnabled )
348
+
349
+ this . createRequest ( Constants . METHODS . START , args , {
350
+ queued : true ,
351
+ sendNow : true ,
352
+ response : ( response : BatchResponse ) => {
353
+ this . _internalState . hasStarted = true
354
+ const startResponse = this . _lpRequest . getLastResponse ( response )
355
+ const isSuccess = this . _lpRequest . isResponseSuccess ( startResponse )
356
+ this . _internalState . startSuccessful = isSuccess
357
+
358
+ if ( isSuccess ) {
359
+
360
+ this . updateSession ( )
361
+
362
+ const messages = startResponse [ Constants . KEYS . MESSAGES ]
363
+ if ( startResponse . actionDefinitions ) {
364
+ messages . actionDefinitions = startResponse . actionDefinitions
365
+ }
366
+ this . _events . emit ( 'messagesReceived' , messages )
339
367
340
- if ( startResponse [ Constants . KEYS . SYNC_INBOX ] ) {
341
- this . _lpInbox . downloadMessages ( )
342
- }
368
+ if ( startResponse [ Constants . KEYS . SYNC_INBOX ] ) {
369
+ this . _lpInbox . downloadMessages ( )
370
+ }
343
371
344
- if ( this . _internalState . devMode ) {
345
- const latestVersion = startResponse [ Constants . KEYS . LATEST_VERSION ]
346
- if ( latestVersion ) {
347
- console . log ( `A newer version of the Leanplum SDK, ${ latestVersion } , is available.
348
- Use "npm update leanplum-sdk" or go to https://docs.leanplum.com/reference#javascript-setup to download it.` )
372
+ if ( this . _internalState . devMode ) {
373
+ const latestVersion = startResponse [ Constants . KEYS . LATEST_VERSION ]
374
+ if ( latestVersion ) {
375
+ console . log ( `A newer version of the Leanplum SDK, ${ latestVersion } , is available.
376
+ Use "npm update leanplum-sdk" or go to https://docs.leanplum.com/reference#javascript-setup to download it.` )
377
+ }
378
+ this . connectSocket ( )
349
379
}
350
- this . connectSocket ( )
351
- }
352
380
353
- this . _varCache . applyDiffs (
354
- startResponse [ Constants . KEYS . VARS ] ,
355
- startResponse [ Constants . KEYS . VARIANTS ] ,
356
- startResponse [ Constants . KEYS . ACTION_DEFINITIONS ] )
357
- this . _varCache . setVariantDebugInfo ( startResponse [ Constants . KEYS . VARIANT_DEBUG_INFO ] )
358
- this . _varCache . token = startResponse [ Constants . KEYS . TOKEN ]
359
- } else {
360
- this . _varCache . loadDiffs ( )
361
- }
381
+ this . _varCache . applyDiffs (
382
+ startResponse [ Constants . KEYS . VARS ] ,
383
+ startResponse [ Constants . KEYS . VARIANTS ] ,
384
+ startResponse [ Constants . KEYS . ACTION_DEFINITIONS ] )
385
+ this . _varCache . setVariantDebugInfo ( startResponse [ Constants . KEYS . VARIANT_DEBUG_INFO ] )
386
+ this . _varCache . token = startResponse [ Constants . KEYS . TOKEN ]
387
+ } else {
388
+ this . _varCache . loadDiffs ( )
389
+ }
362
390
363
- this . _events . emit ( 'start' , { success : isSuccess } )
364
- this . _internalState . triggerStartHandlers ( )
365
- } ,
391
+ this . _events . emit ( 'start' , { success : isSuccess } )
392
+ this . _internalState . triggerStartHandlers ( )
393
+ } ,
394
+ } )
366
395
} )
367
396
}
368
397
@@ -524,6 +553,7 @@ Use "npm update leanplum-sdk" or go to https://docs.leanplum.com/reference#javas
524
553
525
554
this . createRequest ( Constants . METHODS . TRACK , args , {
526
555
queued : true ,
556
+ isPurchase : true ,
527
557
} )
528
558
}
529
559
@@ -618,10 +648,15 @@ Use "npm update leanplum-sdk" or go to https://docs.leanplum.com/reference#javas
618
648
}
619
649
620
650
private createRequest ( action : string , args : ArgsBuilder , options : any = { } ) : void {
621
- this . _lpRequest . request ( action , args , {
622
- devMode : this . _internalState . devMode ,
623
- ...options ,
624
- } )
651
+
652
+ const suppress = this . _migration . duplicateRequest ( action , args , options )
653
+
654
+ if ( ! suppress ) {
655
+ this . _lpRequest . request ( action , args , {
656
+ devMode : this . _internalState . devMode ,
657
+ ...options ,
658
+ } )
659
+ }
625
660
}
626
661
627
662
private connectSocket ( ) : void {
0 commit comments