Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove direct RxJS dependency and have it passed to the plugin #12

Merged
merged 1 commit into from
May 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
const feathers = require('feathers');
const memory = require('feathers-memory');
const rx = require('feathers-rx');
const RxJS = require('rxjs');

const app = feathers()
.configure(rx())
.configure(rx(RxJS))
.use('/messages', memory());

const messages = app.service('messages');
Expand Down
3 changes: 2 additions & 1 deletion examples/basic.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const RxJS = require('rxjs');
const feathers = require('feathers');
const memory = require('feathers-memory');
const rx = require('../lib');

const app = feathers()
.configure(rx())
.configure(rx(RxJS))
.use('/messages', memory());

const messages = app.service('messages');
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@
},
"dependencies": {
"debug": "^2.2.0",
"feathers-commons": "^0.7.1",
"rxjs": "^5.0.0-beta.6"
"feathers-commons": "^0.7.1"
},
"devDependencies": {
"babel-cli": "^6.4.5",
Expand All @@ -60,6 +59,7 @@
"feathers-memory": "^0.7.0",
"jshint": "^2.9.1",
"mocha": "^2.3.4",
"rimraf": "^2.5.2"
"rimraf": "^2.5.2",
"rxjs": "^5.0.0-beta.8"
}
}
29 changes: 21 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,37 @@
import Rx from 'rxjs/Rx';

import reactiveResource from './resource';
import reactiveList from './list';
import strategies from './strategies';
import { makeSorter } from './utils';

const debug = require('debug')('feathers-rx');

