All Projects → ngbox → ng-observe

ngbox / ng-observe

Licence: MIT license
Angular reactivity streamlined...

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to ng-observe

Redux Most
Most.js based middleware for Redux. Handle async actions with monadic streams & reactive programming.
Stars: ✭ 137 (+110.77%)
Mutual labels:  rxjs, observable, reactive-programming, streams
monogram
Aspect-oriented layer on top of the MongoDB Node.js driver
Stars: ✭ 76 (+16.92%)
Mutual labels:  rxjs, observable, streams
observe-component
A library for accessing React component events as reactive observables
Stars: ✭ 36 (-44.62%)
Mutual labels:  rxjs, observable, reactive-programming
angular2-instagram
🔥Instagram like photo filter playground built with Angular2 (Web | Desktop)
Stars: ✭ 91 (+40%)
Mutual labels:  rxjs, angular2, reactive-programming
rxjs-proxify
Turns a Stream of Objects into an Object of Streams
Stars: ✭ 34 (-47.69%)
Mutual labels:  rxjs, observable, reactive-programming
react-mobx-router5
React components for routing solution using router5 and mobx
Stars: ✭ 58 (-10.77%)
Mutual labels:  observer, observable, reactive-programming
Lightweightobservable
📬 A lightweight implementation of an observable sequence that you can subscribe to.
Stars: ✭ 114 (+75.38%)
Mutual labels:  observer, observable, reactive-programming
React Reactive Form
Angular like reactive forms in React.
Stars: ✭ 259 (+298.46%)
Mutual labels:  subscription, observer, observable
Ngx Restangular
Restangular for Angular 2 and higher versions
Stars: ✭ 787 (+1110.77%)
Mutual labels:  rxjs, angular2, observable
Recycle
Convert functional/reactive object description using RxJS into React component
Stars: ✭ 374 (+475.38%)
Mutual labels:  rxjs, observable, reactive-programming
observable-playground
Know your Observables before deploying to production
Stars: ✭ 96 (+47.69%)
Mutual labels:  rxjs, observable, reactive-programming
Evolui
A tiny reactive user interface library, built on top of RxJs.
Stars: ✭ 43 (-33.85%)
Mutual labels:  rxjs, observable, reactive-programming
Od Virtualscroll
🚀 Observable-based virtual scroll implementation in Angular
Stars: ✭ 133 (+104.62%)
Mutual labels:  rxjs, observable, reactive-programming
mutation-observer
A library for idiomatic use of MutationObserver with Angular
Stars: ✭ 32 (-50.77%)
Mutual labels:  observer, observable
EventEmitter
Simple EventEmitter with multiple listeners
Stars: ✭ 19 (-70.77%)
Mutual labels:  observer, observable
Observable
The easiest way to observe values in Swift.
Stars: ✭ 346 (+432.31%)
Mutual labels:  observer, observable
SwiftObserver
Elegant Reactive Primitives for Clean Swift Architecture #NoRx
Stars: ✭ 14 (-78.46%)
Mutual labels:  observer, reactive-programming
state inspector
State change & method call logger. A debugging tool for instance variables and method calls.
Stars: ✭ 24 (-63.08%)
Mutual labels:  observer, observable
Dob
Light and fast 🚀 state management tool using proxy.
Stars: ✭ 713 (+996.92%)
Mutual labels:  observer, observable
Oba
Observe any object's any change
Stars: ✭ 101 (+55.38%)
Mutual labels:  observer, observable


 
ng-observe logo

ng-observe
 

Angular reactivity streamlined...

Why?

  • Unlike AsyncPipe, you can use it in component classes and even in directives.
  • Feels more reactive than unsubscribing on destroy (be it handled by a decorator, triggered by a subject, or done by a direct call in the lifecycle hook).
  • Reduces the complexity of working with streams.
  • Works in zoneless apps. (v1.1.0+)

How it works

  • Extracts emitted value from observables.
  • Marks the component for change detection.
  • Leaves no subscription behind.
  • Clears old subscriptions and creates new ones at each execution if used in getters, setters or methods.

How to use

Install the package, and you are good to go. No module import is necessary.

npm install ng-observe

...or...

yarn add ng-observe

Example

We can subscribe to a stream with the AsyncPipe in component templates, but we can't use it in component or directive classes.

@Component({
  template: '{{ fooBar$ | async }}',
})
class DemoComponent {
  foo$ = of('foo');

  get fooBar$() {
    return foo$.pipe(map(val => val + 'bar'));
  }
}

With ng-observe, we don't need to pipe the stream.

import { OBSERVE, OBSERVE_PROVIDER, ObserveFn } from 'ng-observe';

@Component({
  template: '{{ fooBar }}',
  providers: [OBSERVE_PROVIDER],
})
class DemoComponent {
  foo = this.observe(of('foo'));

  get fooBar() {
    return this.foo.value + 'bar';
  }

  constructor(@Inject(OBSERVE) private observe: ObserveFn) {}
}

You can see other examples at links below:

Important Note: Do not destructure a collection created by the ObserveFn. Otherwise, the reactivity will be lost. Use toValue or toValues to convert elements of the collection to instances of Observed instead.

