Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36,564 changes: 21,783 additions & 14,781 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@
"webpack-cli": "^5.0.2"
},
"dependencies": {
"@babel/runtime": "^7.23.2"
"@babel/runtime": "^7.23.2",
"@react-native-async-storage/async-storage": "^1.22.3"
}
}
1 change: 1 addition & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const defaultOutputOptions = {
name: 'mParticle',
strict: false,
sourcemap: ENVIRONMENT !== 'prod' ? 'inline' : false,
// external: ['@react-native-community/async-storage'],
};

const defaultBabel = babel({
Expand Down
3 changes: 3 additions & 0 deletions src/configAPIClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ export default function ConfigAPIClient(this: IConfigAPIClient) {
config,
JSON.parse(xhr.responseText)
);

console.warn('async config request returns');

completeSDKInitialization(apiKey, config, mpInstance);
mpInstance.Logger.verbose(
'Successfully received configuration from server'
Expand Down
47 changes: 47 additions & 0 deletions src/mp-instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,22 @@ export default function mParticleInstance(instanceName) {
}
}
this.init = function(apiKey, config) {
console.warn('LOCAL DEV');

if (!config) {
console.warn(
'You did not pass a config object to init(). mParticle will not initialize properly'
);
}

console.log('call to runPreConfigFetchInitialization');

console.error('STARTING INIT');

runPreConfigFetchInitialization(this, apiKey, config);

// TODO: Perhaps we break up completeSDKInitialization into two methods, one for fetching config and one for initializing the SDK

// config code - Fetch config when requestConfig = true, otherwise, proceed with SDKInitialization
// Since fetching the configuration is asynchronous, we must pass completeSDKInitialization
// to it for it to be run after fetched
Expand All @@ -112,13 +120,27 @@ export default function mParticleInstance(instanceName) {
!config.hasOwnProperty('requestConfig') ||
config.requestConfig
) {
console.warn('firing async call to fetch config');
new ConfigAPIClient().getSDKConfiguration(
apiKey,
config,
completeSDKInitialization,
this
);

console.warn('async config continues');
} else {
console.warn('non-async call to fetch config');
// debugger;
// var self = this;
// setTimeout(
// function() {
// debugger;
// completeSDKInitialization(apiKey, config, self);
// },
// 0,
// self
// );
completeSDKInitialization(apiKey, config, this);
}
} else {
Expand All @@ -127,6 +149,8 @@ export default function mParticleInstance(instanceName) {
);
return;
}

console.error('ENDING INIT');
};
/**
* Resets the SDK to an uninitialized state and removes cookies/localStorage. You MUST call mParticle.init(apiKey, window.mParticle.config)
Expand Down Expand Up @@ -1285,10 +1309,14 @@ export default function mParticleInstance(instanceName) {

// Some (server) config settings need to be returned before they are set on SDKConfig in a self hosted environment
function completeSDKInitialization(apiKey, config, mpInstance) {
console.warn('completeSDKInitialization called');

var kitBlocker = createKitBlocker(config, mpInstance);

mpInstance._APIClient = new APIClient(mpInstance, kitBlocker);
mpInstance._Forwarders = new Forwarders(mpInstance, kitBlocker);

// TODO: Move this into the store or create a config class
if (config.flags) {
if (
config.flags.hasOwnProperty(
Expand All @@ -1304,6 +1332,9 @@ function completeSDKInitialization(apiKey, config, mpInstance) {
}
}

console.warn('Storage name is set');

// QUESTION: Can we move these two into the _Store object?
// add a new function to apply items to the store that require config to be returned
mpInstance._Store.storageName = mpInstance._Helpers.createMainStorageName(
config.workspaceToken
Expand Down Expand Up @@ -1350,7 +1381,12 @@ function completeSDKInitialization(apiKey, config, mpInstance) {
// https://go.mparticle.com/work/SQDSDKS-6044
if (!mpInstance._Store.webviewBridgeEnabled) {
// Load any settings/identities/attributes from cookie or localStorage

console.error('PERSISTENCE: Starts initializing Storage');

mpInstance._Persistence.initializeStorage();

console.error('PERSISTENCE: Finishes Initialzing Storage');
}

if (mpInstance._Store.webviewBridgeEnabled) {
Expand Down Expand Up @@ -1542,13 +1578,24 @@ function createKitBlocker(config, mpInstance) {
}

function runPreConfigFetchInitialization(mpInstance, apiKey, config) {
// async function runPreConfigFetchInitialization(mpInstance, apiKey, config) {
mpInstance.Logger = new Logger(config);
mpInstance._Store = new Store(config, mpInstance, apiKey);
window.mParticle.Store = mpInstance._Store;
mpInstance.Logger.verbose(
Messages.InformationMessages.StartingInitialization
);

// // QUESTION: Can we try fetching async storage here?
// try {
// console.warn('trying to fetch async storage in preConfig');
// const asyncPersistence = await mpInstance._Persistence.getAsyncStorage();

// console.warn('fetching async storage in preConfig', asyncPersistence);
// } catch (error) {
// console.error('error fetching async storage in preConfig', error);
// }

// Check to see if localStorage is available before main configuration runs
// since we will need this for the current implementation of user persistence
// TODO: Refactor this when we refactor User Identity Persistence
Expand Down
57 changes: 57 additions & 0 deletions src/persistence.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import Constants from './constants';
import Polyfill from './polyfill';
import * as Utils from './utils';

// import AsyncStorage from '@react-native-async-storage/async-storage';
// This version is deprecated, and the above is the "current one". Maybe?
// import AsyncStorage from '@react-native-community/async-storage';

var Base64 = Polyfill.Base64,
Messages = Constants.Messages,
Base64CookieKeys = Constants.Base64CookieKeys,
Expand All @@ -11,6 +15,8 @@ var Base64 = Polyfill.Base64,
export default function _Persistence(mpInstance) {
var self = this;

console.warn('Persistence instantiated');

// https://go.mparticle.com/work/SQDSDKS-5022
this.useLocalStorage = function() {
return (
Expand All @@ -20,12 +26,28 @@ export default function _Persistence(mpInstance) {
};

this.initializeStorage = function() {
console.warn('firing initializeStorage');
// debugger;

// TODO: Try to force Async storage to be used here
// AsyncStorage.setItem('mp-async-storage-test', 'save some random stuff');

try {
console.warn('initialize storage calls getLocalStorage');
var storage,
// QUESTION: Why is getLocalStorage called twice?
localStorageData = self.getLocalStorage(),
// asynStorageData = self.getAsyncStorage(),
cookies = self.getCookie(),
allData;

// console.warn(
// 'initialize storage checks async storage data',
// asynStorageData
// );

console.warn('initialize storge resumes');

// https://go.mparticle.com/work/SQDSDKS-6045
// Determine if there is any data in cookies or localStorage to figure out if it is the first time the browser is loading mParticle
if (!localStorageData && !cookies) {
Expand Down Expand Up @@ -125,7 +147,11 @@ export default function _Persistence(mpInstance) {
}
}
}

// Triggers a second call for get local storage
self.update();

console.warn('initialize storge ends');
} catch (e) {
// If cookies or local storage is corrupt, we want to remove it
// so that in the future, initializeStorage will work
Expand All @@ -142,6 +168,8 @@ export default function _Persistence(mpInstance) {
};

this.update = function() {
console.error('CALLING UPDATE');

if (!mpInstance._Store.webviewBridgeEnabled) {
if (mpInstance._Store.SDKConfig.useCookieStorage) {
self.setCookie();
Expand Down Expand Up @@ -416,7 +444,23 @@ export default function _Persistence(mpInstance) {
return data;
}

// this.getAsyncStorage = async function() {
// try {
// const asyncPersistence = await AsyncStorage.getItem(
// 'mp-async-storage-test'
// );
// console.warn('Returning async storage data', asyncPersistence);
// return asyncPersistence;
// } catch (error) {
// console.error('Error reading from async storage', error);
// return null;
// }
// };

this.getLocalStorage = function() {
// debugger;
console.warn('Reading from local storage');

if (!mpInstance._Store.isLocalStorageAvailable) {
return null;
}
Expand All @@ -437,6 +481,7 @@ export default function _Persistence(mpInstance) {
}

if (Object.keys(obj).length) {
console.warn('returning local storage data', obj);
return obj;
}

Expand Down Expand Up @@ -1058,10 +1103,22 @@ export default function _Persistence(mpInstance) {
};

this.getPersistence = function() {
// this.getPersistence = async function() {
// debugger;
// var asyncPersistence = await this.getAsyncStorage();
// console.warn('async persistence in getPersistence', asyncPersistence);

// debugger;

// TODO: Should we consider a flag for async storage?
var persistence = this.useLocalStorage()
? this.getLocalStorage()
: this.getCookie();

console.warn('local persistence in getPersistence', persistence);

// debugger;

return persistence;
};

Expand Down
4 changes: 3 additions & 1 deletion src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ export default function Store(
mpInstance: MParticleWebSDK,
apiKey?: string
) {
console.warn('Store instantiated');

const defaultStore: Partial<IStore> = {
isEnabled: true,
sessionAttributes: {},
Expand Down Expand Up @@ -527,4 +529,4 @@ function processDirectBaseUrls(
}

return directBaseUrls;
}
}
2 changes: 1 addition & 1 deletion test/src/tests-identity-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ describe('identity-utils', () => {
userIdentities: customerId
};
const callback = sinon.spy();
debugger;
// debugger;
const successfullyCachedIdentity = tryCacheIdentity(
knownIdentities1,
cacheVault,
Expand Down