Skip to content

Commit

Permalink
Merge pull request #6 from feathersjs/revert-2-strategy
Browse files Browse the repository at this point in the history
Revert "Added Strategy support"
  • Loading branch information
daffl committed Apr 28, 2016
2 parents 819c50b + 1ac5fdd commit 8f8b206
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 206 deletions.
8 changes: 1 addition & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ import reactiveList from './list';

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

function FeathersRx(options) {
export default function(options) {
options = Object.assign({
id: 'id',
// Whether to requery service when a change is detected
strategy: reactiveList.strategy.never,
// The merging strategy
merge(current, eventData) {
return Object.assign({}, current, eventData);
Expand Down Expand Up @@ -48,7 +46,3 @@ function FeathersRx(options) {
app.mixins.push(mixin);
};
}

FeathersRx.strategy = reactiveList.strategy;

export default FeathersRx;
83 changes: 25 additions & 58 deletions src/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ import 'rxjs/add/operator/exhaustMap';
import { promisify } from './utils';
import { sorter as createSorter, matcher } from 'feathers-commons/lib/utils';

function List (events, options) {

return function (params) {
export default function(events, options) {
return function(params) {
const query = Object.assign({}, params.query);
const result = this._super.apply(this, arguments);
const inputArgs = arguments;

if(typeof result.then !== 'function') {
return result;
Expand All @@ -23,61 +21,30 @@ function List (events, options) {
// The sort function (if $sort is set)
const sorter = query.$sort ? createSorter(query.$sort) : null;

let stream;

if (options.strategy === List.strategy.never) {
stream = 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.id] !== current[options.id])
),
updaters.map(eventData =>
items => items.map(current => {
if(eventData[options.id] === current[options.id]) {
return options.merge(current, eventData);
}

return current;
}).filter(matches)
)
).scan((current, callback) => {
const result = callback(current);
return sorter ? result.sort(sorter) : result;
}, data)
));
} else if (options.strategy === List.strategy.always) {
stream = source.concat(source.exhaustMap(() =>
Rx.Observable.merge(
events.created.filter(matches),
events.removed,
updaters
).flatMap(() => {
const result = this.find.apply(this, inputArgs);
const source = Rx.Observable.fromPromise(result);
if(typeof result.then !== 'function') {
return Rx.Observable.of(result);
}
return source.map((result) => {
return sorter ? result.sort(sorter) : result;
});
})
));
} else {
throw 'Unsupported feathers-rx strategy type.';
}
const stream = 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.id] !== current[options.id])
),
updaters.map(eventData =>
items => items.map(current => {
if(eventData[options.id] === current[options.id]) {
return options.merge(current, eventData);
}

return current;
}).filter(matches)
)
).scan((current, callback) => {
const result = callback(current);

return sorter ? result.sort(sorter) : result;
}, data)
));

return promisify(stream, result);
};
}

List.strategy = {
always: {},
never: {},
// TODO: Jack
// smart: {}
};

export default List;
Loading

0 comments on commit 8f8b206

Please sign in to comment.