Skip to content

Commit dbc33d3

Browse files
committed
∑Merge branch 'main' of github.com:floodfx/liveviewjs
2 parents 2920d49 + 6add689 commit dbc33d3

File tree

23 files changed

+298
-358
lines changed

23 files changed

+298
-358
lines changed

packages/core/coverage/clover.xml

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<coverage generated="1674275714898" clover="3.2.0">
3-
<project timestamp="1674275714898" name="All files">
4-
<metrics statements="1474" coveredstatements="1190" conditionals="471" coveredconditionals="356" methods="311" coveredmethods="238" elements="2256" coveredelements="1784" complexity="0" loc="1474" ncloc="1474" packages="13" files="47" classes="47"/>
2+
<coverage generated="1674322371963" clover="3.2.0">
3+
<project timestamp="1674322371963" name="All files">
4+
<metrics statements="1474" coveredstatements="1190" conditionals="469" coveredconditionals="354" methods="311" coveredmethods="238" elements="2254" coveredelements="1782" complexity="0" loc="1474" ncloc="1474" packages="13" files="47" classes="47"/>
55
<package name="server">
66
<metrics statements="11" coveredstatements="11" conditionals="0" coveredconditionals="0" methods="0" coveredmethods="0"/>
77
<file name="index.ts" path="/Users/donnie/src/github.com/floodfx/liveviewjs/packages/core/src/server/index.ts">
@@ -194,7 +194,7 @@
194194
<line num="36" count="15" type="stmt"/>
195195
<line num="37" count="15" type="stmt"/>
196196
<line num="39" count="15" type="stmt"/>
197-
<line num="48" count="11511" type="cond" truecount="2" falsecount="0"/>
197+
<line num="48" count="9209" type="cond" truecount="2" falsecount="0"/>
198198
<line num="57" count="8" type="cond" truecount="2" falsecount="0"/>
199199
<line num="61" count="19" type="stmt"/>
200200
<line num="65" count="17" type="cond" truecount="0" falsecount="1"/>
@@ -203,13 +203,13 @@
203203
<line num="70" count="0" type="stmt"/>
204204
<line num="72" count="17" type="stmt"/>
205205
<line num="78" count="5" type="stmt"/>
206-
<line num="82" count="5" type="stmt"/>
207-
<line num="83" count="11510" type="stmt"/>
208-
<line num="84" count="11510" type="stmt"/>
209-
<line num="85" count="6175" type="cond" truecount="1" falsecount="0"/>
206+
<line num="82" count="4" type="stmt"/>
207+
<line num="83" count="9208" type="stmt"/>
208+
<line num="84" count="9208" type="stmt"/>
209+
<line num="85" count="4940" type="cond" truecount="1" falsecount="0"/>
210210
<line num="86" count="3567" type="stmt"/>
211-
<line num="88" count="6175" type="stmt"/>
212-
<line num="91" count="5" type="stmt"/>
211+
<line num="88" count="4940" type="stmt"/>
212+
<line num="91" count="4" type="stmt"/>
213213
<line num="106" count="15" type="stmt"/>
214214
<line num="107" count="1" type="stmt"/>
215215
<line num="108" count="1" type="stmt"/>
@@ -1391,7 +1391,7 @@
13911391
</file>
13921392
</package>
13931393
<package name="server.templates.helpers">
1394-
<metrics statements="90" coveredstatements="90" conditionals="66" coveredconditionals="61" methods="24" coveredmethods="24"/>
1394+
<metrics statements="90" coveredstatements="90" conditionals="64" coveredconditionals="59" methods="24" coveredmethods="24"/>
13951395
<file name="form_for.ts" path="/Users/donnie/src/github.com/floodfx/liveviewjs/packages/core/src/server/templates/helpers/form_for.ts">
13961396
<metrics statements="7" coveredstatements="7" conditionals="8" coveredconditionals="8" methods="1" coveredmethods="1"/>
13971397
<line num="1" count="15" type="stmt"/>
@@ -1469,13 +1469,13 @@
14691469
<line num="26" count="3" type="cond" truecount="2" falsecount="0"/>
14701470
</file>
14711471
<file name="live_title_tag.ts" path="/Users/donnie/src/github.com/floodfx/liveviewjs/packages/core/src/server/templates/helpers/live_title_tag.ts">
1472-
<metrics statements="6" coveredstatements="6" conditionals="10" coveredconditionals="10" methods="1" coveredmethods="1"/>
1472+
<metrics statements="6" coveredstatements="6" conditionals="8" coveredconditionals="8" methods="1" coveredmethods="1"/>
14731473
<line num="1" count="15" type="stmt"/>
1474-
<line num="8" count="15" type="stmt"/>
1475-
<line num="9" count="3" type="cond" truecount="2" falsecount="0"/>
1476-
<line num="10" count="3" type="cond" truecount="2" falsecount="0"/>
1477-
<line num="11" count="3" type="cond" truecount="2" falsecount="0"/>
1478-
<line num="12" count="3" type="cond" truecount="4" falsecount="0"/>
1474+
<line num="4" count="15" type="stmt"/>
1475+
<line num="5" count="3" type="stmt"/>
1476+
<line num="6" count="3" type="cond" truecount="2" falsecount="0"/>
1477+
<line num="7" count="3" type="cond" truecount="2" falsecount="0"/>
1478+
<line num="8" count="3" type="cond" truecount="4" falsecount="0"/>
14791479
</file>
14801480
<file name="options_for_select.ts" path="/Users/donnie/src/github.com/floodfx/liveviewjs/packages/core/src/server/templates/helpers/options_for_select.ts">
14811481
<metrics statements="16" coveredstatements="16" conditionals="11" coveredconditionals="11" methods="8" coveredmethods="8"/>

