Skip to content

Commit

Permalink
Now use babel to compile library before publishing to npm
Browse files Browse the repository at this point in the history
  • Loading branch information
Akryum committed Sep 22, 2016
1 parent a0b6e98 commit bfa303f
Show file tree
Hide file tree
Showing 5 changed files with 388 additions and 133 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules/
lib/
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
src/
299 changes: 167 additions & 132 deletions lib/vue-plugin.js
Original file line number Diff line number Diff line change
@@ -1,160 +1,198 @@
const omit = require('lodash.omit');
'use strict';

let apolloClient = null;
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

class DollarApollo {
constructor(vm) {
this.vm = vm;
this.querySubscriptions = {};
}
var _lodash = require('lodash.omit');

get client() {
return apolloClient;
}
var _lodash2 = _interopRequireDefault(_lodash);

get query() {
return this.client.query;
}
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

watchQuery(options) {
const vm = this.vm;
const observable = this.client.watchQuery(options);
const _subscribe = observable.subscribe.bind(observable);
observable.subscribe = (function(options) {
let sub = _subscribe(options);
vm._apolloSubscriptions.push(sub);
return sub;
}).bind(observable);
return observable;
}
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

get mutate() {
return this.client.mutate;
}
var apolloClient = null;

option(key, options) {
const vm = this.vm;
const $apollo = this;
var DollarApollo = function () {
function DollarApollo(vm) {
_classCallCheck(this, DollarApollo);

let query, observer, sub;
this.vm = vm;
this.querySubscriptions = {};
}

let firstLoadingDone = false;
_createClass(DollarApollo, [{
key: 'watchQuery',
value: function watchQuery(options) {
var vm = this.vm;
var observable = this.client.watchQuery(options);
var _subscribe = observable.subscribe.bind(observable);
observable.subscribe = function (options) {
var sub = _subscribe(options);
vm._apolloSubscriptions.push(sub);
return sub;
}.bind(observable);
return observable;
}
}, {
key: 'option',
value: function option(key, options) {
var vm = this.vm;
var $apollo = this;

let loadingKey = options.loadingKey;
let loadingChangeCb = options.watchLoading;
var query = void 0,
observer = void 0,
sub = void 0;

if (typeof loadingChangeCb === 'function') {
loadingChangeCb = loadingChangeCb.bind(vm);
}
var firstLoadingDone = false;

// Simple query
if (!options.query) {
query = options;
}
var loadingKey = options.loadingKey;
var loadingChangeCb = options.watchLoading;

function generateApolloOptions(variables) {
const apolloOptions = omit(options, [
'variables',
'watch',
'update',
'result',
'error',
'loadingKey',
'watchLoading',
]);
apolloOptions.variables = variables;
return apolloOptions;
}
if (typeof loadingChangeCb === 'function') {
loadingChangeCb = loadingChangeCb.bind(vm);
}

function q(variables) {
applyLoadingModifier(1);
// Simple query
if (!options.query) {
query = options;
}

if (options.forceFetch && observer) {
// Refresh query
observer.refetch(variables, {
forceFetch: !!options.forceFetch
});
} else {
if (sub) {
sub.unsubscribe();
}
function generateApolloOptions(variables) {
var apolloOptions = (0, _lodash2.default)(options, ['variables', 'watch', 'update', 'result', 'error', 'loadingKey', 'watchLoading']);
apolloOptions.variables = variables;
return apolloOptions;
}

// Create observer
observer = $apollo.watchQuery(generateApolloOptions(variables));
function q(variables) {
applyLoadingModifier(1);

if (options.forceFetch && observer) {
// Refresh query
observer.refetch(variables, {
forceFetch: !!options.forceFetch
});
} else {
if (sub) {
sub.unsubscribe();
}

// Create observer
observer = $apollo.watchQuery(generateApolloOptions(variables));

// Create subscription
sub = observer.subscribe({
next: nextResult,
error: catchError
});
}
}

// Create subscription
sub = observer.subscribe({
next: nextResult,
error: catchError
if (typeof options.variables === 'function') {
vm.$watch(options.variables.bind(vm), q, {
immediate: true
});
} else {
q(options.variables);
}
}

if (typeof options.variables === 'function') {
vm.$watch(options.variables.bind(vm), q, {
immediate: true
});
} else {
q(options.variables);
}
function nextResult(_ref) {
var data = _ref.data;

function nextResult({ data }) {
applyData(data);
}
applyData(data);
}

function applyData(data) {
loadingDone();
function applyData(data) {
loadingDone();

if (typeof options.update === 'function') {
vm[key] = options.update.call(vm, data);
} else if (data[key] === undefined) {
console.error(`Missing ${key} attribute on result`, data);
} else {
vm[key] = data[key];
}
if (typeof options.update === 'function') {
vm[key] = options.update.call(vm, data);
} else if (data[key] === undefined) {
console.error('Missing ' + key + ' attribute on result', data);
} else {
vm[key] = data[key];
}

if (typeof options.result === 'function') {
options.result.call(vm, data);
if (typeof options.result === 'function') {
options.result.call(vm, data);
}
}
}

function applyLoadingModifier(value) {
if (loadingKey) {
vm[loadingKey] += value;
}
function applyLoadingModifier(value) {
if (loadingKey) {
vm[loadingKey] += value;
}

if (loadingChangeCb) {
loadingChangeCb(value === 1, value);
if (loadingChangeCb) {
loadingChangeCb(value === 1, value);
}
}
}

function loadingDone() {
if (!firstLoadingDone) {
applyLoadingModifier(-1);
firstLoadingDone = true;
function loadingDone() {
if (!firstLoadingDone) {
applyLoadingModifier(-1);
firstLoadingDone = true;
}
}
}

function catchError(error) {
loadingDone();

if (error.graphQLErrors && error.graphQLErrors.length !== 0) {
console.error(`GraphQL execution errors for query ${query}`);
for (let e of error.graphQLErrors) {
console.error(e);
function catchError(error) {
loadingDone();

if (error.graphQLErrors && error.graphQLErrors.length !== 0) {
console.error('GraphQL execution errors for query ' + query);
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;

try {
for (var _iterator = error.graphQLErrors[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var e = _step.value;

console.error(e);
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
} else if (error.networkError) {
console.error('Error sending the query ' + query, error.networkError);
} else {
console.error(error);
}
} else if (error.networkError) {
console.error(`Error sending the query ${query}`, error.networkError);
} else {
console.error(error);
}

if (typeof options.error === 'function') {
options.error(error);
if (typeof options.error === 'function') {
options.error(error);
}
}
}
}
}
}, {
key: 'client',
get: function get() {
return apolloClient;
}
}, {
key: 'query',
get: function get() {
return this.client.query;
}
}, {
key: 'mutate',
get: function get() {
return this.client.mutate;
}
}]);

return DollarApollo;
}();

function prepare() {
this._apolloSubscriptions = [];
Expand All @@ -163,15 +201,13 @@ function prepare() {
}

function launch() {
let apollo = this.$options.apollo;
var apollo = this.$options.apollo;

if (apollo) {
const queries = omit(apollo, [
'subscribe',
]);
var queries = (0, _lodash2.default)(apollo, ['subscribe']);

// watchQuery
for (let key in queries) {
for (var key in queries) {
this.$apollo.option(key, queries[key]);
}

Expand All @@ -183,7 +219,7 @@ function launch() {
}

module.exports = {
install(Vue, options) {
install: function install(Vue, options) {

apolloClient = options.apolloClient;

Expand All @@ -196,8 +232,8 @@ module.exports = {

created: launch,

destroyed: function() {
this._apolloSubscriptions.forEach((sub) => {
destroyed: function destroyed() {
this._apolloSubscriptions.forEach(function (sub) {
sub.unsubscribe();
});
this._apolloSubscriptions = null;
Expand All @@ -207,6 +243,5 @@ module.exports = {
}

});

}
};
};
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
{
"name": "vue-apollo",
"version": "1.0.0-beta10",
"version": "1.0.0-beta11",
"description": "Vue apollo integration",
"main": "index.js",
"scripts": {
"compile": "babel --presets es2015 -d lib/ src/",
"prepublish": "npm run compile",
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
Expand All @@ -26,5 +28,9 @@
},
"dependencies": {
"lodash.omit": "^4.5.0"
},
"devDependencies": {
"babel-cli": "^6.14.0",
"babel-preset-es2015": "^6.14.0"
}
}
Loading

0 comments on commit bfa303f

Please sign in to comment.