2015-10-02 18:47:36 +00:00
|
|
|
import Rx from 'rx';
|
|
|
|
import debugFactory from 'debug';
|
2015-06-21 02:55:22 +00:00
|
|
|
|
2015-10-02 18:47:36 +00:00
|
|
|
const debug = debugFactory('freecc:rxUtils');
|
|
|
|
|
|
|
|
export function saveInstance(instance) {
|
2015-06-21 02:55:22 +00:00
|
|
|
return new Rx.Observable.create(function(observer) {
|
2015-06-25 22:03:46 +00:00
|
|
|
if (!instance || typeof instance.save !== 'function') {
|
|
|
|
debug('no instance or save method');
|
2015-06-21 02:55:22 +00:00
|
|
|
observer.onNext();
|
|
|
|
return observer.onCompleted();
|
|
|
|
}
|
2015-06-25 22:03:46 +00:00
|
|
|
instance.save(function(err, savedInstance) {
|
2015-06-21 02:55:22 +00:00
|
|
|
if (err) {
|
|
|
|
return observer.onError(err);
|
|
|
|
}
|
2015-06-25 22:03:46 +00:00
|
|
|
debug('instance saved');
|
|
|
|
observer.onNext(savedInstance);
|
2015-06-21 02:55:22 +00:00
|
|
|
observer.onCompleted();
|
|
|
|
});
|
|
|
|
});
|
2015-10-02 18:47:36 +00:00
|
|
|
}
|
2015-06-21 02:55:22 +00:00
|
|
|
|
2015-06-25 22:03:46 +00:00
|
|
|
// alias saveInstance
|
2015-10-02 18:47:36 +00:00
|
|
|
export const saveUser = saveInstance;
|
2015-06-25 22:03:46 +00:00
|
|
|
|
2015-10-02 18:47:36 +00:00
|
|
|
export function observeQuery(Model, method, query) {
|
|
|
|
return Rx.Observable.fromNodeCallback(Model[method], Model)(query);
|
|
|
|
}
|
2015-06-23 02:23:07 +00:00
|
|
|
|
2015-10-02 18:47:36 +00:00
|
|
|
export function observeMethod(context, methodName) {
|
2015-06-29 16:31:42 +00:00
|
|
|
return Rx.Observable.fromNodeCallback(context[methodName], context);
|
2015-10-02 18:47:36 +00:00
|
|
|
}
|