Skip to content

Commit

Permalink
use typed session parameter on mount
Browse files Browse the repository at this point in the history
  • Loading branch information
floodfx committed Jan 30, 2022
1 parent 21af1ae commit 94949aa
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 21 deletions.
5 changes: 3 additions & 2 deletions src/examples/autocomplete/component.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -19,7 +20,7 @@ export class AutocompleteLiveViewComponent extends BaseLiveViewComponent<Autocom
LiveViewInternalEventListener<AutocompleteContext, { type: "run_city_search", city: string }>
{

mount(params: any, session: any, socket: LiveViewSocket<AutocompleteContext>) {
mount(params: LiveViewMountParams, session: Partial<SessionData>, socket: LiveViewSocket<AutocompleteContext>) {
const zip = "";
const city = "";
const stores: Store[] = [];
Expand Down
7 changes: 7 additions & 0 deletions src/examples/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const lvServer = new LiveViewServer({
// support different templates?
});


export const router: LiveViewRouter = {
"/license": new LicenseLiveViewComponent(),
'/sales-dashboard': new SalesDashboardLiveViewComponent(),
Expand All @@ -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();

7 changes: 3 additions & 4 deletions src/examples/license_liveview.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -11,8 +12,7 @@ export class LicenseLiveViewComponent extends BaseLiveViewComponent<LicenseConte
LiveViewExternalEventListener<LicenseContext, "update", Pick<LicenseContext, "seats">>
{

mount(params: any, session: any, socket: LiveViewSocket<LicenseContext>) {
// store this somewhere durable
mount(params: LiveViewMountParams, session: Partial<SessionData>, socket: LiveViewSocket<LicenseContext>) {
const seats = 2;
const amount = calculateLicenseAmount(seats);
return { seats, amount };
Expand Down Expand Up @@ -47,7 +47,6 @@ export class LicenseLiveViewComponent extends BaseLiveViewComponent<LicenseConte
};

handleEvent(event: "update", params: { seats: string }, socket: LiveViewSocket<LicenseContext>) {
// console.log("event:", event, params, socket);
const seats = Number(params.seats || 2);
const amount = calculateLicenseAmount(seats);
return { seats, amount };
Expand Down
16 changes: 7 additions & 9 deletions src/examples/light_liveview.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -9,22 +10,19 @@ export type LightEvent = "on" | "off" | "up" | "down";

export class LightLiveViewComponent extends BaseLiveViewComponent<LightContext, never> implements
LiveViewComponent<LightContext, never>,
LiveViewExternalEventListener<LightContext, "on", any>,
LiveViewExternalEventListener<LightContext, "off", any> {
LiveViewExternalEventListener<LightContext, LightEvent, never> {


mount(params: any, session: any, socket: LiveViewSocket<LightContext>) {
mount(params: LiveViewMountParams, session: Partial<SessionData>, socket: LiveViewSocket<LightContext>) {
return { brightness: 10 };
};

render(context: LightContext) {
return html`
<div id="light">
<h1>Front Porch Light</h1>
<h1>Front Porch Light </h1>
<div class="meter">
<span style="width: ${context.brightness} %>%">
${context.brightness}%
</span>
<div>${context.brightness}%</div><progress id="light_level" value="${context.brightness}" max="100"></progress>
</div>
<button phx-click="off">
Expand All @@ -46,7 +44,7 @@ export class LightLiveViewComponent extends BaseLiveViewComponent<LightContext,
`
};

handleEvent(event: LightEvent, params: any, socket: LiveViewSocket<LightContext>) {
handleEvent(event: LightEvent, params: never, socket: LiveViewSocket<LightContext>) {
const ctx: LightContext = { brightness: socket.context.brightness };
switch (event) {
case 'off':
Expand Down
5 changes: 3 additions & 2 deletions src/examples/pagination/component.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { options_for_select } from "../../server/templates/helpers/options_for_select";
import { live_patch } from "../../server/templates/helpers/live_patch";
import html, { HtmlSafeString, join } from "../../server/templates";
import { BaseLiveViewComponent, LiveViewExternalEventListener, LiveViewSocket, StringPropertyValues } from "../../server/types";
import { BaseLiveViewComponent, LiveViewExternalEventListener, LiveViewMountParams, LiveViewSocket, StringPropertyValues } from "../../server/types";
import { almostExpired, Donation, listItems } from "./data";
import { SessionData } from "express-session";

interface Options {
page: number;
Expand All @@ -16,7 +17,7 @@ export interface PaginateContext {

export class PaginateLiveViewComponent extends BaseLiveViewComponent<PaginateContext, Options> implements LiveViewExternalEventListener<PaginateContext, "select-per-page", Pick<Options, "perPage">> {

mount(params: any, session: any, socket: LiveViewSocket<PaginateContext>) {
mount(params: LiveViewMountParams, session: Partial<SessionData>, socket: LiveViewSocket<PaginateContext>) {
const options = { page: 1, perPage: 10 }
return {
options: options,
Expand Down
5 changes: 3 additions & 2 deletions src/examples/sales_dashboard_liveview.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { SessionData } from "express-session";
import html from "../server/templates";
import { BaseLiveViewComponent, LiveViewExternalEventListener, LiveViewInternalEventListener, LiveViewSocket } from "../server/types";
import { BaseLiveViewComponent, LiveViewExternalEventListener, LiveViewInternalEventListener, LiveViewMountParams, LiveViewSocket } from "../server/types";
import { numberToCurrency } from "./utils";

// generate a random number between min and max
Expand All @@ -23,7 +24,7 @@ export class SalesDashboardLiveViewComponent extends BaseLiveViewComponent<Sales
LiveViewInternalEventListener<SalesDashboardContext, "tick">
{

mount(params: any, session: any, socket: LiveViewSocket<SalesDashboardContext>): SalesDashboardContext {
mount(params: LiveViewMountParams, session: Partial<SessionData>, socket: LiveViewSocket<SalesDashboardContext>): SalesDashboardContext {
if (socket.connected) {
socket.repeat(() => {
socket.sendInternal("tick");
Expand Down
5 changes: 3 additions & 2 deletions src/examples/sorting/component.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { options_for_select } from "../../server/templates/helpers/options_for_select";
import { live_patch } from "../../server/templates/helpers/live_patch";
import html, { HtmlSafeString, join } from "../../server/templates";
import { BaseLiveViewComponent, LiveViewExternalEventListener, LiveViewSocket, StringPropertyValues } from "../../server/types";
import { BaseLiveViewComponent, LiveViewExternalEventListener, LiveViewMountParams, LiveViewSocket, StringPropertyValues } from "../../server/types";
import { almostExpired, Donation, listItems, donations } from "./data";
import { SessionData } from "express-session";

export interface PaginateOptions {
page: number;
Expand All @@ -23,7 +24,7 @@ export class SortLiveViewComponent extends BaseLiveViewComponent<SortContext, Pa
LiveViewExternalEventListener<SortContext, "select-per-page", Pick<PaginateOptions & SortOptions, "perPage">>,
LiveViewExternalEventListener<SortContext, "change-sort", Pick<PaginateOptions & SortOptions, "sort_by" | "sortOrder">> {

mount(params: any, session: any, socket: LiveViewSocket<SortContext>) {
mount(params: LiveViewMountParams, session: Partial<SessionData>, socket: LiveViewSocket<SortContext>) {
const paginateOptions: PaginateOptions = {
page: 1,
perPage: 10,
Expand Down

0 comments on commit 94949aa

Please sign in to comment.