Skip to content

Commit

Permalink
Update examples and add clearHistory
Browse files Browse the repository at this point in the history
  • Loading branch information
mbasso committed Jul 8, 2016
1 parent 729c1c9 commit 3c2720a
Show file tree
Hide file tree
Showing 25 changed files with 176 additions and 45 deletions.
1 change: 1 addition & 0 deletions docs/Summary.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* [publish(channel, param)](/docs/api/Publish.md)
* [setHistoryLimit(limit = 200)](/docs/api/SetHistoryLimit.md)
* [getHistory()](/docs/api/GetHistory.md)
* [clearHistory()](/docs/api/ClearHistory.md)
* [Change Log](/CHANGELOG.md)
* [Authors](/docs/Authors.md)
* [Chat](/docs/Chat.md)
22 changes: 22 additions & 0 deletions docs/api/ClearHistory.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# `clearHistory()`

Clear the history of published events.

#### Example

Assuming that our application has published one event, here is an example.

```js
let history = refractionInstance.getHistory();
/* [{
type: 'newMessage',
payload: {
chatroom: 'refraction-chatroom',
sender: 'example_user',
message: 'Hi all!',
},
}] */
refractionInstance.clearHistory();
history = refractionInstance.getHistory();
/* [] */
```
2 changes: 1 addition & 1 deletion docs/api/GetHistory.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# `getHistory()`

Return the history of published events.
Returns the history of published events.

#### Returns

Expand Down
1 change: 1 addition & 0 deletions docs/api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Here is the list of methods that you can use on its instance:
- [publish(channel, param)](/Publish.md)
- [setHistoryLimit(limit)](/SetHistoryLimit.md)
- [getHistory()](/GetHistory.md)
- [clearHistory()](/ClearHistory.md)

### Importing

Expand Down
19 changes: 14 additions & 5 deletions examples/ReactSimple/components/App.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
/* eslint-disable import/no-unresolved */
import React from 'react';
import InputContainer from '../containers/InputContainer';
import Label from './Label';
import InputName from '../containers/inputs/InputName';
import InputSurname from '../containers/inputs/InputSurname';
import InputAddress from '../containers/inputs/InputAddress';
import LabelContainer from '../containers/LabelContainer';
import PlayButton from '../containers/PlayButton';
import PlayButton from '../containers/buttons/PlayButton';
import ClearButton from '../containers/buttons/ClearButton';
/* eslint-enable import/no-unresolved */

export default () => (
<div>
<p>Write something in the input field and then Click the button</p>
<InputContainer />
<p>Write your data and click play.</p>
<InputName label="Name: " />
<InputSurname label="Surname: " />
<InputAddress label="Address: " />
<Label text="Name: " />
<LabelContainer />
<PlayButton />
<br />
<PlayButton text="Play!" />
<ClearButton text="Clear History" />
</div>
);
4 changes: 2 additions & 2 deletions examples/ReactSimple/components/Button.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ export default class Button extends React.Component {
}

static defaultProps = {
text: 'Play!',
text: '',
}

render() {
const { text, ...others } = this.props;
return (
<button {...others} >{text}</button>
<button style={{ marginRight: '4px' }} {...others} >{text}</button>
);
}
}
10 changes: 8 additions & 2 deletions examples/ReactSimple/components/Input.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
/* eslint-disable import/no-unresolved */
import React from 'react';
import Label from './Label';
/* eslint-enable import/no-unresolved */

