Skip to content

Commit

Permalink
test specs and fixed async dispatch
Browse files Browse the repository at this point in the history
  • Loading branch information
tur-nr committed Mar 20, 2017
1 parent f0905ef commit 614a7aa
Show file tree
Hide file tree
Showing 16 changed files with 782 additions and 550 deletions.
3 changes: 3 additions & 0 deletions .babelrc
Expand Up @@ -5,5 +5,8 @@
"node": "current"
}
}]
],
"plugins": [
"transform-object-rest-spread"
]
}
1 change: 0 additions & 1 deletion .npmignore
Expand Up @@ -6,6 +6,5 @@ test
.gitignore
.npmignore
.travis.yml
bower.json
gulpfile.babel.js
wct.conf.json
18 changes: 12 additions & 6 deletions .travis.yml
@@ -1,21 +1,27 @@
os:
- linux

language: node_js
sudo: true
dist: trusty

node_js: 4
cache:
directories:
- node_modules

addons:
firefox: 46.0
apt:
sources:
- google-chrome
packages:
- google-chrome-stable

cache:
directories:
- node_modules

before_script:
- bower install
- npm run lint

script:
- xvfb-run npm test

after_script:
- cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js
73 changes: 66 additions & 7 deletions dist/polymer-redux.html
@@ -1,8 +1,38 @@
<html><head><script type="text/javascript">var PolymerRedux = (function () {
'use strict';

var commonjsGlobal = typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};





function createCommonjsModule(fn, module) {
return module = { exports: {} }, fn(module, module.exports), module.exports;
}

var window_1 = createCommonjsModule(function (module) {
if (typeof window !== "undefined") {
module.exports = window;
} else if (typeof commonjsGlobal !== "undefined") {
module.exports = commonjsGlobal;
} else if (typeof self !== "undefined") {
module.exports = self;
} else {
module.exports = {};
}
});

var console_1 = console;

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }

// Expose globals
var CustomEvent = window_1.CustomEvent;
var Polymer = window_1.Polymer;

/**
* Polymer Redux
*
Expand All @@ -11,7 +41,16 @@
* @param {Object} store Redux store.
* @return {Function} Class mixin.
*/

