Skip to content

Package @nrgyjs/core

Package Purpose

The @nrgyjs/core package provides the fundamental reactive, scope, and MVC/MVVM primitives used by the Nrgy.js ecosystem.

Overview

The package combines several layers:

  1. A reactive runtime based on atom(), compute(), and effect().
  2. Scopes for lifecycle and resource ownership.
  3. Utility helpers for atom composition and testing.
  4. Controller, view, and view-model abstractions for MVC/MVVM patterns.

Most other Nrgy.js packages build on top of these APIs.

Package Installation

bash
npm install @nrgyjs/core
bash
yarn add @nrgyjs/core
bash
pnpm add @nrgyjs/core

Conceptual Architecture

@nrgyjs/core is organized into several functional areas:

  1. common/*: reusable equality and type helpers.
  2. reactivity/*: atom types, runtime, schedulers, and effect execution.
  3. scope/*: lifecycle boundaries that collect destroyable resources.
  4. utils/*: helper functions built on top of atoms and effects.
  5. mvc/*: controller declarations, view bindings, and view-model helpers.

Feature Documentation

Usage Examples

ts
import { atom, compute, effect } from '@nrgyjs/core';

const count = atom(1);
const doubled = compute(() => count() * 2);

const subscription = effect(doubled, (value) => {
  console.log(value);
});

count.set(2);
subscription.destroy();
ts
import { declareController } from '@nrgyjs/core';

const CounterController = declareController(({ scope }) => {
  const value = scope.atom(0);

  return {
    value,
    increase: () => value.update((prev) => prev + 1),
  };
});

const controller = new CounterController();
controller.increase();
controller.destroy();