Skip to content

Commit

Permalink
Port library to Lit
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepyfran committed Aug 31, 2024
1 parent eb4155c commit 81e07a2
Show file tree
Hide file tree
Showing 15 changed files with 129 additions and 29 deletions.
1 change: 1 addition & 0 deletions packages/components/library/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./src/user-library";
17 changes: 17 additions & 0 deletions packages/components/library/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "@echo/components-library",
"private": true,
"version": "1.0.0",
"scripts": {
"lint": "eslint . --ext ts --report-unused-disable-directives --max-warnings 0",
"typecheck": "tsc --noEmit"
},
"dependencies": {
"@echo/components-shared-controllers": "^1.0.0",
"@echo/core-types": "^1.0.0",
"@echo/services-bootstrap-runtime": "^1.0.0",
"effect": "^3.6.5",
"lit": "^3.2.0",
"@lit/task": "^1.0.1"
}
}
44 changes: 44 additions & 0 deletions packages/components/library/src/user-library.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { StreamEffectController } from "@echo/components-shared-controllers";
import { EffectFnController } from "@echo/components-shared-controllers/src/effect-fn.controller";
import { Library, Player } from "@echo/core-types";
import { LitElement, html } from "lit";
import { customElement } from "lit/decorators.js";
import { map } from "lit/directives/map.js";

/**
* Component that displays the user's library of albums and allows them to
* play them.
*/
@customElement("user-library")
export class UserLibrary extends LitElement {
private _library = new StreamEffectController(this, Library.observeAlbums);
private _playAlbum = new EffectFnController(this, Player.playAlbum);

render() {
return this._library.render({
initial: () => html`<h1>Loading...</h1>`,
item: (albums) => html`
<div>
<br />
${map(
albums,
(album) => html`
<div key="{album.id}">
<h3>${album.name}</h3>
<p>${album.artist.name}</p>
<button @click=${() => this._playAlbum.run(album)}>Play</button>
<hr />
</div>
`,
)}
</div>
`,
});
}
}

declare global {
interface HTMLElementTagNameMap {
userLibrary: UserLibrary;
}
}
1 change: 1 addition & 0 deletions packages/components/library/src/vite-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// <reference types="vite/client" />
7 changes: 7 additions & 0 deletions packages/components/library/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"include": [
"src",
"index.ts"
],
"extends": "../../../tsconfig.json"
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ const isSubscriptionRef = <A, E>(
): streamOrRef is SubscriptionRef.SubscriptionRef<A> =>
"changes" in streamOrRef;

type OutputEffect<A, E> = Effect.Effect<
Stream.Stream<A, E> | SubscriptionRef.SubscriptionRef<A>,
never,
EchoRuntimeServices
>;

