![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Angular reactivity streamlined...
Install the package, and you are good to go. No module import is necessary.
npm install ng-observe
...or...
yarn add ng-observe
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. UsetoValue
ortoValues
to convert elements of the collection to instances ofObserved
instead.
You can read this Medium article to learn about what the motivation behind ng-observe is.
To use ng-observe in your components and directives, add OBSERVE_PROVIDER
to providers array in metadata.
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) {}
}
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) {}
}
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
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');
}
}
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
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;
}
}
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
}
}
FAQs
Angular reactivity streamlined...
The npm package ng-observe receives a total of 216 weekly downloads. As such, ng-observe popularity was classified as not popular.
We found that ng-observe demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?
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.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.