function PolymerRedux(store) {
if (!store) {
throw new TypeError('PolymerRedux: expecting a redux store.');
} else if (!['getState', 'dispatch', 'subscribe'].every(function (k) {
return typeof store[k] === 'function';
})) {
throw new TypeError('PolymerRedux: invalid store object.');
}

var subscribers = new Map();

/**
Expand All @@ -35,7 +74,7 @@
var property = properties[name];
if (Object.prototype.hasOwnProperty.call(property, 'statePath')) {
if (!property.readOnly && property.notify) {
console.warn('PolymerRedux: <' + element.constructor.is + '>.' + name + ' has "notify" enabled, two-way bindings goes against Redux\'s paradigm');
console_1.warn('PolymerRedux: <' + element.constructor.is + '>.' + name + ' has "notify" enabled, two-way bindings goes against Redux\'s paradigm');
}
return true;
}
Expand Down Expand Up @@ -127,6 +166,7 @@

var actions = collect(this.constructor, 'actions');
Object.defineProperty(this, '_reduxActions', {
configurable: true,
value: actions
});
}
Expand Down Expand Up @@ -154,21 +194,40 @@
* @return {Object} The action.
*/
dispatch() {
var actions = this._reduxActions;

// Action creator
var _this = this;

for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}

var actions = this._reduxActions;

// Action creator
var action = args[0];

if (typeof action === 'string') {
if (!actions || typeof actions[action] !== 'function') {
throw new TypeError('PolymerRedux: <' + this.constructor.is + '> has no action creator "' + action + '"');
if (!actions) {
throw new TypeError('PolyerRedux: <' + this.constructor.is + '> has no actions defined.');
} else if (typeof actions[action] !== 'function') {
throw new TypeError('PolymerRedux: <' + this.constructor.is + '> invalid action creator "' + action + '"');
}
action = actions[action].apply(this.constructor, args.slice(1));
action = actions[action].apply(actions, _toConsumableArray(args.slice(1)));
}

// Proxy async dispatch
if (typeof action === 'function') {
var originalAction = action;
action = function action() {
for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
args[_key2] = arguments[_key2];
}

// Replace redux dispatch
args.splice(0, 1, function () {
return _this.dispatch.apply(_this, arguments);
});
return originalAction.apply(undefined, args);
};
}

return store.dispatch(action);
Expand Down
9 changes: 9 additions & 0 deletions gulpfile.babel.js
Expand Up @@ -3,6 +3,8 @@ import gulp from 'gulp';
import del from 'del';
import sequence from 'run-sequence';
import {rollup} from 'rollup';
import nodeResolve from 'rollup-plugin-node-resolve';
import commonjs from 'rollup-plugin-commonjs';
import babelRollup from 'rollup-plugin-babel';
import {Bundler} from 'polymer-bundler';
import {serialize} from 'parse5';
Expand All @@ -18,6 +20,13 @@ gulp.task('build:lib', () => {
return rollup({
entry: 'src/index.js',
plugins: [
nodeResolve({
jsnext: true,
main: true
}),
commonjs({
include: 'node_modules/**'
}),
babelRollup({
babelrc: false,
presets: [['env', {
Expand Down
73 changes: 66 additions & 7 deletions lib/index.js
@@ -1,8 +1,38 @@
var PolymerRedux = (function () {
'use strict';

var commonjsGlobal = typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};





function createCommonjsModule(fn, module) {
return module = { exports: {} }, fn(module, module.exports), module.exports;
}

var window_1 = createCommonjsModule(function (module) {
if (typeof window !== "undefined") {
module.exports = window;
} else if (typeof commonjsGlobal !== "undefined") {
module.exports = commonjsGlobal;
} else if (typeof self !== "undefined") {
module.exports = self;
} else {
module.exports = {};
}
});

var console_1 = console;

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }

// Expose globals
var CustomEvent = window_1.CustomEvent;
var Polymer = window_1.Polymer;

/**
* Polymer Redux
*
Expand All @@ -11,7 +41,16 @@ var _extends = Object.assign || function (target) { for (var i = 1; i < argument
* @param {Object} store Redux store.
* @return {Function} Class mixin.
*/

function PolymerRedux(store) {
if (!store) {
throw new TypeError('PolymerRedux: expecting a redux store.');
} else if (!['getState', 'dispatch', 'subscribe'].every(function (k) {
return typeof store[k] === 'function';
})) {
throw new TypeError('PolymerRedux: invalid store object.');
}

var subscribers = new Map();

/**
Expand All @@ -35,7 +74,7 @@ function PolymerRedux(store) {
var property = properties[name];
if (Object.prototype.hasOwnProperty.call(property, 'statePath')) {
if (!property.readOnly && property.notify) {
console.warn('PolymerRedux: <' + element.constructor.is + '>.' + name + ' has "notify" enabled, two-way bindings goes against Redux\'s paradigm');
console_1.warn('PolymerRedux: <' + element.constructor.is + '>.' + name + ' has "notify" enabled, two-way bindings goes against Redux\'s paradigm');
}
return true;
}
Expand Down Expand Up @@ -127,6 +166,7 @@ function PolymerRedux(store) {

var actions = collect(this.constructor, 'actions');
Object.defineProperty(this, '_reduxActions', {
configurable: true,
value: actions
});
}
Expand Down Expand Up @@ -154,21 +194,40 @@ function PolymerRedux(store) {
* @return {Object} The action.
*/
dispatch() {
var actions = this._reduxActions;

// Action creator
var _this = this;

for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}

var actions = this._reduxActions;

// Action creator
var action = args[0];

if (typeof action === 'string') {
if (!actions || typeof actions[action] !== 'function') {
throw new TypeError('PolymerRedux: <' + this.constructor.is + '> has no action creator "' + action + '"');
if (!actions) {
throw new TypeError('PolyerRedux: <' + this.constructor.is + '> has no actions defined.');
} else if (typeof actions[action] !== 'function') {
throw new TypeError('PolymerRedux: <' + this.constructor.is + '> invalid action creator "' + action + '"');
}
action = actions[action].apply(this.constructor, args.slice(1));
action = actions[action].apply(actions, _toConsumableArray(args.slice(1)));
}

// Proxy async dispatch
if (typeof action === 'function') {
var originalAction = action;
action = function action() {
for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
args[_key2] = arguments[_key2];
}

// Replace redux dispatch
args.splice(0, 1, function () {
return _this.dispatch.apply(_this, arguments);
});
return originalAction.apply(undefined, args);
};
}

return store.dispatch(action);
Expand Down

0 comments on commit 614a7aa

Please sign in to comment.