Skip to main content
Version: 2.x

Reactive communication

micro-lc holds a state object which can be updated key-be-key at first level only. This means that clients of this API are responsible for merging anything that goes beyond the first level.

caution

micro-lc uses rxjs@^7 to dispatch reactive changes. In case of interoperability issues with other reactive libraries like Bacon.js, Callbags, Kefir, xstream, or rxjs lower versions like ^6, we recommend to polyfill Symbol.observable as reported here.

A polyfill, provided by micro-lc, is available by including in your index.html the following script tag:

<script src="https://cdn.jsdelivr.net/npm/@micro-lc/orchestrator/dist/polyfills/symbol-observable.js"></script>

State object can be updated with set method and consumed with subscribe method.

// Application 1 ⤵
microlcApi.set({ userName: 'John Doe' })

// Application 2 ⤵
microlcApi.subscribe((state) => console.log(JSON.stringify(state)))
// output: {"userName":"John Doe"}

// Application 1 ⤵
microlcApi.set({ grants: ['read', 'write'] })

// Application 3 ⤵
microlcApi.subscribe((state) => console.log(JSON.stringify(state)))
// output: {"userName":"John Doe","grants":["read","write"]}
tip

If you need to extend state with specific state management tools, like Redux, you can either add a specific key using this API, or register your tool as an extension.

set

export interface MicrolcApi<E extends Record<string, unknown>> {
readonly set: (event: Partial<E>) => void
// ...rest of the API
}

subscribe

import type { Subscription } from 'rxjs'

export interface MicrolcApi<E extends Record<string, unknown>> {
readonly subscribe: (next: (value: Partial<E>) => void) => Subscription
// ...rest of the API
}

next

caution

This API will completely override the state. Use it with caution.

import type { Subscription } from 'rxjs'

export interface MicrolcApi<E extends Record<string, unknown>> {
readonly next: (value: E) => void
// ...rest of the API
}