npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ng-observe

v1.1.0

Published

Angular reactivity streamlined...

Downloads

1,099

Readme

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