Skip to content

Commit

Permalink
spike of template parts and jexpr
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Nelson committed Dec 9, 2023
1 parent 873bebb commit 077dc84
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ livestate_testbed-*.tar

# In case you use Node.js/npm, you want to ignore these.
npm-debug.log
/assets/node_modules/

node_modules
dist
3 changes: 2 additions & 1 deletion assets/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ import './todo-list-element.ts';
// import './react-todo';
import './join-params';
import './patch-thing';
import './display-error';
import './display-error';
import './live-state-element';
31 changes: 31 additions & 0 deletions assets/js/live-state-element.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { TemplateInstance } from '@github/template-parts'
import { parse, EvalAstFactory } from 'jexpr';
const astFactory = new EvalAstFactory();
import type { TemplatePart, TemplateTypeInit } from '@github/template-parts';

const processor = {
processCallback(_: TemplateInstance, parts: Iterable<TemplatePart>, params: unknown): void {
if (typeof params !== 'object' || !params) return
for (const part of parts) {
const expr = parse(part.expression, astFactory);
part.value = expr?.evaluate(params);
}
}
};


export class LiveStateElement extends HTMLElement {

templateInstances: TemplateInstance[] = [];

connectedCallback() {
this.querySelectorAll('template[livestate]').forEach(tpl => {
const instance = new TemplateInstance((tpl as HTMLTemplateElement), { person: { firstName: 'Bob', lastName: 'Jones' } }, processor);
this.templateInstances.push(instance);
this.appendChild(instance);
});
}

}

window.customElements.define('live-state', LiveStateElement);
74 changes: 72 additions & 2 deletions assets/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions assets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
"build": "tsc && vite build"
},
"dependencies": {
"@github/template-parts": "^0.5.4",
"jexpr": "^1.0.0-pre.4",
"lit": "^2.4.0",
"stampino": "[email protected]:jogibear9988/stampino.git",
"phx-live-state": "file:../../phx-live-state"
},
"devDependencies": {
Expand Down
6 changes: 5 additions & 1 deletion lib/livestate_testbed_web/controllers/page_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,16 @@ defmodule LivestateTestbedWeb.PageController do
conn |> assign(:url, url()) |> render("patchy.html")
end

def markup_test(conn, _params) do
conn |> assign(:url, url()) |> render("markup_test.html")
end

def errors(conn, _params) do
conn |> assign(:url, url()) |> render("errors.html")
end

defp url() do
"#{String.replace(Endpoint.url(), "http:", "ws:")}/socket"
"#{Endpoint.url() |> String.replace("http:", "ws:") |> String.replace("https:", "wss:")}/socket"
|> IO.inspect(label: "building livestate url")
end
end
1 change: 1 addition & 0 deletions lib/livestate_testbed_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ defmodule LivestateTestbedWeb.Router do
get "/react", PageController, :react
get "/join_params", PageController, :join_params
get "/patchy", PageController, :patchy
get "/markup_test", PageController, :markup_test
get "/errors", PageController, :errors
end

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<live-state url={@url} topic="people">
<template>
{{person.firstName}} {{person.lastName}}
</template>
</live-state>

0 comments on commit 077dc84

Please sign in to comment.