- Notifications
You must be signed in to change notification settings - Fork38
Exposing Mongo Cursor as RxJS Observable
License
Urigo/meteor-rxjs
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Harness Meteor reactivity with RxJS.
RxJS is built to simplify complexity dealing with reactive data flows. At the same time, Meteor's Minimongo cursors are a good target for RxJS API due to their reactivity. Thus, combining RxJS and Meteor, we bring together best parts of two worlds.
API documentation is available inside this repository,here.
As soon as you install this package (npm install meteor-rxjs
), you have ability to use a special Mongo collection class that workswith cursor observables instead of the ordinary Mongo cursors. In other words, one can subscribe on the Mongo cursor's data updates now as follows:
import{MongoObservable}from'meteor-rxjs';constTasks=newMongoObservable.Collection<Task>('tasks');Tasks.find({checked:false}).map(tasks=>tasks.length).subscribe(todoCount=>console.log(todoCount));
Since this cursor observable is of RxJS’s type, every other methods and operators available to the observables as part of the RxJS API are also now available to the users, e.g., one can debounce data updates using RxJS’s debouncing operator:
import{Observable}from'rxjs';import{debounce,map}from'rxjs/operators';Tasks.find({checked:false}).pipe(debounce(()=>Observable.interval(50))).pipe(map(tasks=>tasks.length)).subscribe(todoCount=>console.log(todoCount));
Meteor has a lot of packages that extendMongo.Collection
with new methods. SinceMongoObservable.Collection
is a wrapper overMongo.Collection
, you can't use new methods on observable instances directly. The solution here is to passMongo.Collection
's instance to the observable constructor, and use them whenever you need after separately:
letcollection=newMongo.Collection('foo');letobservable=newMongoObservable.Collection(collection);collection.attachSchema(...);// with SimpleSchema package
Angular has tight integration with RxJS since Angular is desinged to support reactive UI updates.One of the realizations of this integration isAsyncPipe
, which is supposed to be used with RxJS observables.
In order to subscribe on the Mongo cursor observable's updates and iterate through the returned list of docs in Angular, one can useAsyncPipe
in an Angular component as follows:
import{MongoObservable,zoneOperator}from'rxjs';constTasks=newMongoObservable.Collection<Task>('tasks');@Component({selector:'task-list',template:`<ul><li *ngFor="let task of tasks | async"></li></ul>`})classTasks{tasks=Tasks.find().pipe(zoneOperator());}
As you can see above we calledzoneOperator
operator of the cursor observable. This is a specialZone operator that is implemeted bymeteor-rxjs
for the Angular users' convenience.This operator runs ngZone each time when new data arrives to the Mongo cursor observable,thus we force UI updates at the right time using it.
It makes sense to improve performance of the aboveTasks
component by debouncing UI updates.In this case we are using Zone operator as well:
classList{tasks=Tasks.find().pipe(zoneOperator()).pipe(debounce(()=>Observable.interval(50))).zone();}
##LicenseMIT
About
Exposing Mongo Cursor as RxJS Observable
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors9
Uh oh!
There was an error while loading.Please reload this page.