Skip to content
On this page

API Reference

This page documents the runtime API for Inglorious Web and the underlying Inglorious Store.

createStore(config)

Creates a store instance to manage entities, event dispatch, and reactive rendering.

Parameters

  • types?: Record<string, object> — type definitions for entities.
  • entities?: Record<string, object> — initial entity state.
  • systems?: object[] — systems that handle events outside entity-specific handlers.
  • middlewares?: Function[] — middleware functions to intercept or transform events.
  • autoCreateEntities?: boolean — automatically create a default entity for each registered type.
  • updateMode?: "auto" | "manual" — whether events are processed immediately or manually.

Example

javascript
const store = createStore({
  types: {
    Counter: {
      create(entity) {
        entity.count = 0
      },
      increment(entity) {
        entity.count += 1
      },
      render(entity) {
        return html`<span>${entity.count}</span>`
      },
    },
  },
  entities: {
    counter: { type: "Counter" },
  },
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

Store methods

The object returned by createStore() exposes the core store API.

store.subscribe(listener)

Subscribe to state updates.

javascript
const unsubscribe = store.subscribe(() => {
  console.log("store updated")
})
1
2
3

Example

javascript
import { mount } from "@inglorious/web"

mount(store, (api) => api.render("app"), document.getElementById("root"), {
  onError(error) {
    console.error("render failed", error)
  },
})
1
2
3
4
5
6
7

What mount does

  • Creates an api object from the store
  • Adds api.render() for entity rendering
  • Hydrates existing markup if the container already has child nodes
  • Subscribes to store updates and re-renders automatically
  • Emits the init event once mounted

Rendering helpers

The docs use lit-html helpers like html, when, repeat, and unsafeHTML.

when(condition, content)

Render content conditionally.

javascript
html` ${when(entity.isOpen, () => html`<div>Open</div>`)} `
1

repeat(items, fn)

Render a list with keyed diffing.

javascript
html`
  <ul>
    ${repeat(
      items,
      (item) => item.id,
      (item) => html`<li>${item.label}</li>`,
    )}
  </ul>
`
1
2
3
4
5
6
7
8
9

unsafeHTML(htmlString)

Render raw HTML.

javascript
html`<div>${unsafeHTML(entity.content)}</div>`
1

Middleware

Middleware can intercept and modify events before they reach handlers.

javascript
function logger(store, event) {
  console.log("event", event)
  return event
}
1
2
3
4

Middleware receives:

  • store — the store object
  • event — the event object with type and optional payload

If a middleware returns null, the event is dropped.

javascript
const loggingMiddleware = (store, event) => {
  console.log("Event:", event.type, event.payload)
  return event // Always return event
}

const store = createStore({
  types,
  middlewares: [loggingMiddleware],
})
1
2
3
4
5
6
7
8
9

Environment Variables

import.meta.env.DEV

Is development mode.

javascript
if (import.meta.env.DEV) {
  console.log("Debug info")
}
1
2
3

import.meta.env.PROD

Is production mode.

javascript
if (import.meta.env.PROD) {
  // Enable analytics
}
1
2
3

TypeScript

Entity Type

typescript
interface Entity {
  type: string
  [key: string]: any
}
1
2
3
4

Template Result

typescript
type TemplateResult = import("lit-html").TemplateResult
1

Update Function

typescript
type UpdateFn<T extends Entity = Entity> = (draft: Draft<T>) => void
1

Need help? Report an issue or ask on discussions.

Released under the MIT License.