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

Don't send messages to destroyed webContents #11

Open
wants to merge 2 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
60 changes: 49 additions & 11 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) {
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);
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,55 @@ 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);
// Notify the renderer processes that we are listening for notifications
send(NOTIFICATION_SERVICE_STARTED, credentials.fcm.token);
started = true;
} 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);
// Forward error to the renderer processes
send(NOTIFICATION_SERVICE_ERROR, e.message);
starting = false;
Copy link
Owner

Choose a reason for hiding this comment

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

starting is never set to false if no error is thrown. Is it intended ?

Copy link
Author

Choose a reason for hiding this comment

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

Yes - in that case started should be true, so it would return around line 44 anyway.

started = false;
}
});
}

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);
};
}