Skip to content

Commit

Permalink
feat: updates to event mechanism to use message type path instead of …
Browse files Browse the repository at this point in the history
…eventType propery

Adds ITechPassword interface support with hook and state interface
  • Loading branch information
ndorin committed Apr 29, 2024
1 parent 5265bf5 commit 1c5ef95
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/lib/shared/hooks/interfaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export * from "./useISetTopBoxcontrols";
export * from "./useIShadesOpenCloseStop";
export * from "./useIShutdownPromptTimer";
export * from "./useISwitchedOutput";
export * from "./useITechPassword";
export * from "./useITransport";
export * from "./useMobileControlTouchpanelController";
export * from "./useTwoWayDisplayBase";
Expand Down
1 change: 1 addition & 0 deletions src/lib/shared/hooks/interfaces/useIShutdownPromptTimer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ export interface IShutdownPromptTimerReturn {
shutdownCancel: () => void;
}

export type IShutdownPromptTimerEventTypes = 'timerStarted' | 'timerFinished' | 'timerCancelled';
33 changes: 33 additions & 0 deletions src/lib/shared/hooks/interfaces/useITechPassword.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { useRoomState } from 'src/lib';
import { ITechPasswordState } from 'src/lib/types/state/state/ITechPasswordState';
import { useWebsocketContext } from 'src/lib/utils';


export function useITechPassword(key: string): ITechPasswordReturn | undefined {
const { sendMessage } = useWebsocketContext();
const techPasswordState = useRoomState(key) as ITechPasswordState | undefined;

if(!techPasswordState) return undefined;

const validatePassword = (password: string) => {
sendMessage(`/room/${key}/validateTechPassword`, {password});
};

const setPassword = (oldPassword: string, newPassword: string) => {
sendMessage(`/room/${key}/setTechPassword`, {oldPassword, newPassword});
};

return { techPasswordState, validatePassword, setPassword };
}

export interface ITechPasswordReturn {
techPasswordState: ITechPasswordState;
validatePassword: (password: string) => void;
setPassword: (oldPassword: string, newPassword: string) => void;
}

export interface ITechPasswordValidationResponse {
isValid: boolean;
}

export type ITechPasswordEventTypes = 'passwordChangedSuccessfully' | 'passwordValidationResult';
4 changes: 2 additions & 2 deletions src/lib/types/state/state/IShutdownPromptTimerState.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DeviceState } from './DeviceState';
import { RoomState } from './RoomState';

export interface IShutdownPromptTimerState extends DeviceState {
export interface IShutdownPromptTimerState extends RoomState {
secondsRemaining?: number;
percentageRemaining?: number;
shutdownPromptSeconds: number;
Expand Down
5 changes: 5 additions & 0 deletions src/lib/types/state/state/ITechPasswordState.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { RoomState } from './RoomState';

export interface ITechPasswordState extends RoomState {
techPasswordLength: number;
}
1 change: 1 addition & 0 deletions src/lib/types/state/state/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export * from './IHasSelectableItemsState';
export * from './IHasSurroundChannelsState';
export * from './IHasSurroundSoundModesState';
export * from './IShutdownPromptTimerState';
export * from './ITechPasswordState';
export * from './LevelControlsState';
export * from './LightingState';
export * from './MatrixRoutingState';
Expand Down
4 changes: 2 additions & 2 deletions src/lib/utils/WebsocketContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { Message, SimpleContent } from '../types';
export interface WebsocketContextType {
sendMessage: (type: string, payload: SimpleContent | unknown ) => void;
sendSimpleMessage: (type: string, payload: boolean | number | string ) => void;
addEventHandler: (key: string, eventType: string, callback: (data: Message) => void) => void;
removeEventHandler: (key: string, eventType: string) => void;
addEventHandler: (eventType: string, key: string, callback: (data: Message) => void) => void;
removeEventHandler: (eventType: string, key: string) => void;
}

const WebsocketContext = createContext<WebsocketContextType>({
Expand Down
21 changes: 15 additions & 6 deletions src/lib/utils/WebsocketProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
useRoomKey,
useWsIsConnected,
} from "../store/runtimeConfig/runtimeSelectors";
import { EventContent, Message } from "../types";
import { Message } from "../types";
import sessionStorageKeys from "../types/classes/session-storage-keys";
import WebsocketContext from "./WebsocketContext";
import { loadValue, saveValue } from "./joinParamsService";
Expand Down Expand Up @@ -90,20 +90,24 @@ const WebsocketProvider = ({ children }: { children: ReactNode }) => {
};

const addEventHandler = useCallback(
(key: string, eventType: string, callback: (data: Message) => void) => {
(eventType: string, key: string, callback: (data: Message) => void) => {
if (!eventHandlers.current[eventType]) {
eventHandlers.current[eventType] = {};
}

eventHandlers.current[eventType][key] = callback;

console.log('event handler added', eventType, key);
},
[]
);

const removeEventHandler = useCallback(
(key: string, eventType: string) => {
(eventType: string, key: string) => {
if (eventHandlers.current[eventType]) {
delete eventHandlers.current[eventType][key];

console.log('event handler removed', eventType, key);
}
},
[]
Expand Down Expand Up @@ -203,9 +207,14 @@ const WebsocketProvider = ({ children }: { children: ReactNode }) => {
break;
}
} else if (message.type.startsWith("/event/")) {
const eventType = (message.content as EventContent).eventType;
if (!eventType) return;
const handlers = eventHandlers.current[eventType];
console.log('event message received', message);
// const eventType = (message.content as EventContent).eventType;
// if (!eventType) return;
const handlers = eventHandlers.current[message.type];

if(!handlers) {
console.log('no handlers found for event type', message.type);
}

if (handlers) {
Object.values(handlers).forEach((handler) => {
Expand Down

0 comments on commit 1c5ef95

Please sign in to comment.