Created
December 10, 2016 10:32
-
-
Save psi-4ward/b262e72ce6f4fa3e536d6448483e5dbe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import feathersClient from '../../../be/node_modules/feathers/client'; | |
import socketioClient from '../../../be/node_modules/feathers-socketio/client'; | |
import ioClient from '../../../be/node_modules/socket.io-client'; | |
import authentication from '../../../be/node_modules/feathers-authentication/client'; | |
import hooks from '../../../be/node_modules/feathers-hooks'; | |
import findIndex from 'lodash/findIndex'; | |
import find from 'lodash/find'; | |
import Vue from 'vue'; | |
export const errorHandlers = []; | |
export const requestInterceptors = []; | |
export const responseInterceptors = []; | |
export const socketio = ioClient('http://psi-pc:3030'); | |
// DEBUG: localStorage.debug = 'feather*'; | |
export const feathers = feathersClient() | |
.configure(socketioClient(socketio)) | |
.configure(hooks()) | |
.configure(authentication({storage: window.localStorage})) | |
.configure(function() { | |
const app = this; | |
app.mixins.push(function(service) { | |
// monky patch send() to fetch errors | |
const oldSend = service.send; | |
service.send = function(...args) { | |
return oldSend.apply(service, args) | |
.catch(e => { | |
errorHandlers.forEach(h => h(e, service, args)); | |
throw e; // re-throw error | |
}); | |
}; | |
// inject upsert method | |
service.upsert = function(data) { | |
if(data._id) return service.update(data._id, data); | |
else return service.create(data); | |
}; | |
// inject subscribe method | |
service.subscribe = function(target, component) { | |
function created(item) { | |
target.push(item); | |
} | |
function updated(item) { | |
let i = findIndex(target, {_id: item._id}); | |
target.splice(i < 0 ? target.length - 1 : i, i < 0 ? 0 : 1, item); | |
} | |
function patched(item) { | |
let i = findIndex(target, {_id: item._id}); | |
target.splice(i < 0 ? target.length - 1 : i, i < 0 ? 0 : 1, item); | |
} | |
function removed(item) { | |
let i = findIndex(target, {_id: item._id}); | |
if(i > -1) target.splice(i, 1); | |
} | |
service.on('created', created); | |
service.on('updated', updated); | |
service.on('patched', patched); | |
service.on('removed', removed); | |
function unsubscribe() { | |
service.off('created', created); | |
service.off('updated', updated); | |
service.off('patched', patched); | |
service.off('removed', removed); | |
} | |
// remove eventListener on component destroy | |
if(component !== undefined && !component.__proto__ instanceof Vue) { | |
throw new Error('First param of subscribe has to be VueComponent instance!'); | |
} else { | |
if(!component._events['hook:beforeDestroy']) component._events['hook:beforeDestroy'] = []; | |
component._events['hook:beforeDestroy'].push(unsubscribe); | |
} | |
return unsubscribe; | |
}; | |
service.before(function(hook) { | |
// Apply all request interceptors | |
requestInterceptors.forEach(h => h(hook, service)); | |
return hook; | |
}); | |
service.after(function(hook) { | |
// apply all response interceptors | |
responseInterceptors.forEach(h => h(hook, service)); | |
return hook; | |
}); | |
}); | |
}); | |
// TODO: remember me (see Login.vue) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment