Socket
Socket
Sign inDemoInstall

connect-state-playground

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

connect-state-playground

Helper function for angular to allow creation of reactive components


Version published
Weekly downloads
18
increased by28.57%
Maintainers
1
Weekly downloads
 
Created
Source

Connect state - Reactive components in Angular

Automatically bind all asynchronous observables into a simple synchronous sink. Extra features including loading indicators and simple API to reload your observables.

ng-connect-state

NOTE: Ivy required

Installation

npm install ng-connect-state @ngneat/until-destroy
# Or if you use yarn
yarn add ng-connect-state @ngneat/until-destroy

@ngneat/until-destroy is a peer dependency.

Examples

import { ConnectState, connectState } from 'ng-connect-state';
import { interval } from 'rxjs';

@ConnectState()
@Component({ template: `
  {{ state.timer }}
  <button (click)="state.reload()"</button>
  Loading: {{ state.loading.timer }}
  `
})
export class InboxComponent {
  ngOnDestroy() { }

  /**
   this exposes state.timer as a synchronous value!
  **/
  state = connectState(this, {
    timer: interval(1000),
  })
}

You can bind multiple observables and reload them individually as well:

@ConnectState()
@Component({})
export class HomeComponent {
  ngOnDestroy() { }

  constructor(private httpClient: HttpClient) {}

  // We'll dispose it on destroy
  state = connectState(this, {
    users: httpClient.get('/users'),
    projects: httpClient.get('/projects'),
  })

  reloadUsers() {
    this.state.reload('users');
  }
  reloadProjects() {
    this.state.reload('projects');
  }
}

Potential Pitfalls

  • The order of decorators is important, make sure to put @ConnectState() before the @Component() decorator.
  • When using overrideComponent in unit tests remember that it overrides metadata and component definition. Invoke ConnectState()(YourComponent); to reapply the decorator.

Contributors ✨

This project's structuce is based on the amazing @ngneat/until-destroy repository! Many thanks.

License

MIT

Keywords

FAQs

Package last updated on 21 Jul 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc