apollo-client-rxjs
Version:
Adds RxJS to ApolloClient
225 lines (215 loc) • 9.54 kB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('apollo-client'), require('rxjs/Observable'), require('rxjs/symbol/observable'), require('rxjs/operator/switchMap'), require('rxjs/observable/combineLatest'), require('rxjs/operator/observeOn'), require('rxjs/scheduler/AsyncAction'), require('rxjs/scheduler/AsyncScheduler')) :
typeof define === 'function' && define.amd ? define(['exports', 'apollo-client', 'rxjs/Observable', 'rxjs/symbol/observable', 'rxjs/operator/switchMap', 'rxjs/observable/combineLatest', 'rxjs/operator/observeOn', 'rxjs/scheduler/AsyncAction', 'rxjs/scheduler/AsyncScheduler'], factory) :
(factory((global.apollo = global.apollo || {}, global.apollo.rxjs = {}),global.apollo,global.Rx,global.Rx.Symbol,global.Rx.Observable.prototype,global.Rx.Observable,global.Rx.Observable.prototype,global.rxjs_scheduler_AsyncAction,global.Rx.Scheduler.async));
}(this, (function (exports,apolloClient,rxjs_Observable,rxjs_symbol_observable,rxjs_operator_switchMap,rxjs_observable_combineLatest,rxjs_operator_observeOn,rxjs_scheduler_AsyncAction,rxjs_scheduler_AsyncScheduler) { 'use strict';
var ObservableQueryRef = (function () {
function ObservableQueryRef(ref) {
if (ref) {
this.setRef(ref);
}
}
ObservableQueryRef.prototype.setRef = function (ref) {
this.ref = ref;
};
ObservableQueryRef.prototype.getRef = function () {
return this.ref;
};
return ObservableQueryRef;
}());
var __extends$1 = (undefined && undefined.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var RxObservableQuery = (function (_super) {
__extends$1(RxObservableQuery, _super);
function RxObservableQuery(apollo, subscribe) {
var _this = _super.call(this, null) || this;
_this.apollo = apollo;
if (subscribe) {
_this._subscribe = subscribe;
}
return _this;
}
RxObservableQuery.prototype.lift = function (operator) {
var observable = new RxObservableQuery(this.apollo);
observable.source = this;
observable.operator = operator;
return observable;
};
RxObservableQuery.prototype.refetch = function (variables) {
return this.getObservableQuery().refetch(variables);
};
RxObservableQuery.prototype.stopPolling = function () {
return this.getObservableQuery().stopPolling();
};
RxObservableQuery.prototype.startPolling = function (p) {
return this.getObservableQuery().startPolling(p);
};
RxObservableQuery.prototype.fetchMore = function (options) {
return this.getObservableQuery().fetchMore(options);
};
RxObservableQuery.prototype.updateQuery = function (mapFn) {
this.getObservableQuery().updateQuery(mapFn);
};
RxObservableQuery.prototype.subscribeToMore = function (options) {
return this.getObservableQuery().subscribeToMore(options);
};
RxObservableQuery.prototype.result = function () {
return this.getObservableQuery().result();
};
RxObservableQuery.prototype.currentResult = function () {
return this.getObservableQuery().currentResult();
};
Object.defineProperty(RxObservableQuery.prototype, "variables", {
get: function () {
return this.getObservableQuery().variables;
},
enumerable: true,
configurable: true
});
RxObservableQuery.prototype.setOptions = function (opts) {
return this.getObservableQuery().setOptions(opts);
};
RxObservableQuery.prototype.setVariables = function (variables, tryFetch) {
if (tryFetch === void 0) { tryFetch = false; }
return this.getObservableQuery().setVariables(variables, tryFetch);
};
RxObservableQuery.prototype._subscribe = function (subscriber) {
if (this.source) {
return this.source['_subscribe'](subscriber);
}
var obs = this.getObservableQuery();
return obs[rxjs_symbol_observable.$$observable]().subscribe(subscriber);
};
RxObservableQuery.prototype.getObservableQuery = function () {
if (this.apollo instanceof ObservableQueryRef) {
var ref = this.apollo;
return ref.getRef();
}
return this.apollo;
};
return RxObservableQuery;
}(rxjs_Observable.Observable));
function isObject(value) {
var type = typeof value;
return value !== null && (type === 'object' || type === 'function');
}
function omit(source) {
var fields = [];
for (var _i = 1; _i < arguments.length; _i++) {
fields[_i - 1] = arguments[_i];
}
var result = {};
if (isObject(source)) {
result = Object.assign(result, source);
fields.forEach(function (field) { return delete result[field]; });
}
return result;
}
function createWithObservableVariables(options, mapFn) {
var observableQueryRef = new ObservableQueryRef();
var varObs = observeVariables(options.variables);
return new RxObservableQuery(observableQueryRef, function (subscriber) {
var sub = rxjs_operator_switchMap.switchMap.call(varObs, (function (newVariables) {
var cleanOptions = omit(options, 'variables');
var newOptions = Object.assign(cleanOptions, { variables: newVariables });
observableQueryRef.setRef(mapFn(newOptions));
return observableQueryRef.getRef();
})).subscribe(subscriber);
return function () { return sub.unsubscribe(); };
});
}
function observeVariables(variables) {
var keys = Object.keys(variables);
return rxjs_Observable.Observable.create(function (observer) {
rxjs_observable_combineLatest.combineLatest.call(undefined, mapVariablesToObservables(variables))
.subscribe(function (values) {
var resultVariables = {};
values.forEach(function (value, i) {
var key = keys[i];
resultVariables[key] = value;
});
observer.next(resultVariables);
});
});
}
function mapVariablesToObservables(variables) {
return Object.keys(variables)
.map(function (key) { return getVariableToObservable(variables[key]); });
}
function getVariableToObservable(variable) {
if (variable instanceof rxjs_Observable.Observable) {
return variable;
}
else if (typeof variable !== 'undefined') {
return new rxjs_Observable.Observable(function (subscriber) {
subscriber.next(variable);
});
}
else {
return new rxjs_Observable.Observable(function (subscriber) {
subscriber.next(null);
});
}
}
var __extends = (undefined && undefined.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var RxApolloClient = (function (_super) {
__extends(RxApolloClient, _super);
function RxApolloClient(options) {
return _super.call(this, options) || this;
}
RxApolloClient.prototype.watchQuery = function (options) {
if (typeof options.variables === 'object') {
return createWithObservableVariables(options, _super.prototype.watchQuery.bind(this));
}
return new RxObservableQuery(_super.prototype.watchQuery.call(this, options));
};
return RxApolloClient;
}(apolloClient.ApolloClient));
function rxify(clientOrWatchQuery) {
if (clientOrWatchQuery instanceof apolloClient.ApolloClient) {
var savedWatchQuery_1 = clientOrWatchQuery.watchQuery;
clientOrWatchQuery.watchQuery = function (options) {
var _this = this;
return wrapWatchQuery(function (opts) { return savedWatchQuery_1.call(_this, opts); })(options);
}.bind(clientOrWatchQuery);
return clientOrWatchQuery;
}
if (isObject(clientOrWatchQuery)) {
return wrapWatchQuery(clientOrWatchQuery);
}
throw new Error('Use ApolloClient or a function that returns ObservableQuery');
}
function wrapAsync(obs) {
return rxjs_operator_observeOn.observeOn.call(obs, new rxjs_scheduler_AsyncScheduler.AsyncScheduler(rxjs_scheduler_AsyncAction.AsyncAction));
}
function wrapWatchQuery(watchQuery) {
return function (options) {
if (typeof options.variables === 'object') {
return wrapAsync(createWithObservableVariables(options, function (newOptions) { return watchQuery(newOptions); }));
}
return wrapAsync(new RxObservableQuery(watchQuery(options)));
};
}
exports.RxApolloClient = RxApolloClient;
exports.RxObservableQuery = RxObservableQuery;
exports.rxify = rxify;
Object.defineProperty(exports, '__esModule', { value: true });
})));
//# sourceMappingURL=apollo-rxjs.umd.js.map