From 94949aaa1a97655c66c0f0b774867285eb68692c Mon Sep 17 00:00:00 2001 From: Donnie Flood Date: Sun, 30 Jan 2022 11:20:44 -0700 Subject: [PATCH] use typed session parameter on mount --- src/examples/autocomplete/component.ts | 5 +++-- src/examples/index.ts | 7 +++++++ src/examples/license_liveview.ts | 7 +++---- src/examples/light_liveview.ts | 16 +++++++--------- src/examples/pagination/component.ts | 5 +++-- src/examples/sales_dashboard_liveview.ts | 5 +++-- src/examples/sorting/component.ts | 5 +++-- 7 files changed, 29 insertions(+), 21 deletions(-) diff --git a/src/examples/autocomplete/component.ts b/src/examples/autocomplete/component.ts index ece52c9f..eaf1c17c 100644 --- a/src/examples/autocomplete/component.ts +++ b/src/examples/autocomplete/component.ts @@ -1,8 +1,9 @@ import html from "../../server/templates"; -import { BaseLiveViewComponent, LiveViewExternalEventListener, LiveViewInternalEventListener, LiveViewSocket } from "../../server/types"; +import { BaseLiveViewComponent, LiveViewExternalEventListener, LiveViewInternalEventListener, LiveViewMountParams, LiveViewSocket } from "../../server/types"; import { WebSocket } from "ws"; import { searchByCity, searchByZip, Store } from "../live-search/data"; import { suggest } from "./data"; +import { SessionData } from "express-session"; export interface AutocompleteContext { zip: string; @@ -19,7 +20,7 @@ export class AutocompleteLiveViewComponent extends BaseLiveViewComponent { - mount(params: any, session: any, socket: LiveViewSocket) { + mount(params: LiveViewMountParams, session: Partial, socket: LiveViewSocket) { const zip = ""; const city = ""; const stores: Store[] = []; diff --git a/src/examples/index.ts b/src/examples/index.ts index f34bd2b9..22ecc15e 100644 --- a/src/examples/index.ts +++ b/src/examples/index.ts @@ -16,6 +16,7 @@ const lvServer = new LiveViewServer({ // support different templates? }); + export const router: LiveViewRouter = { "/license": new LicenseLiveViewComponent(), '/sales-dashboard': new SalesDashboardLiveViewComponent(), @@ -32,5 +33,11 @@ lvServer.registerLiveViewRoutes(router) // register single route lvServer.registerLiveViewRoute("/servers", new ServersLiveViewComponent()) +// add your own routes to the express app +// lvServer.expressApp.get("/", (req, res) => { +// res.send("Hello World!"); +// } + // start server lvServer.start(); + diff --git a/src/examples/license_liveview.ts b/src/examples/license_liveview.ts index 75b24f86..e6110117 100644 --- a/src/examples/license_liveview.ts +++ b/src/examples/license_liveview.ts @@ -1,5 +1,6 @@ +import { SessionData } from "express-session"; import html from "../server/templates"; -import { BaseLiveViewComponent, LiveViewExternalEventListener, LiveViewSocket } from "../server/types"; +import { BaseLiveViewComponent, LiveViewExternalEventListener, LiveViewMountParams, LiveViewSocket } from "../server/types"; import { numberToCurrency } from "./utils"; export interface LicenseContext { @@ -11,8 +12,7 @@ export class LicenseLiveViewComponent extends BaseLiveViewComponent> { - mount(params: any, session: any, socket: LiveViewSocket) { - // store this somewhere durable + mount(params: LiveViewMountParams, session: Partial, socket: LiveViewSocket) { const seats = 2; const amount = calculateLicenseAmount(seats); return { seats, amount }; @@ -47,7 +47,6 @@ export class LicenseLiveViewComponent extends BaseLiveViewComponent) { - // console.log("event:", event, params, socket); const seats = Number(params.seats || 2); const amount = calculateLicenseAmount(seats); return { seats, amount }; diff --git a/src/examples/light_liveview.ts b/src/examples/light_liveview.ts index f09fb860..479ab708 100644 --- a/src/examples/light_liveview.ts +++ b/src/examples/light_liveview.ts @@ -1,5 +1,6 @@ +import { SessionData } from "express-session"; import html from "../server/templates"; -import { BaseLiveViewComponent, LiveViewComponent, LiveViewExternalEventListener, LiveViewSocket } from "../server/types"; +import { BaseLiveViewComponent, LiveViewComponent, LiveViewExternalEventListener, LiveViewMountParams, LiveViewSocket } from "../server/types"; export interface LightContext { brightness: number; @@ -9,22 +10,19 @@ export type LightEvent = "on" | "off" | "up" | "down"; export class LightLiveViewComponent extends BaseLiveViewComponent implements LiveViewComponent, - LiveViewExternalEventListener, - LiveViewExternalEventListener { + LiveViewExternalEventListener { - mount(params: any, session: any, socket: LiveViewSocket) { + mount(params: LiveViewMountParams, session: Partial, socket: LiveViewSocket) { return { brightness: 10 }; }; render(context: LightContext) { return html`
-

Front Porch Light

+

Front Porch Light

- - ${context.brightness}% - +
${context.brightness}%