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 { initSplashScreen, Config } from '@trodi/electron-splashscreen'
import openAboutWindow from 'about-window'
import { Status, Notification } from 'megalodon'
import Authentication from './auth'
import Account from './account'
import StreamingManager from './streaming_manager'
import StreamingManager from './streamingManager'
import Preferences from './preferences'
import Fonts from './fonts'
import Hashtags from './hashtags'
@ -307,7 +308,7 @@ ipcMain.on('get-social-token', (event: Event, _) => {
})
// nedb
ipcMain.on('list-accounts', (event, _) => {
ipcMain.on('list-accounts', (event: Event, _) => {
accountManager.listAccounts()
.catch((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)
.catch((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)
.then((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)
.then(() => {
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)
.then(() => {
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)
.then(() => {
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()
.then((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()
.then(() => {
event.sender.send('response-remove-all-accounts')
@ -399,16 +400,16 @@ ipcMain.on('reset-badge', () => {
})
// streaming
// TODO: use type
let userStreaming: any = null
let userStreaming: StreamingManager | null = 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
accountManager.getAccount(account._id)
.catch((err) => {
log.error(err)
event.sender.send('error-start-user-streaming', err)
})
accountManager.getAccount(account._id!)
.then((acct) => {
// Stop old user streaming
if (userStreaming !== null) {
@ -418,10 +419,10 @@ ipcMain.on('start-user-streaming', (event, obj) => {
userStreaming = new StreamingManager(acct, useWebsocket)
userStreaming.startUser(
(update) => {
(update: Status) => {
event.sender.send('update-start-user-streaming', update)
},
(notification) => {
(notification: Notification) => {
event.sender.send('notification-start-user-streaming', notification)
// Does not exist a endpoint for only mention. And mention is a part of notification.
// So we have to get mention from notification.
@ -432,7 +433,7 @@ ipcMain.on('start-user-streaming', (event, obj) => {
app.dock.setBadge('•')
}
},
(err) => {
(err: Error) => {
log.error(err)
// In macOS, sometimes window is closed (not quit).
// 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) {
userStreaming.stop()
userStreaming = null
}
})
// TODO: use type
let directMessagesStreaming: any = null
let directMessagesStreaming: StreamingManager | null = null
ipcMain.on('start-directmessages-streaming', (event, obj) => {
ipcMain.on('start-directmessages-streaming', (event: Event, obj: StreamingSetting) => {
const { account, useWebsocket } = obj
accountManager.getAccount(account._id)
.catch((err) => {
log.error(err)
event.sender.send('error-start-directmessages-streaming', err)
})
accountManager.getAccount(account._id!)
.then((acct) => {
// Stop old directmessages streaming
if (directMessagesStreaming !== null) {
@ -473,10 +473,10 @@ ipcMain.on('start-directmessages-streaming', (event, obj) => {
directMessagesStreaming.start(
'direct',
'',
(update) => {
(update: Status) => {
event.sender.send('update-start-directmessages-streaming', update)
},
(err) => {
(err: Error) => {
log.error(err)
if (!event.sender.isDestroyed()) {
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) {
directMessagesStreaming.stop()
directMessagesStreaming = null
}
})
// TODO: use type
let localStreaming: any = null
let localStreaming: StreamingManager | null = null
ipcMain.on('start-local-streaming', (event, obj) => {
ipcMain.on('start-local-streaming', (event: Event, obj: StreamingSetting) => {
const { account, useWebsocket } = obj
accountManager.getAccount(account._id)
.catch((err) => {
log.error(err)
event.sender.send('error-start-local-streaming', err)
})
accountManager.getAccount(account._id!)
.then((acct) => {
// Stop old local streaming
if (localStreaming !== null) {
@ -514,10 +513,10 @@ ipcMain.on('start-local-streaming', (event, obj) => {
localStreaming.start(
'public/local',
'',
(update) => {
(update: Status) => {
event.sender.send('update-start-local-streaming', update)
},
(err) => {
(err: Error) => {
log.error(err)
if (!event.sender.isDestroyed()) {
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) {
localStreaming.stop()
localStreaming = null
}
})
// TODO: use type
let publicStreaming: any = null
let publicStreaming: StreamingManager | null = null
ipcMain.on('start-public-streaming', (event, obj) => {
ipcMain.on('start-public-streaming', (event: Event, obj: StreamingSetting) => {
const { account, useWebsocket } = obj
accountManager.getAccount(account._id)
.catch((err) => {
log.error(err)
event.sender.send('error-start-public-streaming', err)
})
accountManager.getAccount(account._id!)
.then((acct) => {
// Stop old public streaming
if (publicStreaming !== null) {
@ -555,10 +553,10 @@ ipcMain.on('start-public-streaming', (event, obj) => {
publicStreaming.start(
'public',
'',
(update) => {
(update: Status) => {
event.sender.send('update-start-public-streaming', update)
},
(err) => {
(err: Error) => {
log.error(err)
if (!event.sender.isDestroyed()) {
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) {
publicStreaming.stop()
publicStreaming = null
}
})
// TODO: use type
let listStreaming: any = null
let listStreaming: StreamingManager | null = 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
accountManager.getAccount(account._id)
.catch((err) => {
log.error(err)
event.sender.send('error-start-list-streaming', err)
})
accountManager.getAccount(account._id!)
.then((acct) => {
// Stop old list streaming
if (listStreaming !== null) {
@ -596,10 +597,10 @@ ipcMain.on('start-list-streaming', (event, obj) => {
listStreaming.start(
'list',
`list=${listID}`,
(update) => {
(update: Status) => {
event.sender.send('update-start-list-streaming', update)
},
(err) => {
(err: Error) => {
log.error(err)
if (!event.sender.isDestroyed()) {
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) {
listStreaming.stop()
listStreaming = null
}
})
// TODO: use type
let tagStreaming: any = null
let tagStreaming: StreamingManager | null = 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
accountManager.getAccount(account._id)
.catch((err) => {
log.error(err)
event.sender.send('error-start-tag-streaming', err)
})
accountManager.getAccount(account._id!)
.then((acct) => {
// Stop old tag streaming
if (tagStreaming !== null) {
@ -637,10 +641,10 @@ ipcMain.on('start-tag-streaming', (event, obj) => {
tagStreaming.start(
'hashtag',
`tag=${tag}`,
(update) => {
(update: Status) => {
event.sender.send('update-start-tag-streaming', update)
},
(err) => {
(err: Error) => {
log.error(err)
if (!event.sender.isDestroyed()) {
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, _) => {

View File

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

View File

@ -1,15 +1,19 @@
import Streaming from './streaming'
import WebSocket from './websocket'
import LocalAccount from '~src/types/localAccount'
export default class StreamingManager {
constructor (account, useWebsocket = false) {
this.account = account
private streaming: Streaming
private websocket: WebSocket
private useWebsocket: boolean
constructor (account: LocalAccount, useWebsocket = false) {
this.streaming = new Streaming(account)
this.websocket = new WebSocket(account)
this.useWebsocket = useWebsocket
}
startUser (updateCallback, notificationCallback, errCallback) {
startUser (updateCallback: Function, notificationCallback: Function, errCallback: Function) {
if (this.useWebsocket) {
this._startUserSocket(updateCallback, notificationCallback, errCallback)
} 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) {
this._startSocket(path, params, updateCallback, errCallback)
} else {
@ -33,11 +37,11 @@ export default class StreamingManager {
/**
* Using streaming for Mastodon
*/
_startUserStreaming (updateCallback, notificationCallback, errCallback) {
_startUserStreaming (updateCallback: Function, notificationCallback: Function, errCallback: Function) {
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}`
this.streaming.start(
target,
@ -53,11 +57,11 @@ export default class StreamingManager {
/**
* Using websocket for Pleroma
*/
_startUserSocket (updateCallback, notificationCallback, errCallback) {
_startUserSocket (updateCallback: Function, notificationCallback: Function, errCallback: Function) {
this.websocket.startUserStreaming(updateCallback, notificationCallback, errCallback)
}
_startSocket (path, params, updateCallback, errCallback) {
_startSocket (path: string, params: string, updateCallback: Function, errCallback: Function) {
let stream = path
if (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 LocalAccount from '~src/types/localAccount'
export default class WebSocket {
constructor (account) {
this.account = account
private client: Mastodon
private listener: SocketListener | null
constructor (account: LocalAccount) {
const url = account.baseURL.replace(/^https:\/\//, 'wss://')
this.client = new Mastodon(
account.accessToken,
account.accessToken!,
url + '/api/v1'
)
this.listener = null
}
startUserStreaming (updateCallback, notificationCallback, errCallback) {
startUserStreaming (updateCallback: Function, notificationCallback: Function, errCallback: Function) {
this.listener = this.client.socket('/streaming', 'user')
this.listener.on('connect', _ => {
log.info('/streaming/?stream=user started')
})
this.listener.on('update', (status) => {
this.listener.on('update', (status: Status) => {
updateCallback(status)
})
this.listener.on('notification', (notification) => {
this.listener.on('notification', (notification: Notification) => {
notificationCallback(notification)
})
this.listener.on('error', (err) => {
this.listener.on('error', (err: Error) => {
errCallback(err)
})
this.listener.on('parser-error', (err) => {
this.listener.on('parser-error', (err: Error) => {
errCallback(err)
})
}
@ -46,21 +49,21 @@ export default class WebSocket {
* When hashtag timeline, the path is `hashtag&tag=tag_name`.
* 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.on('connect', _ => {
log.info(`/streaming/?stream=${stream} started`)
})
this.listener.on('update', status => {
this.listener.on('update', (status: Status) => {
updateCallback(status)
})
this.listener.on('error', (err) => {
this.listener.on('error', (err: Error) => {
errCallback(err)
})
this.listener.on('parser-error', (err) => {
this.listener.on('parser-error', (err: Error) => {
errCallback(err)
})
}
@ -72,10 +75,10 @@ export default class WebSocket {
this.listener.removeAllListeners('notification')
this.listener.removeAllListeners('error')
this.listener.removeAllListeners('parser-error')
this.listener.on('error', (e) => {
this.listener.on('error', (e: Error) => {
log.error(e)
})
this.listener.on('parser-error', (e) => {
this.listener.on('parser-error', (e: Error) => {
log.error(e)
})
this.listener.stop()