refs #850 Replace streamingManager, streaming, and websocket with typescript

This commit is contained in:
AkiraFukushima 2019-04-17 01:09:29 +09:00
parent 8af4806d23
commit a7999a8c11
4 changed files with 130 additions and 112 deletions

View File

@ -10,10 +10,11 @@ import path from 'path'
import ContextMenu from 'electron-context-menu' import ContextMenu from 'electron-context-menu'
import { initSplashScreen, Config } from '@trodi/electron-splashscreen' import { initSplashScreen, Config } from '@trodi/electron-splashscreen'
import openAboutWindow from 'about-window' import openAboutWindow from 'about-window'
import { Status, Notification } from 'megalodon'
import Authentication from './auth' import Authentication from './auth'
import Account from './account' import Account from './account'
import StreamingManager from './streaming_manager' import StreamingManager from './streamingManager'
import Preferences from './preferences' import Preferences from './preferences'
import Fonts from './fonts' import Fonts from './fonts'
import Hashtags from './hashtags' import Hashtags from './hashtags'
@ -307,7 +308,7 @@ ipcMain.on('get-social-token', (event: Event, _) => {
}) })
// nedb // nedb
ipcMain.on('list-accounts', (event, _) => { ipcMain.on('list-accounts', (event: Event, _) => {
accountManager.listAccounts() accountManager.listAccounts()
.catch((err) => { .catch((err) => {
log.error(err) log.error(err)
@ -318,7 +319,7 @@ ipcMain.on('list-accounts', (event, _) => {
}) })
}) })
ipcMain.on('get-local-account', (event, id) => { ipcMain.on('get-local-account', (event: Event, id: string) => {
accountManager.getAccount(id) accountManager.getAccount(id)
.catch((err) => { .catch((err) => {
log.error(err) log.error(err)
@ -329,7 +330,7 @@ ipcMain.on('get-local-account', (event, id) => {
}) })
}) })
ipcMain.on('update-account', (event, acct) => { ipcMain.on('update-account', (event: Event, acct: LocalAccount) => {
accountManager.refresh(acct) accountManager.refresh(acct)
.then((ac) => { .then((ac) => {
event.sender.send('response-update-account', ac) event.sender.send('response-update-account', ac)
@ -339,7 +340,7 @@ ipcMain.on('update-account', (event, acct) => {
}) })
}) })
ipcMain.on('remove-account', (event, id) => { ipcMain.on('remove-account', (event: Event, id: string) => {
accountManager.removeAccount(id) accountManager.removeAccount(id)
.then(() => { .then(() => {
event.sender.send('response-remove-account') event.sender.send('response-remove-account')
@ -349,7 +350,7 @@ ipcMain.on('remove-account', (event, id) => {
}) })
}) })
ipcMain.on('forward-account', (event, acct) => { ipcMain.on('forward-account', (event: Event, acct: LocalAccount) => {
accountManager.forwardAccount(acct) accountManager.forwardAccount(acct)
.then(() => { .then(() => {
event.sender.send('response-forward-account') event.sender.send('response-forward-account')
@ -360,7 +361,7 @@ ipcMain.on('forward-account', (event, acct) => {
}) })
}) })
ipcMain.on('backward-account', (event, acct) => { ipcMain.on('backward-account', (event: Event, acct: LocalAccount) => {
accountManager.backwardAccount(acct) accountManager.backwardAccount(acct)
.then(() => { .then(() => {
event.sender.send('response-backward-account') event.sender.send('response-backward-account')
@ -370,7 +371,7 @@ ipcMain.on('backward-account', (event, acct) => {
}) })
}) })
ipcMain.on('refresh-accounts', (event, _) => { ipcMain.on('refresh-accounts', (event: Event, _) => {
accountManager.refreshAccounts() accountManager.refreshAccounts()
.then((accounts) => { .then((accounts) => {
event.sender.send('response-refresh-accounts', accounts) event.sender.send('response-refresh-accounts', accounts)
@ -380,7 +381,7 @@ ipcMain.on('refresh-accounts', (event, _) => {
}) })
}) })
ipcMain.on('remove-all-accounts', (event, _) => { ipcMain.on('remove-all-accounts', (event: Event, _) => {
accountManager.removeAll() accountManager.removeAll()
.then(() => { .then(() => {
event.sender.send('response-remove-all-accounts') event.sender.send('response-remove-all-accounts')
@ -399,16 +400,16 @@ ipcMain.on('reset-badge', () => {
}) })
// streaming // streaming
// TODO: use type let userStreaming: StreamingManager | null = null
let userStreaming: any = null
ipcMain.on('start-user-streaming', (event, obj) => { type StreamingSetting = {
account: LocalAccount,
useWebsocket: boolean
}
ipcMain.on('start-user-streaming', (event: Event, obj: StreamingSetting) => {
const { account, useWebsocket } = obj const { account, useWebsocket } = obj
accountManager.getAccount(account._id) accountManager.getAccount(account._id!)
.catch((err) => {
log.error(err)
event.sender.send('error-start-user-streaming', err)
})
.then((acct) => { .then((acct) => {
// Stop old user streaming // Stop old user streaming
if (userStreaming !== null) { if (userStreaming !== null) {
@ -418,10 +419,10 @@ ipcMain.on('start-user-streaming', (event, obj) => {
userStreaming = new StreamingManager(acct, useWebsocket) userStreaming = new StreamingManager(acct, useWebsocket)
userStreaming.startUser( userStreaming.startUser(
(update) => { (update: Status) => {
event.sender.send('update-start-user-streaming', update) event.sender.send('update-start-user-streaming', update)
}, },
(notification) => { (notification: Notification) => {
event.sender.send('notification-start-user-streaming', notification) event.sender.send('notification-start-user-streaming', notification)
// Does not exist a endpoint for only mention. And mention is a part of notification. // Does not exist a endpoint for only mention. And mention is a part of notification.
// So we have to get mention from notification. // So we have to get mention from notification.
@ -432,7 +433,7 @@ ipcMain.on('start-user-streaming', (event, obj) => {
app.dock.setBadge('•') app.dock.setBadge('•')
} }
}, },
(err) => { (err: Error) => {
log.error(err) log.error(err)
// In macOS, sometimes window is closed (not quit). // In macOS, sometimes window is closed (not quit).
// When window is closed, we can not send event to webContents; because it is destroyed. // When window is closed, we can not send event to webContents; because it is destroyed.
@ -443,25 +444,24 @@ ipcMain.on('start-user-streaming', (event, obj) => {
} }
) )
}) })
.catch((err) => {
log.error(err)
event.sender.send('error-start-user-streaming', err)
})
}) })
ipcMain.on('stop-user-streaming', (_event, _) => { ipcMain.on('stop-user-streaming', (_event: Event, _) => {
if (userStreaming !== null) { if (userStreaming !== null) {
userStreaming.stop() userStreaming.stop()
userStreaming = null userStreaming = null
} }
}) })
// TODO: use type let directMessagesStreaming: StreamingManager | null = null
let directMessagesStreaming: any = null
ipcMain.on('start-directmessages-streaming', (event, obj) => { ipcMain.on('start-directmessages-streaming', (event: Event, obj: StreamingSetting) => {
const { account, useWebsocket } = obj const { account, useWebsocket } = obj
accountManager.getAccount(account._id) accountManager.getAccount(account._id!)
.catch((err) => {
log.error(err)
event.sender.send('error-start-directmessages-streaming', err)
})
.then((acct) => { .then((acct) => {
// Stop old directmessages streaming // Stop old directmessages streaming
if (directMessagesStreaming !== null) { if (directMessagesStreaming !== null) {
@ -473,10 +473,10 @@ ipcMain.on('start-directmessages-streaming', (event, obj) => {
directMessagesStreaming.start( directMessagesStreaming.start(
'direct', 'direct',
'', '',
(update) => { (update: Status) => {
event.sender.send('update-start-directmessages-streaming', update) event.sender.send('update-start-directmessages-streaming', update)
}, },
(err) => { (err: Error) => {
log.error(err) log.error(err)
if (!event.sender.isDestroyed()) { if (!event.sender.isDestroyed()) {
event.sender.send('error-start-directmessages-streaming', err) event.sender.send('error-start-directmessages-streaming', err)
@ -484,25 +484,24 @@ ipcMain.on('start-directmessages-streaming', (event, obj) => {
} }
) )
}) })
.catch((err) => {
log.error(err)
event.sender.send('error-start-directmessages-streaming', err)
})
}) })
ipcMain.on('stop-directmessages-streaming', (_event, _) => { ipcMain.on('stop-directmessages-streaming', (_event: Event, _) => {
if (directMessagesStreaming !== null) { if (directMessagesStreaming !== null) {
directMessagesStreaming.stop() directMessagesStreaming.stop()
directMessagesStreaming = null directMessagesStreaming = null
} }
}) })
// TODO: use type let localStreaming: StreamingManager | null = null
let localStreaming: any = null
ipcMain.on('start-local-streaming', (event, obj) => { ipcMain.on('start-local-streaming', (event: Event, obj: StreamingSetting) => {
const { account, useWebsocket } = obj const { account, useWebsocket } = obj
accountManager.getAccount(account._id) accountManager.getAccount(account._id!)
.catch((err) => {
log.error(err)
event.sender.send('error-start-local-streaming', err)
})
.then((acct) => { .then((acct) => {
// Stop old local streaming // Stop old local streaming
if (localStreaming !== null) { if (localStreaming !== null) {
@ -514,10 +513,10 @@ ipcMain.on('start-local-streaming', (event, obj) => {
localStreaming.start( localStreaming.start(
'public/local', 'public/local',
'', '',
(update) => { (update: Status) => {
event.sender.send('update-start-local-streaming', update) event.sender.send('update-start-local-streaming', update)
}, },
(err) => { (err: Error) => {
log.error(err) log.error(err)
if (!event.sender.isDestroyed()) { if (!event.sender.isDestroyed()) {
event.sender.send('error-start-local-streaming', err) event.sender.send('error-start-local-streaming', err)
@ -525,25 +524,24 @@ ipcMain.on('start-local-streaming', (event, obj) => {
} }
) )
}) })
.catch((err) => {
log.error(err)
event.sender.send('error-start-local-streaming', err)
})
}) })
ipcMain.on('stop-local-streaming', (_event, _) => { ipcMain.on('stop-local-streaming', (_event: Event, _) => {
if (localStreaming !== null) { if (localStreaming !== null) {
localStreaming.stop() localStreaming.stop()
localStreaming = null localStreaming = null
} }
}) })
// TODO: use type let publicStreaming: StreamingManager | null = null
let publicStreaming: any = null
ipcMain.on('start-public-streaming', (event, obj) => { ipcMain.on('start-public-streaming', (event: Event, obj: StreamingSetting) => {
const { account, useWebsocket } = obj const { account, useWebsocket } = obj
accountManager.getAccount(account._id) accountManager.getAccount(account._id!)
.catch((err) => {
log.error(err)
event.sender.send('error-start-public-streaming', err)
})
.then((acct) => { .then((acct) => {
// Stop old public streaming // Stop old public streaming
if (publicStreaming !== null) { if (publicStreaming !== null) {
@ -555,10 +553,10 @@ ipcMain.on('start-public-streaming', (event, obj) => {
publicStreaming.start( publicStreaming.start(
'public', 'public',
'', '',
(update) => { (update: Status) => {
event.sender.send('update-start-public-streaming', update) event.sender.send('update-start-public-streaming', update)
}, },
(err) => { (err: Error) => {
log.error(err) log.error(err)
if (!event.sender.isDestroyed()) { if (!event.sender.isDestroyed()) {
event.sender.send('error-start-public-streaming', err) event.sender.send('error-start-public-streaming', err)
@ -566,25 +564,28 @@ ipcMain.on('start-public-streaming', (event, obj) => {
} }
) )
}) })
.catch((err) => {
log.error(err)
event.sender.send('error-start-public-streaming', err)
})
}) })
ipcMain.on('stop-public-streaming', (_event, _) => { ipcMain.on('stop-public-streaming', (_event: Event, _) => {
if (publicStreaming !== null) { if (publicStreaming !== null) {
publicStreaming.stop() publicStreaming.stop()
publicStreaming = null publicStreaming = null
} }
}) })
// TODO: use type let listStreaming: StreamingManager | null = null
let listStreaming: any = null
ipcMain.on('start-list-streaming', (event, obj) => { type ListID = {
listID: number
}
ipcMain.on('start-list-streaming', (event: Event, obj: ListID & StreamingSetting) => {
const { listID, account, useWebsocket } = obj const { listID, account, useWebsocket } = obj
accountManager.getAccount(account._id) accountManager.getAccount(account._id!)
.catch((err) => {
log.error(err)
event.sender.send('error-start-list-streaming', err)
})
.then((acct) => { .then((acct) => {
// Stop old list streaming // Stop old list streaming
if (listStreaming !== null) { if (listStreaming !== null) {
@ -596,10 +597,10 @@ ipcMain.on('start-list-streaming', (event, obj) => {
listStreaming.start( listStreaming.start(
'list', 'list',
`list=${listID}`, `list=${listID}`,
(update) => { (update: Status) => {
event.sender.send('update-start-list-streaming', update) event.sender.send('update-start-list-streaming', update)
}, },
(err) => { (err: Error) => {
log.error(err) log.error(err)
if (!event.sender.isDestroyed()) { if (!event.sender.isDestroyed()) {
event.sender.send('error-start-list-streaming', err) event.sender.send('error-start-list-streaming', err)
@ -607,25 +608,28 @@ ipcMain.on('start-list-streaming', (event, obj) => {
} }
) )
}) })
.catch((err) => {
log.error(err)
event.sender.send('error-start-list-streaming', err)
})
}) })
ipcMain.on('stop-list-streaming', (_event, _) => { ipcMain.on('stop-list-streaming', (_event: Event, _) => {
if (listStreaming !== null) { if (listStreaming !== null) {
listStreaming.stop() listStreaming.stop()
listStreaming = null listStreaming = null
} }
}) })
// TODO: use type let tagStreaming: StreamingManager | null = null
let tagStreaming: any = null
ipcMain.on('start-tag-streaming', (event, obj) => { type Tag = {
tag: string
}
ipcMain.on('start-tag-streaming', (event: Event, obj: Tag & StreamingSetting) => {
const { tag, account, useWebsocket } = obj const { tag, account, useWebsocket } = obj
accountManager.getAccount(account._id) accountManager.getAccount(account._id!)
.catch((err) => {
log.error(err)
event.sender.send('error-start-tag-streaming', err)
})
.then((acct) => { .then((acct) => {
// Stop old tag streaming // Stop old tag streaming
if (tagStreaming !== null) { if (tagStreaming !== null) {
@ -637,10 +641,10 @@ ipcMain.on('start-tag-streaming', (event, obj) => {
tagStreaming.start( tagStreaming.start(
'hashtag', 'hashtag',
`tag=${tag}`, `tag=${tag}`,
(update) => { (update: Status) => {
event.sender.send('update-start-tag-streaming', update) event.sender.send('update-start-tag-streaming', update)
}, },
(err) => { (err: Error) => {
log.error(err) log.error(err)
if (!event.sender.isDestroyed()) { if (!event.sender.isDestroyed()) {
event.sender.send('error-start-tag-streaming', err) event.sender.send('error-start-tag-streaming', err)
@ -648,6 +652,10 @@ ipcMain.on('start-tag-streaming', (event, obj) => {
} }
) )
}) })
.catch((err) => {
log.error(err)
event.sender.send('error-start-tag-streaming', err)
})
}) })
ipcMain.on('stop-tag-streaming', (_event, _) => { ipcMain.on('stop-tag-streaming', (_event, _) => {

View File

@ -1,51 +1,54 @@
import Mastodon from 'megalodon' import Mastodon, { StreamListener, Status, Notification } from 'megalodon'
import log from 'electron-log' import log from 'electron-log'
import LocalAccount from '~/src/types/localAccount'
export default class Streaming { export default class Streaming {
constructor (account) { private client: Mastodon
this.account = account private listener: StreamListener | null
constructor (account: LocalAccount) {
this.client = new Mastodon( this.client = new Mastodon(
account.accessToken, account.accessToken!,
account.baseURL + '/api/v1' account.baseURL + '/api/v1'
) )
this.listener = null this.listener = null
} }
startUserStreaming (updateCallback, notificationCallback, errCallback) { startUserStreaming (updateCallback: Function, notificationCallback: Function, errCallback: Function) {
this.listener = this.client.stream('/streaming/user') this.listener = this.client.stream('/streaming/user')
this.listener.on('connect', _ => { this.listener.on('connect', _ => {
log.info('/streaming/user started') log.info('/streaming/user started')
}) })
this.listener.on('update', (status) => { this.listener.on('update', (status: Status) => {
updateCallback(status) updateCallback(status)
}) })
this.listener.on('notification', (notification) => { this.listener.on('notification', (notification: Notification) => {
notificationCallback(notification) notificationCallback(notification)
}) })
this.listener.on('error', (err) => { this.listener.on('error', (err: Error) => {
errCallback(err) errCallback(err)
}) })
this.listener.on('connection-limit-exceeded', err => { this.listener.on('connection-limit-exceeded', (err: Error) => {
errCallback(err) errCallback(err)
}) })
} }
start (path, updateCallback, errCallback) { start (path: string, updateCallback: Function, errCallback: Function) {
this.listener = this.client.stream(path) this.listener = this.client.stream(path)
this.listener.on('connect', _ => { this.listener.on('connect', _ => {
log.info(`${path} started`) log.info(`${path} started`)
}) })
this.listener.on('update', (status) => { this.listener.on('update', (status: Status) => {
updateCallback(status) updateCallback(status)
}) })
this.listener.on('error', (err) => { this.listener.on('error', (err: Error) => {
errCallback(err) errCallback(err)
}) })
@ -61,10 +64,10 @@ export default class Streaming {
this.listener.removeAllListeners('notification') this.listener.removeAllListeners('notification')
this.listener.removeAllListeners('error') this.listener.removeAllListeners('error')
this.listener.removeAllListeners('parser-error') this.listener.removeAllListeners('parser-error')
this.listener.on('error', (e) => { this.listener.on('error', (e: Error) => {
log.error(e) log.error(e)
}) })
this.listener.on('parser-error', (e) => { this.listener.on('parser-error', (e: Error) => {
log.error(e) log.error(e)
}) })
this.listener.stop() this.listener.stop()

View File

@ -1,15 +1,19 @@
import Streaming from './streaming' import Streaming from './streaming'
import WebSocket from './websocket' import WebSocket from './websocket'
import LocalAccount from '~src/types/localAccount'
export default class StreamingManager { export default class StreamingManager {
constructor (account, useWebsocket = false) { private streaming: Streaming
this.account = account private websocket: WebSocket
private useWebsocket: boolean
constructor (account: LocalAccount, useWebsocket = false) {
this.streaming = new Streaming(account) this.streaming = new Streaming(account)
this.websocket = new WebSocket(account) this.websocket = new WebSocket(account)
this.useWebsocket = useWebsocket this.useWebsocket = useWebsocket
} }
startUser (updateCallback, notificationCallback, errCallback) { startUser (updateCallback: Function, notificationCallback: Function, errCallback: Function) {
if (this.useWebsocket) { if (this.useWebsocket) {
this._startUserSocket(updateCallback, notificationCallback, errCallback) this._startUserSocket(updateCallback, notificationCallback, errCallback)
} else { } else {
@ -17,7 +21,7 @@ export default class StreamingManager {
} }
} }
start (path, params, updateCallback, errCallback) { start (path: string, params: string, updateCallback: Function, errCallback: Function) {
if (this.useWebsocket) { if (this.useWebsocket) {
this._startSocket(path, params, updateCallback, errCallback) this._startSocket(path, params, updateCallback, errCallback)
} else { } else {
@ -33,11 +37,11 @@ export default class StreamingManager {
/** /**
* Using streaming for Mastodon * Using streaming for Mastodon
*/ */
_startUserStreaming (updateCallback, notificationCallback, errCallback) { _startUserStreaming (updateCallback: Function, notificationCallback: Function, errCallback: Function) {
this.streaming.startUserStreaming(updateCallback, notificationCallback, errCallback) this.streaming.startUserStreaming(updateCallback, notificationCallback, errCallback)
} }
_startStreaming (path, params, updateCallback, errCallback) { _startStreaming (path: string, params: string, updateCallback: Function, errCallback: Function) {
const target = `/streaming/${path}?${params}` const target = `/streaming/${path}?${params}`
this.streaming.start( this.streaming.start(
target, target,
@ -53,11 +57,11 @@ export default class StreamingManager {
/** /**
* Using websocket for Pleroma * Using websocket for Pleroma
*/ */
_startUserSocket (updateCallback, notificationCallback, errCallback) { _startUserSocket (updateCallback: Function, notificationCallback: Function, errCallback: Function) {
this.websocket.startUserStreaming(updateCallback, notificationCallback, errCallback) this.websocket.startUserStreaming(updateCallback, notificationCallback, errCallback)
} }
_startSocket (path, params, updateCallback, errCallback) { _startSocket (path: string, params: string, updateCallback: Function, errCallback: Function) {
let stream = path let stream = path
if (stream === 'public/local') { if (stream === 'public/local') {
stream = 'public:local' stream = 'public:local'

View File

@ -1,37 +1,40 @@
import Mastodon from 'megalodon' import Mastodon, { WebSocket as SocketListener, Status, Notification } from 'megalodon'
import log from 'electron-log' import log from 'electron-log'
import LocalAccount from '~src/types/localAccount'
export default class WebSocket { export default class WebSocket {
constructor (account) { private client: Mastodon
this.account = account private listener: SocketListener | null
constructor (account: LocalAccount) {
const url = account.baseURL.replace(/^https:\/\//, 'wss://') const url = account.baseURL.replace(/^https:\/\//, 'wss://')
this.client = new Mastodon( this.client = new Mastodon(
account.accessToken, account.accessToken!,
url + '/api/v1' url + '/api/v1'
) )
this.listener = null this.listener = null
} }
startUserStreaming (updateCallback, notificationCallback, errCallback) { startUserStreaming (updateCallback: Function, notificationCallback: Function, errCallback: Function) {
this.listener = this.client.socket('/streaming', 'user') this.listener = this.client.socket('/streaming', 'user')
this.listener.on('connect', _ => { this.listener.on('connect', _ => {
log.info('/streaming/?stream=user started') log.info('/streaming/?stream=user started')
}) })
this.listener.on('update', (status) => { this.listener.on('update', (status: Status) => {
updateCallback(status) updateCallback(status)
}) })
this.listener.on('notification', (notification) => { this.listener.on('notification', (notification: Notification) => {
notificationCallback(notification) notificationCallback(notification)
}) })
this.listener.on('error', (err) => { this.listener.on('error', (err: Error) => {
errCallback(err) errCallback(err)
}) })
this.listener.on('parser-error', (err) => { this.listener.on('parser-error', (err: Error) => {
errCallback(err) errCallback(err)
}) })
} }
@ -46,21 +49,21 @@ export default class WebSocket {
* When hashtag timeline, the path is `hashtag&tag=tag_name`. * When hashtag timeline, the path is `hashtag&tag=tag_name`.
* When list timeline, the path is `list&list=list_id`. * When list timeline, the path is `list&list=list_id`.
*/ */
start (stream, updateCallback, errCallback) { start (stream: string, updateCallback: Function, errCallback: Function) {
this.listener = this.client.socket('/streaming', stream) this.listener = this.client.socket('/streaming', stream)
this.listener.on('connect', _ => { this.listener.on('connect', _ => {
log.info(`/streaming/?stream=${stream} started`) log.info(`/streaming/?stream=${stream} started`)
}) })
this.listener.on('update', status => { this.listener.on('update', (status: Status) => {
updateCallback(status) updateCallback(status)
}) })
this.listener.on('error', (err) => { this.listener.on('error', (err: Error) => {
errCallback(err) errCallback(err)
}) })
this.listener.on('parser-error', (err) => { this.listener.on('parser-error', (err: Error) => {
errCallback(err) errCallback(err)
}) })
} }
@ -72,10 +75,10 @@ export default class WebSocket {
this.listener.removeAllListeners('notification') this.listener.removeAllListeners('notification')
this.listener.removeAllListeners('error') this.listener.removeAllListeners('error')
this.listener.removeAllListeners('parser-error') this.listener.removeAllListeners('parser-error')
this.listener.on('error', (e) => { this.listener.on('error', (e: Error) => {
log.error(e) log.error(e)
}) })
this.listener.on('parser-error', (e) => { this.listener.on('parser-error', (e: Error) => {
log.error(e) log.error(e)
}) })
this.listener.stop() this.listener.stop()