百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
T

tansu

> 前端框架
开源

tansu 是一款轻量级、基于推动的、跨框架的状态管理库。它借鉴了 Svelte stor 最初设计和实现的思想和 API。

100 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

tansu 是一款轻量级、基于推动的、跨框架的状态管理库。它借鉴了 Svelte stor 最初设计和实现的思想和 API。

Tansu

Tansu is a lightweight, push-based framework-agnostic state management library. It borrows the ideas and APIs originally designed and implemented by Svelte stores and extends them with computed and batch.

Main characteristics:

  • small conceptual surface with expressive and very flexible API (functional and class-based);
  • can be used to create "local" (module-level or component-level), collaborating stores;
  • can handle both immutable and mutable data;
  • results in compact code with the absolute minimum of boilerplate.

Implementation wise, it is a tiny (1300 LOC) library without any external dependencies.

Installation

You can add Tansu to your project by installing the @amadeus-it-group/tansu package using your favorite package manager, ex.:

  • yarn add @amadeus-it-group/tansu
  • npm install @amadeus-it-group/tansu

Usage

Check out the Tansu API documentation.

The functional part of the API to manage your reactive state can be categorized into three distinct groups:

  • Base store: writable
  • Computed stores: derived, computed, readable
  • Utilities: batch, asReadable, asWritable

writable

api documentation

Writable: A Fundamental Building Block

A writable serves as the foundational element of a "store" – a container designed to encapsulate a value, enabling observation and modification of its state. You can change the internal value using the set or update methods.

To receive notifications whenever the value undergoes a change, the subscribe() method, paired with a callback function, can be employed.

Basic usage

typescript
import {writable} from "@amadeus-it-group/tansu";
const value$ = writable(0);

const unsubscribe = values$.subscribe((value) => {
  console.log(`value = ${value}`);
});

value$.set(1);
value$.update((value) => value + 1);

output:

  value = 0
  value = 1
  value = 2

Setup and teardown

The writable's second parameter allows for receiving notifications when at least one subscriber subscribes or when there are no more subscribers.

typescript
import {writable} from "@amadeus-it-group/tansu";

const value$ = writable(0, () => {
  console.log('At least one subscriber');

  return () => {
    console.log('No more subscriber');
  }
});

const unsubscribe = values$.subscribe((value) => {
  console.log(`value = ${value}`);
});

value$.set(1);
unsubscribe();

output:

  At least one subscriber
  value = 0
  value = 1
  No more subscriber

derived

api documentation

A derived store calculates its value based on one or more other stores provided as parameters. Since its value is derived from other stores, it is a read-only store and does not have any set or update methods.

Single store

typescript
import {writable, derived} from "@amadeus-it-group/tansu";

const value$ = writable(1);
const double$ = derived(value$, (value) => value * 2);

double$.subscribe((double) => console.log('Double value', double));
value$.set(2);

output:

Double value 2
Double value 4

Multiple stores

typescript
import {writable, derived} from "@amadeus-it-group/tansu";

const a$ = writable(1);
const b$ = writable(1);
const sum$ = derived([a$, b$], ([a, b]) => a + b);

sum$.subscribe((sum) => console.log('Sum', sum));
a$.set(2);

output:

Sum 2
Sum 3

Asynchronous set

A derived can directly manipulate its value using the set method instead of relying on the returned value of the provided function. This flexibility allows you to manage asynchronous operations or apply filtering logic before updating the observable's value.

…

output:

Double (asynchronous) -1
Even 0
Even 2
Double (asynchronous) 4

computed

api documentation

A computed store is another variant of a derived store, with the following characteristics:

  • Implicit Dependencies: Unlike in a derived store, there is no requirement to explicitly declare dependencies.

  • Dynamic Dependency Listening: Dependencies are determined based on their usage. This implies that a dependency not actively used is not automatically "listened" to, optimizing resource utilization.

Switch map

This capability to subscribe/unsubscribe to the dependency allows to create switch maps in a natural way.

typescript
import {writable, computed} from "@amadeus-it-group/tansu";

const switchToA$ = writable(true);
const a$ = writable(1);
const b$ = writable(0);

const computedValue$ = computed(() => {
  if (switchToA$()) {
    console.log('Return a$');
    return a$();
  } else {
    console.log('Return b$');
    return b$();
  }
});

computedValue$.subscribe((value) => console.log('Computed value:', value));
a$.set(2);
switchToA$.set(false);
a$.set(3);
a$.set(4);
switchToA$.set(true);

output:

Return a$
Computed value: 1
Return a$
Computed value: 2
Return b$
Computed value: 0
Return a$
Computed value: 4

When switchToA$.set(false) is called, the subscription to a$ is canceled, which means that subsequent changes to a$ will no longer trigger the calculation., which is only performed again when switchToA$ is set back to true.

readable

api documentation

Similar to Svelte stores, this function generates a store where the value cannot be externally modified.

typescript
import {readable} from '@amadeus-it-group/tansu';

const time = readable(new Date(), (set) => {
  const interval = setInterval(() => {
    set(new Date());
  }, 1000);

  return () => clearInterval(interval);
});

derived vs computed

While derived and computed may appear similar, they exhibit distinct characteristics that can significantly impact effectiveness based on use-cases:

  • Declaration of Dependencies:

    • computed: No explicit declaration of dependencies is required, providing more flexibility in code composition.
    • derived: Requires explicit declaration of dependencies.
  • Performance:

    • computed: Better performance by re-running the function only based on changes in the stores involved in the last run.
    • derived: Re-run the function each time a dependent store changes.
  • Asynchronous State:

    • computed: Unable to manage asynchronous state.
    • derived: Can handle asynchronous state with the set method.
  • Skipping Value Emission:

    • computed: Does not provide a mechanism to skip emitting values.
    • derived: Allows skipping the emission of values by choosing not to call the provided set method.
  • Setup and Teardown:

    • computed: Lacks explicit setup and teardown methods.
    • derived: Supports setup and teardown methods, allowing actions such as adding or removing DOM listeners.
    • When the last listener unsubscribes and then subscribes again, the derived function is rerun due to its setup-teardown functionality. In contrast, a computed provides the last value without recomputing if dependencies haven't changed in the meantime.

While computed feels more intuitive in many use-cases, derived excels in scenarios where computed falls short, particularly in managing asynchronous state and providing more granular control over value emissions.

Carefully choosing between them based on specific requirements enhances the effectiveness of state management in your application.

Getting the value

There are three ways for getting the value of a store:

typescript
import {writable, get} from "@amadeus-it-group/tansu";

const count$ = writable(1);
const unsubscribe = count$.subscribe((count) => {
  // Will be called with the updated value synchronously first, then each time count$ changes.
  // `unsubscribe` must be called to prevent future calls.
  console.log(count);
});

// A store is also a function that you can call to get the instant value.
console.log(count$());

// Equivalent to
console.log(get(count$));

[!NOTE] Getting the instant value implies the subscription and unsubription on the store:

  • It can be important to know in case of setup/teardown functions.
  • In the same scope, prefer to store the value once in a local variable instead of calling store$() several times.

When called inside a reactive context (i.e. inside a computed), getting the value serves to know and "listen" the dependent stores.

batch

api documentation

Contrary to other libraries like Angular with signals or Svelte with runes, where the callback of a subscription is executed asynchronously (usually referenced as an "effect"), we have maintained the constraint of synchronicity between the store changes and their subscriptions in Tansu.

While it is acceptable for these frameworks to defer these calls since their goals are well-known in advance (to optimize their final rendering), this is not the case for Tansu, where the goal is to be adaptable to any situation.

The problem with synchronous subscriptions is that it can create "glitches". Subscribers and computed store callbacks that are run too many times can create incorrect intermediate values.

See for example the asymmetric diamond dependency problem, which still exists in Svelte stores, while it has been fixed in Tansu.

There is also another use case.

Let's have a look at the following example:

typescript
import {writable, derived} from '@amadeus-it-group/tansu';

const firstName = writable('Arsène');
const lastName = writable('Lupin');
const fullName = derived([firstName, lastName], ([a, b]) => `${a} ${b}`);
fullName.subscribe((name) => console.log(name)); // logs any change to fullName
firstName.set('Sherlock');
lastName.set('Holmes');
console.log('Process end');

output:

Arsène Lupin
Sherlock Lupin
Sherlock Holmes
Process end

The fullName store successively went through different states, including an inconsistent one, as Sherlock Lupin does not exist! Even if it can be seen as just an intermediate state, it is fundamental for a state management to only manage consistent data in order to prevent issues and optimize the code.

In Tansu, the batch is available to defer synchronously the subscribers calls, and de facto the dependent derived or computed calculation to solve all kind of multiple dependencies issues.

The previous example is resolved this way:

typescript
import {writable, derived, computed, batch} from '@amadeus-it-group/tansu';

const firstName = writable('Arsène');
const lastName = writable('Lupin');
const fullName = derived([firstName, lastName], ([a, b]) => `${a} ${b}`);
// note that the fullName store could alternatively be create with computed:
// const fullName = computed(() => `${firstName()} ${lastName()}`);
fullName.subscribe((name) => console.log(name)); // logs any change to fullName
batch(() => {
    firstName.set('Sherlock');
    lastName.set('Holmes');
});
console.log('Process end');

output:

Arsène Lupin
Sherlock Holmes
Process end

[!NOTE]

  • Retrieving the immediate value of a store (using any of the three methods mentioned earlier: calling subscribe with a subscriber that will be called with the value synchronously, using get or calling the store as a function) always provides the value based on the up-to-date values of all dependent stores (even if this requires re-computations of a computed or a derived inside the batch)
  • all calls to subscribers (excluding the first synchronous call during the subscribe process) are deferred until the end of the batch
  • if a subscriber has already been notified of

Issues· 10 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

TypeScriptagnosticangularcomputedderived

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类前端框架
定价开源

> 相关工具

R
React
用于构建用户界面的 JavaScript 库
V
Vue.js
渐进式 JavaScript 框架
N
Next.js
基于 React 的全栈 Web 框架