Skip to content

Commit 894309b

Browse files
committed
Merge pull request #181 from optimizely/jordan/refactor-everything!
Refactor reactor, evaluator and change observer for 1.2
2 parents 10f6f2f + ba88051 commit 894309b

28 files changed

+1613
-919
lines changed

.eslintrc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22
"env": {
33
"node": true,
44
"browser": true,
5-
"amd": true
5+
"amd": true,
6+
"es6": true
67
},
78
"ecmaFeatures": {
89
"arrowFunctions": true,
910
"classes": true,
1011
"defaultParams": true,
1112
"destructuring": true,
1213
"forOf": true,
14+
"modules": true,
1315
"objectLiteralShorthandMethods": true,
1416
"objectLiteralShorthandProperties": true,
1517
"spread": true,
@@ -58,7 +60,7 @@
5860
"use-isnan": 1,
5961
"valid-jsdoc": 0,
6062
"valid-typeof": 1,
61-
"block-scoped-var": 1,
63+
"block-scoped-var": 0,
6264
"complexity": 0,
6365
"consistent-return": 0,
6466
"curly": 1,

docs/src/docs/07-api.md

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,6 @@ Dispatches a message to all registered Stores. This process is done synchronousl
2929
reactor.dispatch('addUser', { name: 'jordan' })
3030
```
3131

32-
#### `Reactor#batch(fn)`
33-
34-
_added in 1.1_
35-
36-
Allows multiple dispatches within the `fn` function before notifying any observers.
37-
38-
```javascript
39-
reactor.batch(function() {
40-
reactor.dispatch('addUser', { name: 'jordan' })
41-
reactor.dispatch('addUser', { name: 'james' })
42-
})
43-
44-
// does a single notify to all observers
45-
```
46-
4732
#### `Reactor#evaluate(Getter | KeyPath)`
4833

4934
Returns the immutable value for some KeyPath or Getter in the reactor state. Returns `undefined` if a keyPath doesn't have a value.
@@ -85,6 +70,41 @@ reactor.observe([
8570
])
8671
```
8772
73+
#### `Reactor#batch(fn)`
74+
75+
_added in 1.1_
76+
77+
Allows multiple dispatches within the `fn` function before notifying any observers.
78+
79+
```javascript
80+
reactor.batch(function() {
81+
reactor.dispatch('addUser', { name: 'jordan' })
82+
reactor.dispatch('addUser', { name: 'james' })
83+
})
84+
85+
// does a single notify to all observers
86+
```
87+
88+
#### `Reactor#batchStart()`
89+
90+
_added in 1.2_
91+
92+
Sets the reactor in batch mode, where dispatches don't cause observer notification until `batchEnd()` is called.
93+
94+
```javascript
95+
// the following is equivalent to the `reactor.batch` example
96+
reactor.batchStart()
97+
reactor.dispatch('addUser', { name: 'jordan' })
98+
reactor.dispatch('addUser', { name: 'james' })
99+
reactor.batchEnd()
100+
```
101+
102+
#### `Reactor#batchEnd()`
103+
104+
_added in 1.2_
105+
106+
Signifies the end of reactor batching and will notify all observers of the changes that happened since `batchStart`
107+
88108
#### `Reactor#serialize()`
89109
90110
_added in 1.1_

grunt/karma.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ module.exports = {
2323
webpack: {
2424
module: {
2525
loaders: [
26-
{ test: /\.js$/, loader: 'jstransform-loader' },
26+
{ test: /\.js$/, loader: 'babel-loader' },
2727
],
2828
},
2929
},
@@ -37,6 +37,10 @@ module.exports = {
3737
autoWatch: false,
3838

3939
singleRun: true,
40+
41+
client: {
42+
captureConsole: false,
43+
},
4044
},
4145

4246
phantom: {
@@ -66,7 +70,7 @@ module.exports = {
6670
webpack: {
6771
module: {
6872
loaders: [
69-
{ test: /\.js$/, loader: 'jstransform-loader' },
73+
{ test: /\.js$/, loader: 'babel-loader' },
7074
],
7175
postLoaders: [
7276
{

grunt/sauce.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
var each = require('../src/utils').each
21
var sauceConfig = {
32
username: 'nuclearjs',
43
accessKey: process.env.SAUCE_ACCESS_KEY,
@@ -71,7 +70,9 @@ var batches = {
7170
},
7271
}
7372

74-
each(batches, function(value, key) {
73+
for (var key in batches) { // eslint-disable-line guard-for-in
74+
var value = batches[key]
75+
7576
exports[key] = {
7677
sauceLabs: sauceConfig,
7778
// mobile emulators are really slow
@@ -81,4 +82,4 @@ each(batches, function(value, key) {
8182
browsers: Object.keys(value),
8283
reporters: ['progress', 'saucelabs'],
8384
}
84-
})
85+
}

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
},
3232
"devDependencies": {
3333
"grunt": "^0.4.5",
34+
"babel-core": "^5.8.29",
35+
"babel-loader": "^5.3.2",
3436
"grunt-contrib-clean": "^0.6.0",
3537
"grunt-eslint": "^14.0.0",
3638
"grunt-githooks": "^0.3.1",

src/change-observer.js

Lines changed: 0 additions & 98 deletions
This file was deleted.

src/create-react-mixin.js

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,44 @@
1-
var each = require('./utils').each
1+
import { each } from './utils'
2+
3+
/**
4+
* Returns a mapping of the getDataBinding keys to
5+
* the reactor values
6+
*/
7+
function getState(reactor, data) {
8+
let state = {}
9+
each(data, (value, key) => {
10+
state[key] = reactor.evaluate(value)
11+
})
12+
return state
13+
}
14+
215
/**
316
* @param {Reactor} reactor
417
*/
5-
module.exports = function(reactor) {
18+
export default function(reactor) {
619
return {
7-
getInitialState: function() {
20+
getInitialState() {
821
return getState(reactor, this.getDataBindings())
922
},
1023

11-
componentDidMount: function() {
12-
var component = this
13-
component.__unwatchFns = []
14-
each(this.getDataBindings(), function(getter, key) {
15-
var unwatchFn = reactor.observe(getter, function(val) {
16-
var newState = {}
17-
newState[key] = val
18-
component.setState(newState)
24+
componentDidMount() {
25+
this.__unwatchFns = []
26+
each(this.getDataBindings(), (getter, key) => {
27+
const unwatchFn = reactor.observe(getter, (val) => {
28+
this.setState({
29+
[key]: val
30+
})
1931
})
2032

21-
component.__unwatchFns.push(unwatchFn)
33+
this.__unwatchFns.push(unwatchFn)
2234
})
2335
},
2436

25-
componentWillUnmount: function() {
37+
componentWillUnmount() {
2638
while (this.__unwatchFns.length) {
2739
this.__unwatchFns.shift()()
2840
}
2941
},
3042
}
3143
}
3244

33-
/**
34-
* Returns a mapping of the getDataBinding keys to
35-
* the reactor values
36-
*/
37-
function getState(reactor, data) {
38-
var state = {}
39-
each(data, function(value, key) {
40-
state[key] = reactor.evaluate(value)
41-
})
42-
return state
43-
}

0 commit comments

Comments
 (0)