Skip to content

Update electron-config to electron-store #42

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"license": "MIT",
"types": "src/electron-push-receiver.d.ts",
"dependencies": {
"electron-config": "^1.0.0",
"electron-store": "^2.0.0",
"push-receiver": "^2.0.2"
},
"devDependencies": {
Expand Down
18 changes: 9 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { register, listen } = require('push-receiver');
const { ipcMain } = require('electron');
const Config = require('electron-config');
const Store = require('electron-store');
const {
START_NOTIFICATION_SERVICE,
NOTIFICATION_SERVICE_STARTED,
Expand All @@ -9,7 +9,7 @@ const {
TOKEN_UPDATED,
} = require('./constants');

const config = new Config();
const store = new Store();

module.exports = {
START_NOTIFICATION_SERVICE,
Expand All @@ -28,24 +28,24 @@ function setup(webContents) {
// Will be called by the renderer process
ipcMain.on(START_NOTIFICATION_SERVICE, async (_, senderId) => {
// Retrieve saved credentials
let credentials = config.get('credentials');
let credentials = store.get('credentials');
// Retrieve saved senderId
const savedSenderId = config.get('senderId');
const savedSenderId = store.get('senderId');
if (started) {
webContents.send(NOTIFICATION_SERVICE_STARTED, (credentials.fcm || {}).token);
return;
}
started = true;
try {
// Retrieve saved persistentId : avoid receiving all already received notifications on start
const persistentIds = config.get('persistentIds') || [];
const persistentIds = store.get('persistentIds') || [];
// Register if no credentials or if senderId has changed
if (!credentials || savedSenderId !== senderId) {
credentials = await register(senderId);
// Save credentials for later use
config.set('credentials', credentials);
store.set('credentials', credentials);
// Save senderId
config.set('senderId', senderId);
store.set('senderId', senderId);
// Notify the renderer process that the FCM token has changed
webContents.send(TOKEN_UPDATED, credentials.fcm.token);
}
Expand All @@ -64,9 +64,9 @@ function setup(webContents) {
// Will be called on new notification
function onNotification(webContents) {
return ({ notification, persistentId }) => {
const persistentIds = config.get('persistentIds') || [];
const persistentIds = store.get('persistentIds') || [];
// Update persistentId
config.set('persistentIds', [...persistentIds, persistentId]);
store.set('persistentIds', [...persistentIds, persistentId]);
// Notify the renderer process that a new notification has been received
webContents.send(NOTIFICATION_RECEIVED, notification);
};
Expand Down