-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
899 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { Observable } from 'rxjs'; | ||
export declare class MeteorObservable { | ||
static call<T>(name: string, ...args: any[]): Observable<T>; | ||
static subscribe<T>(name: string, ...args: any[]): Observable<T>; | ||
static autorun(): Observable<Tracker.Computation>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
'use strict'; | ||
import { Observable } from 'rxjs'; | ||
import { isMeteorCallbacks, forkZone, removeObserver } from './utils'; | ||
function throwInvalidCallback(method) { | ||
throw new Error("Invalid " + method + " arguments:\n your last param can't be a callback function, \n please remove it and use \".subscribe\" of the Observable!"); | ||
} | ||
export var MeteorObservable = (function () { | ||
function MeteorObservable() { | ||
} | ||
MeteorObservable.call = function (name) { | ||
var args = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
args[_i - 1] = arguments[_i]; | ||
} | ||
var lastParam = args[args.length - 1]; | ||
if (isMeteorCallbacks(lastParam)) { | ||
throwInvalidCallback('MeteorObservable.call'); | ||
} | ||
var zone = forkZone(); | ||
return Observable.create(function (observer) { | ||
Meteor.call.apply(Meteor, [name].concat(args.concat([ | ||
function (error, result) { | ||
zone.run(function () { | ||
error ? observer.error(error) : | ||
observer.next(result); | ||
observer.complete(); | ||
}); | ||
} | ||
]))); | ||
}); | ||
}; | ||
MeteorObservable.subscribe = function (name) { | ||
var args = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
args[_i - 1] = arguments[_i]; | ||
} | ||
var lastParam = args[args.length - 1]; | ||
if (isMeteorCallbacks(lastParam)) { | ||
throwInvalidCallback('MeteorObservable.subscribe'); | ||
} | ||
var zone = forkZone(); | ||
var observers = []; | ||
var subscribe = function () { | ||
return Meteor.subscribe.apply(Meteor, [name].concat(args.concat([{ | ||
onError: function (error) { | ||
zone.run(function () { | ||
observers.forEach(function (observer) { return observer.error(error); }); | ||
}); | ||
}, | ||
onReady: function () { | ||
zone.run(function () { | ||
observers.forEach(function (observer) { return observer.next(); }); | ||
}); | ||
} | ||
} | ||
]))); | ||
}; | ||
var subHandler = null; | ||
return Observable.create(function (observer) { | ||
observers.push(observer); | ||
// Execute subscribe lazily. | ||
if (subHandler === null) { | ||
subHandler = subscribe(); | ||
} | ||
return function () { | ||
removeObserver(observers, observer, function () { return subHandler.stop(); }); | ||
}; | ||
}); | ||
}; | ||
MeteorObservable.autorun = function () { | ||
var zone = forkZone(); | ||
var observers = []; | ||
var autorun = function () { | ||
return Tracker.autorun(function (computation) { | ||
zone.run(function () { | ||
observers.forEach(function (observer) { return observer.next(computation); }); | ||
}); | ||
}); | ||
}; | ||
var handler = null; | ||
return Observable.create(function (observer) { | ||
observers.push(observer); | ||
// Execute autorun lazily. | ||
if (handler === null) { | ||
handler = autorun(); | ||
} | ||
return function () { | ||
removeObserver(observers, observer, function () { return handler.stop(); }); | ||
}; | ||
}); | ||
}; | ||
return MeteorObservable; | ||
}()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { Observable } from 'rxjs'; | ||
import { ObservableCursor } from './ObservableCursor'; | ||
import Selector = Mongo.Selector; | ||
import ObjectID = Mongo.ObjectID; | ||
import SortSpecifier = Mongo.SortSpecifier; | ||
import FieldSpecifier = Mongo.FieldSpecifier; | ||
import Modifier = Mongo.Modifier; | ||
export declare module MongoObservable { | ||
interface ConstructorOptions { | ||
connection?: Object; | ||
idGeneration?: string; | ||
transform?: Function; | ||
} | ||
interface AllowDenyOptionsObject<T> { | ||
insert?: (userId: string, doc: T) => boolean; | ||
update?: (userId: string, doc: T, fieldNames: string[], modifier: any) => boolean; | ||
remove?: (userId: string, doc: T) => boolean; | ||
fetch?: string[]; | ||
transform?: Function; | ||
} | ||
function fromExisting<T>(collection: Mongo.Collection<T>): Collection<T>; | ||
class Collection<T> { | ||
private _collection; | ||
constructor(nameOrExisting: string | Mongo.Collection<T>, options?: ConstructorOptions); | ||
readonly collection: Mongo.Collection<T>; | ||
allow(options: AllowDenyOptionsObject<T>): boolean; | ||
deny(options: AllowDenyOptionsObject<T>): boolean; | ||
rawCollection(): any; | ||
rawDatabase(): any; | ||
insert(doc: T): Observable<string>; | ||
remove(selector: Selector | ObjectID | string): Observable<number>; | ||
update(selector: Selector | ObjectID | string, modifier: Modifier, options?: { | ||
multi?: boolean; | ||
upsert?: boolean; | ||
}): Observable<number>; | ||
upsert(selector: Selector | ObjectID | string, modifier: Modifier, options?: { | ||
multi?: boolean; | ||
}): Observable<number>; | ||
find(selector?: Selector | ObjectID | string, options?: { | ||
sort?: SortSpecifier; | ||
skip?: number; | ||
limit?: number; | ||
fields?: FieldSpecifier; | ||
reactive?: boolean; | ||
transform?: Function; | ||
}): ObservableCursor<T>; | ||
findOne(selector?: Selector | ObjectID | string, options?: { | ||
sort?: SortSpecifier; | ||
skip?: number; | ||
fields?: FieldSpecifier; | ||
reactive?: boolean; | ||
transform?: Function; | ||
}): T; | ||
private _createObservable<T>(observers); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import { Observable } from 'rxjs'; | ||
import { ObservableCursor } from './ObservableCursor'; | ||
import { removeObserver } from './utils'; | ||
export var MongoObservable; | ||
(function (MongoObservable) { | ||
'use strict'; | ||
function fromExisting(collection) { | ||
return new MongoObservable.Collection(collection); | ||
} | ||
MongoObservable.fromExisting = fromExisting; | ||
var Collection = (function () { | ||
function Collection(nameOrExisting, options) { | ||
if (nameOrExisting instanceof Mongo.Collection) { | ||
this._collection = nameOrExisting; | ||
} | ||
else { | ||
this._collection = new Mongo.Collection(nameOrExisting, options); | ||
} | ||
} | ||
Object.defineProperty(Collection.prototype, "collection", { | ||
get: function () { | ||
return this._collection; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
Collection.prototype.allow = function (options) { | ||
return this._collection.allow(options); | ||
}; | ||
Collection.prototype.deny = function (options) { | ||
return this._collection.deny(options); | ||
}; | ||
Collection.prototype.rawCollection = function () { | ||
return this._collection.rawCollection(); | ||
}; | ||
Collection.prototype.rawDatabase = function () { | ||
return this._collection.rawDatabase(); | ||
}; | ||
Collection.prototype.insert = function (doc) { | ||
var observers = []; | ||
var obs = this._createObservable(observers); | ||
this._collection.insert(doc, function (error, docId) { | ||
observers.forEach(function (observer) { | ||
error ? observer.error(error) : | ||
observer.next(docId); | ||
observer.complete(); | ||
}); | ||
}); | ||
return obs; | ||
}; | ||
Collection.prototype.remove = function (selector) { | ||
var observers = []; | ||
var obs = this._createObservable(observers); | ||
this._collection.remove(selector, function (error, removed) { | ||
observers.forEach(function (observer) { | ||
error ? observer.error(error) : | ||
observer.next(removed); | ||
observer.complete(); | ||
}); | ||
}); | ||
return obs; | ||
}; | ||
Collection.prototype.update = function (selector, modifier, options) { | ||
var observers = []; | ||
var obs = this._createObservable(observers); | ||
this._collection.update(selector, modifier, options, function (error, updated) { | ||
observers.forEach(function (observer) { | ||
error ? observer.error(error) : | ||
observer.next(updated); | ||
observer.complete(); | ||
}); | ||
}); | ||
return obs; | ||
}; | ||
Collection.prototype.upsert = function (selector, modifier, options) { | ||
var observers = []; | ||
var obs = this._createObservable(observers); | ||
this._collection.upsert(selector, modifier, options, function (error, affected) { | ||
observers.forEach(function (observer) { | ||
error ? observer.error(error) : | ||
observer.next(affected); | ||
observer.complete(); | ||
}); | ||
}); | ||
return obs; | ||
}; | ||
Collection.prototype.find = function (selector, options) { | ||
var cursor = this._collection.find.apply(this._collection, arguments); | ||
return ObservableCursor.create(cursor); | ||
}; | ||
Collection.prototype.findOne = function (selector, options) { | ||
return this._collection.findOne.apply(this._collection, arguments); | ||
}; | ||
Collection.prototype._createObservable = function (observers) { | ||
return Observable.create(function (observer) { | ||
observers.push(observer); | ||
return function () { | ||
removeObserver(observers, observer); | ||
}; | ||
}); | ||
}; | ||
return Collection; | ||
}()); | ||
MongoObservable.Collection = Collection; | ||
})(MongoObservable || (MongoObservable = {})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { Observable } from 'rxjs'; | ||
export declare class ObservableCursor<T> extends Observable<T[]> { | ||
private _zone; | ||
private _data; | ||
private _cursor; | ||
private _hCursor; | ||
private _observers; | ||
static create<T>(cursor: Mongo.Cursor<T>): ObservableCursor<T>; | ||
constructor(cursor: Mongo.Cursor<T>); | ||
readonly cursor: Mongo.Cursor<T>; | ||
stop(): void; | ||
dispose(): void; | ||
fetch(): Array<T>; | ||
observe(callbacks: Mongo.ObserveCallbacks): Meteor.LiveQueryHandle; | ||
observeChanges(callbacks: Mongo.ObserveChangesCallbacks): Meteor.LiveQueryHandle; | ||
_runComplete(): void; | ||
_runNext(data: Array<T>): void; | ||
_addedAt(doc: any, at: any, before: any): void; | ||
_changedAt(doc: any, old: any, at: any): void; | ||
_removedAt(doc: any, at: any): void; | ||
_handleChange(): void; | ||
_observeCursor(cursor: Mongo.Cursor<T>): any; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
'use strict'; | ||
import { Observable } from 'rxjs'; | ||
import { gZone, forkZone, removeObserver } from './utils'; | ||
export var ObservableCursor = (function (_super) { | ||
__extends(ObservableCursor, _super); | ||
function ObservableCursor(cursor) { | ||
var _this = this; | ||
_super.call(this, function (observer) { | ||
_this._observers.push(observer); | ||
if (!_this._hCursor) { | ||
_this._hCursor = _this._observeCursor(cursor); | ||
} | ||
return function () { | ||
removeObserver(_this._observers, observer, function () { return _this.stop(); }); | ||
}; | ||
}); | ||
this._data = []; | ||
this._observers = []; | ||
_.extend(this, _.omit(cursor, 'count', 'map')); | ||
this._cursor = cursor; | ||
this._zone = forkZone(); | ||
} | ||
ObservableCursor.create = function (cursor) { | ||
return new ObservableCursor(cursor); | ||
}; | ||
Object.defineProperty(ObservableCursor.prototype, "cursor", { | ||
get: function () { | ||
return this._cursor; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
ObservableCursor.prototype.stop = function () { | ||
var _this = this; | ||
this._zone.run(function () { | ||
_this._runComplete(); | ||
}); | ||
if (this._hCursor) { | ||
this._hCursor.stop(); | ||
} | ||
this._hCursor = null; | ||
}; | ||
ObservableCursor.prototype.dispose = function () { | ||
this._observers = null; | ||
this._cursor = null; | ||
}; | ||
ObservableCursor.prototype.fetch = function () { | ||
return this._cursor.fetch(); | ||
}; | ||
ObservableCursor.prototype.observe = function (callbacks) { | ||
return this._cursor.observe(callbacks); | ||
}; | ||
ObservableCursor.prototype.observeChanges = function (callbacks) { | ||
return this._cursor.observeChanges(callbacks); | ||
}; | ||
ObservableCursor.prototype._runComplete = function () { | ||
this._observers.forEach(function (observer) { | ||
observer.complete(); | ||
}); | ||
}; | ||
ObservableCursor.prototype._runNext = function (data) { | ||
this._observers.forEach(function (observer) { | ||
observer.next(data); | ||
}); | ||
}; | ||
ObservableCursor.prototype._addedAt = function (doc, at, before) { | ||
this._data.splice(at, 0, doc); | ||
this._handleChange(); | ||
}; | ||
ObservableCursor.prototype._changedAt = function (doc, old, at) { | ||
this._data[at] = doc; | ||
this._handleChange(); | ||
}; | ||
; | ||
ObservableCursor.prototype._removedAt = function (doc, at) { | ||
this._data.splice(at, 1); | ||
this._handleChange(); | ||
}; | ||
; | ||
ObservableCursor.prototype._handleChange = function () { | ||
var _this = this; | ||
this._zone.run(function () { | ||
_this._runNext(_this._data); | ||
}); | ||
}; | ||
; | ||
ObservableCursor.prototype._observeCursor = function (cursor) { | ||
var _this = this; | ||
return gZone.run(function () { return cursor.observe({ | ||
addedAt: _this._addedAt.bind(_this), | ||
changedAt: _this._changedAt.bind(_this), | ||
removedAt: _this._removedAt.bind(_this) | ||
}); }); | ||
}; | ||
return ObservableCursor; | ||
}(Observable)); |
Oops, something went wrong.