refs #850 Replace unreadNotification with typescript

This commit is contained in:
AkiraFukushima 2019-04-16 23:50:53 +09:00
parent 82ad1b22cf
commit 8af4806d23
4 changed files with 31 additions and 31 deletions

View File

@ -17,11 +17,12 @@ import StreamingManager from './streaming_manager'
import Preferences from './preferences' import Preferences from './preferences'
import Fonts from './fonts' import Fonts from './fonts'
import Hashtags from './hashtags' import Hashtags from './hashtags'
import UnreadNotification from './unread_notification' import UnreadNotification from './unreadNotification'
import i18n from '../config/i18n' import i18n from '../config/i18n'
import Language from '../constants/language' import Language from '../constants/language'
import LocalAccount from '~src/types/localAccount' import LocalAccount from '~src/types/localAccount'
import LocalTag from '~src/types/localTag' import LocalTag from '~src/types/localTag'
import { UnreadNotification as UnreadNotificationConfig } from '~/src/types/unreadNotification'
/** /**
* Context menu * Context menu
@ -657,13 +658,13 @@ ipcMain.on('stop-tag-streaming', (_event, _) => {
}) })
// sounds // sounds
ipcMain.on('fav-rt-action-sound', (_event, _) => { ipcMain.on('fav-rt-action-sound', (_event: Event, _) => {
const preferences = new Preferences(preferencesDBPath) const preferences = new Preferences(preferencesDBPath)
preferences.load() preferences.load()
.then((conf) => { .then((conf) => {
if (conf.general.sound.fav_rb) { if (conf.general.sound.fav_rb) {
const sound = path.join(soundBasePath, 'operation_sound01.wav') const sound = path.join(soundBasePath, 'operation_sound01.wav')
simplayer(sound, (err) => { simplayer(sound, (err: Error) => {
if (err) log.error(err) if (err) log.error(err)
}) })
} }
@ -671,13 +672,13 @@ ipcMain.on('fav-rt-action-sound', (_event, _) => {
.catch(err => log.error(err)) .catch(err => log.error(err))
}) })
ipcMain.on('toot-action-sound', (_event, _) => { ipcMain.on('toot-action-sound', (_event: Event, _) => {
const preferences = new Preferences(preferencesDBPath) const preferences = new Preferences(preferencesDBPath)
preferences.load() preferences.load()
.then((conf) => { .then((conf) => {
if (conf.general.sound.toot) { if (conf.general.sound.toot) {
const sound = path.join(soundBasePath, 'operation_sound02.wav') const sound = path.join(soundBasePath, 'operation_sound02.wav')
simplayer(sound, (err) => { simplayer(sound, (err: Error) => {
if (err) log.error(err) if (err) log.error(err)
}) })
} }
@ -686,7 +687,7 @@ ipcMain.on('toot-action-sound', (_event, _) => {
}) })
// preferences // preferences
ipcMain.on('get-preferences', (event, _) => { ipcMain.on('get-preferences', (event: Event, _) => {
const preferences = new Preferences(preferencesDBPath) const preferences = new Preferences(preferencesDBPath)
preferences.load() preferences.load()
.then((conf) => { .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) const preferences = new Preferences(preferencesDBPath)
preferences.update(data) preferences.update(data)
.then((conf) => { .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) const preferences = new Preferences(preferencesDBPath)
preferences.update( 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) const preferences = new Preferences(preferencesDBPath)
preferences.load() preferences.load()
.then((conf) => { .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) const preferences = new Preferences(preferencesDBPath)
preferences.update( 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) const preferences = new Preferences(preferencesDBPath)
preferences.load() preferences.load()
.then((conf) => { .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) const preferences = new Preferences(preferencesDBPath)
preferences.update( preferences.update(
{ {
@ -813,7 +814,7 @@ ipcMain.on('list-fonts', (event: Event, _) => {
}) })
// Unread notifications // Unread notifications
ipcMain.on('get-unread-notification', (event, accountID) => { ipcMain.on('get-unread-notification', (event: Event, accountID: string) => {
unreadNotification.findOne({ unreadNotification.findOne({
accountID: accountID accountID: accountID
}) })
@ -826,9 +827,9 @@ ipcMain.on('get-unread-notification', (event, accountID) => {
}) })
}) })
ipcMain.on('update-unread-notification', (event, obj) => { ipcMain.on('update-unread-notification', (event: Event, config: UnreadNotificationConfig) => {
const { accountID } = obj const { accountID } = config
unreadNotification.insertOrUpdate(accountID, obj) unreadNotification.insertOrUpdate(accountID!, config)
.then(_ => { .then(_ => {
event.sender.send('response-update-unread-notification', true) event.sender.send('response-update-unread-notification', true)
}) })
@ -839,7 +840,7 @@ ipcMain.on('update-unread-notification', (event, obj) => {
}) })
// Application control // Application control
ipcMain.on('relaunch', (_event, _) => { ipcMain.on('relaunch', (_event: Event, _) => {
app.relaunch() app.relaunch()
app.exit() app.exit()
}) })

View File

@ -1,8 +1,11 @@
import { isEmpty } from 'lodash' import { isEmpty } from 'lodash'
import Datastore from 'nedb' import Datastore from 'nedb'
import { UnreadNotification as Config } from '~/src/types/unreadNotification'
export default class UnreadNotification { export default class UnreadNotification {
constructor (path) { private db: Datastore
constructor (path: string) {
this.db = new Datastore({ this.db = new Datastore({
filename: path, filename: path,
autoload: true autoload: true
@ -21,19 +24,19 @@ export default class UnreadNotification {
// Add unique index. // Add unique index.
this.db.ensureIndex({ fieldName: 'accountID', unique: true, sparse: true }, (err) => { this.db.ensureIndex({ fieldName: 'accountID', unique: true, sparse: true }, (err) => {
if (err) reject(err) if (err) reject(err)
resolve(null) resolve({})
}) })
}) })
}) })
} }
insertOrUpdate (accountID, obj) { insertOrUpdate (accountID: string, config: Config): Promise<number> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this.db.update( this.db.update(
{ {
accountID: accountID accountID: accountID
}, },
obj, config,
{ {
upsert: true upsert: true
}, },
@ -44,9 +47,9 @@ export default class UnreadNotification {
}) })
} }
findOne (obj) { findOne (obj: any): Promise<Config> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this.db.findOne(obj, (err, doc) => { this.db.findOne<Config>(obj, (err, doc) => {
if (err) return reject(err) if (err) return reject(err)
if (isEmpty(doc)) return reject(new EmptyRecordError('empty')) if (isEmpty(doc)) return reject(new EmptyRecordError('empty'))
resolve(doc) resolve(doc)
@ -55,9 +58,4 @@ export default class UnreadNotification {
} }
} }
class EmptyRecordError extends Error { class EmptyRecordError extends Error {}
constructor (msg) {
super(msg)
this.name = 'EmptyRecordError'
}
}

View File

@ -47,8 +47,8 @@ const actions: ActionTree<TimelineState, RootState> = {
ipcRenderer.send('get-unread-notification', rootState.Settings.accountID) ipcRenderer.send('get-unread-notification', rootState.Settings.accountID)
}) })
}, },
changeUnreadNotification: ({ dispatch, state, rootState }, timeline: object): Promise<boolean> => { changeUnreadNotification: ({ dispatch, state, rootState }, timeline: any): Promise<boolean> => {
const settings = Object.assign({}, state.unreadNotification, timeline, { const settings: UnreadNotification = Object.assign({}, state.unreadNotification, timeline, {
accountID: rootState.Settings.accountID accountID: rootState.Settings.accountID
}) })
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {

View File

@ -1,4 +1,5 @@
export type UnreadNotification = { export type UnreadNotification = {
accountID?: string,
direct: boolean, direct: boolean,
local: boolean, local: boolean,
public: boolean public: boolean