Skip to content

Commit

Permalink
fix: rewrite module without class
Browse files Browse the repository at this point in the history
- having the object creation was leading to bugs on the OutSystems side, as it wasn't a singleton, new instances of the object kept being created
- fixes unit tests accordingly

https://outsystemsrd.atlassian.net/browse/RMET-3477
  • Loading branch information
OS-martacarlos committed Jun 20, 2024
1 parent ee16feb commit caf4d5a
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 192 deletions.
4 changes: 2 additions & 2 deletions outsystems/nodes/ODC/initOneSignal.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var builder = window.plugins.OneSignal.startInit($parameters.ApiKey);
builder.handleNotificationReceived(OSOneSignal.Manager.notificationReceivedDelegate);
builder.handleNotificationOpened(OSOneSignal.Manager.notificationOpenedDelegate);
builder.handleNotificationReceived(OSOneSignal.notificationReceivedDelegate);
builder.handleNotificationOpened(OSOneSignal.notificationOpenedDelegate);
builder.inFocusDisplaying($parameters.InFocusDisplayOption);

// Set your iOS Settings
Expand Down
8 changes: 4 additions & 4 deletions outsystems/nodes/ODC/registerDispatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ var onReady = function(scope) {
}
});

OSOneSignal.Manager.setNotificationReceivedCallback(scope.handleNotificationReceived);
OSOneSignal.Manager.setNotificationOpenedCallback(scope.handleNotificationOpened);
OSOneSignal.setNotificationReceivedCallback(scope.handleNotificationReceived);
OSOneSignal.setNotificationOpenedCallback(scope.handleNotificationOpened);

}

var onDestroy = function(scope) {
console.log("onDestroy scope");
OSOneSignal.Manager.unregisterReceivedCallback(scope.handleNotificationReceived);
OSOneSignal.Manager.unregisterOpenedCallback(scope.handleNotificationOpened);
OSOneSignal.unregisterReceivedCallback(scope.handleNotificationReceived);
OSOneSignal.unregisterOpenedCallback(scope.handleNotificationOpened);
}

OSCommonPlugin.PluginManager.createScope(`OneSignalPlugin_${$parameters.Token}`, onReady, onDestroy);
53 changes: 24 additions & 29 deletions outsystems/scripts/ODC/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,26 @@
import { OneSignalCallback } from './definitions';