function FeathersRx(options) {
function FeathersRx(Rx, options) {
if(!Rx) {
throw new Error('You have to pass an instance of RxJS as the first paramter.');
}

if(!Rx.Observable) {
throw new Error('The RxJS instance does not seem to provide an `Observable` type.');
}

const listStrategies = strategies(Rx);

options = Object.assign({
idField: 'id',
dataField: 'data',
// Whether to requery service when a change is detected
listStrategy: strategies.smart,
listStrategy: 'smart',
// The merging strategy
merge(current, eventData) {
return Object.assign({}, current, eventData);
}
},
sorter: makeSorter
}, options);

if(typeof options.listStrategy === 'string') {
options.listStrategy = listStrategies[options.listStrategy];
}

const mixin = function(service) {
const app = this;
const mixin = {
Expand All @@ -35,8 +49,8 @@ function FeathersRx(options) {

app.methods.forEach(method => {
if(typeof service[method] === 'function') {
mixin[method] = method === 'find' ? reactiveList(events, options) :
reactiveResource(events, options, method);
mixin[method] = method === 'find' ? reactiveList(Rx, events, options) :
reactiveResource(Rx, events, options, method);
}
});

Expand All @@ -50,7 +64,6 @@ function FeathersRx(options) {
};
}

FeathersRx.Rx = Rx;
FeathersRx.strategy = strategies;

export default FeathersRx;
4 changes: 1 addition & 3 deletions src/list.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import Rx from 'rxjs/Rx';
import 'rxjs/add/operator/exhaustMap';
import { promisify } from './utils';

export default function(events, options) {
export default function(Rx, events, options) {
return function (params = {}) {
const query = Object.assign({}, params.query);
const result = this._super.apply(this, arguments);
Expand Down
5 changes: 1 addition & 4 deletions src/resource.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import Rx from 'rxjs/Rx';
import 'rxjs/add/operator/exhaustMap';

import { promisify } from './utils';

// The position of the params parameters for a service method so that we can extend them
Expand All @@ -10,7 +7,7 @@ export const paramsPositions = {
patch: 2
};

export default function(events, settings, method) {
export default function(Rx, events, settings, method) {
return function() {
const result = this._super.apply(this, arguments);

Expand Down
125 changes: 63 additions & 62 deletions src/strategies.js
Original file line number Diff line number Diff line change
@@ -1,72 +1,73 @@
import Rx from 'rxjs/Rx';
import { matcher } from 'feathers-commons/lib/utils';
import { makeSorter } from './utils';

export default {
never(source) {
return source;
},
export default function(Rx) {
return {
never(source) {
return source;
},

always(source, events, options, args) {
const params = args[0] || {};
const query = Object.assign({}, params.query);
const _super = this._super.bind(this);
always(source, events, options, args) {
const params = args[0] || {};
const query = Object.assign({}, params.query);
const _super = this._super.bind(this);

// A function that returns if an item matches the query
const matches = matcher(query);
// A function that sorts a limits a result (paginated or not)
const sortAndTrim = makeSorter(query, options);
// A function that returns if an item matches the query
const matches = matcher(query);
// A function that sorts a limits a result (paginated or not)
const sortAndTrim = makeSorter(query, options);

return source.concat(source.exhaustMap(() =>
Rx.Observable.merge(
events.created.filter(matches),
events.removed,
Rx.Observable.merge(events.updated, events.patched)
).flatMap(() => {
const result = _super(...args);
const source = Rx.Observable.fromPromise(result);
return source.concat(source.exhaustMap(() =>
Rx.Observable.merge(
events.created.filter(matches),
events.removed,
Rx.Observable.merge(events.updated, events.patched)
).flatMap(() => {
const result = _super(...args);
const source = Rx.Observable.fromPromise(result);

return source.map(sortAndTrim);
})
));
},
return source.map(sortAndTrim);
})
));
},

smart(source, events, options, args) {
const params = args[0] || {};
const query = Object.assign({}, params.query);
// A function that returns if an item matches the query
const matches = matcher(query);
// A function that sorts a limits a result (paginated or not)
const sortAndTrim = makeSorter(query, options);
smart(source, events, options, args) {
const params = args[0] || {};
const query = Object.assign({}, params.query);
// A function that returns if an item matches the query
const matches = matcher(query);
// A function that sorts a limits a result (paginated or not)
const sortAndTrim = makeSorter(query, options);

return source.concat(source.exhaustMap(data =>
Rx.Observable.merge(
events.created.filter(matches).map(eventData =>
items => items.concat(eventData)
),
events.removed.map(eventData =>
items => items.filter(current =>
eventData[options.idField] !== current[options.idField]
)
),
Rx.Observable.merge(events.updated, events.patched).map(eventData =>
items => items.map(current => {
if(eventData[options.idField] === current[options.idField]) {
return options.merge(current, eventData);
}
return source.concat(source.exhaustMap(data =>
Rx.Observable.merge(
events.created.filter(matches).map(eventData =>
items => items.concat(eventData)
),
events.removed.map(eventData =>
items => items.filter(current =>
eventData[options.idField] !== current[options.idField]
)
),
Rx.Observable.merge(events.updated, events.patched).map(eventData =>
items => items.map(current => {
if(eventData[options.idField] === current[options.idField]) {
return options.merge(current, eventData);
}

return current;
}).filter(matches)
)
).scan((current, callback) => {
const isPaginated = !!current[options.dataField];
if (isPaginated) {
current[options.dataField] = callback(current.data);
} else {
current = callback(current);
}
return sortAndTrim(current);
}, data)
));
}
};
return current;
}).filter(matches)
)
).scan((current, callback) => {
const isPaginated = !!current[options.dataField];
if (isPaginated) {
current[options.dataField] = callback(current.data);
} else {
current = callback(current);
}
return sortAndTrim(current);
}, data)
));
}
};
}
19 changes: 10 additions & 9 deletions test/list.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Rx from 'rxjs/Rx';
import assert from 'assert';
import feathers from 'feathers';
import memory from 'feathers-memory';
Expand All @@ -11,7 +12,7 @@ describe('reactive lists', () => {
describe('default', function () {
beforeEach(done => {
app = feathers()
.configure(rx())
.configure(rx(Rx))
.use('/messages', memory());

service = app.service('messages').rx();
Expand All @@ -27,7 +28,7 @@ describe('reactive lists', () => {
describe('custom id', function () {
beforeEach(done => {
app = feathers()
.configure(rx())
.configure(rx(Rx))
.use('/messages', memory({ idField: 'customId' }));

service = app.service('messages').rx({idField: 'customId'});
Expand All @@ -43,7 +44,7 @@ describe('reactive lists', () => {
describe('pagination', function () {
beforeEach(done => {
app = feathers()
.configure(rx())
.configure(rx(Rx))
.use('/messages', memory({ paginate: { default: 3 }}));

service = app.service('messages').rx();
Expand All @@ -61,8 +62,8 @@ describe('reactive lists', () => {
describe('default', function () {
beforeEach(done => {
app = feathers()
.configure(rx({
listStrategy: rx.strategy.always
.configure(rx(Rx, {
listStrategy: 'always'
}))
.use('/messages', memory());

Expand All @@ -79,8 +80,8 @@ describe('reactive lists', () => {
describe('custom id', function () {
beforeEach(done => {
app = feathers()
.configure(rx({
listStrategy: rx.strategy.always
.configure(rx(Rx, {
listStrategy: 'always'
}))
.use('/messages', memory({ idField: 'customId' }));

Expand All @@ -97,8 +98,8 @@ describe('reactive lists', () => {
describe('pagination', function () {
beforeEach(done => {
app = feathers()
.configure(rx({
listStrategy: rx.strategy.always
.configure(rx(Rx, {
listStrategy: 'always'
}))
.use('/messages', memory({ paginate: { default: 3 }}));

Expand Down
7 changes: 4 additions & 3 deletions test/resource.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Rx from 'rxjs/Rx';
import assert from 'assert';
import feathers from 'feathers';
import memory from 'feathers-memory';
Expand All @@ -11,7 +12,7 @@ describe('reactive resources', () => {
describe('standard id', function () {
beforeEach(done => {
app = feathers()
.configure(rx())
.configure(rx(Rx))
.use('/messages', memory());

service = app.service('messages').rx({idField: 'customId'});
Expand All @@ -29,7 +30,7 @@ describe('reactive resources', () => {
describe('custom id on service', function () {
beforeEach(done => {
app = feathers()
.configure(rx())
.configure(rx(Rx))
.use('/messages', memory({ idField: 'customId' }));

service = app.service('messages').rx({ idField: 'customId' });
Expand All @@ -48,7 +49,7 @@ describe('reactive resources', () => {
describe('custom id on params', function () {
beforeEach(done => {
app = feathers()
.configure(rx())
.configure(rx(Rx))
.configure(hooks())
.use('/messages', memory({ idField: 'customId' }));

Expand Down