packages/core/coverage/coverage-final.json

Lines changed: 2 additions & 2 deletions
Large diffs are not rendered by default.

packages/core/dist/liveview.d.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -963,11 +963,7 @@ interface LiveTitleOptions {
963963
title: string;
964964
}
965965

966-
interface LiveTitleTagOptions {
967-
prefix?: string;
968-
suffix?: string;
969-
}
970-
declare const live_title_tag: (title: string, options?: LiveTitleTagOptions) => HtmlSafeString;
966+
declare const live_title_tag: (options: LiveTitleOptions) => HtmlSafeString;
971967

972968
declare type Options = string[] | Record<string, string>;
973969
declare type Selected = string | string[];
@@ -2035,8 +2031,8 @@ declare const mime: Mime;
20352031
* e.g. NodeExpressServerAdaptor or DenoOakServerAdaptor
20362032
*/
20372033
interface LiveViewServerAdaptor<THttpMiddleware, TWsMiddleware> {
2038-
httpMiddleware(): THttpMiddleware;
2039-
wsMiddleware(): TWsMiddleware;
2034+
httpMiddleware: THttpMiddleware;
2035+
wsMiddleware: TWsMiddleware;
20402036
}
20412037

20422038
export { AnyLiveContext, AnyLiveEvent, AnyLiveInfo, AnyLivePushEvent, BaseLiveComponent, BaseLiveView, ConsumeUploadedEntriesMeta, CsrfGenerator, FileSystemAdaptor, FlashAdaptor, HtmlSafeString, HttpLiveComponentSocket, HttpLiveViewSocket, HttpRequestAdaptor, IdGenerator, Info, JS, LiveComponent, LiveComponentMeta, LiveComponentSocket, LiveContext, LiveEvent, LiveInfo, LiveTitleOptions, LiveView, LiveViewChangeset, LiveViewChangesetErrors, LiveViewChangesetFactory, LiveViewHtmlPageTemplate, LiveViewManager, LiveViewMeta, LiveViewMountParams, LiveViewRouter, LiveViewServerAdaptor, LiveViewSocket, LiveViewTemplate, LiveViewWrapperTemplate, MimeSource, Parts, PathParams, Phx, PubSub, Publisher, SerDe, SessionData, SessionFlashAdaptor, SingleProcessPubSub, Subscriber, SubscriberFunction, SubscriberId, UploadConfig, UploadConfigOptions, UploadEntry, WsAdaptor, WsCloseListener, WsHandler, WsHandlerConfig, WsHandlerContext, WsLiveComponentSocket, WsLiveViewSocket, WsMessageRouter, WsMsgListener, createLiveComponent, createLiveView, deepDiff, diffArrays, diffArrays2, error_tag, escapehtml, form_for, handleHttpLiveView, hashLiveComponent, html, join, live_file_input, live_img_preview, live_patch, live_title_tag, matchRoute, mime, newChangesetFactory, nodeHttpFetch, options_for_select, safe, submit, telephone_input, text_input };

