From 8af4806d231d4bd54c4d614dd172ea067f7fe1b8 Mon Sep 17 00:00:00 2001
From: AkiraFukushima
Date: Tue, 16 Apr 2019 23:50:53 +0900
Subject: [PATCH] refs #850 Replace unreadNotification with typescript
---
src/main/index.ts | 35 ++++++++++---------
..._notification.js => unreadNotification.ts} | 22 ++++++------
src/renderer/store/Settings/Timeline.ts | 4 +--
src/types/unreadNotification.ts | 1 +
4 files changed, 31 insertions(+), 31 deletions(-)
rename src/main/{unread_notification.js => unreadNotification.ts} (74%)
diff --git a/src/main/index.ts b/src/main/index.ts
index 61ec5487..35699c15 100644
--- a/src/main/index.ts
+++ b/src/main/index.ts
@@ -17,11 +17,12 @@ import StreamingManager from './streaming_manager'
import Preferences from './preferences'
import Fonts from './fonts'
import Hashtags from './hashtags'
-import UnreadNotification from './unread_notification'
+import UnreadNotification from './unreadNotification'
import i18n from '../config/i18n'
import Language from '../constants/language'
import LocalAccount from '~src/types/localAccount'
import LocalTag from '~src/types/localTag'
+import { UnreadNotification as UnreadNotificationConfig } from '~/src/types/unreadNotification'
/**
* Context menu
@@ -657,13 +658,13 @@ ipcMain.on('stop-tag-streaming', (_event, _) => {
})
// sounds
-ipcMain.on('fav-rt-action-sound', (_event, _) => {
+ipcMain.on('fav-rt-action-sound', (_event: Event, _) => {
const preferences = new Preferences(preferencesDBPath)
preferences.load()
.then((conf) => {
if (conf.general.sound.fav_rb) {
const sound = path.join(soundBasePath, 'operation_sound01.wav')
- simplayer(sound, (err) => {
+ simplayer(sound, (err: Error) => {
if (err) log.error(err)
})
}
@@ -671,13 +672,13 @@ ipcMain.on('fav-rt-action-sound', (_event, _) => {
.catch(err => log.error(err))
})
-ipcMain.on('toot-action-sound', (_event, _) => {
+ipcMain.on('toot-action-sound', (_event: Event, _) => {
const preferences = new Preferences(preferencesDBPath)
preferences.load()
.then((conf) => {
if (conf.general.sound.toot) {
const sound = path.join(soundBasePath, 'operation_sound02.wav')
- simplayer(sound, (err) => {
+ simplayer(sound, (err: Error) => {
if (err) log.error(err)
})
}
@@ -686,7 +687,7 @@ ipcMain.on('toot-action-sound', (_event, _) => {
})
// preferences
-ipcMain.on('get-preferences', (event, _) => {
+ipcMain.on('get-preferences', (event: Event, _) => {
const preferences = new Preferences(preferencesDBPath)
preferences.load()
.then((conf) => {
@@ -697,7 +698,7 @@ ipcMain.on('get-preferences', (event, _) => {
})
})
-ipcMain.on('update-preferences', (event, data) => {
+ipcMain.on('update-preferences', (event: Event, data: any) => {
const preferences = new Preferences(preferencesDBPath)
preferences.update(data)
.then((conf) => {
@@ -708,7 +709,7 @@ ipcMain.on('update-preferences', (event, data) => {
})
})
-ipcMain.on('change-collapse', (_, value) => {
+ipcMain.on('change-collapse', (_, value: boolean) => {
const preferences = new Preferences(preferencesDBPath)
preferences.update(
{
@@ -721,7 +722,7 @@ ipcMain.on('change-collapse', (_, value) => {
})
})
-ipcMain.on('get-collapse', (event, _) => {
+ipcMain.on('get-collapse', (event: Event, _) => {
const preferences = new Preferences(preferencesDBPath)
preferences.load()
.then((conf) => {
@@ -729,7 +730,7 @@ ipcMain.on('get-collapse', (event, _) => {
})
})
-ipcMain.on('change-global-header', (event, value) => {
+ipcMain.on('change-global-header', (event: Event, value: boolean) => {
const preferences = new Preferences(preferencesDBPath)
preferences.update(
{
@@ -745,7 +746,7 @@ ipcMain.on('change-global-header', (event, value) => {
})
})
-ipcMain.on('get-global-header', (event, _) => {
+ipcMain.on('get-global-header', (event: Event, _) => {
const preferences = new Preferences(preferencesDBPath)
preferences.load()
.then((conf) => {
@@ -753,7 +754,7 @@ ipcMain.on('get-global-header', (event, _) => {
})
})
-ipcMain.on('change-language', (event, value) => {
+ipcMain.on('change-language', (event: Event, value: string) => {
const preferences = new Preferences(preferencesDBPath)
preferences.update(
{
@@ -813,7 +814,7 @@ ipcMain.on('list-fonts', (event: Event, _) => {
})
// Unread notifications
-ipcMain.on('get-unread-notification', (event, accountID) => {
+ipcMain.on('get-unread-notification', (event: Event, accountID: string) => {
unreadNotification.findOne({
accountID: accountID
})
@@ -826,9 +827,9 @@ ipcMain.on('get-unread-notification', (event, accountID) => {
})
})
-ipcMain.on('update-unread-notification', (event, obj) => {
- const { accountID } = obj
- unreadNotification.insertOrUpdate(accountID, obj)
+ipcMain.on('update-unread-notification', (event: Event, config: UnreadNotificationConfig) => {
+ const { accountID } = config
+ unreadNotification.insertOrUpdate(accountID!, config)
.then(_ => {
event.sender.send('response-update-unread-notification', true)
})
@@ -839,7 +840,7 @@ ipcMain.on('update-unread-notification', (event, obj) => {
})
// Application control
-ipcMain.on('relaunch', (_event, _) => {
+ipcMain.on('relaunch', (_event: Event, _) => {
app.relaunch()
app.exit()
})
diff --git a/src/main/unread_notification.js b/src/main/unreadNotification.ts
similarity index 74%
rename from src/main/unread_notification.js
rename to src/main/unreadNotification.ts
index 3b61a79d..b2cdc242 100644
--- a/src/main/unread_notification.js
+++ b/src/main/unreadNotification.ts
@@ -1,8 +1,11 @@
import { isEmpty } from 'lodash'
import Datastore from 'nedb'
+import { UnreadNotification as Config } from '~/src/types/unreadNotification'
export default class UnreadNotification {
- constructor (path) {
+ private db: Datastore
+
+ constructor (path: string) {
this.db = new Datastore({
filename: path,
autoload: true
@@ -21,19 +24,19 @@ export default class UnreadNotification {
// Add unique index.
this.db.ensureIndex({ fieldName: 'accountID', unique: true, sparse: true }, (err) => {
if (err) reject(err)
- resolve(null)
+ resolve({})
})
})
})
}
- insertOrUpdate (accountID, obj) {
+ insertOrUpdate (accountID: string, config: Config): Promise {
return new Promise((resolve, reject) => {
this.db.update(
{
accountID: accountID
},
- obj,
+ config,
{
upsert: true
},
@@ -44,9 +47,9 @@ export default class UnreadNotification {
})
}
- findOne (obj) {
+ findOne (obj: any): Promise {
return new Promise((resolve, reject) => {
- this.db.findOne(obj, (err, doc) => {
+ this.db.findOne(obj, (err, doc) => {
if (err) return reject(err)
if (isEmpty(doc)) return reject(new EmptyRecordError('empty'))
resolve(doc)
@@ -55,9 +58,4 @@ export default class UnreadNotification {
}
}
-class EmptyRecordError extends Error {
- constructor (msg) {
- super(msg)
- this.name = 'EmptyRecordError'
- }
-}
+class EmptyRecordError extends Error {}
diff --git a/src/renderer/store/Settings/Timeline.ts b/src/renderer/store/Settings/Timeline.ts
index bc3f83a6..a9cd1c5a 100644
--- a/src/renderer/store/Settings/Timeline.ts
+++ b/src/renderer/store/Settings/Timeline.ts
@@ -47,8 +47,8 @@ const actions: ActionTree = {
ipcRenderer.send('get-unread-notification', rootState.Settings.accountID)
})
},
- changeUnreadNotification: ({ dispatch, state, rootState }, timeline: object): Promise => {
- const settings = Object.assign({}, state.unreadNotification, timeline, {
+ changeUnreadNotification: ({ dispatch, state, rootState }, timeline: any): Promise => {
+ const settings: UnreadNotification = Object.assign({}, state.unreadNotification, timeline, {
accountID: rootState.Settings.accountID
})
return new Promise((resolve, reject) => {
diff --git a/src/types/unreadNotification.ts b/src/types/unreadNotification.ts
index b1861521..8269cf5d 100644
--- a/src/types/unreadNotification.ts
+++ b/src/types/unreadNotification.ts
@@ -1,4 +1,5 @@
export type UnreadNotification = {
+ accountID?: string,
direct: boolean,
local: boolean,
public: boolean