Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@dhmk/atom

Package Overview
Dependencies
Maintainers
0
Versions
25
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@dhmk/atom

Lightweight mobx-like observable values, computed values and side-effects

  • 2.0.1-test.12
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
22
increased by340%
Maintainers
0
Weekly downloads
 
Created
Source

@dhmk-atom

MobX-like observable atoms, computeds and observers.

Install: npm install @dhmk/atom

Example

import { observable, observe, act } from "@dhmk-atom";

const x = observable({
  id: 123,
  get computedProp() {
    return this.id.toString();
  },
});

const dispose = observe(() => console.log(x.computedProp));

act(() => (x.id = 456));

dispose();

API

atom(value: T, opts?: AtomOptions): WritableAtom<T>

Creates a value atom which is similar to MobX observable.box(value, { deep: false }).

type AtomOptions = {
  equals?: (next: T, prev: T) => boolean;
  onBecomeObserved?: () => void;
  onBecomeUnobserved?: () => void;
  set?: (setter: (x: T) => void) => (x: T) => void;
};

type WritableAtom<T> = {
  (): T;
  get(): T;
  set(x: T): void;
};

atom(fn: () => T, opts?: ComputedAtomOptions): Atom<T>

Creates a computed atom which is similar to MobX computed.

type ComputedAtomOptions = {
  equals?: (next: T, prev: T) => boolean;
  onBecomeObserved?: () => void;
  onBecomeUnobserved?: () => void;
};

type ComputedAtom<T> = {
  (): T;
  get(): T;
};

observable(x)

Creates observable object or array. See also observableObject, observableArray, as.

observe(fn: (self: Observer) => void, opts?: ObserverOptions): Observer

Similar to MobX autorun.

type ObserverOptions = {
  scheduler?: (run: Function) => void;
  onBecomeObserved?: () => void;
  onBecomeUnobserved?: () => void;
};

type Observer = {
  // dispose
  (): void;

  // schedule observer for execution even if no tracked atoms were changed
  invalidate(): void;

  readonly isInitial: boolean;
};

act(fn: () => T): T

MobX runInAction.

untracked(fn: () => T): T

MobX untracked.

as(x: { get, set? })

Instructs observableObject to initialize property as given object instead of default behavior.

observableObject(x)

observableObject({
  id: 123,
  get computedProp() {
    return this.id.toString();
  },
  withOptions: as(atom("abc", { onBecomeObserved() {} })),
});

observableArray(x)

ValueAtom

ComputedAtom

EffectAtom

Keywords

FAQs

Package last updated on 21 Nov 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc