);
}
```
That's it! [Click here to see the full example](https://stackblitz.com/edit/vitejs-vite-rert8c7a?file=src%2FApp.tsx).
Through these examples, you should have understood the basic usage of CCState. Next, you can read to learn about CCState's core APIs.
## Core APIs
CCState provides several simple concepts to help developers better manage application states. And it can be used as an external store to drive UI frameworks like React.
### State
`State` is the most basic value unit in CCState. A `State` can store any type of value, which can be accessed or modified through the store's `get`/`set` methods. Before explaining why it's designed this way, let's first look at the basic capabilities of `State`.
```typescript
import { store, state } from 'ccstate';
const store = createStore();
const userId$ = state(0);
store.get(userId$); // 0
store.set(userId$, 100);
store.get(userId$); // 100
const user$ = state<({
name: 'e7h4n',
avatar: 'https://avatars.githubusercontent.com/u/813596',
} | undefined>(undefined);
store.set({
name: 'yc-kanyun',
avatar: 'https://avatars.githubusercontent.com/u/168416598'
});
```
These examples should be very easy to understand. You might notice a detail in the examples: all variables returned by `state` have a `$` suffix. This is a naming convention used to distinguish an CCState signal type from other regular types. CCState signal types must be accessed through the store's get/set methods, and since it's common to convert an CCState signal type to a regular type using get, the `$` suffix helps avoid naming conflicts.
### Store
In CCState, declaring a `State` doesn't mean the value will be stored within the `State` itself. In fact, a `State` acts like a key in a Map, and CCState needs to create a Map to store the corresponding value for each `State` - this Map is the `Store`.
```typescript
const count$ = state(0); // count$: { init: 0 }
const store = createStore(); // imagine this as new Map()
store.set(count$, 10); // simply imagine as map[count$] = 10
const otherStore = createStore(); // another new Map()
otherStore.get(count$); // anotherMap[$count] ?? $count.init, returns 0
```
This should be easy to understand. If `Store` only needed to support `State` types, a simple Map would be sufficient. However, CCState needs to support another signal type. Next, let's introduce `Computed`, CCState's reactive computation unit.
### Computed
`Computed` is CCState's reactive computation unit. You can write derived computation logic in `Computed`, such as sending HTTP requests, data transformation, data aggregation, etc.
```typescript
import { computed, createStore } from 'ccstate';
const userId$ = state(0);
const user$ = computed(async (get) => {
const userId = get(userId$);
const resp = await fetch('/api/users/' + userId);
return resp.json();
});
const store = createStore();
const user = await store.get(user$);
```
Does this example seem less intuitive than `State`? Here's a mental model that might help you better understand what's happening:
- `computed(fn)` returns an object `{read: fn}`, which is assigned to `user$`
- When `store.get(user$)` encounters an object which has a read function, it calls that function: `user$.read(store.get)`
This way, `Computed` receives a get accessor that can access other signal in the store. This get accessor is similar to `store.get` and can be used to read both `State` and `Computed`. The reason CCState specifically passes a get method to `Computed`, rather than allowing direct access to the store within `Computed`, is to shield the logic within `Computed` from other store methods like `store.set`. The key characteristic of `Computed` is that it can only read states from the store but cannot modify them. In other words, `Computed` is side-effect free.
In most cases, side-effect free computation logic is extremely useful. They can be executed any number of times and have few requirements regarding execution timing. `Computed` is one of the most powerful features in CCState, and you should try to write your logic as `Computed` whenever possible, unless you need to perform set operations on the `Store`.
The computed callback also receives a second argument, `{ signal }`, which you can use when needed. This `AbortSignal` is created only when accessed and belongs to one evaluation: repeated reads return the same signal, and it is aborted when the computed actually reevaluates, even if the next evaluation does not read its signal. For an unmounted computed, this happens on its next read rather than immediately when a dependency changes. Reading the signal later from an obsolete evaluation returns an aborted signal and does not affect the current evaluation. Promises remain ordinary cached values; CCState does not inspect, wrap, or cancel them. An operation such as `fetch` can opt into cancellation by accepting the signal. Watch callbacks reuse this behavior and can also be cancelled using the signal passed to `store.watch`.
### Command
`Command` is CCState's logic unit for organizing side effects. It has both `set` and `get` accessors from the store, allowing it to not only read other signal types but also modify `State` or call other `Command`.
```typescript
import { command, createStore } from 'ccstate';
const user$ = state