/**
* Controller that takes an effect that produces a stream or a subscription ref
* and exposes a render method that renders maps the different states of the
Expand All @@ -52,17 +58,20 @@ export class StreamEffectController<A, E> implements ReactiveController {

constructor(
host: ReactiveControllerHost,
private readonly _streamEffect: Effect.Effect<
Stream.Stream<A, E> | SubscriptionRef.SubscriptionRef<A>,
never,
EchoRuntimeServices
>,
private readonly _streamEffect:
| OutputEffect<A, E>
| (() => OutputEffect<A, E>),
) {
(this.host = host).addController(this);
}

hostConnected(): void {
const consumer$ = this._streamEffect.pipe(
const streamEffect =
typeof this._streamEffect === "function"
? this._streamEffect()
: this._streamEffect;

const consumer$ = streamEffect.pipe(
Effect.flatMap((streamOrRef) => {
const stream = isSubscriptionRef(streamOrRef)
? streamOrRef.changes
Expand Down
2 changes: 2 additions & 0 deletions packages/services/bootstrap-runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
"@echo/services-add-provider-workflow": "^1.0.0",
"@echo/services-app-init": "^1.0.0",
"@echo/services-bootstrap": "^1.0.0",
"@echo/services-library": "^1.0.0",
"@echo/services-player": "^1.0.0",
"effect": "^3.6.5"
}
}
11 changes: 8 additions & 3 deletions packages/services/bootstrap-runtime/src/runtime.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { AddProviderWorkflowLive } from "@echo/services-add-provider-workflow";
import { AppInitLive } from "@echo/services-app-init";
import { MainLive } from "@echo/services-bootstrap";
import { LibraryLive } from "@echo/services-library";
import { PlayerLive } from "@echo/services-player";
import { Layer, ManagedRuntime } from "effect";
import { globalValue } from "effect/GlobalValue";

Expand All @@ -11,9 +13,12 @@ import { globalValue } from "effect/GlobalValue";
export const getOrCreateRuntime = () =>
globalValue("echo-runtime", () =>
ManagedRuntime.make(
Layer.mergeAll(AppInitLive, AddProviderWorkflowLive).pipe(
Layer.provideMerge(MainLive),
),
Layer.mergeAll(
AppInitLive,
AddProviderWorkflowLive,
LibraryLive,
PlayerLive,
).pipe(Layer.provideMerge(MainLive)),
),
);

Expand Down
1 change: 1 addition & 0 deletions packages/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
},
"dependencies": {
"@echo/components-add-provider": "^1.0.0",
"@echo/components-library": "^1.0.0",
"@echo/components-shared-controllers": "^1.0.0",
"@echo/core-types": "^1.0.0",
"@echo/services-bootstrap-runtime": "^1.0.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/web/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { initializeWorkers } from "@echo/services-bootstrap-workers";
import { AppInit } from "@echo/core-types";
import { EffectController } from "@echo/components-shared-controllers";
import "@echo/components-add-provider";
import "@echo/components-library";

initializeWorkers();

Expand All @@ -20,8 +21,7 @@ export class MyElement extends LitElement {
complete: () => html`
<div>
<add-provider></add-provider>
<!-- <user-library /> -->
<!-- <provider-status /> -->
<user-library></user-library>
</div>
`,
error: () =>
Expand Down
10 changes: 10 additions & 0 deletions tools/plop-templates/components/generator.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ module.exports = {
path: "packages/components/{{dashCase name}}/index.ts",
templateFile: `${__dirname}/template/index.ts.hbs`,
},
{
type: "add",
path: "packages/components/{{dashCase name}}/src/{{dashCase name}}.ts",
templateFile: `${__dirname}/template/component.ts.hbs`,
},
{
type: "add",
path: "packages/components/{{dashCase name}}/src/vite-env.d.ts",
templateFile: `${__dirname}/template/vite-env.d.ts.hbs`,
},
{
type: "add",
path: "packages/components/{{dashCase name}}/package.json",
Expand Down
18 changes: 18 additions & 0 deletions tools/plop-templates/components/template/component.ts.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { LitElement, html } from "lit";
import { customElement } from "lit/decorators.js";

/**
* Description of what the {{properCase name}} component does.
*/
@customElement("{{dashCase name}}")
export class {{pascalCase name}} extends LitElement {
render() {
return html`<h1>{{pascalCase name}}</h1>`;
}
}

declare global {
interface HTMLElementTagNameMap {
"{{dashCase name}}": {{pascalCase name}};
}
}
19 changes: 1 addition & 18 deletions tools/plop-templates/components/template/index.ts.hbs
Original file line number Diff line number Diff line change
@@ -1,18 +1 @@
import { LitElement, html } from "lit";
import { customElement } from "lit/decorators.js";

/**
* Description of what the {{properCase name}} component does.
*/
@customElement("{{dashCase name}}")
export class {{pascalCase name}} extends LitElement {
render() {
return html`<h1>{{pascalCase name}}</h1>`;
}
}

declare global {
interface HTMLElementTagNameMap {
"{{dashCase name}}": {{pascalCase name}};
}
}
export * from "./src/{{dashCase name}}";
1 change: 1 addition & 0 deletions tools/plop-templates/components/template/package.json.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"typecheck": "tsc --noEmit"
},
"dependencies": {
"@echo/components-shared-controllers": "^1.0.0",
"@echo/core-types": "^1.0.0",
"@echo/services-bootstrap-runtime": "^1.0.0",
"effect": "^3.6.5",
Expand Down
1 change: 1 addition & 0 deletions tools/plop-templates/components/template/vite-env.d.ts.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// <reference types="vite/client" />

0 comments on commit 81e07a2

Please sign in to comment.