From ab6ec981604a32611d972ede634abe7ccd19b0d2 Mon Sep 17 00:00:00 2001 From: AAGaming Date: Tue, 2 Aug 2022 18:54:55 -0400 Subject: API for patching existing routes, lower power use --- frontend/src/components/DeckyRouterState.tsx | 40 ++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 5 deletions(-) (limited to 'frontend/src/components/DeckyRouterState.tsx') diff --git a/frontend/src/components/DeckyRouterState.tsx b/frontend/src/components/DeckyRouterState.tsx index 4aab7abd..c7da4034 100644 --- a/frontend/src/components/DeckyRouterState.tsx +++ b/frontend/src/components/DeckyRouterState.tsx @@ -6,17 +6,21 @@ export interface RouterEntry { component: ComponentType; } +export type RoutePatch = (route: RouteProps) => RouteProps; + interface PublicDeckyRouterState { routes: Map; + routePatches: Map>; } export class DeckyRouterState { private _routes = new Map(); + private _routePatches = new Map>(); public eventBus = new EventTarget(); publicState(): PublicDeckyRouterState { - return { routes: this._routes }; + return { routes: this._routes, routePatches: this._routePatches }; } addRoute(path: string, component: RouterEntry['component'], props: RouterEntry['props'] = {}) { @@ -24,6 +28,26 @@ export class DeckyRouterState { this.notifyUpdate(); } + addPatch(path: string, patch: RoutePatch) { + let patchList = this._routePatches.get(path); + if (!patchList) { + patchList = new Set(); + this._routePatches.set(path, patchList); + } + patchList.add(patch); + this.notifyUpdate(); + return patch; + } + + removePatch(path: string, patch: RoutePatch) { + const patchList = this._routePatches.get(path); + patchList?.delete(patch); + if (patchList?.size == 0) { + this._routePatches.delete(path); + } + this.notifyUpdate(); + } + removeRoute(path: string) { this._routes.delete(path); this.notifyUpdate(); @@ -36,6 +60,8 @@ export class DeckyRouterState { interface DeckyRouterStateContext extends PublicDeckyRouterState { addRoute(path: string, component: RouterEntry['component'], props: RouterEntry['props']): void; + addPatch(path: string, patch: RoutePatch): RoutePatch; + removePatch(path: string, patch: RoutePatch): void; removeRoute(path: string): void; } @@ -54,6 +80,7 @@ export const DeckyRouterStateContextProvider: FC = ({ children, deckyRout useEffect(() => { function onUpdate() { + console.log('test', deckyRouterState.publicState()); setPublicDeckyRouterState({ ...deckyRouterState.publicState() }); } @@ -62,12 +89,15 @@ export const DeckyRouterStateContextProvider: FC = ({ children, deckyRout return () => deckyRouterState.eventBus.removeEventListener('update', onUpdate); }, []); - const addRoute = (path: string, component: RouterEntry['component'], props: RouterEntry['props'] = {}) => - deckyRouterState.addRoute(path, component, props); - const removeRoute = (path: string) => deckyRouterState.removeRoute(path); + const addRoute = deckyRouterState.addRoute.bind(deckyRouterState); + const addPatch = deckyRouterState.addPatch.bind(deckyRouterState); + const removePatch = deckyRouterState.removePatch.bind(deckyRouterState); + const removeRoute = deckyRouterState.removeRoute.bind(deckyRouterState); return ( - + {children} ); -- cgit v1.2.3