Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions __test__/support/environment/TestEnvironmentHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { SubscriptionType } from 'src/shared/subscriptions/constants';
import { CoreModuleDirector } from '../../../src/core/CoreModuleDirector';
import NotificationsNamespace from '../../../src/onesignal/NotificationsNamespace';
import OneSignal from '../../../src/onesignal/OneSignal';
import { ONESIGNAL_EVENTS } from '../../../src/onesignal/OneSignalEvents';
import User from '../../../src/onesignal/User';
import UserNamespace from '../../../src/onesignal/UserNamespace';
import Context from '../../../src/page/models/Context';
Expand All @@ -23,7 +22,6 @@ declare const global: {

export function initOSGlobals(config: TestEnvironmentConfig = {}) {
global.OneSignal = OneSignal;
global.OneSignal.EVENTS = ONESIGNAL_EVENTS;
global.OneSignal.config = TestContext.getFakeMergedConfig(config);
global.OneSignal._context = new Context(global.OneSignal.config);
global.OneSignal._initialized = true;
Expand Down
7 changes: 0 additions & 7 deletions __test__/unit/events/eventsUnique.test.ts

This file was deleted.

2 changes: 1 addition & 1 deletion __test__/unit/push/registerForPush.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('Register for push', () => {
const promise = OneSignal.User.PushSubscription.optIn();

expect(spy).not.toHaveBeenCalled();
OneSignalEvent._trigger(OneSignal.EVENTS.SDK_INITIALIZED);
OneSignalEvent._trigger('initializeInternal');
await promise;
expect(OneSignal._initialized).toBe(true);
expect(spy).toHaveBeenCalledTimes(1);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
},
{
"path": "./build/releases/OneSignalSDK.page.es6.js",
"limit": "46.23 kB",
"limit": "45.63 kB",
"gzip": true
},
{
Expand Down
11 changes: 4 additions & 7 deletions src/onesignal/NotificationsNamespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,10 @@ export default class NotificationsNamespace extends EventListenerBase {
this._permission = permissionNative === 'granted';

if (typeof OneSignal !== 'undefined') {
OneSignal._emitter.on(
OneSignal.EVENTS.NOTIFICATION_PERMISSION_CHANGED_AS_STRING,
(permissionNative: NotificationPermission) => {
this._permissionNative = permissionNative;
this._permission = permissionNative === 'granted';
},
);
OneSignal._emitter.on('permissionChangeAsString', (permissionNative) => {
this._permissionNative = permissionNative;
this._permission = permissionNative === 'granted';
});
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/onesignal/OneSignal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@

test('login then accept web push permissions - it should make two user calls', async () => {
const { promise, resolve } = Promise.withResolvers();
OneSignal._emitter.on(OneSignal.EVENTS.SUBSCRIPTION_CHANGED, resolve);
OneSignal._emitter.on('change', resolve);
setGetUserResponse();
setCreateUserResponse({
onesignalId: ONESIGNAL_ID,
Expand Down Expand Up @@ -1203,4 +1203,4 @@
);

const showLocalNotificationSpy = vi.spyOn(MainHelper, 'showLocalNotification');
showLocalNotificationSpy.mockImplementation(async () => {});

Check failure on line 1206 in src/onesignal/OneSignal.test.ts

View workflow job for this annotation

GitHub Actions / test

Unexpected empty async arrow function
12 changes: 3 additions & 9 deletions src/onesignal/OneSignal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,11 @@
import Log from '../shared/libraries/Log';
import DebugNamespace from './DebugNamesapce';
import NotificationsNamespace from './NotificationsNamespace';
import { ONESIGNAL_EVENTS } from './OneSignalEvents';
import { SessionNamespace } from './SessionNamespace';
import SlidedownNamespace from './SlidedownNamespace';
import UserNamespace from './UserNamespace';

export default class OneSignal {
static EVENTS = ONESIGNAL_EVENTS;

static _consentGiven = false;

private static async _initializeCoreModuleAndOSNamespaces() {
Expand Down Expand Up @@ -176,14 +173,11 @@
OneSignal._initAlreadyCalled = true;

OneSignal._emitter.on(
OneSignal.EVENTS.NOTIFICATION_PERMISSION_CHANGED_AS_STRING,
'permissionChangeAsString',
checkAndTriggerSubscriptionChanged,
);
OneSignal._emitter.on(
OneSignal.EVENTS.SUBSCRIPTION_CHANGED,
_onSubscriptionChanged,
);
OneSignal._emitter.on(OneSignal.EVENTS.SDK_INITIALIZED, onSdkInitialized);
OneSignal._emitter.on('change', _onSubscriptionChanged);
OneSignal._emitter.on('initializeInternal', onSdkInitialized);

window.addEventListener('focus', () => {
// Checks if permission changed every time a user focuses on the page,
Expand Down Expand Up @@ -277,7 +271,7 @@
private static _pendingInit = true;

static _emitter: Emitter = new Emitter();
static _cache: any = {};

Check warning on line 274 in src/onesignal/OneSignal.ts

View workflow job for this annotation

GitHub Actions / test

Unexpected any. Specify a different type
static _initCalled = false;
static _initAlreadyCalled = false;
static _context: Context;
Expand Down
28 changes: 11 additions & 17 deletions src/onesignal/PushSubscriptionNamespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
onInternalSubscriptionSet,
} from 'src/shared/listeners';
import { IDManager } from 'src/shared/managers/IDManager';
import type { SubscriptionChangeEvent } from '../page/models/SubscriptionChangeEvent';
import type { EventsMap } from 'src/shared/services/types';
import { EventListenerBase } from '../page/userModel/EventListenerBase';
import Log from '../shared/libraries/Log';
import { isCompleteSubscriptionObject } from '../shared/managers/utils';
Expand Down Expand Up @@ -56,20 +56,14 @@ export default class PushSubscriptionNamespace extends EventListenerBase {
Log._error(e);
});

OneSignal._emitter.on(
OneSignal.EVENTS.SUBSCRIPTION_CHANGED,
async (change: SubscriptionChangeEvent | undefined) => {
this._id = change?.current.id;
this._token = change?.current.token;
},
);

OneSignal._emitter.on(
OneSignal.EVENTS.NOTIFICATION_PERMISSION_CHANGED_AS_STRING,
async (permission: NotificationPermission) => {
this._permission = permission;
},
);
OneSignal._emitter.on('change', async (change) => {
this._id = change?.current.id;
this._token = change?.current.token;
});

OneSignal._emitter.on('permissionChangeAsString', async (permission) => {
this._permission = permission;
});
}

get id(): string | null | undefined {
Expand Down Expand Up @@ -110,14 +104,14 @@ export default class PushSubscriptionNamespace extends EventListenerBase {

addEventListener(
event: 'change',
listener: (change: SubscriptionChangeEvent) => void,
listener: (change: EventsMap['change']) => void,
): void {
OneSignal._emitter.on(event, listener);
}

removeEventListener(
event: 'change',
listener: (change: SubscriptionChangeEvent) => void,
listener: (change: EventsMap['change']) => void,
): void {
OneSignal._emitter.off(event, listener);
}
Expand Down
5 changes: 3 additions & 2 deletions src/onesignal/UserNamespace.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { UserEventsMap } from 'src/shared/services/types';
import type { UserChangeEvent } from '../page/models/UserChangeEvent';
import { EventListenerBase } from '../page/userModel/EventListenerBase';
import Emitter from '../shared/libraries/Emitter';
Expand All @@ -10,7 +11,7 @@ export default class UserNamespace extends EventListenerBase {

readonly PushSubscription = new PushSubscriptionNamespace(false);

static _emitter = new Emitter();
static _emitter = new Emitter<UserEventsMap>();

constructor(
initialize: boolean,
Expand Down Expand Up @@ -111,7 +112,7 @@ export default class UserNamespace extends EventListenerBase {

addEventListener(
event: 'change',
listener: (userChange: UserChangeEvent) => void,
listener: (userChange: UserEventsMap['change']) => void,
): void {
UserNamespace._emitter.on(event, listener);
}
Expand Down
68 changes: 30 additions & 38 deletions src/page/bell/Bell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,13 @@
import OneSignalEvent from '../../shared/services/OneSignalEvent';
import { once } from '../../shared/utils/utils';
import { DismissPrompt } from '../models/Dismiss';
import type { SubscriptionChangeEvent } from '../models/SubscriptionChangeEvent';
import { ResourceLoadState } from '../services/DynamicResourceLoader';
import Badge from './Badge';
import Button from './Button';
import Dialog from './Dialog';
import Launcher from './Launcher';
import Message from './Message';
import {
BellEvent,
BellState,
type BellStateValue,
MESSAGE_TIMEOUT,
Expand Down Expand Up @@ -173,7 +171,7 @@

private _installEventHooks() {
// Install event hooks
OneSignal._emitter.on(BellEvent._SubscribeClick, () => {
OneSignal._emitter.on('notifyButtonSubscribeClick', () => {
const subscribeButton = this._dialog._subscribeButton;
if (subscribeButton) {
subscribeButton.disabled = true;
Expand Down Expand Up @@ -206,7 +204,7 @@
});
});

OneSignal._emitter.on(BellEvent._UnsubscribeClick, () => {
OneSignal._emitter.on('notifyButtonUnsubscribeClick', () => {
const unsubscribeButton = this._dialog._unsubscribeButton;
if (unsubscribeButton) {
unsubscribeButton.disabled = true;
Expand Down Expand Up @@ -234,7 +232,7 @@
});
});

OneSignal._emitter.on(BellEvent._Hovering, () => {
OneSignal._emitter.on('notifyButtonHovering', () => {
this._hovering = true;
this._launcher._activateIfInactive();

Expand All @@ -254,7 +252,7 @@
new Promise<void>((resolve) => {
// If a message is being shown
if (this._message._queued.length > 0) {
return this._message._dequeue().then((msg: any) => {

Check warning on line 255 in src/page/bell/Bell.ts

View workflow job for this annotation

GitHub Actions / test

Unexpected any. Specify a different type
this._message._content = msg;
this._message._contentType = MessageType._Queued;
resolve();
Expand All @@ -278,7 +276,7 @@
});
});

OneSignal._emitter.on(BellEvent._Hovered, () => {
OneSignal._emitter.on('notifyButtonHover', () => {
// If a message is displayed (and not a tip), don't control it. Visitors have no control over messages
if (this._message._contentType === MessageType._Message) {
return;
Expand Down Expand Up @@ -317,34 +315,31 @@
}
});

OneSignal._emitter.on(
OneSignal.EVENTS.SUBSCRIPTION_CHANGED,
async (isSubscribed: SubscriptionChangeEvent) => {
if (isSubscribed.current.optedIn) {
if (this._badge._shown && this._options.prenotify) {
this._badge._hide();
}
if (this._dialog._notificationIcons === null) {
const icons = await getNotificationIcons();
this._dialog._notificationIcons = icons;
}
OneSignal._emitter.on('change', async (isSubscribed) => {
if (isSubscribed?.current.optedIn) {
if (this._badge._shown && this._options.prenotify) {
this._badge._hide();
}

const permission =
await OneSignal._context._permissionManager._getPermissionStatus();
let bellState: BellStateValue;
if (isSubscribed.current.optedIn) {
bellState = BellState._Subscribed;
} else if (permission === 'denied') {
bellState = BellState._Blocked;
} else {
bellState = BellState._Unsubscribed;
if (this._dialog._notificationIcons === null) {
const icons = await getNotificationIcons();
this._dialog._notificationIcons = icons;
}
this._setState(bellState, this._ignoreSubscriptionState);
},
);
}

const permission =
await OneSignal._context._permissionManager._getPermissionStatus();
let bellState: BellStateValue;
if (isSubscribed?.current.optedIn) {
bellState = BellState._Subscribed;
} else if (permission === 'denied') {
bellState = BellState._Blocked;
} else {
bellState = BellState._Unsubscribed;
}
this._setState(bellState, this._ignoreSubscriptionState);
});

OneSignal._emitter.on(BellEvent._StateChanged, (state) => {
OneSignal._emitter.on('notifyButtonStateChange', (state) => {
if (!this._launcher._element) {
// Notify button doesn't exist
return;
Expand All @@ -359,12 +354,9 @@
}
});

OneSignal._emitter.on(
OneSignal.EVENTS.NOTIFICATION_PERMISSION_CHANGED_AS_STRING,
() => {
this._updateState();
},
);
OneSignal._emitter.on('permissionChangeAsString', () => {
this._updateState();
});
}

private _addDefaultClasses() {
Expand Down Expand Up @@ -721,7 +713,7 @@
const lastState = this._state;
this._state = newState;
if (lastState !== newState && !silent) {
OneSignalEvent._trigger(BellEvent._StateChanged, {
OneSignalEvent._trigger('notifyButtonStateChange', {
from: lastState,
to: newState,
});
Expand Down
12 changes: 6 additions & 6 deletions src/page/bell/Button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
import OneSignalEvent from 'src/shared/services/OneSignalEvent';
import AnimatedElement from './AnimatedElement';
import type Bell from './Bell';
import { BellEvent, MESSAGE_TIMEOUT, MessageType } from './constants';
import { MESSAGE_TIMEOUT, MessageType } from './constants';

export default class Button extends AnimatedElement {
public _isHandlingClick: boolean = false;

Check failure on line 14 in src/page/bell/Button.ts

View workflow job for this annotation

GitHub Actions / test

Type boolean trivially inferred from a boolean literal, remove type annotation
public _events: Record<string, string>;
public _bell: Bell;

Expand Down Expand Up @@ -73,14 +73,14 @@
limitIsEmpty(this._events.mouse) ||
limitGetLast(this._events.mouse) === 'out'
) {
OneSignalEvent._trigger(BellEvent._Hovering);
OneSignalEvent._trigger('notifyButtonHovering');
}
limitStorePut(this._events.mouse, 'over');
}

_onHovered() {
limitStorePut(this._events.mouse, 'out');
OneSignalEvent._trigger(BellEvent._Hovered);
OneSignalEvent._trigger('notifyButtonHover');
}

_onTap() {
Expand All @@ -99,8 +99,8 @@
if (this._isHandlingClick) return;
this._isHandlingClick = true;

OneSignalEvent._trigger(BellEvent._BellClick);
OneSignalEvent._trigger(BellEvent._LauncherClick);
OneSignalEvent._trigger('notifyButtonButtonClick');
OneSignalEvent._trigger('notifyButtonLauncherClick');

try {
if (
Expand All @@ -116,7 +116,7 @@
// The user is actually subscribed, register him for notifications
registerForPushNotifications();
this._bell._ignoreSubscriptionState = true;
OneSignal._emitter.once(OneSignal.EVENTS.SUBSCRIPTION_CHANGED, () => {
OneSignal._emitter.once('change', () => {
this._bell._message
._display(
MessageType._Message,
Expand Down
6 changes: 3 additions & 3 deletions src/page/bell/Dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { getPlatformNotificationIcon } from 'src/shared/utils/utils';
import OneSignalEvent from '../../shared/services/OneSignalEvent';
import AnimatedElement from './AnimatedElement';
import type Bell from './Bell';
import { BellEvent, BellState } from './constants';
import { BellState } from './constants';

const STATIC_RESOURCES_URL = new URL('https://media.onesignal.com/web-sdk');

Expand Down Expand Up @@ -132,12 +132,12 @@ export default class Dialog extends AnimatedElement {
a notification shown in this resubscription case.
*/
OneSignal._doNotShowWelcomeNotification = false;
OneSignalEvent._trigger(BellEvent._SubscribeClick);
OneSignalEvent._trigger('notifyButtonSubscribeClick');
});
}
if (this._unsubscribeButton) {
this._unsubscribeButton.addEventListener('click', () =>
OneSignalEvent._trigger(BellEvent._UnsubscribeClick),
OneSignalEvent._trigger('notifyButtonUnsubscribeClick'),
);
}
this._bell._setCustomColorsIfSpecified();
Expand Down
Loading
Loading