# atom.io/foundations/subject

Source: docs/source/pages/docs/foundations/subject.mdx
URL: /docs/foundations/subject

# <low-emphasis>atom.io</low-emphasis>/foundations/subject

Subjects are small keyed observable primitives. `Subject` holds subscribers and emits
values explicitly; `StatefulSubject` also stores the latest value on the instance.

### stateful subject
Source: docs/source/exhibits/foundations/subject/stateful-subject.ts

```ts
import { StatefulSubject } from "atom.io/foundations/subject"

const count = new StatefulSubject(0)

const unsubscribe = count.subscribe(`logger`, (value) => {
	console.log(value)
})

count.next(1)
count.state // 1

unsubscribe()
```

## package contents

<table-wrapper>

| Export | Description |
| --- | --- |
| `Subject` | A keyed observable subject. |
| `Subject.prototype.subscribe(key, subscriber)` | Register a subscriber under a string key and return an unsubscribe callback. |
| `Subject.prototype.next(value)` | Call all current subscribers with a value. |
| `StatefulSubject` | A subject that stores its latest state. |
| `StatefulSubject.prototype.state` | The latest value sent with `next`. |

</table-wrapper>

When you call `next(value)`, `state` updates before subscribers are notified.
