MCPcopy
hub / github.com/LegendApp/legend-state

github.com/LegendApp/legend-state @v2.1.15 sqlite

repository ↗ · DeepWiki ↗ · release v2.1.15 ↗
467 symbols 1,425 edges 107 files 0 documented · 0%
README

Legend-State

Legend-State is a super fast and powerful state library for JavaScript apps with four primary goals:

1. 🦄 As easy as possible to use

There is no boilerplate and there are no contexts, actions, reducers, dispatchers, sagas, thunks, or epics. It doesn't modify your data at all, and you can just call get() to get the raw data and set() to change it.

In React components you can call use() on any observable to get the raw data and automatically re-render whenever it changes.

// Create an observable object
const state$ = observable({ settings: { theme: 'dark' } })

// Just get and set
const theme = state$.settings.theme.get();
state$.settings.theme.set('light')

// observe re-runs when accessed observables change
observe(() => {
    console.log(state$.settings.theme.get())
})

// Automatically re-render components when observables change
enableReactTracking({ auto: true })

const Component = function Component() {
    const theme = state$.settings.theme.get()

    return 

Theme: {theme}


}

2. ⚡️ The fastest React state library

Legend-State beats every other state library on just about every metric and is so optimized for arrays that it even beats vanilla JS on the "swap" and "replace all rows" benchmarks. At only 4kb and with the massive reduction in boilerplate code, you'll have big savings in file size too.

<img src="https://www.legendapp.com/img/dev/state/times.png" />

See the documentation for more details.

3. 🔥 Fine-grained reactivity for minimal renders

Legend-State lets you make your renders super fine-grained, so your apps will be much faster because React has to do less work. The best way to be fast is to render less, less often.

function FineGrained() {
    const count$ = useObservable(0)

    useInterval(() => {
        count$.set(v => v + 1)
    }, 600)

    // The text updates itself so the component doesn't re-render
    return (



            Count: <Memo>{count$}</Memo>



    )
}

4. 💾 Powerful persistence

Legend-State includes a powerful persistence plugin system for local caching and remote sync. It easily enables offline-first apps by tracking changes made while offline that save when coming online, managing conflict resolution, and syncing only small diffs. We use Legend-State as the sync systems in Legend and Bravely, so it is by necessity very full featured while being simple to set up.

Local persistence plugins for the browser and React Native are included, and a remote sync plugin for Firebase will be ready soon.

import { ObservablePersistLocalStorage } from '@legendapp/state/persist-plugins/local-storage'
import { persistObservable } from '@legendapp/state/persist'

const state$ = observable({ store: { bigObject: { ... } } })

// Persist this observable
persistObservable(state$, {
    pluginLocal: ObservablePersistLocalStorage,
    local: 'store' // Unique name
})

Install

npm install @legendapp/state or yarn add @legendapp/state

Example

import { observable, observe } from "@legendapp/state"
import { persistObservable } from "@legendapp/state/persist"

// Create an observable object
const state$ = observable({ settings: { theme: 'dark' } })

// get() returns the raw data
state$.settings.theme.get() === 'dark'

// observe re-runs when any observables change
observe(() => {
    console.log(state$.settings.theme.get())
})

// Assign to state$ with set
state$.settings.theme.set('light')

// Automatically persist state$. Refresh this page to try it.
persistObservable(state$, { local: 'exampleState' })

// Components re-render only when accessed observables change
// This is the code for the example on your right ----->
function Component() {
    const theme = state$.settings.theme.use()
    // state$.settings.theme is automatically tracked for changes

    const toggle = () => {
        state$.settings.theme.set(theme =>
            theme === 'dark' ? 'light' : 'dark'
        )
    }

    return (





Theme: {theme}


            <Button onClick={toggle}>
                Toggle theme
            </Button>



    )
}

Highlights

  • ✨ Super easy to use 😌
  • ✨ Super fast ⚡️
  • ✨ Super small at 4kb 🐥
  • ✨ Fine-grained reactivity 🔥
  • ✨ No boilerplate
  • ✨ Designed for maximum performance and scalability
  • ✨ React components re-render only on changes
  • ✨ Very strongly typed with TypeScript
  • ✨ Persistence plugins for automatically saving/loading from storage
  • ✨ State can be global or within components

Read more about why Legend-State might be right for you.

Documentation

See the documentation site.

Community

Join us on Slack to get involved with the Legend community.

👩‍⚖️ License

MIT


Legend-State is created and maintained by Jay Meistrich with Legend and Bravely.

<a href="https://www.legendapp.com"><img src="https://www.legendapp.com/img/LogoTextOnWhite.png" height="56" alt="Legend" /></a>
<span>&nbsp;&nbsp;&nbsp;&nbsp;</span>
<a href="https://www.bravely.io"><img src="https://www.legendapp.com/img/bravely-logo.png" height="56" alt="Bravely" /></a>

Extension points exported contracts — how you extend this code

ObservablePersistLocal (Interface)
(no doc) [6 implementers]
src/observableInterfaces.ts
ObserveOptions (Interface)
(no doc)
src/internalTypes.d.ts
TrackingState (Interface)
(no doc)
src/tracking.ts
ObserveOptions (Interface)
(no doc)
src/observe.ts
BatchItem (Interface)
(no doc)
src/batching.ts
ObservablePrimitiveState (Interface)
(no doc)
src/ObservablePrimitive.ts
BindKey (Interface)
(no doc)
src/react/reactInterfaces.ts
Options (Interface)
(no doc)
src/helpers/pageHash.ts

Core symbols most depended-on inside this repo

get
called by 584
src/observableInterfaces.ts
set
called by 419
src/observableInterfaces.ts
observable
called by 393
src/observable.ts
onChange
called by 53
src/observableInterfaces.ts
assign
called by 46
src/observableInterfaces.ts
get
called by 44
src/config/enableReactTracking.ts
peek
called by 42
src/observableInterfaces.ts
when
called by 40
src/when.ts

Shape

Function 265
Method 111
Interface 69
Class 22

Languages

TypeScript100%

Modules by API surface

src/observableInterfaces.ts60 symbols
src/persist-plugins/firebase.ts34 symbols
src/helpers.ts22 symbols
src/ObservableObject.ts21 symbols
src/persist/persistObservable.ts18 symbols
src/persist-plugins/indexeddb.ts18 symbols
src/persist-plugins/local-storage.ts17 symbols
src/batching.ts14 symbols
src/persist-plugins/async-storage.ts12 symbols
src/is.ts12 symbols
src/persist-plugins/mmkv.ts11 symbols
tests/persist-sessionstorage.test.ts10 symbols

Dependencies from manifests, versioned

@babel/types7.20.2 · 1×
@commitlint/config-conventional17.4.4 · 1×
@evilmartians/lefthook1.3.3 · 1×
@jest/globals29.6.1 · 1×
@react-native-async-storage/async-storage1.19.3 · 1×
@release-it/conventional-changelog5.1.1 · 1×
@rollup/plugin-commonjs23.0.2 · 1×
@rollup/plugin-node-resolve15.0.1 · 1×
@rollup/plugin-typescript9.0.2 · 1×
@swc/cli0.1.57 · 1×
@swc/core1.3.14 · 1×
@tanstack/react-query4.14.6 · 1×

For agents

$ claude mcp add legend-state \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact