summaryrefslogtreecommitdiff
path: root/frontend/src/components
diff options
context:
space:
mode:
authorAAGaming <aa@mail.catvibers.me>2022-08-02 18:54:55 -0400
committerAAGaming <aa@mail.catvibers.me>2022-08-02 18:54:55 -0400
commitab6ec981604a32611d972ede634abe7ccd19b0d2 (patch)
tree49f04b47f959cf48703a9b36fecc1d183d98cf7a /frontend/src/components
parentf1e809781ad60cf3f6cddf0a0b94273b61bf1999 (diff)
downloaddecky-loader-03eab07cbf6665260614b8725915ebf3679f4af2.tar.gz
decky-loader-03eab07cbf6665260614b8725915ebf3679f4af2.zip
API for patching existing routes, lower power usev2.0.4-ab6ec98-pre
Diffstat (limited to 'frontend/src/components')
-rw-r--r--frontend/src/components/DeckyRouterState.tsx40
1 files changed, 35 insertions, 5 deletions
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<string, RouterEntry>;
+ routePatches: Map<string, Set<RoutePatch>>;
}
export class DeckyRouterState {
private _routes = new Map<string, RouterEntry>();
+ private _routePatches = new Map<string, Set<RoutePatch>>();
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<Props> = ({ children, deckyRout
useEffect(() => {
function onUpdate() {
+ console.log('test', deckyRouterState.publicState());
setPublicDeckyRouterState({ ...deckyRouterState.publicState() });
}
@@ -62,12 +89,15 @@ export const DeckyRouterStateContextProvider: FC<Props> = ({ 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 (
- <DeckyRouterStateContext.Provider value={{ ...publicDeckyRouterState, addRoute, removeRoute }}>
+ <DeckyRouterStateContext.Provider
+ value={{ ...publicDeckyRouterState, addRoute, addPatch, removePatch, removeRoute }}
+ >
{children}
</DeckyRouterStateContext.Provider>
);