Skip to content

Commit 3a66d99

Browse files
committed
refactor(auth): inline userDirector functions
1 parent 45467c8 commit 3a66d99

5 files changed

Lines changed: 50 additions & 68 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
},
8282
{
8383
"path": "./build/releases/OneSignalSDK.page.es6.js",
84-
"limit": "43.793 kB",
84+
"limit": "43.75 kB",
8585
"gzip": true
8686
},
8787
{

src/onesignal/OneSignal.test.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -819,9 +819,12 @@ describe('OneSignal - No Consent Required', () => {
819819
await promise;
820820

821821
// accepting permissions patches the existing subscription with the token
822-
await vi.waitUntil(() => updateSubscriptionFn.mock.calls.length >= 1, {
823-
interval: 1,
824-
});
822+
await vi.waitUntil(
823+
() => updateSubscriptionFn.mock.calls.length >= 1,
824+
{
825+
interval: 1,
826+
},
827+
);
825828

826829
let pushSub: SubscriptionSchema | undefined;
827830
await vi.waitUntil(

src/onesignal/userDirector.ts

Lines changed: 0 additions & 57 deletions
This file was deleted.

src/page/managers/LoginManager.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import { IdentityConstants } from 'src/core/constants';
2+
import { IdentityModel } from 'src/core/models/IdentityModel';
3+
import { PropertiesModel } from 'src/core/models/PropertiesModel';
24
import { SubscriptionModel } from 'src/core/models/SubscriptionModel';
35
import { LoginUserOperation } from 'src/core/operations/LoginUserOperation';
46
import { TransferSubscriptionOperation } from 'src/core/operations/TransferSubscriptionOperation';
57
import { ModelChangeTags } from 'src/core/types/models';
68
import { db } from 'src/shared/database/client';
79
import { getSubscriptionType } from 'src/shared/environment/detect';
810
import { getAppId } from 'src/shared/helpers/main';
11+
import Log from 'src/shared/libraries/Log';
912
import { IDManager } from 'src/shared/managers/IDManager';
10-
import { resetUserModels } from '../../onesignal/userDirector';
11-
import Log from '../../shared/libraries/Log';
1213

1314
export default class LoginManager {
1415
// Other internal classes should await on this if they access users
@@ -73,7 +74,16 @@ export default class LoginManager {
7374
}
7475

7576
private static _resetAndGetIdentityModel() {
76-
resetUserModels();
77+
const newIdentityModel = new IdentityModel();
78+
const newPropertiesModel = new PropertiesModel();
79+
80+
const sdkId = IDManager._createLocalId();
81+
newIdentityModel._onesignalId = sdkId;
82+
newPropertiesModel._onesignalId = sdkId;
83+
84+
OneSignal._coreDirector._identityModelStore._replace(newIdentityModel);
85+
OneSignal._coreDirector._propertiesModelStore._replace(newPropertiesModel);
86+
7787
return OneSignal._coreDirector._getIdentityModel();
7888
}
7989

src/shared/managers/subscription/page.ts

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import { createUserOnServer } from 'src/onesignal/userDirector';
1+
import type { SubscriptionModel } from 'src/core/models/SubscriptionModel';
2+
import { CreateSubscriptionOperation } from 'src/core/operations/CreateSubscriptionOperation';
3+
import { LoginUserOperation } from 'src/core/operations/LoginUserOperation';
24
import LoginManager from 'src/page/managers/LoginManager';
35
import FuturePushSubscriptionRecord from 'src/page/userModel/FuturePushSubscriptionRecord';
46
import type { ContextInterface } from 'src/shared/context/types';
@@ -12,6 +14,7 @@ import {
1214
PermissionBlockedError,
1315
SWRegistrationError,
1416
} from 'src/shared/errors/common';
17+
import { getAppId } from 'src/shared/helpers/main';
1518
import {
1619
incrementPageViewCount,
1720
isFirstPageView,
@@ -48,25 +51,48 @@ function executeCallback<T>(callback?: (...args: any[]) => T, ...args: any[]) {
4851
}
4952
}
5053

54+
async function createSubscribedUser(
55+
pushModel: SubscriptionModel,
56+
): Promise<void> {
57+
const identityModel = OneSignal._coreDirector._getIdentityModel();
58+
const appId = getAppId();
59+
60+
OneSignal._coreDirector._operationRepo._enqueue(
61+
new LoginUserOperation(
62+
appId,
63+
identityModel._onesignalId,
64+
identityModel._externalId,
65+
),
66+
);
67+
await OneSignal._coreDirector._operationRepo._enqueueAndWait(
68+
new CreateSubscriptionOperation({
69+
...pushModel.toJSON(),
70+
appId,
71+
onesignalId: identityModel._onesignalId,
72+
subscriptionId: pushModel.id!,
73+
}),
74+
);
75+
}
76+
5177
export const updatePushSubscriptionModelWithRawSubscription = async (
5278
rawPushSubscription: RawPushSubscription,
5379
) => {
5480
// incase a login op was called before user accepts the notifcations permissions, we need to wait for it to finish
5581
// otherwise there would be two login ops in the same bucket for LoginOperationExecutor which would error
5682
await LoginManager._switchingUsersPromise;
5783

84+
// for new Anonymous/not-logged-in users, we need to create a new push subscription model and also save its push id to IndexedDB
5885
let pushModel = await OneSignal._coreDirector._getPushSubscriptionModel();
59-
// for new users, we need to create a new push subscription model and also save its push id to IndexedDB
6086
if (!pushModel) {
6187
pushModel =
6288
OneSignal._coreDirector._generatePushSubscriptionModel(
6389
rawPushSubscription,
6490
);
65-
return createUserOnServer();
91+
return createSubscribedUser(pushModel);
6692
}
6793
// for users with data failed to create a user or user + subscription on the server
6894
if (IDManager._isLocalId(pushModel.id)) {
69-
return createUserOnServer();
95+
return createSubscribedUser(pushModel);
7096
}
7197

7298
// in case of notification state changes, we need to update its web_auth, web_p256, and other keys

0 commit comments

Comments
 (0)