declare class OneSignalManagerClass {
#private;
/**
* Public delegate for OneSignal's handleNotificationReceived
*/
notificationReceivedDelegate(jsonData: any): void;
/**
* Public delegate for OneSignal's handleNotificationOpened
*/
notificationOpenedDelegate(jsonData: any): void;
/**
* Sets the current callback for OneSignal's Notification Received events.
*/
setNotificationReceivedCallback(callback: OneSignalCallback): void;
/**
* Sets the current callback for OneSignal's Notification Opened events.
*/
setNotificationOpenedCallback(callback: OneSignalCallback): void;
/**
* Unregister the old callback of OneSignal's Notification Received
*/
unregisterReceivedCallback(callback: OneSignalCallback): void;
/**
* Unregister the old callback of OneSignal's Notification Opened
*/
unregisterOpenedCallback(callback: OneSignalCallback): void;
}
export declare const Manager: OneSignalManagerClass;
export {};
/**
* Public delegate for OneSignal's handleNotificationReceived
*/
export declare function notificationReceivedDelegate(jsonData: any): void;
/**
* Public delegate for OneSignal's handleNotificationOpened
*/
export declare function notificationOpenedDelegate(jsonData: any): void;
/**
* Sets the current callback for OneSignal's Notification Received events.
*/
export declare function setNotificationReceivedCallback(callback: OneSignalCallback): void;
/**
* Sets the current callback for OneSignal's Notification Opened events.
*/
export declare function setNotificationOpenedCallback(callback: OneSignalCallback): void;
/**
* Unregister the old callback of OneSignal's Notification Received
*/
export declare function unregisterReceivedCallback(callback: OneSignalCallback): void;
/**
* Unregister the old callback of OneSignal's Notification Opened
*/
export declare function unregisterOpenedCallback(callback: OneSignalCallback): void;
95 changes: 34 additions & 61 deletions outsystems/scripts/ODC/manager.js
Original file line number Diff line number Diff line change
@@ -1,69 +1,42 @@
(function(global, factory) {
typeof exports === "object" && typeof module !== "undefined" ? factory(exports) : typeof define === "function" && define.amd ? define(["exports"], factory) : (global = typeof globalThis !== "undefined" ? globalThis : global || self, factory(global.OSOneSignal = {}));
})(this, function(exports2) {
"use strict";var __typeError = (msg) => {
throw TypeError(msg);
};
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);

var _notificationReceivedCallback, _notificationOpenedCallback;
class OneSignalManagerClass {
constructor() {
__privateAdd(this, _notificationReceivedCallback, []);
__privateAdd(this, _notificationOpenedCallback, []);
}
/**
* Public delegate for OneSignal's handleNotificationReceived
*/
notificationReceivedDelegate(jsonData) {
__privateGet(this, _notificationReceivedCallback).forEach(function(callback) {
callback.call(void 0, jsonData);
});
}
/**
* Public delegate for OneSignal's handleNotificationOpened
*/
notificationOpenedDelegate(jsonData) {
__privateGet(this, _notificationOpenedCallback).forEach(function(callback) {
callback.call(void 0, jsonData);
});
}
/**
* Sets the current callback for OneSignal's Notification Received events.
*/
setNotificationReceivedCallback(callback) {
__privateGet(this, _notificationReceivedCallback).push(callback);
}
/**
* Sets the current callback for OneSignal's Notification Opened events.
*/
setNotificationOpenedCallback(callback) {
__privateGet(this, _notificationOpenedCallback).push(callback);
}
/**
* Unregister the old callback of OneSignal's Notification Received
*/
unregisterReceivedCallback(callback) {
var cbIndex = __privateGet(this, _notificationReceivedCallback).indexOf(callback);
if (cbIndex >= 0) {
delete __privateGet(this, _notificationReceivedCallback)[cbIndex];
}
"use strict";
var notificationReceivedCallback = [];
var notificationOpenedCallback = [];
function notificationReceivedDelegate(jsonData) {
notificationReceivedCallback.forEach(function(callback) {
callback(jsonData);
});
}
function notificationOpenedDelegate(jsonData) {
notificationOpenedCallback.forEach(function(callback) {
callback(jsonData);
});
}
function setNotificationReceivedCallback(callback) {
notificationReceivedCallback.push(callback);
}
function setNotificationOpenedCallback(callback) {
notificationOpenedCallback.push(callback);
}
function unregisterReceivedCallback(callback) {
var cbIndex = notificationReceivedCallback.indexOf(callback);
if (cbIndex >= 0) {
delete notificationReceivedCallback[cbIndex];
}
/**
* Unregister the old callback of OneSignal's Notification Opened
*/
unregisterOpenedCallback(callback) {
var cbIndex = __privateGet(this, _notificationOpenedCallback).indexOf(callback);
if (cbIndex >= 0) {
delete __privateGet(this, _notificationOpenedCallback)[cbIndex];
}
}
function unregisterOpenedCallback(callback) {
var cbIndex = notificationOpenedCallback.indexOf(callback);
if (cbIndex >= 0) {
delete notificationOpenedCallback[cbIndex];
}
}
_notificationReceivedCallback = new WeakMap();
_notificationOpenedCallback = new WeakMap();
const Manager = new OneSignalManagerClass();
exports2.Manager = Manager;
exports2.notificationOpenedDelegate = notificationOpenedDelegate;
exports2.notificationReceivedDelegate = notificationReceivedDelegate;
exports2.setNotificationOpenedCallback = setNotificationOpenedCallback;
exports2.setNotificationReceivedCallback = setNotificationReceivedCallback;
exports2.unregisterOpenedCallback = unregisterOpenedCallback;
exports2.unregisterReceivedCallback = unregisterReceivedCallback;
Object.defineProperty(exports2, Symbol.toStringTag, { value: "Module" });
});
102 changes: 49 additions & 53 deletions src/outsystems/index.ts
Original file line number Diff line number Diff line change
@@ -1,63 +1,59 @@
import { OneSignalCallback } from "./definitions";

class OneSignalManagerClass {

#notificationReceivedCallback: OneSignalCallback[] = [];
#notificationOpenedCallback: OneSignalCallback[] = [];

/**
* Public delegate for OneSignal's handleNotificationReceived
*/
notificationReceivedDelegate(jsonData: any) {
this.#notificationReceivedCallback.forEach(function(callback:OneSignalCallback){
callback.call(undefined, jsonData);
});
}
var notificationReceivedCallback: OneSignalCallback[] = [];
var notificationOpenedCallback: OneSignalCallback[] = [];

/**
* Public delegate for OneSignal's handleNotificationReceived
*/
export function notificationReceivedDelegate(jsonData: any) {
notificationReceivedCallback.forEach(function(callback:OneSignalCallback){
callback(jsonData);
});
}

/**
* Public delegate for OneSignal's handleNotificationOpened
*/
notificationOpenedDelegate(jsonData: any) {
this.#notificationOpenedCallback.forEach(function(callback:OneSignalCallback){
callback.call(undefined, jsonData);
});
}
/**
* Public delegate for OneSignal's handleNotificationOpened
*/
export function notificationOpenedDelegate(jsonData: any) {
notificationOpenedCallback.forEach(function(callback:OneSignalCallback){
callback(jsonData);
});
}

