@tronicboy/lit-observe-directive
TypeScript icon, indicating that this package has built-in type declarations

0.2.0 • Public • Published

License

Distributed under the MIT license.

About this package

This is a directive for Lit that allows you to subscribe to RxJS observables in html template literals.

Usage

import { LitElement, css, html } from "lit";
import { customElement } from "lit/decorators.js";
import { map, scan } from "rxjs";
import { webSocket } from "rxjs/webSocket";
import { observe } from "@tronicboy/lit-observe-directive";

const tagName = "wc-lit-websocket";

@customElement(tagName)
export class WcLitWebsocket extends LitElement {
  private socket$ = webSocket<string>({
    url: "ws://localhost:5200",
    protocol: "echo-protocol",
  });
  private messages$ = this.socket$.pipe(scan((acc, message) => [message, ...acc], [] as string[]));

  render() {
    return html`
      <ul>
        ${observe(this.messages$.pipe(map((messages) => messages.map((message) => html`<li>${message}</li>`))))}
      </ul>`;
  }
}

Little bit cluttered but it gets the job done.

Code Style: Google

Readme

Keywords

Package Sidebar

Install

npm i @tronicboy/lit-observe-directive

Weekly Downloads

6

Version

0.2.0

License

MIT

Unpacked Size

31.5 kB

Total Files

22

Last publish

Collaborators

  • tronicboy