-
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.
Copied mostly from Angular2-Meteor
- Loading branch information
0 parents
commit e163f52
Showing
34 changed files
with
1,394 additions
and
0 deletions.
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,4 @@ | ||
export * from './observable-collection'; | ||
export * from './meteor-observable'; | ||
export * from './observable-cursor'; | ||
export * from './zone-operator'; |
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,8 @@ | ||
"use strict"; | ||
function __export(m) { | ||
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; | ||
} | ||
__export(require('./observable-collection')); | ||
__export(require('./meteor-observable')); | ||
__export(require('./observable-cursor')); | ||
__export(require('./zone-operator')); |
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,5 @@ | ||
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>; | ||
} |
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,55 @@ | ||
'use strict'; | ||
var rxjs_1 = require('rxjs'); | ||
var utils_1 = require('./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!"); | ||
} | ||
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 (utils_1.isMeteorCallbacks(lastParam)) { | ||
throwInvalidCallback('MeteorObservable.call'); | ||
} | ||
return rxjs_1.Observable.create(function (observer) { | ||
Meteor.call.apply(Meteor, [name].concat(args.concat([ | ||
function (error, result) { | ||
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 (utils_1.isMeteorCallbacks(lastParam)) { | ||
throwInvalidCallback('MeteorObservable.subscribe'); | ||
} | ||
return rxjs_1.Observable.create(function (observer) { | ||
var handler = Meteor.subscribe.apply(Meteor, [name].concat(args.concat([{ | ||
onError: function (error) { | ||
observer.error(error); | ||
observer.complete(); | ||
}, | ||
onReady: function () { | ||
observer.next(); | ||
observer.complete(); | ||
} | ||
} | ||
]))); | ||
return function () { return handler.stop(); }; | ||
}); | ||
}; | ||
return MeteorObservable; | ||
}()); | ||
exports.MeteorObservable = 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,55 @@ | ||
import { Observable } from 'rxjs'; | ||
import { ObservableCursor } from './observable-cursor'; | ||
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; | ||
} | ||
class Collection<T> { | ||
private _collection; | ||
constructor(name: string, options?: ConstructorOptions); | ||
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,99 @@ | ||
"use strict"; | ||
var rxjs_1 = require('rxjs'); | ||
var observable_cursor_1 = require('./observable-cursor'); | ||
var MongoObservable; | ||
(function (MongoObservable) { | ||
'use strict'; | ||
var Collection = (function () { | ||
function Collection(name, options) { | ||
this._collection = new Mongo.Collection(name, 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(this._collection, arguments); | ||
return observable_cursor_1.ObservableCursor.create(cursor); | ||
}; | ||
Collection.prototype.findOne = function (selector, options) { | ||
return this._collection.findOne.apply(this._collection, arguments); | ||
}; | ||
Collection.prototype._createObservable = function (observers) { | ||
return rxjs_1.Observable.create(function (observer) { | ||
observers.push(observer); | ||
return function () { | ||
var index = observers.indexOf(observer); | ||
if (index !== -1) { | ||
observers.splice(index, 1); | ||
} | ||
}; | ||
}); | ||
}; | ||
return Collection; | ||
}()); | ||
MongoObservable.Collection = Collection; | ||
})(MongoObservable = exports.MongoObservable || (exports.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,22 @@ | ||
import { Observable } from 'rxjs'; | ||
export declare class ObservableCursor<T> extends Observable<T[]> { | ||
private _data; | ||
private _cursor; | ||
private _hCursor; | ||
private _observers; | ||
static create<T>(cursor: Mongo.Cursor<T>): ObservableCursor<T>; | ||
constructor(cursor: Mongo.Cursor<T>); | ||
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,101 @@ | ||
'use strict'; | ||
var __extends = (this && this.__extends) || function (d, b) { | ||
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
var rxjs_1 = require('rxjs'); | ||
var utils_1 = require('./utils'); | ||
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 () { | ||
var index = _this._observers.indexOf(observer); | ||
if (index !== -1) { | ||
_this._observers.splice(index, 1); | ||
} | ||
if (!_this._observers.length) { | ||
_this.stop(); | ||
} | ||
}; | ||
}); | ||
this._data = []; | ||
this._observers = []; | ||
_.extend(this, _.omit(cursor, 'count', 'map')); | ||
this._cursor = cursor; | ||
} | ||
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 () { | ||
if (this._hCursor) { | ||
this._hCursor.stop(); | ||
} | ||
this._runComplete(); | ||
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 () { | ||
this._runNext(this._data); | ||
}; | ||
; | ||
ObservableCursor.prototype._observeCursor = function (cursor) { | ||
var _this = this; | ||
return utils_1.gZone.run(function () { return cursor.observe({ | ||
addedAt: _this._addedAt.bind(_this), | ||
changedAt: _this._changedAt.bind(_this), | ||
removedAt: _this._removedAt.bind(_this) | ||
}); }); | ||
}; | ||
return ObservableCursor; | ||
}(rxjs_1.Observable)); | ||
exports.ObservableCursor = ObservableCursor; |
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,11 @@ | ||
export declare type CallbacksObject = { | ||
onReady?: Function; | ||
onError?: Function; | ||
onStop?: Function; | ||
}; | ||
export declare type MeteorCallbacks = ((...args) => any) | CallbacksObject; | ||
export declare const subscribeEvents: string[]; | ||
export declare function isMeteorCallbacks(callbacks: any): boolean; | ||
export declare function isCallbacksObject(callbacks: any): boolean; | ||
export declare const g: any; | ||
export declare const gZone: 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,23 @@ | ||
'use strict'; | ||
exports.subscribeEvents = ['onReady', 'onError', 'onStop']; | ||
function isMeteorCallbacks(callbacks) { | ||
return _.isFunction(callbacks) || isCallbacksObject(callbacks); | ||
} | ||
exports.isMeteorCallbacks = isMeteorCallbacks; | ||
// Checks if callbacks of {@link CallbacksObject} type. | ||
function isCallbacksObject(callbacks) { | ||
return callbacks && exports.subscribeEvents.some(function (event) { | ||
return _.isFunction(callbacks[event]); | ||
}); | ||
} | ||
exports.isCallbacksObject = isCallbacksObject; | ||
; | ||
exports.g = typeof global === 'object' ? global : | ||
typeof window === 'object' ? window : | ||
typeof self === 'object' ? self : this; | ||
var fakeZone = { | ||
run: function (func) { | ||
return func(); | ||
} | ||
}; | ||
exports.gZone = exports.g.Zone ? exports.g.Zone.current : fakeZone; |
Oops, something went wrong.