> For the complete documentation index, see [llms.txt](https://docs.activejs.dev/activejs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.activejs.dev/activejs/utilities/stream.md).

# Stream

Stream is a simple construct that creates a subscription by subscribing to the provided Observable and adds the ability to re-subscribe.

Given an Observable, the Stream immediately subscribes to it. The `Subscription` instance is saved as Stream's property. The Stream keeps the reference to the provided Observable and uses it for re-subscription when asked.

See [API reference](https://api.activejs.dev/classes/stream) for more details.

### Class Signature

```typescript
class Stream {
    constructor(observable: Observable<any>){
    }
}
```

### Explicit Usage Example

```typescript
import {timer} from 'rxjs';
import {tap} from 'rxjs/operators';
import {Stream} from '@activejs/core';

// create a cold Observable that emits at a 1-second interval.
// RxJS timer emits incremental numbers starting from 0
const randomNumberLogger$ = timer(0, 1000).pipe(
    tap(count => console.log(count))
);

// create a Stream using the randomNumberLogger$
const randomNumberStream = new Stream(randomNumberLogger$);

// each passing second we'll see a number logged in console
// 0, 1, 2, 3, and so on

// restart the timer
randomNumberStream.resubscribe();

// and the timer will start again
// again we'll see 0, 1, 2, 3, and so on

// you can also stop the stream similar to a simple Subscription
randomNumberStream.unsubscribe();
```

### Implicit Usage Example

Stream is implicitly used to create streams when you call `createStream` on an Action, Unit, System, or Cluster.

```typescript
// initialize an Action, that accepts a value of type 'A' | 'B' | 'C'
const anAction = new Action<'A' | 'B' | 'C'>();

// create stream
const alertStream = anAction.createStream(value$ => {
    return value$.pipe(
        tap(value => {
            alert(value); // or do something else entirely
        })
    );
})

// create another stream
const consoleStream = anAction.createStream(value$ => {
    return value$.pipe(
        tap(value => {
            console.log(value); // or do something else entirely
        })
    );
})

// our setup is complete, now we can dispatch/trigger it from anywhere
anAction.dispatch('A');

// stop the consoleStream
consoleStream.unsubscribe();

// and so on
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.activejs.dev/activejs/utilities/stream.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