export default class extends React.Component {
export default class Input extends React.Component {

static propTypes = {
value: React.PropTypes.string.isRequired,
label: React.PropTypes.string,
}

static defaultProps = {
value: '',
}

render() {
const { label, ...others } = this.props;
return (
<input {...this.props} />
<div>
<Label text={label} />
<input style={{ marginLeft: 8 }} {...others} />
</div>
);
}
}
5 changes: 4 additions & 1 deletion examples/ReactSimple/components/Label.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@
import React from 'react';
/* eslint-enable import/no-unresolved */

export default class extends React.Component {
export default class Label extends React.Component {

static propTypes = {
text: React.PropTypes.string.isRequired,
}

static defaultProps = {
text: '',
style: {
display: 'inline-block',
},
}

render() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import Button from '../components/Button';
import { connect } from 'refraction-react';
/* eslint-enable import/no-unresolved */

export default connect({
export default (func) => connect({
actions: {
onClick: 'play',
onClick: func,
},
})(Button);
6 changes: 3 additions & 3 deletions examples/ReactSimple/containers/InputContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import Input from '../components/Input';
import { connect } from 'refraction-react';
/* eslint-enable import/no-unresolved */

export default connect({
export default (changeEvent) => connect({
actions: {
onChange: 'onInputChange',
onChange: changeEvent,
},
subscriptions: {
// update his own value
onInputChange: ({ payload }) => ({ value: payload }),
[changeEvent]: ({ payload }) => ({ value: payload }),
},
})(Input);
2 changes: 1 addition & 1 deletion examples/ReactSimple/containers/LabelContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ import { connect } from 'refraction-react';

export default connect({
subscriptions: {
onInputChange: ({ payload }) => ({ text: payload }),
onNameChange: ({ payload }) => ({ text: payload }),
},
})(Label);
3 changes: 3 additions & 0 deletions examples/ReactSimple/containers/buttons/ClearButton.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import getButtonContainer from '../ButtonContainer';

export default getButtonContainer('clear');
3 changes: 3 additions & 0 deletions examples/ReactSimple/containers/buttons/PlayButton.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import getButtonContainer from '../ButtonContainer';

export default getButtonContainer('play');
3 changes: 3 additions & 0 deletions examples/ReactSimple/containers/inputs/InputAddress.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import getInputContainer from '../InputContainer';

export default getInputContainer('onAddressChange');
3 changes: 3 additions & 0 deletions examples/ReactSimple/containers/inputs/InputName.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import getInputContainer from '../InputContainer';

export default getInputContainer('onNameChange');
3 changes: 3 additions & 0 deletions examples/ReactSimple/containers/inputs/InputSurname.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import getInputContainer from '../InputContainer';

export default getInputContainer('onSurnameChange');
4 changes: 2 additions & 2 deletions examples/ReactSimple/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "refraction-react-example",
"version": "1.0.0",
"version": "1.1.0",
"description": "Refraction example using react and player",
"scripts": {
"start": "node server.js",
Expand All @@ -22,7 +22,7 @@
"react-dom": "15.1.0",
"refraction-react": "1.0.0",
"refraction-player": "1.0.0",
"refraction": "1.0.0"
"refraction": "1.1.0"
},
"devDependencies": {
"babel-core": "6.3.15",
Expand Down
29 changes: 26 additions & 3 deletions examples/ReactSimple/refraction/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,43 @@ import play from 'refraction-player';

class Refract extends Refraction {

onInputChange = (e) => {
this.publish('onInputChange', {
onNameChange = (e) => {
this.publishEvent(e, 'onNameChange');
}

onSurnameChange = (e) => {
this.publishEvent(e, 'onSurnameChange');
}

onAddressChange = (e) => {
this.publishEvent(e, 'onAddressChange');
}

publishEvent = (e, channel) => {
this.publish(channel, {
type: 'event',
payload: e,
});
}

play = () => {
const track = [...this.getHistory()];

// clear fields
this.onNameChange('');
this.onSurnameChange('');
this.onAddressChange('');

play({
refraction: this,
track: this.getHistory(),
track,
});
}

clear = () => {
this.clearHistory();
}

}

export default new Refract(middlewares);
12 changes: 8 additions & 4 deletions examples/RxSimple/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
</head>
<body>
<div id="root">
<p>Write something in the input field and then Click the button</p>
<input id="input" value="" />
<p id="label"></p>
<button id="button">Play!</button>
<p>Write your data and click play.</p>
<p>Name: <input id="name" value="" /></p>
<p>Surname: <input id="surname" value="" /></p>
<p>Address: <input id="address" value="" /></p>
<p>Name: <span id="label"></span></p>
<br />
<button class="button" id="playButton">Play!</button>
<button class="button" id="clearButton">Clear History</button>
</div>
<script src="/static/bundle.js"></script>
</body>
Expand Down
37 changes: 24 additions & 13 deletions examples/RxSimple/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,34 @@ import refraction from './refraction/';
/* eslint-enable import/no-unresolved */

(() => {
const input = document.getElementById('input');
input.onInputChange = ({ payload }) => {
input.value = payload;
const setButton = (name, action) => {
const button = document.getElementById(name);
button.onclick = refraction[action];
};
refraction.subscribe(input);

const setInput = (name, subscription) => {
const input = document.getElementById(name);
input[subscription] = ({ payload }) => {
input.value = payload;
};
refraction.subscribe(input);

Rx.Observable
.fromEvent(input, 'keydown')
.debounce(500)
.subscribe(refraction[subscription]);
};

setInput('name', 'onNameChange');
setInput('surname', 'onSurnameChange');
setInput('address', 'onAddressChange');

setButton('playButton', 'play');
setButton('clearButton', 'clear');

const label = document.getElementById('label');
label.onInputChange = ({ payload }) => {
label.onNameChange = ({ payload }) => {
label.innerHTML = payload;
};
refraction.subscribe(label);

const button = document.getElementById('button');
button.onclick = refraction.play;

Rx.Observable
.fromEvent(input, 'keydown')
.debounce(500)
.subscribe(refraction.onInputChange);
})();
4 changes: 2 additions & 2 deletions examples/RxSimple/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "refraction-rx-example",
"version": "1.0.0",
"version": "1.1.0",
"description": "Refraction example using Rx",
"scripts": {
"start": "node server.js",
Expand All @@ -19,7 +19,7 @@
"dependencies": {
"rx": "4.1.0",
"refraction-player": "1.0.0",
"refraction": "1.0.0"
"refraction": "1.1.0"
},
"devDependencies": {
"babel-core": "6.3.15",
Expand Down
29 changes: 26 additions & 3 deletions examples/RxSimple/refraction/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,43 @@ import play from 'refraction-player';

class Refract extends Refraction {

onInputChange = (e) => {
this.publish('onInputChange', {
onNameChange = (e) => {
this.publishEvent(e, 'onNameChange');
}

onSurnameChange = (e) => {
this.publishEvent(e, 'onSurnameChange');
}

onAddressChange = (e) => {
this.publishEvent(e, 'onAddressChange');
}

publishEvent = (e, channel) => {
this.publish(channel, {
type: 'event',
payload: e,
});
}

play = () => {
const track = [...this.getHistory()];

// clear fields
this.onNameChange('');
this.onSurnameChange('');
this.onAddressChange('');

play({
refraction: this,
track: this.getHistory(),
track,
});
}

clear = () => {
this.clearHistory();
}

}

export default new Refract(middlewares);
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "refraction",
"version": "1.0.1",
"version": "1.1.0",
"description": "A guard that represent central point of control in your application.",
"main": "lib/index.js",
"jsnext:main": "es/index.js",
Expand Down
Loading

0 comments on commit 3c2720a

Please sign in to comment.