You can read this Medium article to learn about what the motivation behind ng-observe is.

API

OBSERVE_PROVIDER

To use ng-observe in your components and directives, add OBSERVE_PROVIDER to providers array in metadata.

ObserveFn

This function is used to extract a single stream's value. You can inject it via the OBSERVE injection token.

import { OBSERVE, OBSERVE_PROVIDER, ObserveFn } from 'ng-observe';

@Component({
  template: '{{ foo.value }}',
  providers: [OBSERVE_PROVIDER],
})
class Component {
  foo = this.observe(of('foo'));

  constructor(@Inject(OBSERVE) private observe: ObserveFn) {}
}

You can extract multiple streams' value too.

import { OBSERVE, OBSERVE_PROVIDER, ObserveFn } from 'ng-observe';

@Component({
  template: '{{ state.foo }} {{ state.bar }}',
  providers: [OBSERVE_PROVIDER],
})
class Component {
  state = this.observe({ foo: of('foo'), bar: of('bar') });

  constructor(@Inject(OBSERVE) private observe: ObserveFn) {}
}

It works with arrays as well.

import { OBSERVE, OBSERVE_PROVIDER, ObserveFn } from 'ng-observe';

@Component({
  template: '{{ state[0] }} {{ state[1] }}',
  providers: [OBSERVE_PROVIDER],
})
class Component {
  state = this.observe([of('foo'), of('bar')]);

  constructor(@Inject(OBSERVE) private observe: ObserveFn) {}
}

ObserveService

You can call ObserveService's value and collection methods explicitly instead of ObserveFn. This offers a very slight (ignorable in most cases) performance improvement.

import { ObserveService } from 'ng-observe';

@Component({
  template: '{{ foo.value }} {{ state[0] }} {{ state[1] }}',
  providers: [ObserveService],
})
class Component {
  foo = this.observe.value(of('foo'));

  state = this.observe.collection([of('foo'), of('bar')]);

  constructor(private observe: ObserveService) {}
}

Observed

ObserveFn infers types for you, but if you want to assign an observed value later, you can use Observed class for type annotation.

import { OBSERVE, OBSERVE_PROVIDER, Observed } from 'ng-observe';

@Component({
  template: '{{ foo.value }}',
  providers: [OBSERVE_PROVIDER],
})
class Component {
  foo: Observed<string>;

  constructor(@Inject(OBSERVE) private observe: ObserveFn) {
    this.foo = this.observe(of('foo'));
  }
}

toValue

toValue converts an element in the collection to a reactive observed value. Returns an instance of the Observed class.

import { OBSERVE, OBSERVE_PROVIDER, Observed, ObserveFn, toValue } from 'ng-observe';

@Component({
  template: '{{ foo.value }} {{ bar.value }}',
  providers: [OBSERVE_PROVIDER],
})
class Component {
  foo: Observed<string>;

  bar: Observed<string>;

  constructor(@Inject(OBSERVE) private observe: ObserveFn) {
    const state = this.observe({ foo: of('foo'), bar: of('bar') });
    this.foo = toValue(state, 'foo');
    this.bar = toValue(state, 'bar');
  }
}

toMappedValue

You can use toMappedValue to get a reactive observed value mapped from the collection. Returns an instance of the Observed class.

import { OBSERVE, OBSERVE_PROVIDER, Observed, ObserveFn, toMappedValue } from 'ng-observe';

@Component({
  template: '{{ fooBar.value }}',
  providers: [OBSERVE_PROVIDER],
})
class Component {
  fooBar: Observed<string>;

  constructor(@Inject(OBSERVE) private observe: ObserveFn) {
    const state = this.observe({ foo: of('foo'), bar: of('bar') });
    this.fooBar = toMappedValue(state, ({ foo, bar }) => `${foo} ${bar}`);
  }
}

toValues

toValues converts all elements in collection to reactive observed values. Returns an array/object the indices/keys of which will be the same with the input collection. Each element will be an instance of the Observed class.

import { OBSERVE, OBSERVE_PROVIDER, Observed, ObserveFn, toValues } from 'ng-observe';

@Component({
  template: '{{ foo.value }} {{ bar.value }}',
  providers: [OBSERVE_PROVIDER],
})
class Component {
  foo: Observed<string>;

  bar: Observed<string>;

  constructor(@Inject(OBSERVE) private observe: ObserveFn) {
    const state = this.observe({ foo: of('foo'), bar: of('bar') });
    const { foo, bar } = toValues(state);
    this.foo = foo;
    this.bar = bar;
  }
}

isCollection

Collections observed by ng-observe are plain arrays or objects, but you can detect them with isCollection function. It returns true when input is an observed collection, and false when not.

import { isCollection, OBSERVE, OBSERVE_PROVIDER, ObserveFn } from 'ng-observe';

@Component({
  template: '<!-- not important for this example -->',
  providers: [OBSERVE_PROVIDER],
})
class Component {
  constructor(@Inject(OBSERVE) private observe: ObserveFn) {
    const state = this.observe({ foo: of('foo'), bar: of('bar') });
    console.log(isCollection(state)); // true
  }
}

Sponsors

volosoft


Developed by NG Box

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].