Skip to content

Commit c415394

Browse files
author
Oguz Gelal
committed
prettier format. fixes with declarations
1 parent 4c072e1 commit c415394

23 files changed

+2220
-271
lines changed

package.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@
3636
"babel-plugin-transform-react-jsx": "^6.4.0",
3737
"benchmark": "^2.1.4",
3838
"cross-env": "^5.1.4",
39+
"eslint": "^4.19.1",
3940
"immutadot": "^1.0.0",
40-
"jsdoc": "^3.5.5",
41+
"lint-staged": "^7.1.1",
4142
"lodash": "^4.17.10",
4243
"prettier": "1.12.1",
4344
"rimraf": "^2.6.2",
@@ -56,16 +57,15 @@
5657
"build:umd": "cross-env BABEL_ENV=rollup NODE_ENV=development rollup -c",
5758
"build:umd:min": "cross-env BABEL_ENV=rollup NODE_ENV=production rollup -c",
5859
"build": "npm run clean && npm run build:commonjs && npm run build:es && npm run build:umd && npm run build:umd:min",
59-
"prettier": "./node_modules/.bin/prettier --config .prettierrc --write src/**/*.js",
6060
"clean": "rimraf dist"
6161
},
6262
"lint-staged": {
63-
"*.{js,json,css,md}": [
64-
"npm run prettier",
63+
"{src,test}/**/*.js": [
64+
"./node_modules/.bin/prettier --config .prettierrc --write {src,test}/**/*.js",
6565
"git add"
6666
]
6767
},
6868
"files": [
6969
"dist"
7070
]
71-
}
71+
}

src/broadcasts/broadcast.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,21 @@ import { BEFORE_BROADCAST, AFTER_BROADCAST } from '../middlewares/hookTypes';
88
const broadcast = (eventKey, payload, options = {}) => {
99
executeHooks({ id: BEFORE_BROADCAST }, eventKey, payload);
1010

11-
const newState = invokeDeclaration({
11+
invokeDeclaration({
1212
declaration: (ctx.onEvent || {})[eventKey] || null,
1313
eventKey,
14-
payload,
15-
})
14+
payload
15+
});
1616

1717
executeHooks({ id: AFTER_BROADCAST }, eventKey, payload);
18-
}
18+
};
1919

2020
export default (eventKey, payload, options = {}) => {
21-
const opts = Object.assign(defaultOptions, options)
21+
const opts = Object.assign(defaultOptions, options);
2222

2323
if (opts.defer) {
24-
setTimeout(() => broadcast(eventKey, payload, opts))
24+
setTimeout(() => broadcast(eventKey, payload, opts));
2525
} else {
26-
broadcast(eventKey, payload, opts)
26+
broadcast(eventKey, payload, opts);
2727
}
2828
};

src/broadcasts/broadcastHelpers.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ import { fail } from '../utils/alert';
22