/**
* Sets the current callback for OneSignal's Notification Received events.
*/
setNotificationReceivedCallback(callback:OneSignalCallback) {
this.#notificationReceivedCallback.push(callback);
}
/**
* Sets the current callback for OneSignal's Notification Received events.
*/
export function setNotificationReceivedCallback(callback:OneSignalCallback) {
notificationReceivedCallback.push(callback);
}

/**
* Sets the current callback for OneSignal's Notification Opened events.
*/
setNotificationOpenedCallback(callback:OneSignalCallback) {
this.#notificationOpenedCallback.push(callback);
}
/**
* Sets the current callback for OneSignal's Notification Opened events.
*/
export function setNotificationOpenedCallback(callback:OneSignalCallback) {
notificationOpenedCallback.push(callback);
}

/**
* Unregister the old callback of OneSignal's Notification Received
*/
unregisterReceivedCallback(callback:OneSignalCallback) {
var cbIndex = this.#notificationReceivedCallback.indexOf(callback);
if(cbIndex >= 0) {
//this.#notificationReceivedCallback[cbIndex] = undefined;
delete this.#notificationReceivedCallback[cbIndex];
}
/**
* Unregister the old callback of OneSignal's Notification Received
*/
export function unregisterReceivedCallback(callback:OneSignalCallback) {
var cbIndex = notificationReceivedCallback.indexOf(callback);
if(cbIndex >= 0) {
//notificationReceivedCallback[cbIndex] = undefined;
delete notificationReceivedCallback[cbIndex];
}
}

/**
* Unregister the old callback of OneSignal's Notification Opened
*/
unregisterOpenedCallback(callback:OneSignalCallback) {
var cbIndex = this.#notificationOpenedCallback.indexOf(callback);
if(cbIndex >= 0) {
//this.#notificationOpenedCallback[cbIndex] = undefined;
delete this.#notificationOpenedCallback[cbIndex];
}
/**
* Unregister the old callback of OneSignal's Notification Opened
*/
export function unregisterOpenedCallback(callback:OneSignalCallback) {
var cbIndex = notificationOpenedCallback.indexOf(callback);
if(cbIndex >= 0) {
//notificationOpenedCallback[cbIndex] = undefined;
delete notificationOpenedCallback[cbIndex];
}
}

export const Manager = new OneSignalManagerClass();
}
44 changes: 2 additions & 42 deletions test/onesignalmanager.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

import { Manager } from '../src/outsystems/index'
import * as Manager from '../src/outsystems/index'

describe("One Signal Manager: Notification Received Callback Tests", () => {

Expand Down Expand Up @@ -135,50 +135,10 @@ describe("One Signal Manager: Both Callback Tests", () => {
expect(receivedCallback.mock.calls.length).toBe(0);

Manager.notificationReceivedDelegate(1);

expect(receivedCallback).toHaveBeenCalledWith(1);
expect(receivedCallback.mock.calls.length).toBe(1);
expect(openCallback.mock.calls.length).toBe(1);
});

it("if multiple Opened callback are registered, they should be called accordignly", () => {

let mockCallback1 = jest.fn((num: number) => num++);
let mockCallback2 = jest.fn((num: number) => num++);

Manager.setNotificationOpenedCallback(mockCallback1);
Manager.setNotificationOpenedCallback(mockCallback2);

expect(mockCallback1.mock.calls.length).toBe(0);
expect(mockCallback2.mock.calls.length).toBe(0);

Manager.notificationOpenedDelegate(0);

expect(mockCallback1.mock.calls.length).toBe(1);
expect(mockCallback2.mock.calls.length).toBe(1);

expect(mockCallback1).toHaveBeenCalledWith(0);
expect(mockCallback2).toHaveBeenCalledWith(0);
});

it("if multiple Opened callback are registered, but one is removed, only one should be called", () => {

let mockCallback1 = jest.fn((num: number) => num++);
let mockCallback2 = jest.fn((num: number) => num++);

Manager.setNotificationOpenedCallback(mockCallback1);
Manager.setNotificationOpenedCallback(mockCallback2);

expect(mockCallback1.mock.calls.length).toBe(0);
expect(mockCallback2.mock.calls.length).toBe(0);

Manager.unregisterOpenedCallback(mockCallback1);
Manager.notificationOpenedDelegate(0);

expect(mockCallback1.mock.calls.length).toBe(0);
expect(mockCallback2.mock.calls.length).toBe(1);

expect(mockCallback2).toHaveBeenCalledWith(0);
});

});
2 changes: 1 addition & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default defineConfig({
build: {
minify: false,
outDir: 'outsystems/scripts/ODC',
target: 'es2020',
target: 'modules',
lib: {
entry: './src/outsystems/index.ts',
name: 'OSOneSignal',
Expand Down

0 comments on commit caf4d5a

Please sign in to comment.