Skip to content

Commit 7995d2c

Browse files
authored
Merge branch 'alpha' into fix-1798
2 parents 306792c + aff0a00 commit 7995d2c

20 files changed

+700
-110
lines changed

changelogs/CHANGELOG_alpha.md

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
# [5.1.0-alpha.9](https://github.com/parse-community/Parse-SDK-JS/compare/5.1.0-alpha.8...5.1.0-alpha.9) (2024-05-04)
2+
3+
4+
### Features
5+
6+
* Improve installation object `Parse.Installation.currentInstallation` to support web push notifications ([#2119](https://github.com/parse-community/Parse-SDK-JS/issues/2119)) ([4fc62ce](https://github.com/parse-community/Parse-SDK-JS/commit/4fc62cec0c4ea704f48ec501a5f0182836de45d1))
7+
18
# [5.1.0-alpha.8](https://github.com/parse-community/Parse-SDK-JS/compare/5.1.0-alpha.7...5.1.0-alpha.8) (2024-05-02)
29

310

integration/test/ParseUserTest.js

+34-3
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ describe('Parse User', () => {
113113

114114
it('can login users with installationId', async () => {
115115
Parse.User.enableUnsafeCurrentUser();
116-
const currentInstallation = await Parse.CoreManager.getInstallationController().currentInstallationId();
116+
const currentInstallationId = await Parse.CoreManager.getInstallationController().currentInstallationId();
117117
const installationId = '12345678';
118118
const user = new Parse.User();
119119
user.set('username', 'parse');
@@ -132,7 +132,7 @@ describe('Parse User', () => {
132132
let sessions = await sessionQuery.find({ useMasterKey: true });
133133
expect(sessions.length).toBe(2);
134134
expect(sessions[0].get('installationId')).toBe(installationId);
135-
expect(sessions[1].get('installationId')).toBe(currentInstallation);
135+
expect(sessions[1].get('installationId')).toBe(currentInstallationId);
136136
expect(sessions[0].get('sessionToken')).toBe(user.getSessionToken());
137137
expect(sessions[1].get('sessionToken')).toBe(loggedUser.getSessionToken());
138138

@@ -142,12 +142,43 @@ describe('Parse User', () => {
142142
});
143143
sessions = await sessionQuery.find({ useMasterKey: true });
144144
expect(sessions.length).toBe(2);
145-
expect(sessions[0].get('installationId')).toBe(currentInstallation);
145+
expect(sessions[0].get('installationId')).toBe(currentInstallationId);
146146
expect(sessions[1].get('installationId')).toBe(installationId);
147147
expect(sessions[0].get('sessionToken')).toBe(loggedUser.getSessionToken());
148148
expect(sessions[1].get('sessionToken')).toBe(installationUser.getSessionToken());
149149
});
150150

151+
it('can get current installation', async () => {
152+
const currentInstallationId = await Parse.CoreManager.getInstallationController().currentInstallationId();
153+
const installation = await Parse.Installation.currentInstallation();
154+
expect(installation.installationId).toBe(currentInstallationId);
155+
expect(installation.deviceType).toBe(Parse.Installation.DEVICE_TYPES.WEB);
156+
await installation.save();
157+
expect(installation.id).toBeDefined();
158+
expect(installation.createdAt).toBeDefined();
159+
expect(installation.updatedAt).toBeDefined();
160+
const data = {
161+
deviceToken: '1234',
162+
badge: 1,
163+
appIdentifier: 'com.parse.server',
164+
appName: 'Parse JS SDK',
165+
appVersion: '1.0.0',
166+
parseVersion: '1.0.0',
167+
localeIdentifier: 'en-US',
168+
timeZone: 'GMT',
169+
channels: ['test'],
170+
GCMSenderId: '1234',
171+
pushType: 'test',
172+
};
173+
installation.set(data);
174+
await installation.save();
175+
const query = new Parse.Query(Parse.Installation);
176+
const result = await query.get(installation.id, { useMasterKey: true });
177+
Object.keys(data).forEach(key => {
178+
expect(result[key]).toEqual(data[key]);
179+
});
180+
});
181+
151182
it('can login with userId', async () => {
152183
Parse.User.enableUnsafeCurrentUser();
153184

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "parse",
3-
"version": "5.1.0-alpha.8",
3+
"version": "5.1.0-alpha.9",
44
"description": "Parse JavaScript SDK",
55
"homepage": "https://parseplatform.org",
66
"keywords": [

src/CoreManager.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import type { HookDeclaration, HookDeleteArg } from './ParseHooks';
1414
import type ParseConfig from './ParseConfig';
1515
import type LiveQueryClient from './LiveQueryClient';
1616
import type ParseSchema from './ParseSchema';
17+
import type ParseInstallation from './ParseInstallation';
1718

1819
type AnalyticsController = {
1920
track: (name: string, dimensions: { [key: string]: string }) => Promise<any>,
@@ -41,6 +42,8 @@ type FileController = {
4142
};
4243
type InstallationController = {
4344
currentInstallationId: () => Promise<string>,
45+
currentInstallation: () => Promise<ParseInstallation | null>,
46+
updateInstallationOnDisk: (installation: ParseInstallation) => Promise<void>,
4447
};
4548
type ObjectController = {
4649
fetch: (
@@ -376,7 +379,11 @@ const CoreManager = {
376379
},
377380

378381
setInstallationController(controller: InstallationController) {
379-
requireMethods('InstallationController', ['currentInstallationId'], controller);
382+
requireMethods(
383+
'InstallationController',
384+
['currentInstallationId', 'currentInstallation', 'updateInstallationOnDisk'],
385+
controller
386+
);
380387
config['InstallationController'] = controller;
381388
},
382389

src/InstallationController.js

-38
This file was deleted.

src/InstallationController.ts

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import CoreManager from './CoreManager';
2+
import Storage from './Storage';
3+
import ParseInstallation from './ParseInstallation';
4+
import uuidv4 from './uuid';
5+
6+
const CURRENT_INSTALLATION_KEY = 'currentInstallation';
7+
const CURRENT_INSTALLATION_ID_KEY = 'currentInstallationId';
8+
9+
let iidCache: string | null = null;
10+
let currentInstallationCache = null;
11+
let currentInstallationCacheMatchesDisk = false;
12+
13+
const InstallationController = {
14+
async updateInstallationOnDisk(installation: ParseInstallation): Promise<void> {
15+
const path = Storage.generatePath(CURRENT_INSTALLATION_KEY);
16+
await Storage.setItemAsync(path, JSON.stringify(installation.toJSON()));
17+
this._setCurrentInstallationCache(installation);
18+
},
19+
20+
async currentInstallationId(): Promise<string> {
21+
if (typeof iidCache === 'string') {
22+
return iidCache;
23+
}
24+
const path = Storage.generatePath(CURRENT_INSTALLATION_ID_KEY);
25+
let iid = await Storage.getItemAsync(path);
26+
if (!iid) {
27+
iid = uuidv4();
28+
return Storage.setItemAsync(path, iid).then(() => {
29+
iidCache = iid;
30+
return iid;
31+
});
32+
}
33+
iidCache = iid;
34+
return iid;
35+
},
36+
37+
async currentInstallation(): Promise<ParseInstallation | null> {
38+
if (currentInstallationCache) {
39+
return currentInstallationCache;
40+
}
41+
if (currentInstallationCacheMatchesDisk) {
42+
return null;
43+
}
44+
const path = Storage.generatePath(CURRENT_INSTALLATION_KEY);
45+
let installationData = await Storage.getItemAsync(path);
46+
currentInstallationCacheMatchesDisk = true;
47+
if (installationData) {
48+
installationData = JSON.parse(installationData);
49+
installationData.className = '_Installation';
50+
const current = ParseInstallation.fromJSON(installationData);
51+
currentInstallationCache = current;
52+
return current;
53+
}
54+
const installationId = await this.currentInstallationId();
55+
const installation = new ParseInstallation();
56+
installation.set('deviceType', ParseInstallation.DEVICE_TYPES.WEB);
57+
installation.set('installationId', installationId);
58+
installation.set('parseVersion', CoreManager.get('VERSION'));
59+
currentInstallationCache = installation;
60+
await Storage.setItemAsync(path, JSON.stringify(installation.toJSON()))
61+
return installation;
62+
},
63+
64+
_clearCache() {
65+
iidCache = null;
66+
currentInstallationCache = null;
67+
currentInstallationCacheMatchesDisk = false;
68+
},
69+
70+
_setInstallationIdCache(iid: string) {
71+
iidCache = iid;
72+
},
73+
74+
_setCurrentInstallationCache(installation: ParseInstallation, matchesDisk: boolean = true) {
75+
currentInstallationCache = installation;
76+
currentInstallationCacheMatchesDisk = matchesDisk;
77+
},
78+
};
79+
80+
module.exports = InstallationController;
81+
export default InstallationController;

src/Parse.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -199,12 +199,12 @@ const Parse: ParseType = {
199199
CoreManager.setIfNeeded('EventEmitter', EventEmitter);
200200
CoreManager.setIfNeeded('LiveQuery', new ParseLiveQuery());
201201
CoreManager.setIfNeeded('CryptoController', CryptoController);
202+
CoreManager.setIfNeeded('EventuallyQueue', EventuallyQueue);
203+
CoreManager.setIfNeeded('InstallationController', InstallationController);
202204
CoreManager.setIfNeeded('LocalDatastoreController', LocalDatastoreController);
203205
CoreManager.setIfNeeded('StorageController', StorageController);
204206
CoreManager.setIfNeeded('WebSocketController', WebSocketController);
205207

206-
CoreManager.setIfNeeded('EventuallyQueue', EventuallyQueue);
207-
208208
if (process.env.PARSE_BUILD === 'browser') {
209209
Parse.IndexedDB = CoreManager.setIfNeeded('IndexedDBStorageController', IndexedDBStorageController);
210210
}
@@ -464,7 +464,6 @@ const Parse: ParseType = {
464464
},
465465
};
466466

467-
CoreManager.setInstallationController(InstallationController);
468467
CoreManager.setRESTController(RESTController);
469468

470469
if (process.env.PARSE_BUILD === 'node') {

src/ParseInstallation.js

-20
This file was deleted.

0 commit comments

Comments
 (0)