import { withRegion } from "effector";

The method is based on the idea of region-based memory management (see Region-based memory management for reference).

Methods

withRegion(unit, callback)

The method allows to explicitly transfer ownership of all units (including links created with sample, forward, etc…) defined in the callback to unit. As an implication, all the created links will be erased as soon as clearNode is called on Unit.

Formulae

withRegion(unit: Unit<T> | Step, callback: () => void): void

Arguments

  1. unit: Unit | Step — which will serve as “local area” or “region” owning all the units created within the provided callback.
  2. callback: () => void — The callback where all the relevant units should be defined.

Examples

import { createDomain, createEvent, restore, withRegion, clearNode } from "effector";

const first = createEvent();
const second = createEvent();
const $store = restore(first, "");
const domain = createDomain();

withRegion(domain, () => {
  // Following links created with `forward` or `sample` are owned by the provided unit `domain`
  // and will be disposed as soon as `clearNode` is called on `domain`.
  forward({
    from: second,
    to: first,
  });
});

$store.watch(console.log);

first("hello");
second("world");

clearNode(domain);

second("will not trigger updates of `$store`");
Contributors