Skip to content

Commit 8556069

Browse files
authored
Merge pull request #978 from OneSignal/user-model/login-fixes
User model/login fixes
2 parents 27d7330 + dcb2cab commit 8556069

File tree

9 files changed

+108
-151
lines changed

9 files changed

+108
-151
lines changed

src/core/CoreModuleDirector.ts

Lines changed: 34 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,10 @@ import OneSignal from "../onesignal/OneSignal";
1818
/* Contains OneSignal User-Model-specific logic*/
1919

2020
export class CoreModuleDirector {
21-
private initPromise: Promise<void>;
2221

23-
constructor(private core: CoreModule) {
24-
this.initPromise = core.initPromise.then(() => {}).catch(e => {
25-
Log.error(e);
26-
});
27-
}
22+
constructor(private core: CoreModule) {}
2823

29-
public async generatePushSubscriptionModel(rawPushSubscription: RawPushSubscription): Promise<void> {
24+
public generatePushSubscriptionModel(rawPushSubscription: RawPushSubscription): void {
3025
logMethodCall("CoreModuleDirector.generatePushSubscriptionModel", { rawPushSubscription });
3126
// new subscription
3227
const pushModel = new OSModel<SupportedSubscription>(
@@ -39,19 +34,18 @@ export class CoreModuleDirector {
3934
pushModel.setOneSignalId(user.onesignalId);
4035
}
4136
// don't propagate since we will be including the subscription in the user create call
42-
await OneSignal.coreDirector.add(ModelName.PushSubscriptions, pushModel as OSModel<SupportedModel>, false);
37+
OneSignal.coreDirector.add(ModelName.PushSubscriptions, pushModel as OSModel<SupportedModel>, false);
4338
}
4439

4540
public async resetModelRepoAndCache(): Promise<void> {
4641
await this.core.resetModelRepoAndCache();
4742
}
4843

49-
public async hydrateUser(user: UserData): Promise<void> {
44+
public hydrateUser(user: UserData): void {
5045
logMethodCall("CoreModuleDirector.hydrateUser", { user });
5146
try {
52-
await this.initPromise;
53-
const identity = await this.getIdentityModel();
54-
const properties = await this.getPropertiesModel();
47+
const identity = this.getIdentityModel();
48+
const properties = this.getPropertiesModel();
5549

5650
const { onesignal_id: onesignalId } = user.identity;
5751

@@ -69,23 +63,20 @@ export class CoreModuleDirector {
6963

7064
// subscriptions are duplicable, so we hydrate them separately
7165
// when hydrating, we should have the full subscription object (i.e. include ID from server)
72-
this._hydrateSubscriptions(user.subscriptions as SubscriptionModel[], onesignalId).catch(e => {
73-
throw new OneSignalError(`Error hydrating subscriptions: ${e}`);
74-
});
66+
this._hydrateSubscriptions(user.subscriptions as SubscriptionModel[], onesignalId);
7567
} catch (e) {
7668
Log.error(`Error hydrating user: ${e}`);
7769
}
7870
}
7971

80-
private async _hydrateSubscriptions(subscriptions: SubscriptionModel[], onesignalId: string): Promise<void> {
72+
private _hydrateSubscriptions(subscriptions: SubscriptionModel[], onesignalId: string): void {
8173
logMethodCall("CoreModuleDirector._hydrateSubscriptions", { subscriptions });
8274

8375
if (!subscriptions) {
8476
return;
8577
}
8678

87-
await this.initPromise;
88-
const modelStores = await this.getModelStores();
79+
const modelStores = this.getModelStores();
8980

9081
const getModelName = (subscription: SupportedSubscription) => {
9182
if (subscription.type === "Email") {
@@ -104,7 +95,7 @@ export class CoreModuleDirector {
10495
* We don't want to create a new model in this case, so we use the token to identify the model.
10596
*/
10697
const existingSubscription = !!subscription.token ?
107-
await this.getSubscriptionOfTypeWithToken(modelName, subscription.token) :
98+
this.getSubscriptionOfTypeWithToken(modelName, subscription.token) :
10899
undefined;
109100

110101
if (existingSubscription) {
@@ -127,59 +118,54 @@ export class CoreModuleDirector {
127118

128119
/* O P E R A T I O N S */
129120

130-
public async add(modelName: ModelName, model: OSModel<SupportedModel>, propagate: boolean = true): Promise<void> {
121+
public add(modelName: ModelName, model: OSModel<SupportedModel>, propagate: boolean = true): void {
131122
logMethodCall("CoreModuleDirector.add", { modelName, model });
132-
const modelStores = await this.getModelStores();
123+
const modelStores = this.getModelStores();
133124
modelStores[modelName].add(model, propagate);
134125
}
135126

136-
public async remove(modelName: ModelName, modelId: string): Promise<void> {
127+
public remove(modelName: ModelName, modelId: string): void {
137128
logMethodCall("CoreModuleDirector.remove", { modelName, modelId });
138-
const modelStores = await this.getModelStores();
129+
const modelStores = this.getModelStores();
139130
modelStores[modelName].remove(modelId);
140131
}
141132

142133
/* G E T T E R S */
143134

144-
public async getModelByTypeAndId(modelName: ModelName, modelId: string):
145-
Promise<OSModel<SupportedModel> | undefined> {
135+
public getModelByTypeAndId(modelName: ModelName, modelId: string): OSModel<SupportedModel> | undefined {
146136
logMethodCall("CoreModuleDirector.getModelByTypeAndId", { modelName, modelId });
147-
await this.initPromise;
148-
const modelStores = await this.getModelStores();
137+
const modelStores = this.getModelStores();
149138
return modelStores[modelName].models[modelId];
150139
}
151140

152-
public async getEmailSubscriptionModels(): Promise<{ [key: string]: OSModel<SupportedSubscription> }> {
141+
public getEmailSubscriptionModels(): { [key: string]: OSModel<SupportedSubscription> } {
153142
logMethodCall("CoreModuleDirector.getEmailSubscriptionModels");
154-
await this.initPromise;
155-
const modelStores = await this.getModelStores();
143+
const modelStores = this.getModelStores();
156144
return modelStores.emailSubscriptions.models as { [key: string]: OSModel<SupportedSubscription> };
157145
}
158146

159-
public async getSmsSubscriptionModels(): Promise<{ [key: string]: OSModel<SupportedSubscription> }> {
147+
public getSmsSubscriptionModels(): { [key: string]: OSModel<SupportedSubscription> } {
160148
logMethodCall("CoreModuleDirector.getSmsSubscriptionModels");
161-
await this.initPromise;
162-
const modelStores = await this.getModelStores();
149+
const modelStores = this.getModelStores();
163150
return modelStores.smsSubscriptions.models as { [key: string]: OSModel<SupportedSubscription> };
164151
}
165152

166153
/**
167154
* Returns all push subscription models, including push subscriptions from other browsers.
168155
*/
169-
public async getAllPushSubscriptionModels(): Promise<{ [key: string]: OSModel<SupportedSubscription> }> {
156+
public getAllPushSubscriptionModels(): { [key: string]: OSModel<SupportedSubscription> } {
170157
logMethodCall("CoreModuleDirector.getAllPushSubscriptionModels");
171-
await this.initPromise;
172-
const modelStores = await this.getModelStores();
158+
const modelStores = this.getModelStores();
173159
return modelStores.pushSubscriptions.models as { [key: string]: OSModel<SupportedSubscription> };
174160
}
175161

176162
/**
177163
* Gets the current push subscription model for the current browser.
178164
* @returns The push subscription model for the current browser, or undefined if no push subscription exists.
179165
*/
166+
// TO DO: make synchronous by making getting the current push token synchronous
180167
public async getCurrentPushSubscriptionModel(): Promise<OSModel<SupportedSubscription> | undefined> {
181168
logMethodCall("CoreModuleDirector.getPushSubscriptionModel");
182-
await this.initPromise;
183169
const pushToken = await MainHelper.getCurrentPushToken();
184170

185171
if (!pushToken) {
@@ -189,27 +175,24 @@ export class CoreModuleDirector {
189175
return this.getSubscriptionOfTypeWithToken(ModelName.PushSubscriptions, pushToken);
190176
}
191177

192-
public async getIdentityModel(): Promise<OSModel<SupportedIdentity> | undefined> {
178+
public getIdentityModel(): OSModel<SupportedIdentity> | undefined {
193179
logMethodCall("CoreModuleDirector.getIdentityModel");
194-
await this.initPromise;
195-
const modelStores = await this.getModelStores();
180+
const modelStores = this.getModelStores();
196181
const modelKeys = Object.keys(modelStores.identity.models);
197182
return modelStores.identity.models[modelKeys[0]] as OSModel<SupportedIdentity>;
198183
}
199184

200-
public async getPropertiesModel(): Promise<OSModel<UserPropertiesModel> | undefined> {
185+
public getPropertiesModel(): OSModel<UserPropertiesModel> | undefined {
201186
logMethodCall("CoreModuleDirector.getPropertiesModel");
202-
await this.initPromise;
203-
const modelStores = await this.getModelStores();
187+
const modelStores = this.getModelStores();
204188
const modelKeys = Object.keys(modelStores.properties.models);
205189
return modelStores.properties.models[modelKeys[0]] as OSModel<UserPropertiesModel>;
206190
}
207191

208192
public async getAllSubscriptionsModels(): Promise<OSModel<SupportedSubscription>[]> {
209193
logMethodCall("CoreModuleDirector.getAllSubscriptionsModels");
210-
await this.initPromise;
211-
const emailSubscriptions = await this.getEmailSubscriptionModels();
212-
const smsSubscriptions = await this.getSmsSubscriptionModels();
194+
const emailSubscriptions = this.getEmailSubscriptionModels();
195+
const smsSubscriptions = this.getSmsSubscriptionModels();
213196
const pushSubscription = await this.getCurrentPushSubscriptionModel();
214197

215198
const subscriptions = Object.values(emailSubscriptions)
@@ -218,20 +201,18 @@ export class CoreModuleDirector {
218201
return subscriptions;
219202
}
220203

221-
public async getSubscriptionOfTypeWithToken(type: ModelName, token: string):
222-
Promise<OSModel<SupportedSubscription> | undefined>
204+
public getSubscriptionOfTypeWithToken(type: ModelName, token: string): OSModel<SupportedSubscription> | undefined
223205
{
224206
logMethodCall("CoreModuleDirector.getSubscriptionOfTypeWithToken", { type, token });
225-
await this.initPromise;
226207
switch (type) {
227208
case ModelName.EmailSubscriptions:
228-
const emailSubscriptions = await this.getEmailSubscriptionModels();
209+
const emailSubscriptions = this.getEmailSubscriptionModels();
229210
return Object.values(emailSubscriptions).find(subscription => subscription.data.token === token);
230211
case ModelName.SmsSubscriptions:
231-
const smsSubscriptions = await this.getSmsSubscriptionModels();
212+
const smsSubscriptions = this.getSmsSubscriptionModels();
232213
return Object.values(smsSubscriptions).find(subscription => subscription.data.token === token);
233214
case ModelName.PushSubscriptions:
234-
const pushSubscriptions = await this.getAllPushSubscriptionModels();
215+
const pushSubscriptions = this.getAllPushSubscriptionModels();
235216
return Object.values(pushSubscriptions).find(subscription => subscription.data.token === token);
236217
default:
237218
return undefined;
@@ -240,8 +221,7 @@ export class CoreModuleDirector {
240221

241222
/* P R I V A T E */
242223

243-
private async getModelStores(): Promise<ModelStoresMap<SupportedModel>> {
244-
await this.initPromise;
224+
private getModelStores(): ModelStoresMap<SupportedModel> {
245225
return (this.core.modelRepo?.modelStores as ModelStoresMap<SupportedModel>);
246226
}
247227
}

src/core/operationRepo/Operation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export class Operation<Model> {
7373
if (!model) {
7474
throw new OneSignalError("Operation.fromJSON: model is undefined");
7575
}
76-
const osModel = await OneSignal.coreDirector?.getModelByTypeAndId(modelName, model.modelId);
76+
const osModel = OneSignal.coreDirector?.getModelByTypeAndId(modelName, model.modelId);
7777

7878
if (!!osModel) {
7979
const operation = new Operation<SupportedModel>(changeType, modelName);

src/onesignal/OneSignal.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,12 @@ import { OneSignalDeferredLoadedCallback } from "../page/models/OneSignalDeferre
4545
import UserDirector from "./UserDirector";
4646
import { ModelName, SupportedModel } from "../core/models/SupportedModels";
4747
import { OSModel } from "../core/modelRepo/OSModel";
48+
import UserData from "../core/models/UserData";
4849

4950
export default class OneSignal {
5051
private static async _initializeCoreModuleAndUserNamespace() {
5152
const core = new CoreModule();
53+
await core.initPromise;
5254
OneSignal.coreDirector = new CoreModuleDirector(core);
5355
OneSignal.User = new UserNamespace();
5456
}
@@ -101,7 +103,7 @@ export default class OneSignal {
101103
await Database.setJWTToken(token);
102104
}
103105

104-
const identityModel = await this.coreDirector.getIdentityModel();
106+
const identityModel = this.coreDirector.getIdentityModel();
105107

106108
if (!identityModel) {
107109
throw new OneSignalError('Login: No identity model found');
@@ -127,7 +129,21 @@ export default class OneSignal {
127129
// set the external id on the user locally
128130
LoginManager.setExternalId(identityModel, externalId);
129131

130-
const userData = await UserDirector.getAllUserData();
132+
let userData: Partial<UserData>;
133+
const pushSubscription = await this.coreDirector.getCurrentPushSubscriptionModel();
134+
if (!isIdentified) {
135+
userData = await UserDirector.getAllUserData();
136+
} else {
137+
userData = {
138+
identity: {
139+
external_id: externalId,
140+
}
141+
};
142+
143+
if (pushSubscription) {
144+
userData.subscriptions = [pushSubscription.data];
145+
}
146+
}
131147
await this.coreDirector.resetModelRepoAndCache();
132148
await UserDirector.initializeUser(true);
133149
await OneSignal.User.PushSubscription._resubscribeToPushModelChanges();
@@ -158,7 +174,7 @@ export default class OneSignal {
158174
const pushSubModel = await this.coreDirector.getCurrentPushSubscriptionModel();
159175
await this.coreDirector.resetModelRepoAndCache();
160176
// add the push subscription model back to the repo since we need at least 1 sub to create a new user
161-
await this.coreDirector.add(ModelName.PushSubscriptions, pushSubModel as OSModel<SupportedModel>, false);
177+
this.coreDirector.add(ModelName.PushSubscriptions, pushSubModel as OSModel<SupportedModel>, false);
162178
await UserDirector.initializeUser(false);
163179
await OneSignal.User.PushSubscription._resubscribeToPushModelChanges();
164180
}

0 commit comments

Comments
 (0)