All Projects → ngneat → from-event

ngneat / from-event

Licence: MIT license
🦊 ViewChild and FromEvent — a Match Made in Angular Heaven

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language
HTML
75241 projects
CSS
56736 projects

Projects that are alternatives of or similar to from-event

rx-postmessenger
Minimal RxJS adapter for the window.postMessage API for request-response streams and notification streams across frame windows.
Stars: ✭ 27 (-79.23%)
Mutual labels:  events, rxjs
rxact
Rxact is an observable application management for Javascript app
Stars: ✭ 21 (-83.85%)
Mutual labels:  rxjs
spellbook
Spellbook is a bookmark extension for Chrome and Firefox
Stars: ✭ 19 (-85.38%)
Mutual labels:  rxjs
pg-reactive
PostgreSQL + RxJS = pg-reactive
Stars: ✭ 38 (-70.77%)
Mutual labels:  rxjs
game-map-editor
game-map-editor
Stars: ✭ 17 (-86.92%)
Mutual labels:  events
hackertab.dev
Hackertab turns your New Tab page into a geeky one that keeps you as a developer updated with the best tools, news, jobs and events.
Stars: ✭ 229 (+76.15%)
Mutual labels:  events
observable-input
angular input properties as observable streams
Stars: ✭ 49 (-62.31%)
Mutual labels:  rxjs
cyberevents
The protocol for EVENTs and TICKETs
Stars: ✭ 16 (-87.69%)
Mutual labels:  events
niue
A tiny shared state and event library for React
Stars: ✭ 17 (-86.92%)
Mutual labels:  events
dry-events
Pub/sub system
Stars: ✭ 102 (-21.54%)
Mutual labels:  events
azure-service-bus-go
Golang library for Azure Service Bus -- https://aka.ms/azsb
Stars: ✭ 67 (-48.46%)
Mutual labels:  events
rx-stomp
STOMP adaptor for RxJS
Stars: ✭ 76 (-41.54%)
Mutual labels:  rxjs
eventcatalog
Discover, Explore and Document your Event Driven Architectures powered by Markdown.
Stars: ✭ 392 (+201.54%)
Mutual labels:  events
muon-java
Muon Core for the JVM. APIs and Microservices taken to the next level
Stars: ✭ 18 (-86.15%)
Mutual labels:  events
tutorial
Tutorials to help you build your first Swim app
Stars: ✭ 27 (-79.23%)
Mutual labels:  events
recks
🐶 React-like RxJS-based framework
Stars: ✭ 133 (+2.31%)
Mutual labels:  rxjs
space-state
Demo app for Subjecting State to Good Behavior
Stars: ✭ 46 (-64.62%)
Mutual labels:  rxjs
Angular-Reactive-Demo-Shop
Angular Demo Shop
Stars: ✭ 79 (-39.23%)
Mutual labels:  rxjs
reactive-graphql
A GraphQL implementation based around RxJS, very well suited for client side only GraphQL usage
Stars: ✭ 58 (-55.38%)
Mutual labels:  rxjs
tsee
Typed EventEmitter implemented with tsargs
Stars: ✭ 22 (-83.08%)
Mutual labels:  events

MIT commitizen PRs styled with prettier All Contributors ngneat

ViewChild and FromEvent — a Match Made in Angular Heaven

Turn ViewChild and ContentChild queries into an RxJS stream.

Installation

npm install @ngneat/from-event

Usage

Use the FromEvent decorator with ViewChild or ContentChild. Note that it expects to get ElementRef.

import { FromEvent } from '@ngneat/from-event';

@Component({
  selector: 'my-btn',
  template: `
    <button #button>
      <ng-content></ng-content>
    </button>
  `
})
class ButtonComponent implements AfterViewInit {
  @FromEvent('click')
  @ViewChild('button') 
  click$: Observable<MouseEvent>;

  ngAfterViewInit() {
    this.click$.subscribe(console.log);
  }
}

You are not limited to use it only inside AfterViewInit or AfterContentInit:

@Component({
  template: `
    <button #plus>+1</button>
    <button #minus>-1</button>
    <button #reset>Reset</button>
    {{ counter$ | async }}
  `,
})
export class CounterComponent {
  @FromEvent('click')
  @ViewChild('plus')
  plus$: Observable<MouseEvent>;

  @FromEvent('click')
  @ViewChild('minus')
  minus$: Observable<MouseEvent>;

  @FromEvent('click')
  @ViewChild('reset')
  reset$: Observable<MouseEvent>;

  count$ = merge(
    this.plus$.pipe(mapTo(1)), 
    this.minus$.pipe(mapTo(-1))
  ).pipe(
    startWith(0),
    scan((acc, curr) => acc + curr)
  );

  counter$ = this.reset$.pipe(
    startWith(null),
    switchMap(() => this.count$)
  );
}

A common example is using it with switchMap():

import { FromEvent } from '@ngneat/from-event';

@Component({
  selector: 'my-comp',
  template: `
    <button #trigger>Trigger</button>
  `
})
class MyComponent implements AfterViewInit {
  @FromEvent('click')
  @ViewChild('trigger') 
  trigger$: Observable<MouseEvent>;

  ngAfterViewInit() {
    this.trigger$.pipe(switchMap(() => service.doSomething())).subscribe(result => {
      // Do something with the result
    });
  }
}

Use the FromEvents decorator with ViewChildren or ContentChildren. Note that it expects to get ElementRef.

import { FromEvents } from '@ngneat/from-event';

@Component({
  template: `
    <my-btn id="1">Click One</my-btn>
    <my-btn id="2">Click Two</my-btn>
    <my-btn id="3">Click Three</my-btn>
  `
})
class HostComponent implements AfterViewInit, OnDestroy {
  @FromEvents('click')
  @ViewChildren(ButtonComponent, { read: ElementRef }) 
  clicks$: Observable<MouseEvent>;

  ngAfterViewInit() {
    this.clicks$.subscribe(e => console.log(e.target));
  }
}

Have fun, and don't forget to unsubscribe. If you work with Ivy, you can do it with until-destroy.

Contributors

Thanks goes to these wonderful people (emoji key):


Netanel Basal

💻

Toni Villena

💻 💡

This project follows the all-contributors specification. Contributions of any kind welcome!

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].