33
export const validateBroadcastDeclaration = ({ declaration }) => {
44
if (!declaration.on) {
5-
fail('Broadcast channel declarations should have at least one trigger', 'f2+fpC38gEa8+V');
5+
fail(
6+
'Broadcast channel declarations should have at least one trigger',
7+
'f2+fpC38gEa8+V'
8+
);
69
}
710
if (typeof declaration.on !== 'string' && !Array.isArray(declaration.on)) {
811
fail(

src/config/broadcastDefaults.js

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,26 @@
11
export default {
2-
32
/**
43
* If true, the broadcast will take effect after the current event
54
* loop tick - basically be wrapped with setSimeout(___, 0).
6-
*
5+
*
76
* This option is good for high frequency events such as "on key pressed"
87
* when you want the broadcast to be non-blocking.
9-
*
8+
*
109
* Type <bool>
1110
* Default is false
1211
*/
1312

1413
defer: false,
1514

16-
1715
/**
1816
* TODO: awaiting implementation
19-
*
17+
*
2018
* Debounces the execution of a broadcast. This option should is good
2119
* executing effects on high frequency effects (ie. autocomplete)
22-
*
20+
*
2321
* Type <int> (milliseconds)
2422
* Default is null
2523
*/
2624

27-
debounce: null,
28-
}
25+
debounce: null
26+
};

src/config/constants.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
21
// context of a declaration
32
export const DECLARATION_BROADCAST = 'DECLARATION_BROADCAST';
4-
export const DECLARATION_SUBSCRIPTION = 'DECLARATION_SUBSCRIPTION';
3+
export const DECLARATION_SUB = 'DECLARATION_SUB';
4+
export const DECLARATION_SUB_IMMEDIATE = 'DECLARATION_SUB_IMMEDIATE';

src/config/declarationDefaults.js

+7-9
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,28 @@
11
export default {
2-
32
/**
43
* TODO: awaiting implementation
5-
*
4+
*
65
* If true, the broadcast will take effect after the current event
76
* loop tick - basically be wrapped with setSimeout(___, 0).
8-
*
7+
*
98
* This option is good for high frequency events such as "on key pressed"
109
* when you want the broadcast to be non-blocking.
11-
*
10+
*
1211
* Type <bool>
1312
* Default is false
1413
*/
1514

1615
defer: false,
1716

18-
1917
/**
2018
* TODO: awaiting implementation
21-
*
19+
*
2220
* Debounces the execution of a broadcast. This option should is good
2321
* executing effects on high frequency effects (ie. autocomplete)
24-
*
22+
*
2523
* Type <int> (milliseconds)
2624
* Default is null
2725
*/
2826

29-
debounce: null,
30-
}
27+
debounce: null
28+
};

src/config/reclareDefaults.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
export default {
2-
}
1+
export default {};

src/ctx/startReclare.js

+28-9
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,19 @@ import { validateConfiguration } from './ctxHelpers';
66
import { validateBroadcastDeclaration } from '../broadcasts/broadcastHelpers';
77
import { validateSubscriptionDeclaration } from '../subscriptions/subscriptionHelpers';
88

9-
import { BEFORE_START, BEFORE_STATE, AFTER_START } from '../middlewares/hookTypes';
10-
import { DECLARATION_BROADCAST, DECLARATION_SUBSCRIPTION } from '../config/constants'
9+
import {
10+
BEFORE_START,
11+
BEFORE_STATE,
12+
AFTER_START
13+
} from '../middlewares/hookTypes';
1114

12-
export default (config) => {
15+
import {
16+
DECLARATION_BROADCAST,
17+
DECLARATION_SUB,
18+
DECLARATION_SUB_IMMEDIATE
19+
} from '../config/constants';
20+
21+
export default config => {
1322
validateConfiguration(config);
1423

1524
/**
@@ -37,29 +46,39 @@ export default (config) => {
3746
ctx.started = true;
3847

3948
// Initialise the settings
40-
ctx.settings = Object.assign(reclareDefaults, config.options || {})
49+
ctx.settings = Object.assign(reclareDefaults, config.options || {});
4150

4251
// Initialise the state
4352
ctx.state = config.initialState || {};
4453

45-
// Set broadcast channel declarations
54+
// Set on broadcasts
4655
ctx.onEvent = parseDeclarations({
4756
type: DECLARATION_BROADCAST,
4857
declarations: config.onEvent,
4958
customValidateDeclaration: validateBroadcastDeclaration,
5059
customValidateSituation: null,
5160
customValidateReducer: null,
52-
customValidateReaction: null,
61+
customValidateReaction: null
62+
});
63+
64+
// Run right after reducers changes the state (before reactions)
65+
ctx.onImmediateStateChange = parseDeclarations({
66+
type: DECLARATION_SUB_IMMEDIATE,
67+
declarations: config.onStateChange,
68+
customValidateDeclaration: validateSubscriptionDeclaration,
69+
customValidateSituation: null,
70+
customValidateReducer: null,
71+
customValidateReaction: null
5372
});
5473

55-
// Set state change declarations
74+
// Run when state changed and the declaration finishes its execution (after reactions)
5675
ctx.onStateChange = parseDeclarations({
57-
type: DECLARATION_SUBSCRIPTION,
76+
type: DECLARATION_SUB,
5877
declarations: config.onStateChange,
5978
customValidateDeclaration: validateSubscriptionDeclaration,
6079
customValidateSituation: null,
6180
customValidateReducer: null,
62-
customValidateReaction: null,
81+
customValidateReaction: null
6382
});
6483

6584
executeHooks({ id: AFTER_START });

src/declarations/invokeDeclaration.js

+33-59
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,15 @@
1-
/**
2-
* declaration - [{ situations, reactions, reactionsElse, ...etc }]
3-
* declarationObject - { situations, reactions, reactionsElse, ...etc }
4-
*/
51
import ctx from '../ctx';
6-
import evaluateSituations from '../situations/evaluateSituations';
2+
73
import invokeReactions from '../reactions/invokeReactions';
84
import invokeReducers from '../reducers/invokeReducers';
95
import executeHooks from '../middlewares/executeHooks';
10-
import getState from '../../state/getState';
11-
12-
import {
13-
DECLARATION_HIT,
14-
DECLARATION_MISS,
15-
DECLARATION_TRIGGERED,
16-
} from '../middlewares/hookTypes';
17-
18-
const invokeDeclarationObject = ({ declarationObject, eventKey, payload }) => {
19-
20-
const {
21-
unparsed,
22-
situations,
23-
reducers,
24-
reducersElse,
25-
reactions,
26-
reactionsElse,
27-
} = declarationObject;
6+
import invokeDeclarationObject from './invokeDeclarationObject';
287

29-
executeHooks({ id: DECLARATION_TRIGGERED }, eventKey, payload, unparsed)
30-
31-
const situationHolds = evaluateSituations({
32-
situations,
33-
eventKey,
34-
payload,
35-
});
36-
37-
return {
38-
reducers: situationHolds ? reducers : reducersElse,
39-
reactions: situationHolds ? reactions : reactionsElse,
40-
}
41-
}
42-
43-
export default ({
44-
declaration,
45-
eventKey,
46-
payload,
47-
}) => {
8+
import { DECLARATION_HIT, DECLARATION_MISS } from '../middlewares/hookTypes';
489

10+
// `eventKey` and `payload` gets populated when declaration is invoked from a broadcast
11+
// `prevState` gets populated when declaration is invoked from a subscription
12+
export default ({ declaration, eventKey, payload, prevState }) => {
4913
let reducerQueue = [];
5014
let reactionQueue = [];
5115

@@ -56,31 +20,41 @@ export default ({
5620
}
5721

5822
// run through the declarations on an event
59-
(declaration || []).map(
60-
declarationObject => {
61-
const { reducers, reactions } = invokeDeclarationObject({
62-
declarationObject, eventKey, payload,
63-
})
64-
reducerQueue = reducerQueue.concat(reducers || []);
65-
reactionQueue = reactionQueue.concat(reactions || []);
66-
}
67-
)
23+
(declaration || []).map(declarationObject => {
24+
// run a declaration - evaluate situations,
25+
// get reactions / reducers if it holds
26+
const { reducers, reactions } = invokeDeclarationObject({
27+
declarationObject,
28+
prevState,
29+
eventKey,
30+
payload
31+
});
32+
33+
// queue reducers / reactions to run after all
34+
// situations are evaluated
35+
reducerQueue = reducerQueue.concat(reducers || []);
36+
reactionQueue = reactionQueue.concat(reactions || []);
37+
});
6838

69-
// keep the old state
70-
const prevState = ctx.state;
39+
// keep the old state
40+
const stateBeforeReducers = ctx.state;
7141

7242
// execute reducers in the queue
7343
invokeReducers({
7444
reducers: reducerQueue,
7545
eventKey,
76-
payload,
77-
})
46+
payload
47+
});
48+
49+
// TODO: immediate subscriptions
7850

7951
// execute reactions in the queue, pass also the old state
8052
invokeReactions({
8153
reactions: reactionQueue,
82-
prevState,
54+
prevState: stateBeforeReducers,
8355
eventKey,
84-
payload,
85-
})
86-
}
56+
payload
57+
});
58+
59+
// TODO: subscriptions
60+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// declaration - [{ situations, reactions, reactionsElse, ...etc }]
2+
// declarationObject - { situations, reactions, reactionsElse, ...etc }
3+
4+
import evaluateSituations from '../situations/evaluateSituations';
5+
import executeHooks from '../middlewares/executeHooks';
6+
import { DECLARATION_TRIGGERED } from '../middlewares/hookTypes';
7+
8+
export default ({ declarationObject, prevState, eventKey, payload }) => {
9+
const {
10+
unparsed,
11+
situations,
12+
reducers,
13+
reducersElse,
14+
reactions,
15+
reactionsElse
16+
} = declarationObject;
17+
18+
executeHooks({ id: DECLARATION_TRIGGERED }, eventKey, payload, unparsed);
19+
20+
const situationHolds = evaluateSituations({
21+
situations,
22+
prevState,
23+
eventKey,
24+
payload
25+
});
26+
27+
return {
28+
reducers: situationHolds ? reducers : reducersElse,
29+
reactions: situationHolds ? reactions : reactionsElse
30+
};
31+
};

0 commit comments

Comments
 (0)