Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw errors #12

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
78 changes: 63 additions & 15 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,32 @@ module.exports = {
};

// To be sure that start is called only once
let starting = false;
let started = false;
let webContentses = [];

// To be call from the main process
function setup(webContents) {
function setup(webContents, {throwErrors} = {}) {
addWebConents(webContents)

// Will be called by the renderer process
ipcMain.on(START_NOTIFICATION_SERVICE, async (_, senderId) => {
// Retrieve saved credentials
let credentials = config.get('credentials');
// Retrieve saved senderId
const savedSenderId = config.get('senderId');
if (started) {
webContents.send(NOTIFICATION_SERVICE_STARTED, (credentials.fcm || {}).token);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Accessing 'fcm' from 'credentials' object will results in error when 'credentials' object is null.

can we re-write like ((credentials && credentials.fcm) || {}).token)

try {
webContents.send(NOTIFICATION_SERVICE_STARTED, (credentials.fcm || {}).token);
} catch (e) {
console.error('PUSH_RECEIVER:::Error while sending to webContents', e);
}
return;
}
started = true;
if (starting) {
return
}
starting = true;
try {
// Retrieve saved persistentId : avoid receiving all already received notifications on start
const persistentIds = config.get('persistentIds') || [];
Expand All @@ -46,28 +57,65 @@ function setup(webContents) {
config.set('credentials', credentials);
// Save senderId
config.set('senderId', senderId);
// Notify the renderer process that the FCM token has changed
webContents.send(TOKEN_UPDATED, credentials.fcm.token);
// Notify the renderer processes that the FCM token has changed
send(TOKEN_UPDATED, credentials.fcm.token);
}
// Listen for GCM/FCM notifications
await listen(Object.assign({}, credentials, { persistentIds }), onNotification(webContents));
// Notify the renderer process that we are listening for notifications
webContents.send(NOTIFICATION_SERVICE_STARTED, credentials.fcm.token);
} catch (e) {
console.error('PUSH_RECEIVER:::Error while starting the service', e);
// Forward error to the renderer process
webContents.send(NOTIFICATION_SERVICE_ERROR, e.message);
let client = await listen(Object.assign({}, credentials, { persistentIds }), onNotification(webContents));
client.on('parserError', (error) => {
console.error('PUSH_RECEIVER:::Error from parser', error);
send(NOTIFICATION_SERVICE_ERROR, error.message);
if (throwErrors) {
throw error
}
})
// Notify the renderer processes that we are listening for notifications
send(NOTIFICATION_SERVICE_STARTED, credentials.fcm.token);
started = true;
} catch (error) {
console.error('PUSH_RECEIVER:::Error while starting the service', error);
// Forward error to the renderer processes
send(NOTIFICATION_SERVICE_ERROR, error.message);
starting = false;
started = false;
if (throwErrors) {
throw error
}
}
});
}

function addWebConents(webContents) {
webContentses.push(webContents)
webContents.on('destroyed', () => {
removeWebContents(webContents)
})
}

function removeWebContents(webContents) {
let i = webContentses.indexOf(webContents)
if (i > -1) {
webContentses.splice(i, 1)
}
}

function send(channel, arg1) {
webContentses.forEach((webContents) => {
try {
webContents.send(channel, arg1)
} catch (e) {
console.error('PUSH_RECEIVER:::Error while sending to webContents', e);
}
})
}

// Will be called on new notification
function onNotification(webContents) {
function onNotification() {
return ({ notification, persistentId }) => {
const persistentIds = config.get('persistentIds') || [];
// Update persistentId
config.set('persistentIds', [...persistentIds, persistentId]);
// Notify the renderer process that a new notification has been received
webContents.send(NOTIFICATION_RECEIVED, notification);
// Notify the renderer processes that a new notification has been received
send(NOTIFICATION_RECEIVED, notification);
};
}