-
-
Notifications
You must be signed in to change notification settings - Fork 596
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Improve installation object `Parse.Installation.currentInstalla…
…tion` to support web push notifications (#2119)
- Loading branch information
Showing
17 changed files
with
690 additions
and
107 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file was deleted.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import CoreManager from './CoreManager'; | ||
import Storage from './Storage'; | ||
import ParseInstallation from './ParseInstallation'; | ||
import uuidv4 from './uuid'; | ||
|
||
const CURRENT_INSTALLATION_KEY = 'currentInstallation'; | ||
const CURRENT_INSTALLATION_ID_KEY = 'currentInstallationId'; | ||
|
||
let iidCache: string | null = null; | ||
let currentInstallationCache = null; | ||
let currentInstallationCacheMatchesDisk = false; | ||
|
||
const InstallationController = { | ||
async updateInstallationOnDisk(installation: ParseInstallation): Promise<void> { | ||
const path = Storage.generatePath(CURRENT_INSTALLATION_KEY); | ||
await Storage.setItemAsync(path, JSON.stringify(installation.toJSON())); | ||
this._setCurrentInstallationCache(installation); | ||
}, | ||
|
||
async currentInstallationId(): Promise<string> { | ||
if (typeof iidCache === 'string') { | ||
return iidCache; | ||
} | ||
const path = Storage.generatePath(CURRENT_INSTALLATION_ID_KEY); | ||
let iid = await Storage.getItemAsync(path); | ||
if (!iid) { | ||
iid = uuidv4(); | ||
return Storage.setItemAsync(path, iid).then(() => { | ||
iidCache = iid; | ||
return iid; | ||
}); | ||
} | ||
iidCache = iid; | ||
return iid; | ||
}, | ||
|
||
async currentInstallation(): Promise<ParseInstallation | null> { | ||
if (currentInstallationCache) { | ||
return currentInstallationCache; | ||
} | ||
if (currentInstallationCacheMatchesDisk) { | ||
return null; | ||
} | ||
const path = Storage.generatePath(CURRENT_INSTALLATION_KEY); | ||
let installationData = await Storage.getItemAsync(path); | ||
currentInstallationCacheMatchesDisk = true; | ||
if (installationData) { | ||
installationData = JSON.parse(installationData); | ||
installationData.className = '_Installation'; | ||
const current = ParseInstallation.fromJSON(installationData); | ||
currentInstallationCache = current; | ||
return current; | ||
} | ||
const installationId = await this.currentInstallationId(); | ||
const installation = new ParseInstallation(); | ||
installation.set('deviceType', ParseInstallation.DEVICE_TYPES.WEB); | ||
installation.set('installationId', installationId); | ||
installation.set('parseVersion', CoreManager.get('VERSION')); | ||
currentInstallationCache = installation; | ||
await Storage.setItemAsync(path, JSON.stringify(installation.toJSON())) | ||
return installation; | ||
}, | ||
|
||
_clearCache() { | ||
iidCache = null; | ||
currentInstallationCache = null; | ||
currentInstallationCacheMatchesDisk = false; | ||
}, | ||
|
||
_setInstallationIdCache(iid: string) { | ||
iidCache = iid; | ||
}, | ||
|
||
_setCurrentInstallationCache(installation: ParseInstallation, matchesDisk: boolean = true) { | ||
currentInstallationCache = installation; | ||
currentInstallationCacheMatchesDisk = matchesDisk; | ||
}, | ||
}; | ||
|
||
module.exports = InstallationController; | ||
export default InstallationController; |
This file contains 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
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.