![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
zen-observable
Advanced tools
The zen-observable npm package provides an implementation of the Observable pattern, which is used for managing asynchronous data streams. It allows you to create, transform, and subscribe to data streams in a declarative manner.
Creating an Observable
This feature allows you to create a new Observable instance. The observer object is used to emit values and signal completion. The subscriber receives these values and handles them accordingly.
const { Observable } = require('zen-observable');
const observable = new Observable(observer => {
observer.next('Hello');
observer.next('World');
observer.complete();
});
observable.subscribe({
next(value) { console.log(value); },
complete() { console.log('Done'); }
});
Transforming Observables
This feature allows you to transform the data emitted by an Observable using operators like map. In this example, each value emitted by the original Observable is multiplied by 2.
const { Observable } = require('zen-observable');
const observable = new Observable(observer => {
observer.next(1);
observer.next(2);
observer.next(3);
observer.complete();
});
const transformed = observable.map(x => x * 2);
transformed.subscribe({
next(value) { console.log(value); },
complete() { console.log('Done'); }
});
Combining Observables
This feature allows you to combine multiple Observables into a single Observable. In this example, two Observables are combined and their values are emitted in sequence.
const { Observable } = require('zen-observable');
const observable1 = new Observable(observer => {
observer.next('A');
observer.complete();
});
const observable2 = new Observable(observer => {
observer.next('B');
observer.complete();
});
const combined = Observable.from([observable1, observable2]).flatMap(x => x);
combined.subscribe({
next(value) { console.log(value); },
complete() { console.log('Done'); }
});
RxJS is a library for reactive programming using Observables, to make it easier to compose asynchronous or callback-based code. It is more feature-rich compared to zen-observable and includes a wide range of operators for transforming, combining, and querying Observables.
Bacon.js is a functional reactive programming library for JavaScript. It provides a comprehensive set of tools for working with event streams and properties. Compared to zen-observable, Bacon.js offers more advanced features for handling complex data flows.
Most.js is a high-performance reactive programming library that focuses on speed and efficiency. It provides a minimalistic API for working with streams. While zen-observable is simpler and easier to use, most.js is optimized for performance and can handle large-scale data streams more efficiently.
An implementation of Observables for JavaScript. Requires Promises or a Promise polyfill.
npm install zen-observable
import Observable from 'zen-observable';
Observable.of(1, 2, 3).subscribe(x => console.log(x));
let observable = new Observable(observer => {
// Emit a single value after 1 second
let timer = setTimeout(() => {
observer.next('hello');
observer.complete();
}, 1000);
// On unsubscription, cancel the timer
return () => clearTimeout(timer);
});
Creates a new Observable object using the specified subscriber function. The subscriber function is called whenever the subscribe
method of the observable object is invoked. The subscriber function is passed an observer object which has the following methods:
next(value)
Sends the next value in the sequence.error(exception)
Terminates the sequence with an exception.complete()
Terminates the sequence successfully.closed
A boolean property whose value is true
if the observer's subscription is closed.The subscriber function can optionally return either a cleanup function or a subscription object. If it returns a cleanup function, that function will be called when the subscription has closed. If it returns a subscription object, then the subscription's unsubscribe
method will be invoked when the subscription has closed.
// Logs 1, 2, 3
Observable.of(1, 2, 3).subscribe(x => {
console.log(x);
});
Returns an observable which will emit each supplied argument.
let list = [1, 2, 3];
// Iterate over an object
Observable.from(list).subscribe(x => {
console.log(x);
});
// Convert something 'observable' to an Observable instance
Observable.from(otherObservable).subscribe(x => {
console.log(x);
});
Converts value
to an Observable.
value
is an implementation of Observable, then it is converted to an instance of Observable as defined by this library.value
.let subscription = observable.subscribe({
next(x) { console.log(x) },
error(err) { console.log(`Finished with error: ${ err }`) },
complete() { console.log('Finished') }
});
Subscribes to the observable. Observer objects may have any of the following methods:
next(value)
Receives the next value of the sequence.error(exception)
Receives the terminating error of the sequence.complete()
Called when the stream has completed successfully.Returns a subscription object that can be used to cancel the stream.
let subscription = observable.subscribe(
x => console.log(x),
err => console.log(`Finished with error: ${ err }`),
() => console.log('Finished')
);
Subscribes to the observable with callback functions. Returns a subscription object that can be used to cancel the stream.
observable.forEach(x => {
console.log(`Received value: ${ x }`);
}).then(() => {
console.log('Finished successfully')
}).catch(err => {
console.log(`Finished with error: ${ err }`);
})
Subscribes to the observable and returns a Promise for the completion value of the stream. The callback
argument is called once for each value in the stream.
Observable.of(1, 2, 3).filter(value => {
return value > 2;
}).subscribe(value => {
console.log(value);
});
// 3
Returns a new Observable that emits all values which pass the test implemented by the callback
argument.
Returns a new Observable that emits the results of calling the callback
argument for every value in the stream.
Observable.of(1, 2, 3).map(value => {
return value * 2;
}).subscribe(value => {
console.log(value);
});
// 2
// 4
// 6
Observable.of(0, 1, 2, 3, 4).reduce((previousValue, currentValue) => {
return previousValue + currentValue;
}).subscribe(result => {
console.log(result);
});
// 10
Returns a new Observable that applies a function against an accumulator and each value of the stream to reduce it to a single value.
Observable.of(1, 2, 3).concat(
Observable.of(4, 5, 6),
Observable.of(7, 8, 9)
).subscribe(result => {
console.log(result);
});
// 1, 2, 3, 4, 5, 6, 7, 8, 9
Merges the current observable with additional observables.
let observable = Observable.of(1, 2, 3);
for (let value of await observable.all()) {
console.log(value);
}
// 1, 2, 3
Returns a Promise
for an array containing all of the values produced by the observable.
FAQs
An Implementation of ES Observables
We found that zen-observable demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.