Skip to content

Commit

Permalink
new build with rollup & relocated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bsaphier committed Sep 21, 2018
1 parent 26caa8a commit faf819c
Show file tree
Hide file tree
Showing 15 changed files with 197 additions and 10 deletions.
21 changes: 19 additions & 2 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
{
"presets": [ "@babel/preset-env", "@babel/preset-react" ],
"presets": [
["@babel/preset-env", {
"modules": false,
"targets": {
"browsers": ["last 2 Chrome versions", "safari >=11"]
}
}],
"@babel/preset-react"
],
"plugins": [
"@babel/proposal-class-properties",
"@babel/proposal-object-rest-spread"
]
],
"env": {
"test": {
"presets": ["@babel/preset-env", "@babel/preset-react"],
"plugins": [
"@babel/proposal-class-properties",
"@babel/proposal-object-rest-spread"
]
}
}
}
1 change: 1 addition & 0 deletions dist/index.es.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

65 changes: 65 additions & 0 deletions lib/RRWA-component.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

/* Export everything, for testing. */
import { Component } from 'react';
import { connect } from 'react-redux';
import { clearEvtQueue } from './action-creators';
export class RRWA extends Component {
constructor(props) {
super(props);

_defineProperty(this, "processEvent", ({
event
}) => {
event(this.audioContext, this.getCurrTime);
});

_defineProperty(this, "getCurrTime", () => {
return this.audioContext.currentTime;
});

const AudioContext = window.AudioContext || window.webkitAudioContext;

if (AudioContext) {
this.audioContext = new AudioContext();
} else {
throw new Error('This environment does not support the web audio API.');
}
}

componentDidMount() {
if (this.props.events.length) {
this.props.events.forEach(this.processEvent);
this.props.clearQ();
}
}

shouldComponentUpdate(nextProps) {
return nextProps.events.length > 0;
}

componentDidUpdate() {
if (this.props.events.length) {
this.props.events.forEach(this.processEvent);
this.props.clearQ();
}
}

componentWillUnmount() {
this.audioContext.close();
}

render() {
return null;
}

}
export const mapState = ({
webAudioReducer
}) => _objectSpread({}, webAudioReducer);
export const mapDispatch = dispatch => ({
clearQ: () => dispatch(clearEvtQueue())
});
export default connect(mapState, mapDispatch)(RRWA);
14 changes: 14 additions & 0 deletions lib/action-creators.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { QUEUE_EVENT, CLEAR_EVT_QUEUE } from './constants';
export const clearEvtQueue = () => ({
type: CLEAR_EVT_QUEUE
});
/**
* `event` can be a single event or an array of events. An array passed as
* `event` will be concatenated with the current event queue.
* @param {Event | Event[]} event
*/

export const emit = event => ({
type: QUEUE_EVENT,
event
});
5 changes: 5 additions & 0 deletions lib/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/** @constant */
export const QUEUE_EVENT = 'QUEUE_EVENT';
/** @constant */

export const CLEAR_EVT_QUEUE = 'CLEAR_EVT_QUEUE';
4 changes: 4 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import * as actionCreators from './action-creators';
import webAudioReducer from './web-audio-reducer';
import RRWAEngine from './RRWA-component';
export { RRWAEngine, actionCreators, webAudioReducer };
45 changes: 45 additions & 0 deletions lib/web-audio-reducer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { QUEUE_EVENT, CLEAR_EVT_QUEUE } from './constants';
/** @constant */

const INIT_STATE = {
events: []
};

const webAudioReducer = (state = INIT_STATE, action) => {
const nextState = {
events: [...state.events]
};

switch (action.type) {
case QUEUE_EVENT:
queueEvent(state, action, nextState);
break;

case CLEAR_EVT_QUEUE:
nextState.events = [];
break;

default:
break;
}

return nextState;
};

export default webAudioReducer;

function queueEvent(state, action, nextState) {
if (Array.isArray(action.event)) {
action.event.forEach((event, idx) => {
nextState.events.push({
key: state.events.length + idx,
event: event
});
});
} else {
nextState.events.push({
key: state.events.length,
event: action.event
});
}
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"jest": {
"verbose": true,
"collectCoverage": true,
"setupTestFrameworkScriptFile": "./src/test/setup.js"
"setupTestFrameworkScriptFile": "./test/setup.js"
},
"devDependencies": {
"@babel/cli": "^7.1.0",
Expand Down Expand Up @@ -68,6 +68,7 @@
"webaudio",
"web-audio",
"webaudioapi",
"react-redux",
"web-audio-api"
]
}
35 changes: 35 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import babel from 'rollup-plugin-babel';
import resolve from 'rollup-plugin-node-resolve';
import commonjs from 'rollup-plugin-commonjs';
import filesize from 'rollup-plugin-filesize';
import { terser } from 'rollup-plugin-terser';

const env = process.env.NODE_ENV;
const pkg = require('./package.json');

export default {
input: 'src/index.js',
output: {
file: {
es: pkg.module,
cjs: pkg.main
}[env],
format: env
},
plugins: [
resolve({
customResolveOptions: {
moduleDirectory: 'node_modules'
}
}),
babel({
exclude: 'node_modules/**',
plugins: ['@babel/external-helpers'],
externalHelpers: true
}),
commonjs(),
terser(),
filesize()
],
external: ['react', 'redux', 'react-redux']
};
3 changes: 1 addition & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import * as actionCreators from './action-creators';
import webAudioReducer from './web-audio-reducer';
import RRWAEngine from './RRWA-component';


module.exports = {
export {
RRWAEngine,
actionCreators,
webAudioReducer
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import TestRenderer from 'react-test-renderer';
import { RRWA, mapState, mapDispatch } from '../RRWA-component';
import { RRWA, mapState, mapDispatch } from '../src/RRWA-component';

class MockAudioContext {
constructor() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as actions from '../action-creators';
import * as types from '../constants';
import * as actions from '../src/action-creators';
import * as types from '../src/constants';


describe('actions: ', () => {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import webAudioReducer from '../web-audio-reducer';
import * as types from '../constants';
import webAudioReducer from '../src/web-audio-reducer';
import * as types from '../src/constants';

const event1 = function() {};
const event2 = function() {};
Expand Down

0 comments on commit faf819c

Please sign in to comment.