npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

compago

v5.0.5

Published

A minimalist MVC framework for modern browsers.

Downloads

115

Readme

Compago

Actions Status npm

Compago is a minimalist framework inspired by Domain-Driven Design for building applications using Web Components.

Although most components are isomorphic and can be used independently, Compago works best with Lit for which it offers extensions focused on simplifying advanced state management in web components.

Installation

Node.js:

npm i compago
import { ... } from "compago";

Components can also be imported separately, to avoid loading extra dependencies, for example:

import { Result } from "compago/result";
import { Observable } from "compago/observable";

Deno:

import { ... } from "https://raw.githubusercontent.com/zandaqo/compago/master/mod.ts"

State Management with Observables

UI frameworks like React and Lit provide built-in mechanisms to deal with simple UI state expressed as primitive values such as useState hooks in React or Lit's reactive properties. However, those mechanisms are cumbersome to work with when dealing with a complex domain state that involves nested objects and arrays. To give reactivity to complex domain objects, Compago introduces the Observable class. Observable wraps a given domain object into a proxy that reacts to changes on the object and all its nested structures with a change event. When used in Lit elements, additional helpers like @observer, @observe decorators and bond directive make reactivity and data binding seamless.

Quick Example

import { html, LitElement } from "lit";
import { bond, Observable, observe, observer } from "compago";

class Todo {
  description = "";
  done = false;
}

@observer()
class TodoItem extends LitElement {
  // Create an observable of a Todo object tied to the element.
  // The observable will be treated as internal state and updates within the observable
  // (and all nested objects) will be propagated
  // through the usual lifecycle of reactive properties.
  @observe()
  state = new Observable(new Todo());

  // You can hook into updates to the observable properties just like reactive ones.
  // Names for the nested properties are given as a path, e.g. `state.done`, `state.description`
  protected updated(changedProperties: Map<string, unknown>) {
    // check if the description of the todo has changed
    if (changedProperties.has("state.description")) {
      console.log("Todo description has changed!");
    }
  }

  render() {
    return html`
      <div>
        <input .value=${this.state.description}
          <-- Bind the input value to the 'description' property of our observable -->
          @input=${bond({ to: this.state, key: "description" })} />
        <input type="checkbox" ?checked=${this.state.done}
          <-- Bind the 'checked' attribute of the input to the 'done' property of our observable -->
          @click=${
      bond({ to: this.state, key: "done", attirubute: "checked" })
    } />
      </div>
    `;
  }
}

Observables

Observable makes monitoring changes on JavaScript objects as seamless as possible using the built-in Proxy and EventTarget interfaces. Observable wraps a given object into a proxy that emits change events whenever a change happens to the object or its "nested" objects and arrays. Since Observable is an extension of DOM's EventTarget, listening to changes is done through the standard event handling mechanisms.

import { Observable } from "compago";

class Todo {
  description = "...";
  done = false;
}

const todo = new Observable(new Todo());

todo.addEventListener("change", (event) => {
  console.log(event);
});

todo.done;
//=> false

todo.done = true;
//=> ChangeEvent {
//=>  type: 'change',
//=>  kind: 'SET'
//=>  path: '.done'
//=>  previous: false,
//=>  defaultPrevented: false,
//=>  cancelable: false,
//=>  timeStamp: ...
//=>}
//=> true

todo.done;
//=> true

JSON.stringify(todo);
//=> { "description": "...", "done": true }

Observable only monitors own, public, enumerable, non-symbolic properties, thus, any other sort of properties (i.e. private, symbolic, or non-enumerable) can be used to manipulate data without triggering change events, e.g. to store computed properties.

Using with Lit

Observables complement Lit element's reactive properties allowing separation of complex domain state (held in observables) and UI state (held in properties). To simplify working with observables, Compago offers ObserverElement mixin or observer and observe decorators that handle attaching and detaching from observables and triggering LitElement updates when observables detect changes.

import { observer, observe } from 'compago';

class Comment {
  id = 0;
  text = '';
  meta = {
    author: '';
    date: new Date();
  }
}

