Registry / llm-agents / mobx-utils

mobx-utils

JSON →
library6.1.1jsnpmunverified

MobX-utils is a companion library for MobX, offering a collection of common patterns and utility functions to simplify reactive state management in applications. It provides solutions for handling asynchronous operations, view models, observable resources, and more, building directly on top of the core MobX library. The current stable version is 6.1.1 and it actively tracks the major versions of MobX, currently requiring `mobx@^6.0.0` as a peer dependency. Key differentiators include its `fromPromise` utility for observable promise states, `createViewModel` for easily creating editable views of data, and `lazyObservable` for demand-driven data fetching. Its release cadence is tied to MobX's evolution, with updates typically addressing compatibility or introducing new patterns.

npm install mobx-utils
INSTALL
IMPORT
SIG · MOBX-UTILS
M
mobx-utils
llm-agentsjavascriptv6.1.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

fromPromise
import { fromPromise } from 'mobx-utils'
const fromPromise = require('mobx-utils').fromPromise
MobX-utils v6+ is primarily ESM. Use named imports.
createViewModel
import { createViewModel } from 'mobx-utils'
import mobxUtils from 'mobx-utils'; const { createViewModel } = mobxUtils;
All utilities are named exports; there is no default export.
lazyObservable
import { lazyObservable } from 'mobx-utils'
import * as mobxUtils from 'mobx-utils'; mobxUtils.lazyObservable;
Destructuring named imports is the idiomatic way to access utilities.

Demonstrates `fromPromise` to create an observable wrapper around an asynchronous operation, tracking its pending, fulfilled, and rejected states, and consuming the result reactively.

import { makeObservable, observable, action } from 'mobx'; import { fromPromise, PromiseState } from 'mobx-utils'; type User = { id: number; name: string }; class UserStore { userPromise: PromiseState<User | null> = fromPromise(Promise.resolve(null)); constructor() { makeObservable(this, { userPromise: observable.ref, fetchUser: action }); } async fetchUser(id: number) { this.userPromise = fromPromise( new Promise<User>((resolve) => { console.log(`Fetching user ${id}...`); setTimeout(() => { const user = { id, name: `User ${id}` }; console.log(`User ${id} fetched.`); resolve(user); }, 1000); }) ); } } const store = new UserStore(); store.fetchUser(1); // Observe the promise state store.userPromise.case({ pending: () => console.log("Loading user..."), fulfilled: (user) => console.log(`User loaded: ${user?.name}`), rejected: (error) => console.error(`Failed to load user: ${error}`) }); setTimeout(() => { // Re-fetch to see it in action again store.fetchUser(2); store.userPromise.case({ pending: () => console.log("Loading user 2..."), fulfilled: (user) => console.log(`User 2 loaded: ${user?.name}`), rejected: (error) => console.error(`Failed to load user 2: ${error}`) }); }, 2000);
Debug
Known issues
breakingMobX-utils v6+ has a peer dependency on MobX v6+. Using older MobX versions (v4 or v5) will lead to runtime errors or unexpected behavior due to significant API changes in MobX itself (e.g., introduction of `makeObservable` and changes to decorator usage).
fix
Ensure your project is using `mobx@^6.0.0` or later. Update your MobX setup to use `makeObservable` or `makeAutoObservable` instead of legacy decorators.
affects: >=6.0.0
gotchaSince MobX v6, the `mobx-utils` package is primarily distributed as ES Modules. Attempting to `require()` it directly in a CommonJS environment without proper transpilation or module resolution configuration can lead to import errors.
fix
Configure your build system (Webpack, Rollup, Babel) to correctly handle ES Modules. For Node.js projects, ensure `"type": "module"` is set in `package.json` or use `.mjs` file extensions, and utilize `import` statements.
affects: >=6.0.0
gotchaThe `fromPromise` utility's `value` property will hold the initial value, the resolved value, or the rejected error. To reliably differentiate between these states (especially if the initial value could be the same as a resolved value, or to check for rejections), always consult the `state` property (`pending`, `fulfilled`, `rejected`).
fix
Always use the `.state` property (e.g., `myPromise.state === 'rejected'`) or the `.case()` method for robust handling of different promise outcomes, rather than relying solely on the `.value`.
affects: >=3.0.0
gotcha`createViewModel` creates a shallow observable copy of an object. If the source observable object changes its *reference* (e.g., `store.data = newData`) rather than its *properties* (e.g., `store.data.name = 'new name'`), the ViewModel will not automatically update to the new source object.
fix
Ensure the object passed to `createViewModel` is itself observable, and that changes to the data are made by modifying its observable properties directly, rather than reassigning the entire object reference.
affects: >=3.0.0
gotchaThe `now()` utility's reactivity depends on a global timer with a fixed interval. While convenient for simple time-based UI updates, relying on it for high-precision or complex animations can lead to less efficient rendering or unexpected timing discrepancies.
fix
For precise timing or performance-critical animations, consider implementing a custom observable timer or leveraging browser-native APIs like `requestAnimationFrame` with MobX's `reaction` or `autorun` for more granular control.
affects: >=3.0.0
Errors
Common errors & fixes
Uncaught TypeError: Cannot read properties of undefined (reading 'state') at ...
Accessing properties of a `fromPromise` result (e.g., `.state` or `.value`) before the promise has been properly initialized or if `fromPromise` was called with an invalid input leading to an `undefined` or `null` return.
fix
Ensure `fromPromise` is always initialized with a valid `Promise` instance or a placeholder `Promise.resolve(null)`. Check for `null` or `undefined` on the `fromPromise` instance before attempting to access its properties if it's conditionally rendered or initialized.
TypeError: mobx_utils__WEBPACK_IMPORTED_MODULE_1__.fromPromise is not a function (or similar Webpack/Rollup message)
This error typically indicates that `mobx-utils` is being imported incorrectly, most commonly when a CommonJS `require()` pattern is used for an ESM-first package, or due to misconfigured module resolution in a build tool.
fix
For MobX-utils v6+, ensure you are using ES Module `import { fromPromise } from 'mobx-utils'` syntax. Verify that your `package.json` has `"type": "module"` or that your build pipeline correctly transpiles and resolves ESM imports.
Error: [mobx-utils] ViewModel: expected observable object, got [object Object]
`createViewModel` was called with a plain JavaScript object or a non-observable value. `createViewModel` expects an object that has been made observable by MobX (e.g., with `makeObservable`, `observable`, or `@observable`).
fix
Ensure the object passed to `createViewModel` is a MobX observable. For class instances, call `makeObservable(this)` in the constructor. For plain objects, wrap them with `observable({})` before passing to `createViewModel`.
Upgrade
Version history
6.1.1latest on npm
Audit
Dependencies
mobxrequiredRuntime peer dependency required for all utilities to function correctly.
Agent activity
17 hits · last 30 days
node
16
OpenAI (training)
1
Resources
mobx-utils — npm install mobx-utils · libregistry