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

Revert "Added Strategy support" #6

Merged
merged 1 commit into from
Apr 28, 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
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