1
1
import Polyfill from './polyfill' ;
2
- import Types from './types' ;
2
+ import Types , { CommerceEventType , EventType , ProductActionType , PromotionActionType } from './types' ;
3
3
import Constants from './constants' ;
4
- import mParticleInstance from './mp-instance.js' ;
4
+ import mParticleInstance , { IMParticleWebSDKInstance } from './mp-instance.js' ;
5
5
import _BatchValidator from './mockBatchCreator' ;
6
6
import MPSideloadedKit from './sideloadedKit' ;
7
+ import { IMParticleInstanceManager } from './sdkRuntimeModels' ;
8
+ import { IStore } from './store' ;
9
+ import { Dictionary } from '@mparticle/web-sdk' ;
10
+ import { valueof } from './utils' ;
7
11
8
12
if ( ! Array . prototype . forEach ) {
9
13
Array . prototype . forEach = Polyfill . forEach ;
@@ -17,20 +21,23 @@ if (!Array.prototype.filter) {
17
21
Array . prototype . filter = Polyfill . filter ;
18
22
}
19
23
24
+ // https://go.mparticle.com/work/SQDSDKS-6768
20
25
if ( ! Array . isArray ) {
26
+ // @ts -ignore
21
27
Array . prototype . isArray = Polyfill . isArray ;
22
28
}
23
29
24
- function mParticle ( ) {
25
- var self = this ;
30
+ function mParticleInstanceManager ( this : IMParticleInstanceManager ) {
31
+ const self = this ;
26
32
// Only leaving this here in case any clients are trying to access mParticle.Store, to prevent from throwing
27
- this . Store = { } ;
28
- this . _instances = { } ;
33
+ this . Store = { } as IStore ;
34
+ this . _instances = { } as Dictionary < IMParticleWebSDKInstance > ;
29
35
this . IdentityType = Types . IdentityType ;
30
- this . EventType = Types . EventType ;
31
- this . CommerceEventType = Types . CommerceEventType ;
32
- this . PromotionType = Types . PromotionActionType ;
33
- this . ProductActionType = Types . ProductActionType ;
36
+ this . EventType = EventType as unknown as valueof < typeof EventType > ;
37
+ this . CommerceEventType = CommerceEventType as unknown as valueof < typeof CommerceEventType > ;
38
+ this . PromotionType = PromotionActionType as unknown as valueof < typeof PromotionActionType > ;
39
+ this . ProductActionType = ProductActionType as unknown as valueof < typeof ProductActionType > ;
40
+
34
41
this . MPSideloadedKit = MPSideloadedKit ;
35
42
36
43
if ( typeof window !== 'undefined' ) {
@@ -65,7 +72,7 @@ function mParticle() {
65
72
? Constants . DefaultInstance
66
73
: instanceName
67
74
) . toLowerCase ( ) ;
68
- var client = self . _instances [ instanceName ] ;
75
+ let client : IMParticleWebSDKInstance = self . _instances [ instanceName ] ;
69
76
if ( client === undefined ) {
70
77
client = new mParticleInstance ( instanceName ) ;
71
78
self . _instances [ instanceName ] = client ;
@@ -75,7 +82,7 @@ function mParticle() {
75
82
} ;
76
83
77
84
this . getInstance = function getInstance ( instanceName ) {
78
- var client ;
85
+ let client : IMParticleWebSDKInstance ;
79
86
if ( ! instanceName ) {
80
87
instanceName = Constants . DefaultInstance ;
81
88
client = self . _instances [ instanceName ] ;
@@ -480,11 +487,17 @@ function mParticle() {
480
487
} ;
481
488
}
482
489
483
- var mparticleInstance = new mParticle ( ) ;
490
+ const mParticleManager = new mParticleInstanceManager ( ) ;
484
491
485
492
if ( typeof window !== 'undefined' ) {
486
- window . mParticle = mparticleInstance ;
493
+ // mParticle is the global object used to access the SDK and predates instance manager,
494
+ // when mParticle was a singleton. We now support multiple instances. Calling methods
495
+ // on mParticle directly will access the default instance, but mParticle can also be used
496
+ // as the instance manager in self hosted mode.
497
+ window . mParticle = mParticleManager ;
498
+
499
+ // https://go.mparticle.com/work/SQDSDKS-5053
487
500
window . mParticle . _BatchValidator = new _BatchValidator ( ) ;
488
501
}
489
502
490
- export default mparticleInstance ;
503
+ export default mParticleManager ;
0 commit comments