Skip to content

Commit

Permalink
chore(chat): remove outdated message buffer before load (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
xxshady authored May 8, 2024
1 parent b1056bf commit a975f14
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 70 deletions.
2 changes: 0 additions & 2 deletions chat/client/html/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,6 @@ window.addEventListener("load", () => {
}
}
});

alt.emit("chatloaded");
});

function saveBuffer() {
Expand Down
55 changes: 17 additions & 38 deletions chat/client/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import * as alt from "alt-client";
import { CHAT_MESSAGE_EVENT } from "../shared/index.js";

let buffer = [];

let loaded = false;
let opened = false;

const view = new alt.WebView("http://resource/client/html/index.html");
Expand All @@ -16,14 +13,6 @@ function addMessage(name, text) {
}
}

view.on("chatloaded", () => {
for (const msg of buffer) {
addMessage(msg.name, msg.text);
}

loaded = true;
});

view.on("chatmessage", (text) => {
alt.emitServer(CHAT_MESSAGE_EVENT, text);

Expand All @@ -32,38 +21,28 @@ view.on("chatmessage", (text) => {
view.unfocus();
});

export function pushMessage(name, text) {
if (!loaded) {
buffer.push({ name, text });
} else {
addMessage(name, text);
}
}

export function pushLine(text) {
pushMessage(null, text);
addMessage(null, text);
}

alt.onServer(CHAT_MESSAGE_EVENT, pushMessage);
alt.onServer(CHAT_MESSAGE_EVENT, addMessage);

alt.on("keyup", (key) => {
if (loaded) {
if (!opened && key === 0x54 && alt.gameControlsEnabled()) {
opened = true;
view.emit("openChat", false);
alt.toggleGameControls(false);
view.focus();
} else if (!opened && key === 0xbf && alt.gameControlsEnabled()) {
opened = true;
view.emit("openChat", true);
alt.toggleGameControls(false);
view.focus();
} else if (opened && key == 0x1b) {
opened = false;
view.emit("closeChat");
alt.toggleGameControls(true);
view.unfocus();
}
if (!opened && key === 0x54 && alt.gameControlsEnabled()) {
opened = true;
view.emit("openChat", false);
alt.toggleGameControls(false);
view.focus();
} else if (!opened && key === 0xbf && alt.gameControlsEnabled()) {
opened = true;
view.emit("openChat", true);
alt.toggleGameControls(false);
view.focus();
} else if (opened && key == 0x1b) {
opened = false;
view.emit("closeChat");
alt.toggleGameControls(true);
view.unfocus();
}
});

Expand Down
2 changes: 0 additions & 2 deletions freeroam-extended/client/html/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,6 @@ window.addEventListener("load", () => {
}
}
});

alt.emit("chatloaded");
});

function saveBuffer() {
Expand Down
24 changes: 1 addition & 23 deletions freeroam-extended/client/src/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,9 @@ import { playerData } from "./playerdata"
import { view } from "./view"

export const chatData = {
loaded: false,
opened: false,
}

interface IBufferItem {
name: string | null
text: string
}

const buffer: IBufferItem[] = []

export function toggleChat(): void {
view.isVisible = !view.isVisible
}
Expand All @@ -25,15 +17,8 @@ function addMessage(name: string | null, text: string) {
view.emit("addString", text)
}

export function pushMessage(name: string | null, text: string): void {
if (!chatData.loaded)
buffer.push({ name, text })
else
addMessage(name, text)
}

export function pushLine(text: string): void {
pushMessage(null, text)
addMessage(null, text)
}

alt.on("windowFocusChange", (state) => {
Expand All @@ -45,13 +30,6 @@ alt.on("windowFocusChange", (state) => {
})
})

view.on("chatloaded", () => {
for (const msg of buffer)
addMessage(msg.name, msg.text)

chatData.loaded = true
})

view.on("chatmessage", (text: string) => {
alt.emitServer("chat:message", text)

Expand Down
7 changes: 2 additions & 5 deletions freeroam-extended/client/src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { toggleNoclip } from "./noclip"
import { playerData } from "./playerdata"
import { view } from "./view"
import { KeyCode, Permission, PermissionState } from "altv-enums"
import type { VoiceConnectionState } from "alt-client"

alt.on("connectionComplete", () => {
setTimeout(() => {
Expand All @@ -23,7 +22,7 @@ alt.on("localMetaChange", (key: string, newValue: any) => {

alt.onServer("airport_state", setWeaponsUsage)

alt.onServer("chat:message", chat.pushMessage)
alt.onServer("chat:message", chat.pushLine)

alt.onServer("noclip", toggleNoclip)

Expand All @@ -40,8 +39,6 @@ alt.onServer("announce", (header: string, body: string, time: number) => {
})

alt.on("keyup", (key) => {
if (!chat.chatData.loaded) return

switch (key) {
case KeyCode.F2: {
playerData.areNametagsVisible = !playerData.areNametagsVisible
Expand Down Expand Up @@ -120,4 +117,4 @@ alt.onServer("esp", (state: boolean) => {

alt.on("voiceConnection", (state: alt.VoiceConnectionState) => {
view.emit("setVoiceConnectionState", state)
})
})

0 comments on commit a975f14

Please sign in to comment.