Created
December 21, 2017 21:52
-
-
Save btk/551e3f22978242ce7ccfb28d852a056c to your computer and use it in GitHub Desktop.
Generic stored api file for react native expo project.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Storage from 'react-native-storage'; | |
import { AsyncStorage } from 'react-native'; | |
var EventEmitter = require('events').EventEmitter; | |
EventEmitter.defaultMaxListeners = 40; | |
import { Segment } from 'expo'; | |
Segment.initialize({androidWriteKey: "xxx", iosWriteKey: "xxx"}); | |
const _FLUSH = false; | |
// Flush all user generated data to assigned currents | |
const _DEVELOPMENT = true; | |
const nativeClientVersion = "1.0.0"; | |
// Version of game that will be synced, called by client! | |
let storage = new Storage({ | |
size: 1000, | |
storageBackend: AsyncStorage, | |
defaultExpires: null, | |
enableCache: true, | |
sync: {} | |
}); | |
class API { | |
constructor(){ | |
this.event = new EventEmitter(); | |
this.development = _DEVELOPMENT; | |
this.version = nativeClientVersion; | |
this.segment = Segment; | |
console.log("API: Created instance"); | |
this.initRDS(); | |
// Ram Data Storage | |
if(_FLUSH){ | |
this.flush(); | |
} | |
} | |
flush(){ | |
// Flush to the begining state | |
this.setData("sample", "default"); | |
console.log("API: RDS variables are flushed"); | |
} | |
initRDS(){ | |
this.getData("userId").then(uid => { | |
this.segment.identify(uid); | |
console.log("API: userId: ", uid); | |
}, err => { | |
if(err.name == "NotFoundError"){ | |
let uid = "Demo"; | |
this.setData("userId", uid); | |
this.segment.identify(uid); | |
console.log("API: First time userId set"); | |
} | |
}); | |
} | |
// These are like kinda private; | |
// But fuck it, use them in the general app, who cares. | |
setData(key, data){ | |
return storage.save({key, data}); | |
} | |
getData(key){ | |
// returns promise | |
return storage.load({key}); | |
} | |
} | |
let apiInstance = new API(); | |
export default apiInstance; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment