Skip to content

Commit

Permalink
refs #102 Replcae EventStream to StreamListener
Browse files Browse the repository at this point in the history
  • Loading branch information
h3poteto committed Dec 9, 2019
1 parent eb00cec commit 982656c
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 409 deletions.
3 changes: 2 additions & 1 deletion example/browser/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ module.exports = {
]
},
plugins: [],
// https-proxy-agent and socks-proxy-agent is node library, so can't compile for browser.
// So replace net, tls and dns which are node libraries.
node: {
fs: 'empty',
net: 'empty',
tls: 'empty',
dns: 'empty'
Expand Down
7 changes: 1 addition & 6 deletions example/browser/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1972,7 +1972,7 @@ md5.js@^1.3.4:
oauth "^0.9.15"
request "^2.87.0"
socks-proxy-agent h3poteto/node-socks-proxy-agent#master
typescript "^3.4.5"
typescript "3.5.3"
ws "^7.0.1"

mem@^4.0.0:
Expand Down Expand Up @@ -3284,11 +3284,6 @@ typescript@3.5.3:
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.5.3.tgz#c830f657f93f1ea846819e929092f5fe5983e977"
integrity sha512-ACzBtm/PhXBDId6a6sDJfroT2pOWt/oOnk4/dElG5G33ZL776N3Y6/6bKZJBFpd+b05F3Ct9qDjMeJmRWtE2/g==

typescript@^3.4.5:
version "3.7.2"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.7.2.tgz#27e489b95fa5909445e9fef5ee48d81697ad18fb"
integrity sha512-ml7V7JfiN2Xwvcer+XAf2csGO1bPBdRbFCkYBczNZggrBZ9c7G3riSUeJmqEU5uOtXNPMhE3n+R4FA/3YOAWOQ==

union-value@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/union-value/-/union-value-1.0.1.tgz#0b6fe7b835aecda61c6ea4d4f02c14221e109847"
Expand Down
2 changes: 1 addition & 1 deletion example/javascript/streaming.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const access_token = process.env.MASTODON_ACCESS_TOKEN
const client = new Mastodon(access_token, BASE_URL + '/api/v1')

const stream = client.stream('/streaming/public')
stream.on('connect', event => {
stream.on('connect', _ => {
console.log('connect')
})

Expand Down
4 changes: 2 additions & 2 deletions example/typescript/proxy_streaming.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Mastodon, { Status, Notification, EventStream, ProxyConfig } from 'megalodon'
import Mastodon, { Status, Notification, StreamListener, ProxyConfig } from 'megalodon'

declare var process: {
env: {
Expand All @@ -21,7 +21,7 @@ const proxy: ProxyConfig = {

const client = new Mastodon(access_token, BASE_URL + '/api/v1', 'megalodon', proxy)

const stream: EventStream = client.stream('/streaming/public')
const stream: StreamListener = client.stream('/streaming/public')
stream.on('connect', _ => {
console.log('connect')
})
Expand Down
4 changes: 2 additions & 2 deletions example/typescript/streaming.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Mastodon, { Status, Notification, EventStream } from 'megalodon'
import Mastodon, { Status, Notification, StreamListener } from 'megalodon'

declare var process: {
env: {
Expand All @@ -12,7 +12,7 @@ const access_token: string = process.env.MASTODON_ACCESS_TOKEN

const client = new Mastodon(access_token, BASE_URL + '/api/v1')

const stream: EventStream = client.stream('/streaming/public')
const stream: StreamListener = client.stream('/streaming/public')
stream.on('connect', _ => {
console.log('connect')
})
Expand Down
190 changes: 0 additions & 190 deletions src/event_stream.ts

This file was deleted.

5 changes: 2 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import Mastodon, { MegalodonInstance } from './mastodon'
import StreamListener from './stream_listener'
import StreamListener, { StreamListenerError } from './stream_listener'
import WebSocket from './web_socket'
import Response from './response'
import OAuth from './oauth'
import EventStream from './event_stream'
/**
* Entities
*/
Expand Down Expand Up @@ -39,14 +38,14 @@ import { ProxyConfig } from './proxy_config'

export {
StreamListener,
StreamListenerError,
WebSocket,
Response,
OAuth,
MegalodonInstance,
RequestCanceledError,
isCancel,
ProxyConfig,
EventStream,
/**
* Entities
*/
Expand Down
10 changes: 4 additions & 6 deletions src/mastodon.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { OAuth2 } from 'oauth'
import axios, { AxiosResponse, CancelTokenSource, AxiosRequestConfig } from 'axios'

// import StreamListener from './stream_listener'
import EventStream from './event_stream'
import StreamListener from './stream_listener'
import WebSocket from './web_socket'
import OAuth from './oauth'
import Response from './response'
Expand All @@ -24,7 +23,7 @@ export interface MegalodonInstance {
post<T = any>(path: string, params: object): Promise<Response<T>>
del(path: string, params: object): Promise<Response<{}>>
cancel(): void
stream(path: string, reconnectInterval: number): EventStream
stream(path: string, reconnectInterval: number): StreamListener
socket(path: string, strea: string): WebSocket
}

Expand Down Expand Up @@ -480,7 +479,7 @@ export default class Mastodon implements MegalodonInstance {
* @param reconnectInterval interval of reconnect
* @returns streamListener, which inherits from EventEmitter
*/
public stream(path: string, reconnectInterval = 1000): EventStream {
public stream(path: string, reconnectInterval = 1000): StreamListener {
const headers = {
'Cache-Control': 'no-cache',
Accept: 'text/event-stream',
Expand All @@ -489,8 +488,7 @@ export default class Mastodon implements MegalodonInstance {
'User-Agent': this.userAgent
}
const url = this.baseUrl + path + `?access_token=${this.accessToken}`
// const streaming = new StreamListener(url, headers, this.proxyConfig, reconnectInterval)
const streaming = new EventStream(url, headers, this.proxyConfig, reconnectInterval)
const streaming = new StreamListener(url, headers, this.proxyConfig, reconnectInterval)
process.nextTick(() => {
streaming.start()
})
Expand Down
Loading

0 comments on commit 982656c

Please sign in to comment.