Skip to content

Commit

Permalink
refactor: move packet listener
Browse files Browse the repository at this point in the history
  • Loading branch information
Rexogamer committed Feb 16, 2025
1 parent 92e3ca3 commit d1b8d01
Showing 1 changed file with 32 additions and 47 deletions.
79 changes: 32 additions & 47 deletions src/MainView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from 'react';
import {StatusBar, View} from 'react-native';

import type {API} from 'revolt.js';
import type {API, ClientboundNotification} from 'revolt.js';

import {app, randomizeRemark, setFunction} from '@clerotri/Generic';
import {client} from '@clerotri/lib/client';
Expand All @@ -29,29 +29,6 @@ import {CVChannel} from '@clerotri/lib/types';
import {checkLastVersion, openLastChannel} from '@clerotri/lib/utils';
import {LoginViews} from '@clerotri/pages/LoginViews';

function handleUserSettingsPacket(
packet: any,
setOrderedServers: (servers: any) => void,
channelNotificationSettings: any,
serverNotificationSettings: any,
) {
console.log('[WEBSOCKET] Synced settings updated');
const newSettings = packet.update;
try {
if ('ordering' in newSettings) {
const newOrderedServers = JSON.parse(newSettings.ordering[1]).servers;
setOrderedServers(newOrderedServers);
}
if ('notifications' in newSettings) {
const {server, channel} = JSON.parse(newSettings.notifications[1]);
channelNotificationSettings.current = channel;
serverNotificationSettings.current = server;
}
} catch (err) {
console.log(`[APP] Error fetching settings: ${err}`);
}
}

function LoggedInViews({
channelNotificationSettings,
serverNotificationSettings,
Expand All @@ -69,8 +46,6 @@ function LoggedInViews({
return currentChannel;
});

const {setOrderedServers} = useContext(OrderedServersContext);

const [notificationMessage, setNotificationMessage] =
useState<API.Message | null>(null);

Expand Down Expand Up @@ -114,22 +89,10 @@ function LoggedInViews({
);
}

function onUserSettingsPacket(p: any) {
handleUserSettingsPacket(
p,
setOrderedServers,
channelNotificationSettings,
serverNotificationSettings,
);
}

async function onNewPacket(p: any) {
async function onNewPacket(p: ClientboundNotification) {
if (p.type === 'Message') {
await onMessagePacket(p);
}
if (p.type === 'UserSettingsUpdate') {
onUserSettingsPacket(p);
}
}

function setUpPacketListener() {
Expand Down Expand Up @@ -196,9 +159,7 @@ export function MainView() {

function handleConnectingEvent() {
app.setLoadingStage('connecting');
console.log(
`[APP] Connecting to instance... (${new Date().getTime()})`,
);
console.log(`[APP] Connecting to instance... (${new Date().getTime()})`);
}

function handleConnectedEvent() {
Expand All @@ -218,9 +179,7 @@ export function MainView() {
]);
try {
fetchedOrderedServers = JSON.parse(rawSettings.ordering[1]).servers;
const notificationSettings = JSON.parse(
rawSettings.notifications[1],
);
const notificationSettings = JSON.parse(rawSettings.notifications[1]);
fetchedChannelNotificationSettings = notificationSettings.channel;
fetchedServerNotificationSettings = notificationSettings.server;
} catch (err) {
Expand All @@ -231,8 +190,7 @@ export function MainView() {
}

setOrderedServers(fetchedOrderedServers);
channelNotificationSettings.current =
fetchedChannelNotificationSettings;
channelNotificationSettings.current = fetchedChannelNotificationSettings;
serverNotificationSettings.current = fetchedServerNotificationSettings;
setStatus('loggedIn');

Expand All @@ -241,6 +199,25 @@ export function MainView() {
setUpNotifeeListener(client);
}

function handleUserSettingsPacket(
packet: any, // ClientboundNotification where packet.type === 'UserSettingsUpdate'
) {
console.log('[WEBSOCKET] Synced settings updated');
const newSettings = packet.update;
try {
if ('ordering' in newSettings) {
const newOrderedServers = JSON.parse(newSettings.ordering[1]).servers;
setOrderedServers(newOrderedServers);
}
if ('notifications' in newSettings) {
const {server, channel} = JSON.parse(newSettings.notifications[1]);
channelNotificationSettings.current = channel;
serverNotificationSettings.current = server;
}
} catch (err) {
console.log(`[APP] Error fetching settings: ${err}`);
}
}
function handleServerDeletion(server: string) {
const currentServer = app.getCurrentServer();
if (currentServer === server) {
Expand All @@ -249,17 +226,25 @@ export function MainView() {
}
}

function onNewPacket(p: ClientboundNotification) {
if (p.type === 'UserSettingsUpdate') {
handleUserSettingsPacket(p);
}
}

function setUpListeners() {
client.on('connecting', handleConnectingEvent);
client.on('connected', handleConnectedEvent);
client.on('ready', handleReadyEvent);
client.on('packet', onNewPacket);
client.on('server/delete', handleServerDeletion);
}

function cleanupListeners() {
client.removeListener('connecting', handleConnectingEvent);
client.removeListener('connected', handleConnectedEvent);
client.removeListener('ready', handleReadyEvent);
client.removeListener('packet', onNewPacket);
client.removeListener('server/delete', handleServerDeletion);
}

Expand Down

0 comments on commit d1b8d01

Please sign in to comment.