Skip to content

Commit 757d937

Browse files
refactor: Migrate mParticle Instance Manager to TypeScript
1 parent 42c56ad commit 757d937

File tree

1 file changed

+29
-15
lines changed

1 file changed

+29
-15
lines changed
Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import Polyfill from './polyfill';
2-
import Types from './types';
2+
import Types, { CommerceEventType, EventType, ProductActionType, PromotionActionType } from './types';
33
import Constants from './constants';
4-
import mParticleInstance from './mp-instance.js';
4+
import mParticleInstance, { IMParticleWebSDKInstance } from './mp-instance.js';
55
import _BatchValidator from './mockBatchCreator';
66
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';
711

812
if (!Array.prototype.forEach) {
913
Array.prototype.forEach = Polyfill.forEach;
@@ -17,21 +21,27 @@ if (!Array.prototype.filter) {
1721
Array.prototype.filter = Polyfill.filter;
1822
}
1923

24+
// https://go.mparticle.com/work/SQDSDKS-6768
2025
if (!Array.isArray) {
26+
// @ts-ignore
2127
Array.prototype.isArray = Polyfill.isArray;
2228
}
2329

24-
function mParticle() {
30+
31+
// Question: Should we just change this to mParticleInsanceManager?
32+
export default function mParticle(this: IMParticleInstanceManager, instanceName?: string) {
2533
var self = this;
2634
// Only leaving this here in case any clients are trying to access mParticle.Store, to prevent from throwing
27-
this.Store = {};
28-
this._instances = {};
35+
this.Store = {} as IStore;
36+
this._instances = {} as Dictionary<IMParticleWebSDKInstance>;
2937
this.IdentityType = Types.IdentityType;
30-
this.EventType = Types.EventType;
31-
this.CommerceEventType = Types.CommerceEventType;
32-
this.PromotionType = Types.PromotionActionType;
33-
this.ProductActionType = Types.ProductActionType;
34-
this.MPSideloadedKit = MPSideloadedKit;
38+
this.EventType = EventType as unknown as valueof<typeof EventType>;
39+
this.CommerceEventType = CommerceEventType as unknown as valueof<typeof CommerceEventType>;
40+
this.PromotionType = PromotionActionType as unknown as valueof<typeof PromotionActionType>;
41+
this.ProductActionType = ProductActionType as unknown as valueof<typeof ProductActionType>;
42+
43+
// QUESTION: Should this be a 'new' instance?
44+
this.MPSideloadedKit = MPSideloadedKit as unknown as valueof<typeof MPSideloadedKit>;
3545

3646
if (typeof window !== 'undefined') {
3747
this.isIOS =
@@ -67,7 +77,10 @@ function mParticle() {
6777
).toLowerCase();
6878
var client = self._instances[instanceName];
6979
if (client === undefined) {
70-
client = new mParticleInstance(apiKey, config, instanceName);
80+
81+
// FIXME: This is not an accurate signature for the constructor
82+
// client = new mParticleInstance(apiKey, config, instanceName);
83+
client = new mParticleInstance(instanceName);
7184
self._instances[instanceName] = client;
7285
}
7386

@@ -480,11 +493,12 @@ function mParticle() {
480493
};
481494
}
482495

483-
var mparticleInstance = new mParticle();
496+
// Can we get rid of this and just rename the class to mparticleInstanceManager?
497+
const mparticleInstance = new mParticle();
484498

485499
if (typeof window !== 'undefined') {
486500
window.mParticle = mparticleInstance;
487-
window.mParticle._BatchValidator = new _BatchValidator();
488-
}
489501

490-
export default mparticleInstance;
502+
// https://go.mparticle.com/work/SQDSDKS-5053
503+
window.mParticle._BatchValidator = new _BatchValidator();
504+
}

0 commit comments

Comments
 (0)