Skip to content

Commit 4c2728e

Browse files
committed
add idb with upgrade method
as get and set subscription database helpers add more database helpers clean up usage of new database helpers avoid top level await clean up logMethodCall usage have conditional methods for subscription manager
1 parent becfe8b commit 4c2728e

58 files changed

Lines changed: 1065 additions & 1719 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

__test__/support/environment/TestEnvironment.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import type {
44
ServerAppConfig,
55
} from 'src/shared/config/types';
66
import type { RecursivePartial } from 'src/shared/context/types';
7+
import { clearAll } from 'src/shared/database/client';
78
import MainHelper from 'src/shared/helpers/MainHelper';
89
import { DUMMY_ONESIGNAL_ID, DUMMY_PUSH_TOKEN } from '../../constants';
910
import { generateNewSubscription } from '../helpers/core';
1011
import {
1112
initOSGlobals,
12-
resetDatabase,
1313
stubDomEnvironment,
1414
stubNotification,
1515
} from './TestEnvironmentHelpers';
@@ -32,8 +32,7 @@ export interface TestEnvironmentConfig {
3232
export class TestEnvironment {
3333
static async initialize(config: TestEnvironmentConfig = {}) {
3434
// reset db & localStorage
35-
resetDatabase();
36-
35+
await clearAll();
3736
const oneSignal = await initOSGlobals(config);
3837

3938
if (config.useMockIdentityModel) {

__test__/support/environment/TestEnvironmentHelpers.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { type DOMWindow, JSDOM, ResourceLoader } from 'jsdom';
22
import CoreModule from 'src/core/CoreModule';
33
import { SubscriptionModel } from 'src/core/models/SubscriptionModel';
44
import { ModelChangeTags } from 'src/core/types/models';
5+
import { setPushId, setPushToken } from 'src/shared/database/subscription';
56
import {
67
NotificationType,
78
SubscriptionType,
@@ -14,7 +15,6 @@ import UserNamespace from '../../../src/onesignal/UserNamespace';
1415
import Context from '../../../src/page/models/Context';
1516
import { getSlidedownElement } from '../../../src/page/slidedown/SlidedownElement';
1617
import Emitter from '../../../src/shared/libraries/Emitter';
17-
import Database from '../../../src/shared/services/Database';
1818
import { CUSTOM_LINK_CSS_CLASSES } from '../../../src/shared/slidedown/constants';
1919
import {
2020
DEFAULT_USER_AGENT,
@@ -23,18 +23,11 @@ import {
2323
DUMMY_SUBSCRIPTION_ID_3,
2424
} from '../../constants';
2525
import MockNotification from '../mocks/MockNotification';
26-
import Random from '../utils/Random';
2726
import TestContext from './TestContext';
2827
import { type TestEnvironmentConfig } from './TestEnvironment';
2928

3029
declare const global: any;
3130

32-
export function resetDatabase() {
33-
// Erase and reset IndexedDb database name to something random
34-
Database.resetInstance();
35-
Database.databaseInstanceName = Random.getRandomString(10);
36-
}
37-
3831
export async function initOSGlobals(config: TestEnvironmentConfig = {}) {
3932
global.OneSignal = OneSignal;
4033
global.OneSignal.EVENTS = ONESIGNAL_EVENTS;
@@ -153,8 +146,8 @@ export const setupSubModelStore = async ({
153146
token,
154147
onesignalId,
155148
});
156-
await Database.setPushId(pushModel.id);
157-
await Database.setPushToken(pushModel.token);
149+
await setPushId(pushModel.id);
150+
await setPushToken(pushModel.token);
158151
OneSignal.coreDirector.subscriptionModelStore.replaceAll(
159152
[pushModel],
160153
ModelChangeTags.NO_PROPOGATE,

__test__/unit/notifications/permission.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function expectPermissionChangeEvent(
1919

2020
describe('Notifications namespace permission properties', () => {
2121
beforeEach(async () => {
22-
TestEnvironment.initialize();
22+
await TestEnvironment.initialize();
2323
});
2424

2525
afterEach(() => {

__test__/unit/pushSubscription/nativePermissionChange.test.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ import {
88
import { TestEnvironment } from '__test__/support/environment/TestEnvironment';
99
import { createPushSub } from '__test__/support/environment/TestEnvironmentHelpers';
1010
import { MockServiceWorker } from '__test__/support/mocks/MockServiceWorker';
11+
import { db } from 'src/shared/database/client';
12+
import { setAppState as setDBAppState } from 'src/shared/database/config';
1113
import * as PermissionUtils from 'src/shared/helpers/permissions';
1214
import Emitter from 'src/shared/libraries/Emitter';
1315
import { checkAndTriggerSubscriptionChanged } from 'src/shared/listeners';
1416
import { AppState } from 'src/shared/models/AppState';
15-
import Database from 'src/shared/services/Database';
1617
import MainHelper from '../../../src/shared/helpers/MainHelper';
1718
import { NotificationPermission } from '../../../src/shared/models/NotificationPermission';
1819

@@ -31,12 +32,12 @@ describe('Notification Types are set correctly on subscription change', () => {
3132
});
3233

3334
afterEach(async () => {
34-
await Database.remove('subscriptions');
35-
await Database.remove('Options');
35+
await db.clear('subscriptions');
36+
await db.clear('Options');
3637
});
3738

3839
const setDbPermission = async (permission: NotificationPermission) => {
39-
await Database.put('Options', {
40+
await db.put('Options', {
4041
key: 'notificationPermission',
4142
value: permission,
4243
});
@@ -77,10 +78,8 @@ describe('Notification Types are set correctly on subscription change', () => {
7778
await MainHelper.checkAndTriggerNotificationPermissionChanged();
7879

7980
// should update the db
80-
const dbPermission = await Database.get(
81-
'Options',
82-
'notificationPermission',
83-
);
81+
const dbPermission = (await db.get('Options', 'notificationPermission'))
82+
?.value;
8483
expect(dbPermission).toBe(NotificationPermission.Granted);
8584
expect(permChangeListener).toHaveBeenCalledWith(true);
8685
expect(permChangeStringListener).toHaveBeenCalledWith('granted');
@@ -89,11 +88,9 @@ describe('Notification Types are set correctly on subscription change', () => {
8988

9089
describe('checkAndTriggerSubscriptionChanged', async () => {
9190
const setAppState = async (appState: Partial<AppState>) => {
92-
const currentAppState = await Database.get<AppState>(
93-
'Options',
94-
'appState',
95-
);
96-
await Database.setAppState({
91+
const currentAppState = (await db.get('Options', 'appState'))
92+
?.value as AppState;
93+
await setDBAppState({
9794
...currentAppState,
9895
...appState,
9996
});

package-lock.json

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"description": "Web push notifications from OneSignal.",
55
"type": "module",
66
"dependencies": {
7+
"idb": "^8.0.3",
78
"jsonp": "github:OneSignal/jsonp#onesignal",
89
"uuid": "^11.1.0"
910
},
@@ -80,12 +81,12 @@
8081
},
8182
{
8283
"path": "./build/releases/OneSignalSDK.page.es6.js",
83-
"limit": "58.6 kB",
84+
"limit": "58 kB",
8485
"gzip": true
8586
},
8687
{
8788
"path": "./build/releases/OneSignalSDK.sw.js",
88-
"limit": "22.4 kB",
89+
"limit": "21.4 kB",
8990
"gzip": true
9091
},
9192
{

src/core/CoreModuleDirector.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import FuturePushSubscriptionRecord from 'src/page/userModel/FuturePushSubscriptionRecord';
2+
import { getPushToken, setPushId } from 'src/shared/database/subscription';
23
import { IDManager } from 'src/shared/managers/IDManager';
34
import {
45
SubscriptionChannel,
56
SubscriptionType,
67
} from 'src/shared/subscriptions/constants';
78
import type { SubscriptionChannelValue } from 'src/shared/subscriptions/types';
9+
import { logMethodCall } from 'src/shared/utils/utils';
810
import SubscriptionHelper from '../../src/shared/helpers/SubscriptionHelper';
911
import MainHelper from '../shared/helpers/MainHelper';
1012
import { RawPushSubscription } from '../shared/models/RawPushSubscription';
11-
import Database from '../shared/services/Database';
12-
import { logMethodCall } from '../shared/utils/utils';
1313
import CoreModule from './CoreModule';
1414
import { IdentityModel } from './models/IdentityModel';
1515
import { PropertiesModel } from './models/PropertiesModel';
@@ -61,7 +61,7 @@ export class CoreModuleDirector {
6161
new FuturePushSubscriptionRecord(rawPushSubscription).serialize(),
6262
);
6363
model.id = IDManager.createLocalId();
64-
Database.setPushId(model.id);
64+
setPushId(model.id);
6565

6666
// we enqueue a login operation w/ a create subscription operation the first time we generate/save a push subscription model
6767
this.core.subscriptionModelStore.add(model, ModelChangeTags.HYDRATE);
@@ -136,7 +136,7 @@ export class CoreModuleDirector {
136136
logMethodCall(
137137
'CoreModuleDirector.getPushSubscriptionModelByLastKnownToken',
138138
);
139-
const lastKnownPushToken = await Database.getPushToken();
139+
const lastKnownPushToken = await getPushToken();
140140
if (lastKnownPushToken) {
141141
return this.getSubscriptionOfTypeWithToken(
142142
SubscriptionChannel.Push,

src/core/executors/LoginUserOperationExecutor.test.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ import {
1818
setCreateUserError,
1919
setCreateUserResponse,
2020
} from '__test__/support/helpers/requests';
21-
import Database from 'src/shared/services/Database';
21+
import { clearAll } from 'src/shared/database/client';
22+
import { getPushId, setPushId } from 'src/shared/database/subscription';
2223
import {
2324
NotificationType,
2425
SubscriptionType,
@@ -55,7 +56,7 @@ describe('LoginUserOperationExecutor', () => {
5556
});
5657

5758
beforeEach(async () => {
58-
await Database.clear();
59+
await clearAll();
5960
identityModelStore = new IdentityModelStore();
6061
propertiesModelStore = new PropertiesModelStore();
6162
subscriptionModelStore = new SubscriptionModelStore();
@@ -165,7 +166,7 @@ describe('LoginUserOperationExecutor', () => {
165166
DUMMY_ONESIGNAL_ID,
166167
);
167168
propertiesModelStore.model.setProperty('onesignalId', DUMMY_ONESIGNAL_ID);
168-
await Database.setPushId(DUMMY_SUBSCRIPTION_ID);
169+
await setPushId(DUMMY_SUBSCRIPTION_ID);
169170

170171
const subscriptionModel = new SubscriptionModel();
171172
subscriptionModel.setProperty('id', DUMMY_SUBSCRIPTION_ID);
@@ -191,7 +192,7 @@ describe('LoginUserOperationExecutor', () => {
191192
expect(propertiesModelStore.model.getProperty('onesignalId')).toEqual(
192193
DUMMY_ONESIGNAL_ID_2,
193194
);
194-
expect(await Database.getPushId()).toEqual(DUMMY_SUBSCRIPTION_ID_2);
195+
expect(await getPushId()).toEqual(DUMMY_SUBSCRIPTION_ID_2);
195196
expect(subscriptionModel.getProperty('id')).toEqual(
196197
DUMMY_SUBSCRIPTION_ID_2,
197198
);

src/core/executors/LoginUserOperationExecutor.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ import {
33
ExecutionResult,
44
type IOperationExecutor,
55
} from 'src/core/types/operation';
6+
import { getConsentGiven } from 'src/shared/database/config';
7+
import {
8+
getPushId,
9+
setPushId,
10+
setPushToken,
11+
} from 'src/shared/database/subscription';
612
import { getTimeZoneId } from 'src/shared/helpers/general';
713
import { getConsentRequired } from 'src/shared/helpers/localStorage';
814
import {
@@ -11,7 +17,6 @@ import {
1117
} from 'src/shared/helpers/NetworkUtils';
1218
import Log from 'src/shared/libraries/Log';
1319
import { checkAndTriggerUserChanged } from 'src/shared/listeners';
14-
import Database from 'src/shared/services/Database';
1520
import { IdentityConstants, OPERATION_NAME } from '../constants';
1621
import { type IPropertiesModelKeys } from '../models/PropertiesModel';
1722
import { type IdentityModelStore } from '../modelStores/IdentityModelStore';
@@ -80,7 +85,7 @@ export class LoginUserOperationExecutor implements IOperationExecutor {
8085
operations: Operation[],
8186
): Promise<ExecutionResponse> {
8287
const consentRequired = getConsentRequired();
83-
const consentGiven = await Database.getConsentGiven();
88+
const consentGiven = await getConsentGiven();
8489

8590
if (consentRequired && !consentGiven) {
8691
throw new Error('Consent required but not given');
@@ -231,10 +236,10 @@ export class LoginUserOperationExecutor implements IOperationExecutor {
231236
if (!backendSub || !('id' in backendSub)) continue;
232237
idTranslations[localId] = backendSub.id;
233238

234-
const pushSubscriptionId = await Database.getPushId();
239+
const pushSubscriptionId = await getPushId();
235240
if (pushSubscriptionId === localId) {
236-
await Database.setPushId(backendSub.id);
237-
await Database.setPushToken(backendSub.token);
241+
await setPushId(backendSub.id);
242+
await setPushToken(backendSub.token);
238243
}
239244

240245
const model =

src/core/executors/RefreshUserOperationExecutor.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import {
1212
setGetUserError,
1313
setGetUserResponse,
1414
} from '__test__/support/helpers/requests';
15-
import Database from 'src/shared/services/Database';
15+
import { clearAll } from 'src/shared/database/client';
16+
import { setPushId } from 'src/shared/database/subscription';
1617
import {
1718
NotificationType,
1819
SubscriptionType,
@@ -40,7 +41,7 @@ vi.mock('src/shared/libraries/Log');
4041

4142
describe('RefreshUserOperationExecutor', () => {
4243
beforeEach(async () => {
43-
await Database.clear(); // in case subscription model (from previous tests) are loaded from db
44+
await clearAll(); // in case subscription model (from previous tests) are loaded from db
4445
identityModelStore = new IdentityModelStore();
4546
propertiesModelStore = new PropertiesModelStore();
4647
subscriptionModelStore = new SubscriptionModelStore();
@@ -181,7 +182,7 @@ describe('RefreshUserOperationExecutor', () => {
181182
pushSubModel.notification_types = NotificationType.Subscribed;
182183

183184
subscriptionModelStore.add(pushSubModel);
184-
await Database.setPushId(DUMMY_SUBSCRIPTION_ID_2);
185+
await setPushId(DUMMY_SUBSCRIPTION_ID_2);
185186

186187
const executor = getExecutor();
187188
const refreshOp = new RefreshUserOperation(APP_ID, DUMMY_ONESIGNAL_ID);

0 commit comments

Comments
 (0)