maxleap-react-native
Version:
MaxLeap SDK for ReactNative
84 lines (71 loc) • 2.79 kB
JavaScript
;
var _ = require('underscore');
import * as storage from './storage';
import React, {Platform, NativeModules} from 'react-native';
var MaxLeap = NativeModules.MaxLeapCore;
module.exports = function(ML) {
/**
* @class
*
* <p>A ML.User object is a local representation of a user persisted to the
* ML cloud. This class is a subclass of a ML.Object, and retains the
* same functionality of a ML.Object, but also extends it with various
* user specific methods, like authentication, signing up, and validation of
* uniqueness.</p>
*/
ML.Installation = ML.Object.extend("_Installation", /** @lends ML.User.prototype */ {
_isCurrentInstallation: false,
_handleSaveResult: function(makeCurrent) {
this._refreshCache();
if(this._isCurrentInstallation){
ML.Installation._saveCurrentInstallation(this);
}
},
getInstallationId: function() {
return this.get("installationId");
}
}, /** @lends ML.Installation */ {
// Class Variables
// The currently installation.
_currentInstallation: null,
currentAsync: function() {
if (ML.Installation._currentInstallation) {
return ML.Promise.as(ML.Installation._currentInstallation);
}
var installPromise = storage.getCurrentInstallation();
return installPromise.then((data)=>{
ML.Installation._currentInstallation = ML.Object._create("_Installation");
if(data){
var json = JSON.parse(data);
ML.Installation._currentInstallation.id = json._id;
delete json._id;
ML.Installation._currentInstallation._refreshCache();
ML.Installation._currentInstallation._opSetQueue = [{}];
}
return MaxLeap.getDeviceInfo().then(info=>{
for(let key in info){
var value = ML.Installation._currentInstallation.get(key);
if(!value || value!= info[key]){
ML.Installation._currentInstallation.set(key, info[key]);
}
}
var version = ML.Installation._currentInstallation.get('sdkVersion');
if(!version || version != ML.VERSION){
ML.Installation._currentInstallation.set('sdkVersion', ML.VERSION);
}
}).then(()=>{
ML.Installation._currentInstallation._isCurrentInstallation = true;
return ML.Installation._currentInstallation;
});
})
},
/**
* Persists a installation as currentInstallation to localStorage, and into the singleton.
*/
_saveCurrentInstallation: function(installation) {
ML.Installation._currentInstallation = installation;
var json = installation.toJSON();
storage.saveCurrentInstallation(JSON.stringify(json));
}
});
};