Skip to content

Instantly share code, notes, and snippets.

@sean-nicholas
Created March 24, 2018 12:41
Show Gist options
  • Select an option

  • Save sean-nicholas/224f26944a0e3ed9439daf4115687ac4 to your computer and use it in GitHub Desktop.

Select an option

Save sean-nicholas/224f26944a0e3ed9439daf4115687ac4 to your computer and use it in GitHub Desktop.
feathers-reactive with Angular Universal - rx universal
import { Observable } from 'rxjs/Observable'
export function rxUniversal(options = {}) {
const mixin = function (service) {
const app = this
const serviceMixin = {
watch(watchOptions = {}) {
const methods = {}
app.methods.forEach(method => {
if (typeof service[method] === 'function') {
methods[method] = (...args) => {
const serviceMethod = service[method].bind(service)
return Observable.from(serviceMethod(...args))
}
}
})
return methods
}
}
// get the extended service object
const newService = service.mixin(serviceMixin)
// workaround for Firefox
// FF defines Object.prototype.watch(), so uberproto doesn't recognize the mixin's .watch()
// see https://github.com/feathersjs-ecosystem/feathers-reactive/issues/67
if ((Object.prototype as any).watch && (Object.prototype as any).watch === newService.watch) {
newService.watch = serviceMixin.watch
}
}
return function (app) {
app.mixins.push(mixin)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment