From 4520e6473afe901005e7ac532317f4b4f1af9ead Mon Sep 17 00:00:00 2001 From: fusagiko / takayamaki <24884114+takayamaki@users.noreply.github.com> Date: Mon, 3 Apr 2023 10:31:39 +0900 Subject: [Proposal] Make able to write React in Typescript (#16210) Co-authored-by: berlysia Co-authored-by: fusagiko / takayamaki --- app/javascript/mastodon/stream.js | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) (limited to 'app/javascript/mastodon/stream.js') diff --git a/app/javascript/mastodon/stream.js b/app/javascript/mastodon/stream.js index c6d12cd6f..95e0359d0 100644 --- a/app/javascript/mastodon/stream.js +++ b/app/javascript/mastodon/stream.js @@ -59,6 +59,7 @@ const subscribe = ({ channelName, params, onConnect }) => { subscriptionCounters[key] = subscriptionCounters[key] || 0; if (subscriptionCounters[key] === 0) { + // @ts-expect-error sharedConnection.send(JSON.stringify({ type: 'subscribe', stream: channelName, ...params })); } @@ -74,7 +75,9 @@ const unsubscribe = ({ channelName, params, onDisconnect }) => { subscriptionCounters[key] = subscriptionCounters[key] || 1; + // @ts-expect-error if (subscriptionCounters[key] === 1 && sharedConnection.readyState === WebSocketClient.OPEN) { + // @ts-expect-error sharedConnection.send(JSON.stringify({ type: 'unsubscribe', stream: channelName, ...params })); } @@ -83,11 +86,12 @@ const unsubscribe = ({ channelName, params, onDisconnect }) => { }; const sharedCallbacks = { - connected () { + connected() { subscriptions.forEach(subscription => subscribe(subscription)); }, - received (data) { + // @ts-expect-error + received(data) { const { stream } = data; subscriptions.filter(({ channelName, params }) => { @@ -111,11 +115,11 @@ const sharedCallbacks = { }); }, - disconnected () { + disconnected() { subscriptions.forEach(subscription => unsubscribe(subscription)); }, - reconnected () { + reconnected() { }, }; @@ -138,6 +142,7 @@ const channelNameWithInlineParams = (channelName, params) => { * @param {function(Function, Function): { onConnect: (function(): void), onReceive: (function(StreamEvent): void), onDisconnect: (function(): void) }} callbacks * @return {function(): void} */ +// @ts-expect-error export const connectStream = (channelName, params, callbacks) => (dispatch, getState) => { const streamingAPIBaseURL = getState().getIn(['meta', 'streaming_api_base_url']); const accessToken = getState().getIn(['meta', 'access_token']); @@ -147,19 +152,19 @@ export const connectStream = (channelName, params, callbacks) => (dispatch, getS // to using individual connections for each channel if (!streamingAPIBaseURL.startsWith('ws')) { const connection = createConnection(streamingAPIBaseURL, accessToken, channelNameWithInlineParams(channelName, params), { - connected () { + connected() { onConnect(); }, - received (data) { + received(data) { onReceive(data); }, - disconnected () { + disconnected() { onDisconnect(); }, - reconnected () { + reconnected() { onConnect(); }, }); @@ -227,14 +232,19 @@ const handleEventSourceMessage = (e, received) => { const createConnection = (streamingAPIBaseURL, accessToken, channelName, { connected, received, disconnected, reconnected }) => { const params = channelName.split('&'); + // @ts-expect-error channelName = params.shift(); if (streamingAPIBaseURL.startsWith('ws')) { + // @ts-expect-error const ws = new WebSocketClient(`${streamingAPIBaseURL}/api/v1/streaming/?${params.join('&')}`, accessToken); - ws.onopen = connected; - ws.onmessage = e => received(JSON.parse(e.data)); - ws.onclose = disconnected; + // @ts-expect-error + ws.onopen = connected; + ws.onmessage = e => received(JSON.parse(e.data)); + // @ts-expect-error + ws.onclose = disconnected; + // @ts-expect-error ws.onreconnect = reconnected; return ws; @@ -256,7 +266,7 @@ const createConnection = (streamingAPIBaseURL, accessToken, channelName, { conne }; KNOWN_EVENT_TYPES.forEach(type => { - es.addEventListener(type, e => handleEventSourceMessage(/** @type {MessageEvent} */ (e), received)); + es.addEventListener(type, e => handleEventSourceMessage(/** @type {MessageEvent} */(e), received)); }); es.onerror = /** @type {function(): void} */ (disconnected); -- cgit