@observer()
@customElement('comment-element');
class CommentElement extends LitElement {
  // Set up `comment` property to hold a reference to an observable
  // holding a domain state. Any change to the comment will trigger
  // the elements update cycle.
  @observe() comment = new Observable(new Comment()); 
  @property({ type: Boolean }) isEditing = false;
  ...

  updated(changedProperties: PropertyValues<any>): void {
    if (changedProperties.has('comment.text')) {
      // react if the comment's text property has changed
    }
  }
}

While decorators are quite popular in the Lit world, you can achieve the same effect without them by using ObserverMixin and specifying list of observables in a static property:

const CommentElement = ObserverMixin(class extends LitElement {
  static observables = ['comment'];
  ...
  comment = new Observable(new Comment()); 
  ...
})

Data Binding

We often have to take values from DOM elements (e.g. input fields) and update our UI or domain states with them. To simplify the process, Compago offers the bond directive. The directive provides a declarative way to define an event listener that (upon being triggered) takes the specified value from its DOM element, optionally validates and parses it, and sets it on desired object, be it the element itself or an observable domain state object.

class CommentElement extends LitElement {
  // Set up `comment` property to hold a reference to an observable
  // holding a domain state. Any change to the comment will trigger
  // the elements update cycle.
  @observe()
  comment = new Observable(new Comment());
  ...
  render() {
    return html`
      <div>
        <input .value=${this.comment.text}
          <-- Upon input event take the input's value, validate it
          and set as the text of our comment -->
          @input=${
            bond({
              to: this.comment,
              key: "text",
              validate: (text) => text.length < 3000,
            })
          } />
      </div>
    `;
  }
}

Shared State

Observable are fully independent and can be shared between components. For example, a parent can share "parts" of an observable with children as shown in our example [todo app]:

@customElement("todo-app")
@observer()
export class TodoApp extends LitElement {
  // Here we create an observable that holds an array of todo objects.
  @observe()
  state = new Observable({ items: [] as Array<Todo> });
  ...
  render() {
    return html`
      <h1>Todos Compago & Lit</h1>
      <section>
          ${
      repeat(this.state.items, (todo) =>
        todo.id, (todo) =>
        // We supply each todo object to a child element.
        // Now when any of the todo objects is changed, it will trigger
        // re-render of the todo-item and the parent todo-app,
        // but not the sibling todo-items.
        html`<todo-item .todo=${todo}></todo-item>`)
    }
      </section>
  `;
  }
}

@observer()
@customElement("todo-item")
export class TodoItem extends LitElement {
  @observe()
  todo!: Todo;

  render() {
    return html`
      <input type="checkbox" .checked=${this.todo.done} @click=${
      bond({
        to: this.todo,
        key: "done",
        property: "checked",
      })
    }>
      <label>${this.todo.text}</label>
      <button @click=${this.onRemove}>x</button>
    `;
  }
}

Observables can be shared not only between parents and children, but also between siblings, or the same observable can be shared by different components, in fact, you can use a single observable to hold all state in your app (as a "global store").

State Persistance with Repositories

A repository is a design pattern of data-centric, domain-driven design that encapsulates the logic for accessing data sources while abstracting the underlying persistence technology. Web applications exchange data with a variety of data sources, such as servers exposed through a RESTful API, local storage using IndexedDB API, or databases through their respective drivers. Compago offers a unifying interface for repositories and implementations for RESTful APIs (RESTRepository) and local storage with IndexedDB (LocalRepository).

import { RESTRepository } from 'compago';

class Comment {
  id = 0;
  text = '';
  meta = {
    author: '';
    date: new Date();
  }
}

// Create a repository for comments that will access the API end point `/comments`
const remoteRepository = new RESTRepository(Comment, '/comments', 'id');

// retrieve the comment with id 1
// by sending a GET request to `/comments/1` API end-point
const readResult = await remoteRepository.read(1);

readResult.ok
//=> true if successful

const comment = readResult.value
//=> Comment {...}

License

MIT @ Maga D. Zandaqo