packages/core/dist/liveview.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,8 +1078,8 @@ const live_patch = (anchorBody, options) => {
10781078
return html `<a data-phx-link="patch" data-phx-link-state="push" href="${safe(buildHref(options))}"${options.className ? safe(` class="${options.className}"`) : ""}>${anchorBody}</a>`;
10791079
};
10801080

1081-
const live_title_tag = (title, options) => {
1082-
const { prefix, suffix } = options ?? {};
1081+
const live_title_tag = (options) => {
1082+
const { title, prefix, suffix } = options;
10831083
const prefix_data = prefix ? safe(` data-prefix="${prefix}"`) : "";
10841084
const suffix_data = suffix ? safe(` data-suffix="${suffix}"`) : "";
10851085
return html `<title${prefix_data}${suffix_data}>${prefix ?? ""}${title}${suffix ?? ""}</title>`;

packages/core/dist/liveview.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,8 +1071,8 @@ const live_patch = (anchorBody, options) => {
10711071
return html `<a data-phx-link="patch" data-phx-link-state="push" href="${safe(buildHref(options))}"${options.className ? safe(` class="${options.className}"`) : ""}>${anchorBody}</a>`;
10721072
};
10731073

1074-
const live_title_tag = (title, options) => {
1075-
const { prefix, suffix } = options ?? {};
1074+
const live_title_tag = (options) => {
1075+
const { title, prefix, suffix } = options;
10761076
const prefix_data = prefix ? safe(` data-prefix="${prefix}"`) : "";
10771077
const suffix_data = suffix ? safe(` data-suffix="${suffix}"`) : "";
10781078
return html `<title${prefix_data}${suffix_data}>${prefix ?? ""}${title}${suffix ?? ""}</title>`;

packages/core/src/server/server.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
* e.g. NodeExpressServerAdaptor or DenoOakServerAdaptor
44
*/
55
export interface LiveViewServerAdaptor<THttpMiddleware, TWsMiddleware> {
6-
httpMiddleware(): THttpMiddleware;
7-
wsMiddleware(): TWsMiddleware;
6+
httpMiddleware: THttpMiddleware;
7+
wsMiddleware: TWsMiddleware;
88
}

packages/core/src/server/templates/helpers/live_title_tag.test.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,17 @@ import { live_title_tag } from "./live_title_tag";
22

33
describe("live title tag helper", () => {
44
it("returns live title tag", () => {
5-
const result = live_title_tag("title").toString();
5+
const result = live_title_tag({ title: "title" }).toString();
66
expect(result).toBe(`<title>title</title>`);
77
});
88

99
it("returns live title tag with prefix", () => {
10-
const result = live_title_tag("title", {
11-
prefix: "prefix ",
12-
}).toString();
10+
const result = live_title_tag({ title: "title", prefix: "prefix " }).toString();
1311
expect(result).toBe(`<title data-prefix="prefix ">prefix title</title>`);
1412
});
1513

1614
it("returns live title tag with prefix and suffix", () => {
17-
const result = live_title_tag("title", {
18-
prefix: "prefix ",
19-
suffix: " suffix",
20-
}).toString();
15+
const result = live_title_tag({ title: "title", prefix: "prefix ", suffix: " suffix" }).toString();
2116
expect(result).toBe(`<title data-prefix="prefix " data-suffix=" suffix">prefix title suffix</title>`);
2217
});
2318
});

packages/core/src/server/templates/helpers/live_title_tag.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
import { html, HtmlSafeString, safe } from "../htmlSafeString";
2+
import { LiveTitleOptions } from "./live_title";
23

3-
interface LiveTitleTagOptions {
4-
prefix?: string;
5-
suffix?: string;
6-
}
7-
8-
export const live_title_tag = (title: string, options?: LiveTitleTagOptions): HtmlSafeString => {
9-
const { prefix, suffix } = options ?? {};
4+
export const live_title_tag = (options: LiveTitleOptions): HtmlSafeString => {
5+
const { title, prefix, suffix } = options;
106
const prefix_data = prefix ? safe(` data-prefix="${prefix}"`) : "";
117
const suffix_data = suffix ? safe(` data-suffix="${suffix}"`) : "";
128
return html`<title${prefix_data}${suffix_data}>${prefix ?? ""}${title}${suffix ?? ""}</title>`;

packages/deno/src/deno/server.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ interface DenoOakLiveViewServerOptions {
3131
fileSystemAdaptor?: FileSystemAdaptor;
3232
wrapperTemplate?: LiveViewWrapperTemplate;
3333
onError?: (err: any) => void;
34+
debug?: (msg: string) => void;
3435
}
3536

3637
type DenoMiddleware = (
@@ -89,10 +90,11 @@ export class DenoOakLiveViewServer implements LiveViewServerAdaptor<DenoMiddlewa
8990
flashAdaptor: this.flashAdapter,
9091
pubSub: this.pubSub,
9192
onError: options?.onError,
93+
debug: options?.debug,
9294
};
9395
}
9496

95-
wsMiddleware(): (ctx: Context<Record<string, any>, Record<string, any>>) => Promise<void> {
97+
get wsMiddleware(): (ctx: Context<Record<string, any>, Record<string, any>>) => Promise<void> {
9698
return async (ctx: Context<Record<string, any>, Record<string, any>>) => {
9799
// upgrade the request to a websocket connection
98100
const ws = await ctx.upgrade();
@@ -101,7 +103,7 @@ export class DenoOakLiveViewServer implements LiveViewServerAdaptor<DenoMiddlewa
101103
};
102104
}
103105

104-
httpMiddleware(): (
106+
get httpMiddleware(): (
105107
ctx: Context<Record<string, any>, Record<string, any>>,
106108
next: () => Promise<unknown>
107109
) => Promise<void> {

packages/deno/src/example/index.ts

Lines changed: 22 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,43 @@
11
import { BroadcastChannelPubSub } from "../deno/broadcastChannelPubSub.ts";
22
import { DenoOakLiveViewServer } from "../deno/server.ts";
3-
import {
4-
Application,
5-
autocompleteLiveView,
6-
booksLiveView,
7-
counterLiveView,
8-
dashboardLiveView,
9-
decarbLiveView,
10-
helloNameLiveView,
11-
helloToggleEmojiLiveView,
12-
jsCmdsLiveView,
13-
LiveViewRouter,
14-
paginateLiveView,
15-
photosLiveView,
16-
printLiveView,
17-
Router,
18-
searchLiveView,
19-
send,
20-
serversLiveView,
21-
sortLiveView,
22-
volumeLiveView,
23-
volunteerLiveView,
24-
xkcdLiveView,
25-
} from "../deps.ts";
3+
import { Application, Router } from "../deps.ts";
264
import { indexHandler } from "./indexHandler.ts";
275
import { liveHtmlTemplate, wrapperTemplate } from "./liveTemplates.ts";
28-
import { rtCounterLiveView } from "./liveview/rtcounter.ts";
29-
30-
// map request paths to LiveViews
31-
const lvRouter: LiveViewRouter = {
32-
"/autocomplete": autocompleteLiveView,
33-
"/decarbonize": decarbLiveView,
34-
"/prints": printLiveView,
35-
"/volume": volumeLiveView,
36-
"/paginate": paginateLiveView,
37-
"/dashboard": dashboardLiveView,
38-
"/search": searchLiveView,
39-
"/servers": serversLiveView,
40-
"/sort": sortLiveView,
41-
"/volunteers": volunteerLiveView,
42-
"/counter": counterLiveView,
43-
"/jscmds": jsCmdsLiveView,
44-
"/photos": photosLiveView,
45-
"/xkcd": xkcdLiveView,
46-
"/rtcounter": rtCounterLiveView,
47-
"/books": booksLiveView,
48-
"/helloToggle": helloToggleEmojiLiveView,
49-
"/hi/:name": helloNameLiveView,
50-
};
51-
52-
// configure your oak app
53-
const app = new Application();
54-
const router = new Router();
55-
56-
// middleware to log requests
57-
app.use(async (ctx, next) => {
58-
console.log(`${ctx.request.method} ${ctx.request.url} - ${new Date().toISOString()}`);
59-
await next();
60-
});
6+
import { liveRouter } from "./liveview/router.ts";
7+
import { logRequests, serveStatic } from "./oak.ts";
618

629
// initialize the LiveViewServer
63-
const liveView = new DenoOakLiveViewServer(
64-
lvRouter,
10+
const liveServer = new DenoOakLiveViewServer(
11+
liveRouter,
6512
liveHtmlTemplate,
6613
{ title: "Deno Demo", suffix: " · LiveViewJS" },
6714
{
68-
wrapperTemplate: wrapperTemplate,
15+
wrapperTemplate,
6916
pubSub: new BroadcastChannelPubSub(),
17+
// onError: console.error, // uncomment to see errors
18+
// debug: console.log, // uncomment to see messages
7019
}
7120
);
7221

73-
// setup the LiveViewJS HTTP middleware
74-
app.use(liveView.httpMiddleware());
75-
76-
// in Deno, websocket requests come in over http and get "upgraded" to web socket requests
77-
// so we add the wsMiddleware to this http route
78-
router.get("/live/websocket", liveView.wsMiddleware());
79-
80-
// setup index route
22+
// configure oak router
23+
const router = new Router();
24+
// send websocket requests to the LiveViewJS server
25+
router.get("/live/websocket", liveServer.wsMiddleware);
26+
// setup the index route
8127
router.get("/", indexHandler);
28+
// serve static files (images, js, and css) from public directory
29+
router.get("/(.*).(png|jpg|jpeg|gif|js|js.map|css)", serveStatic);
8230

83-
// serve images or js files
84-
router.get("/(.*).(png|jpg|jpeg|gif|js|js.map)", async (ctx) => {
85-
const path = ctx.request.url.pathname;
86-
await send(ctx, path, {
87-
root: "./public/",
88-
});
89-
});
90-
31+
// configure oak application
32+
const app = new Application();
33+
// send http requests to the LiveViewJS server
34+
app.use(liveServer.httpMiddleware);
35+
app.use(logRequests(liveRouter));
9136
// add oak router to app
9237
app.use(router.routes());
9338
app.use(router.allowedMethods());
9439

9540
// listen for requests
96-
const port = Number(Deno.env.get("PORT")) || 9001;
97-
console.log(`LiveViewJS Express is listening on port ${port}!`);
41+
const port = Number(Deno.env.get("PORT") ?? 9001);
42+
console.log(`LiveViewJS (Deno) is listening at: http://localhost:${port}`);
9843
await app.listen({ port });

0 commit comments

Comments
 (0)