CARMI

raw JSON →
1.0.342 verified Fri May 01 auth: no javascript

CARMI (Compiler for Automatic Reactive Modelling of Inference) is an experimental DSL+compiler for reactive state derivation. Current version 1.0.342. It generates JavaScript source code that automatically handles cache invalidation by analyzing which model paths are read/written at compile time, avoiding overhead of traditional FRP or immutable approaches. Key differentiators: zero-overhead conditional dependency tracking, hoisted shared sub-expressions, no tracking when no setters exist, fully incremental computation. Stable but low release cadence (last major update years ago). Ships TypeScript types.

error TypeError: model is not a function
cause compile() returns a string, not a function. You forgot to eval() it.
fix
Change const model = compile(definition); to const model = eval(compile(definition));
error Cannot read property 'get' of undefined
cause Attempting to chain .get() on a model accessor that returned undefined in eval context.
fix
Ensure your model definition uses root and paths correctly; verify that the compiled code runs inside eval.
error ReferenceError: root is not defined
cause Forgetting to import or destructure root from carmi in the compile scope.
fix
Add import { root } from 'carmi' at top of the file where compile() is called.
error Error: setter requires at least one path argument
cause setter() called without any arguments or with non-function arguments.
fix
Use setter(arg0, 'pathToSet') or setter(arg0, arg1) depending on your model depth.
gotcha compile() returns a string; you must eval() it to get the model function.
fix Use eval(compile(modelDefinition)) as shown in docs.
gotcha The DSL uses method chaining on root; forgetting .get() on properties returns a Path object, not a value.
fix Always use .get('key') to access model properties if you need the value.
deprecated The Babel macro approach (require('carmi/macro')) is no longer recommended; use compile() directly.
fix Replace import { compile } from 'carmi/macro' with import { compile } from 'carmi'.
gotcha Setters generated by setter() expect arguments in model index order; mismatch causes silent failures.
fix Ensure that the number and order of args match the paths used in setter(arg0, ...).
npm install carmi
yarn add carmi
pnpm add carmi

Defines a reactive model for a todo list: derived display text, any-not-done flag, and a setter action using compile/eval.

import { compile, root, arg0, setter, splice } from 'carmi';

const todosByIdx = root.keyBy('idx');
const anyTodoNotDone = todosByIdx.anyValues(todo => todo.get('done').not());
const todosDisplayByIdx = todosByIdx.mapValues(todo =>
  todo.get('task').plus(todo.get('done').ternary(' - done', ' - not done'))
);
const todosDisplay = root.map(todo => todosDisplayByIdx.get(todo.get('idx')));

const modelDefinition = {
  todosDisplay,
  anyTodoNotDone,
  setTodoDone: setter(arg0, 'done'),
  spliceTodos: splice(),
};

const todosModel = eval(compile(modelDefinition));
const initialData = [
  { idx: '1', done: false, task: 'write a blog post about carmi' },
  { idx: '2', done: true, task: 'publish to npm' },
  { idx: '3', done: false, task: 'write a demo for carmi' },
];
const todos = todosModel(initialData);
console.log(todos.todosDisplay);
// outputs array with tasks marked done/not done sorted by idx descending