refs #630 Handle delete event of streamings

This commit is contained in:
AkiraFukushima 2019-05-29 22:06:56 +09:00
parent 6247e5ad7c
commit 8731f5f24b
4 changed files with 62 additions and 42 deletions

View File

@ -457,6 +457,9 @@ ipcMain.on('start-user-streaming', (event: Event, obj: StreamingSetting) => {
app.dock.setBadge('•')
}
},
(id: string) => {
event.sender.send('delete-start-user-streaming', id)
},
(err: Error) => {
log.error(err)
// In macOS, sometimes window is closed (not quit).
@ -501,6 +504,9 @@ ipcMain.on('start-directmessages-streaming', (event: Event, obj: StreamingSettin
(update: Status) => {
event.sender.send('update-start-directmessages-streaming', update)
},
(id: string) => {
event.sender.send('delete-start-directmessages-streaming', id)
},
(err: Error) => {
log.error(err)
if (!event.sender.isDestroyed()) {
@ -542,6 +548,9 @@ ipcMain.on('start-local-streaming', (event: Event, obj: StreamingSetting) => {
(update: Status) => {
event.sender.send('update-start-local-streaming', update)
},
(id: string) => {
event.sender.send('delete-start-local-streaming', id)
},
(err: Error) => {
log.error(err)
if (!event.sender.isDestroyed()) {
@ -583,6 +592,9 @@ ipcMain.on('start-public-streaming', (event: Event, obj: StreamingSetting) => {
(update: Status) => {
event.sender.send('update-start-public-streaming', update)
},
(id: string) => {
event.sender.send('delete-start-public-streaming', id)
},
(err: Error) => {
log.error(err)
if (!event.sender.isDestroyed()) {
@ -628,6 +640,9 @@ ipcMain.on('start-list-streaming', (event: Event, obj: ListID & StreamingSetting
(update: Status) => {
event.sender.send('update-start-list-streaming', update)
},
(id: string) => {
event.sender.send('delete-start-list-streaming', id)
},
(err: Error) => {
log.error(err)
if (!event.sender.isDestroyed()) {
@ -673,6 +688,9 @@ ipcMain.on('start-tag-streaming', (event: Event, obj: Tag & StreamingSetting) =>
(update: Status) => {
event.sender.send('update-start-tag-streaming', update)
},
(id: string) => {
event.sender.send('delete-start-tag-streaming', id)
},
(err: Error) => {
log.error(err)
if (!event.sender.isDestroyed()) {

View File

@ -6,15 +6,12 @@ export default class Streaming {
private client: Mastodon
private listener: StreamListener | null
constructor (account: LocalAccount) {
this.client = new Mastodon(
account.accessToken!,
account.baseURL + '/api/v1'
)
constructor(account: LocalAccount) {
this.client = new Mastodon(account.accessToken!, account.baseURL + '/api/v1')
this.listener = null
}
startUserStreaming (updateCallback: Function, notificationCallback: Function, errCallback: Function) {
startUserStreaming(updateCallback: Function, notificationCallback: Function, deleteCallback: Function, errCallback: Function) {
this.listener = this.client.stream('/streaming/user')
this.listener.on('connect', _ => {
@ -29,6 +26,10 @@ export default class Streaming {
notificationCallback(notification)
})
this.listener.on('delete', (id: string) => {
deleteCallback(id)
})
this.listener.on('error', (err: Error) => {
errCallback(err)
})
@ -38,7 +39,7 @@ export default class Streaming {
})
}
start (path: string, updateCallback: Function, errCallback: Function) {
start(path: string, updateCallback: Function, deleteCallback: Function, errCallback: Function) {
this.listener = this.client.stream(path)
this.listener.on('connect', _ => {
log.info(`${path} started`)
@ -48,6 +49,10 @@ export default class Streaming {
updateCallback(status)
})
this.listener.on('delete', (id: string) => {
deleteCallback(id)
})
this.listener.on('error', (err: Error) => {
errCallback(err)
})
@ -57,7 +62,7 @@ export default class Streaming {
})
}
stop () {
stop() {
if (this.listener) {
this.listener.removeAllListeners('connect')
this.listener.removeAllListeners('update')

View File

@ -7,29 +7,29 @@ export default class StreamingManager {
private websocket: WebSocket
private useWebsocket: boolean
constructor (account: LocalAccount, useWebsocket = false) {
constructor(account: LocalAccount, useWebsocket = false) {
this.streaming = new Streaming(account)
this.websocket = new WebSocket(account)
this.useWebsocket = useWebsocket
}
startUser (updateCallback: Function, notificationCallback: Function, errCallback: Function) {
startUser(updateCallback: Function, notificationCallback: Function, deleteCallback: Function, errCallback: Function) {
if (this.useWebsocket) {
this._startUserSocket(updateCallback, notificationCallback, errCallback)
this._startUserSocket(updateCallback, notificationCallback, deleteCallback, errCallback)
} else {
this._startUserStreaming(updateCallback, notificationCallback, errCallback)
this._startUserStreaming(updateCallback, notificationCallback, deleteCallback, errCallback)
}
}
start (path: string, params: string, updateCallback: Function, errCallback: Function) {
start(path: string, params: string, updateCallback: Function, deleteCallback: Function, errCallback: Function) {
if (this.useWebsocket) {
this._startSocket(path, params, updateCallback, errCallback)
this._startSocket(path, params, updateCallback, deleteCallback, errCallback)
} else {
this._startStreaming(path, params, updateCallback, errCallback)
this._startStreaming(path, params, updateCallback, deleteCallback, errCallback)
}
}
stop () {
stop() {
this._stopStreaming()
this._stopSocket()
}
@ -37,43 +37,35 @@ export default class StreamingManager {
/**
* Using streaming for Mastodon
*/
_startUserStreaming (updateCallback: Function, notificationCallback: Function, errCallback: Function) {
this.streaming.startUserStreaming(updateCallback, notificationCallback, errCallback)
_startUserStreaming(updateCallback: Function, notificationCallback: Function, deleteCallback: Function, errCallback: Function) {
this.streaming.startUserStreaming(updateCallback, notificationCallback, deleteCallback, errCallback)
}
_startStreaming (path: string, params: string, updateCallback: Function, errCallback: Function) {
_startStreaming(path: string, params: string, updateCallback: Function, deleteCallback, errCallback: Function) {
const target = `/streaming/${path}?${params}`
this.streaming.start(
target,
updateCallback,
errCallback
)
this.streaming.start(target, updateCallback, deleteCallback, errCallback)
}
_stopStreaming () {
_stopStreaming() {
this.streaming.stop()
}
/**
* Using websocket for Pleroma
*/
_startUserSocket (updateCallback: Function, notificationCallback: Function, errCallback: Function) {
this.websocket.startUserStreaming(updateCallback, notificationCallback, errCallback)
_startUserSocket(updateCallback: Function, notificationCallback: Function, deleteCallback: Function, errCallback: Function) {
this.websocket.startUserStreaming(updateCallback, notificationCallback, deleteCallback, errCallback)
}
_startSocket (path: string, params: string, updateCallback: Function, errCallback: Function) {
_startSocket(path: string, params: string, updateCallback: Function, deleteCallback: Function, errCallback: Function) {
let stream = path
if (stream === 'public/local') {
stream = 'public:local'
}
this.websocket.start(
`${stream}&${params}`,
updateCallback,
errCallback
)
this.websocket.start(`${stream}&${params}`, updateCallback, deleteCallback, errCallback)
}
_stopSocket () {
_stopSocket() {
this.websocket.stop()
}
}

View File

@ -6,16 +6,13 @@ export default class WebSocket {
private client: Mastodon
private listener: SocketListener | null
constructor (account: LocalAccount) {
constructor(account: LocalAccount) {
const url = account.baseURL.replace(/^https:\/\//, 'wss://')
this.client = new Mastodon(
account.accessToken!,
url + '/api/v1'
)
this.client = new Mastodon(account.accessToken!, url + '/api/v1')
this.listener = null
}
startUserStreaming (updateCallback: Function, notificationCallback: Function, errCallback: Function) {
startUserStreaming(updateCallback: Function, notificationCallback: Function, deleteCallback: Function, errCallback: Function) {
this.listener = this.client.socket('/streaming', 'user')
this.listener.on('connect', _ => {
@ -30,6 +27,10 @@ export default class WebSocket {
notificationCallback(notification)
})
this.listener.on('delete', (id: string) => {
deleteCallback(id)
})
this.listener.on('error', (err: Error) => {
errCallback(err)
})
@ -49,7 +50,7 @@ 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: string, updateCallback: Function, errCallback: Function) {
start(stream: string, updateCallback: Function, deleteCallback: Function, errCallback: Function) {
this.listener = this.client.socket('/streaming', stream)
this.listener.on('connect', _ => {
log.info(`/streaming/?stream=${stream} started`)
@ -59,6 +60,10 @@ export default class WebSocket {
updateCallback(status)
})
this.listener.on('delete', (id: string) => {
deleteCallback(id)
})
this.listener.on('error', (err: Error) => {
errCallback(err)
})
@ -68,7 +73,7 @@ export default class WebSocket {
})
}
stop () {
stop() {
if (this.listener) {
this.listener.removeAllListeners('connect')
this.listener.removeAllListeners('update')