Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: connect fn #58

Merged
merged 1 commit into from
Sep 16, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 46 additions & 2 deletions docs/src/content/docs/utilities/connect.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,50 @@ title: connect
description: ngxtension/connect
---

WIP
`connect` is a utility function that connects a signal to an observable and returns a subscription. The subscription is automatically unsubscribed when the component is destroyed. If it's not called in an injection context, it must be called with an injector or DestroyRef.

Link to [connect](https://github.com/nartc/ngxtension-platform/blob/main/libs/ngxtension/connect/src/connect.ts) source code.
```ts
import { connect } from '@ngxtension/connect';
```

## Usage

It can be helpful when you want to have a writable signal, but you want to set its value based on an observable.

For example, you might want to have a signal that represents the current page number, but you want to set its value based on an observable that represents the current page number from a data service.

```ts
@Component()
export class AppComponent implements OnDestroy {
private dataService = inject(DataService);

pageNumber = signal(1);

constructor() {
connect(this.pageNumber, this.dataService.pageNumber$);
}
}
```

You can also use it not in an injection context, but you must provide an injector or DestroyRef.

```ts
@Component()
export class AppComponent implements OnDestroy {
private dataService = inject(DataService);

private injector = inject(Injector);
// or
private destroyRef = inject(DestroyRef);

pageNumber = signal(1);

ngOninit() {
connect(this.pageNumber, this.dataService.pageNumber$, this.injector);

// or

connect(this.pageNumber, this.dataService.pageNumber$, this.destroyRef);
}
}
```