Fully renamed socket to ttp

This commit is contained in:
Alois 2026-04-03 00:38:35 +02:00
commit 76ca664298
13 changed files with 55 additions and 42 deletions

View file

@ -1,4 +1,4 @@
import { useSocket } from "@tensamin/ttp/context"; import { useTTP } from "@tensamin/ttp/context";
import { import {
createContext, createContext,
useContext, useContext,
@ -31,7 +31,7 @@ export default function ConversationProvider(props: { children: ReactNode }) {
); );
const [communities, setCommunities] = useState<Community[] | null>(null); const [communities, setCommunities] = useState<Community[] | null>(null);
const { send } = useSocket(); const { send } = useTTP();
useEffect(() => { useEffect(() => {
let active = true; let active = true;

View file

@ -148,7 +148,7 @@ export default function Screen(props: { children: React.ReactNode }) {
return () => { return () => {
active = false; active = false;
}; };
}, [load, location.pathname]); }, [load, location.pathname, userId]);
if (error !== "" && errorDescription !== "") { if (error !== "" && errorDescription !== "") {
return <ErrorScreen error={error} description={errorDescription} />; return <ErrorScreen error={error} description={errorDescription} />;

View file

@ -11,7 +11,7 @@ import {
} from "@tensamin/ui/cmp/dialog"; } from "@tensamin/ui/cmp/dialog";
import z from "zod"; import z from "zod";
import { toast } from "@tensamin/shared/log"; import { toast } from "@tensamin/shared/log";
import { useSocket } from "@tensamin/ttp/context"; import { useTTP } from "@tensamin/ttp/context";
import { useState } from "react"; import { useState } from "react";
import { Loader2 } from "lucide-react"; import { Loader2 } from "lucide-react";
@ -27,7 +27,7 @@ export default function Page() {
// Add Conversation Button Component // Add Conversation Button Component
function AddConversationButton() { function AddConversationButton() {
const { send } = useSocket(); const { send } = useTTP();
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
function submit(username: string | null) { function submit(username: string | null) {

View file

@ -1,8 +1,8 @@
import { z } from "zod"; import { z } from "zod";
import { socket } from "@tensamin/shared/data"; import { ttp } from "@tensamin/shared/data";
export type RawMessages = z.infer< export type RawMessages = z.infer<
typeof socket.messages_get.response typeof ttp.messages_get.response
>["messages"]; >["messages"];
export type RawMessage = RawMessages[number]; export type RawMessage = RawMessages[number];

View file

@ -1,5 +1,5 @@
import type { BoundSendFn } from "@tensamin/ttp/core"; import type { BoundSendFn } from "@tensamin/ttp/core";
import type { Socket } from "@tensamin/shared/data"; import type { TTP } from "@tensamin/shared/data";
import type { RawMessages } from "../values"; import type { RawMessages } from "../values";
/** /**
@ -11,7 +11,7 @@ import type { RawMessages } from "../values";
* @returns Promise<RawMessages>. * @returns Promise<RawMessages>.
*/ */
export async function getMessages( export async function getMessages(
send: BoundSendFn<Socket>, send: BoundSendFn<TTP>,
amount: number, amount: number,
offset: number, offset: number,
user_id: number, user_id: number,

View file

@ -6,7 +6,7 @@ import { Button } from "@tensamin/ui/cmp/button";
import { Plus, Laugh, Clapperboard } from "lucide-react"; import { Plus, Laugh, Clapperboard } from "lucide-react";
import { useChat } from "../context"; import { useChat } from "../context";
import { useSocket } from "@tensamin/ttp/context"; import { useTTP } from "@tensamin/ttp/context";
import { useCrypto } from "@tensamin/crypto/context"; import { useCrypto } from "@tensamin/crypto/context";
import { log, toast } from "@tensamin/shared/log"; import { log, toast } from "@tensamin/shared/log";
@ -15,7 +15,7 @@ export default function InputComponent() {
const [invertEnterBehavior, setInvertEnterBehavior] = React.useState(false); const [invertEnterBehavior, setInvertEnterBehavior] = React.useState(false);
const { encrypt } = useCrypto(); const { encrypt } = useCrypto();
const { send } = useSocket(); const { send } = useTTP();
const { addLiveMessage, sharedSecret, userId } = useChat(); const { addLiveMessage, sharedSecret, userId } = useChat();
const { load } = useStorage(); const { load } = useStorage();

View file

@ -14,7 +14,7 @@ import type { LiveMessage, RawMessage, RawMessages } from "./values";
import { useCrypto } from "@tensamin/crypto/context"; import { useCrypto } from "@tensamin/crypto/context";
import { useUser } from "@tensamin/user/context"; import { useUser } from "@tensamin/user/context";
import { useStorage } from "@tensamin/storage/context"; import { useStorage } from "@tensamin/storage/context";
import { useSocket } from "@tensamin/ttp/context"; import { useTTP } from "@tensamin/ttp/context";
export const context = createContext<contextType | undefined>(undefined); export const context = createContext<contextType | undefined>(undefined);
@ -29,7 +29,7 @@ export default function Provider(props: { children: ReactNode }) {
const { getSharedSecret } = useCrypto(); const { getSharedSecret } = useCrypto();
const { get } = useUser(); const { get } = useUser();
const { load } = useStorage(); const { load } = useStorage();
const { send, subscribePush } = useSocket(); const { send, subscribePush } = useTTP();
const [liveMessagesState, setLiveMessagesState] = useState<LiveMessage[]>([]); const [liveMessagesState, setLiveMessagesState] = useState<LiveMessage[]>([]);
const [currentSharedSecret, setCurrentSharedSecret] = useState(""); const [currentSharedSecret, setCurrentSharedSecret] = useState("");

View file

@ -1,8 +1,8 @@
import { z } from "zod"; import { z } from "zod";
import { socket } from "@tensamin/shared/data"; import { ttp } from "@tensamin/shared/data";
export type RawMessages = z.infer< export type RawMessages = z.infer<
typeof socket.messages_get.response typeof ttp.messages_get.response
>["messages"]; >["messages"];
export type RawMessage = RawMessages[number]; export type RawMessage = RawMessages[number];

View file

@ -32,10 +32,10 @@ export const failedUser = {
sub_level: 0, sub_level: 0,
user_id: 0, user_id: 0,
username: "unknown", username: "unknown",
} as z.infer<typeof socket.get_user_data.response>; } as z.infer<typeof ttp.get_user_data.response>;
// Socket // TTP
export const socket = { export const ttp = {
identification: { identification: {
request: z request: z
.object({ .object({
@ -162,7 +162,7 @@ export const socket = {
}, },
} satisfies Record<string, { request: z.ZodType; response: z.ZodType }>; } satisfies Record<string, { request: z.ZodType; response: z.ZodType }>;
export type Socket = typeof socket; export type TTP = typeof ttp;
// Storage // Storage
export interface Storage { export interface Storage {

View file

@ -11,7 +11,16 @@ import { Ban, Check, Info, TriangleAlert } from "lucide-react";
export function log( export function log(
logLevel: number, logLevel: number,
logger: string, logger: string,
color: "red" | "green" | "yellow" | "purple" | "blue", color:
| "red"
| "green"
| "yellow"
| "purple"
| "blue"
| "cyan"
| "white"
| "black"
| "gray",
...args: unknown[] ...args: unknown[]
) { ) {
const currentLogLevel = Number(localStorage.getItem("log_level") || 0); const currentLogLevel = Number(localStorage.getItem("log_level") || 0);
@ -24,6 +33,10 @@ export function log(
yellow: "\x1b[33m", yellow: "\x1b[33m",
purple: "\x1b[35m", purple: "\x1b[35m",
blue: "\x1b[34m", blue: "\x1b[34m",
cyan: "\x1b[36m",
white: "\x1b[37m",
black: "\x1b[30m",
gray: "\x1b[90m",
}; };
const resetCode = "\x1b[0m"; const resetCode = "\x1b[0m";
console.log(`${colorCodes[color]}${logger}${resetCode}`, ...args); console.log(`${colorCodes[color]}${logger}${resetCode}`, ...args);

View file

@ -21,8 +21,8 @@ import {
TRANSPORT_URL, TRANSPORT_URL,
} from "./values"; } from "./values";
import { import {
socket as schemas, ttp as schemas,
type Socket as Schemas, type TTP as Schemas,
} from "@tensamin/shared/data"; } from "@tensamin/shared/data";
import Loading from "@tensamin/ui/screens/loading"; import Loading from "@tensamin/ui/screens/loading";
import ErrorScreen from "@tensamin/ui/screens/error"; import ErrorScreen from "@tensamin/ui/screens/error";
@ -140,10 +140,10 @@ type ContextType = {
identified: () => boolean; identified: () => boolean;
}; };
const socketContext = createContext<ContextType | undefined>(undefined); const TTPContext = createContext<ContextType | undefined>(undefined);
/** /**
* Provides socket transport state and authenticated send operations to children. * Provides ttp transport state and authenticated send operations to children.
* @param props Component props with children. * @param props Component props with children.
* @returns Loading, error, or provider-wrapped JSX. * @returns Loading, error, or provider-wrapped JSX.
*/ */
@ -186,7 +186,7 @@ export default function Provider(props: {
const client = clientRef.current; const client = clientRef.current;
if (!client) { if (!client) {
return Promise.reject(new Error("Socket is not connected")); return Promise.reject(new Error("ttp is not connected"));
} }
if (options?.noResponse) { if (options?.noResponse) {
@ -240,7 +240,7 @@ export default function Provider(props: {
setIotaPing(remotePing); setIotaPing(remotePing);
} }
} catch (intervalError) { } catch (intervalError) {
log(1, "Socket", "yellow", "Ping failed", intervalError); log(1, "ttp", "yellow", "Ping failed", intervalError);
} }
}, PING_INTERVAL); }, PING_INTERVAL);
@ -310,7 +310,7 @@ export default function Provider(props: {
setErrorDescription( setErrorDescription(
"Unable to connect to the server after multiple attempts. Please check your internet connection or try again later.", "Unable to connect to the server after multiple attempts. Please check your internet connection or try again later.",
); );
log(0, "Socket", "red", "Reconnection attempts exhausted", reason); log(0, "ttp", "red", "Reconnection attempts exhausted", reason);
return; return;
} }
@ -355,7 +355,7 @@ export default function Provider(props: {
return; return;
} }
log(0, "Socket", "red", "Disconnected", closeError, { log(0, "ttp", "red", "Disconnected", closeError, {
stopSending: isStopSendingError(closeError), stopSending: isStopSendingError(closeError),
}); });
scheduleReconnect(closeError); scheduleReconnect(closeError);
@ -381,7 +381,7 @@ export default function Provider(props: {
return; return;
} }
log(0, "Socket", "red", "Connection attempt failed", connectError); log(0, "ttp", "red", "Connection attempt failed", connectError);
scheduleReconnect(connectError); scheduleReconnect(connectError);
} }
} }
@ -490,7 +490,7 @@ export default function Provider(props: {
log( log(
isFatal ? 0 : 1, isFatal ? 0 : 1,
"Socket", "ttp",
isFatal ? "red" : "yellow", isFatal ? "red" : "yellow",
"Identification handshake failed", "Identification handshake failed",
protocolErrorDetails ?? identificationError, protocolErrorDetails ?? identificationError,
@ -578,20 +578,20 @@ export default function Provider(props: {
} }
return ( return (
<socketContext.Provider value={contextValue}> <TTPContext.Provider value={contextValue}>
{props.children} {props.children}
</socketContext.Provider> </TTPContext.Provider>
); );
} }
/** /**
* Returns the active socket context and enforces provider usage. * Returns the active ttp context and enforces provider usage.
* @returns Socket context API for transport operations and connection state. * @returns ttp context API for transport operations and connection state.
*/ */
export function useSocket(): ContextType { export function useTTP(): ContextType {
const context = useContext(socketContext); const context = useContext(TTPContext);
if (!context) { if (!context) {
throw new Error("useSocket must be used within a SocketProvider"); throw new Error("useTTP must be used within a TTPProvider");
} }
return context; return context;
} }

View file

@ -199,7 +199,7 @@ export function createTransportClient<T extends SchemaMap>(
*/ */
const handleIncomingMessage = (message: TypedMessage) => { const handleIncomingMessage = (message: TypedMessage) => {
if (message.type !== "pong") { if (message.type !== "pong") {
log(2, "ttp", "blue", "Received:", message.type, message.data, { log(2, "ttp", "cyan", "Received:", message.type, message.data, {
id: message.id, id: message.id,
}); });
} }
@ -563,7 +563,7 @@ export function createTransportClient<T extends SchemaMap>(
} }
if (type !== "ping") { if (type !== "ping") {
log(2, "ttp", "purple", "Sent:", type, payload, { id: options.id }); log(2, "ttp", "gray", "Sent:", type, payload, { id: options.id });
} }
const expectsResponse = !options?.noResponse; const expectsResponse = !options?.noResponse;

View file

@ -1,7 +1,7 @@
import * as React from "react"; import * as React from "react";
import { useSocket } from "@tensamin/ttp/context"; import { useTTP } from "@tensamin/ttp/context";
import { socket as schemas } from "@tensamin/shared/data"; import { ttp as schemas } from "@tensamin/shared/data";
import type z from "zod"; import type z from "zod";
export type User = z.infer<typeof schemas.get_user_data.response>; export type User = z.infer<typeof schemas.get_user_data.response>;
@ -20,7 +20,7 @@ const UserContext = React.createContext<contextValue | undefined>(undefined);
export default function UserProvider(props: { children: React.ReactNode }) { export default function UserProvider(props: { children: React.ReactNode }) {
const storageRef = React.useRef<Record<number, User>>({}); const storageRef = React.useRef<Record<number, User>>({});
const { send } = useSocket(); const { send } = useTTP();
/** /**
* Executes get. * Executes get.