@@ -6,10 +6,19 @@ import { validateConfiguration } from './ctxHelpers';
6
6
import { validateBroadcastDeclaration } from '../broadcasts/broadcastHelpers' ;
7
7
import { validateSubscriptionDeclaration } from '../subscriptions/subscriptionHelpers' ;
8
8
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' ;
11
14
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 => {
13
22
validateConfiguration ( config ) ;
14
23
15
24
/**
@@ -37,29 +46,39 @@ export default (config) => {
37
46
ctx . started = true ;
38
47
39
48
// Initialise the settings
40
- ctx . settings = Object . assign ( reclareDefaults , config . options || { } )
49
+ ctx . settings = Object . assign ( reclareDefaults , config . options || { } ) ;
41
50
42
51
// Initialise the state
43
52
ctx . state = config . initialState || { } ;
44
53
45
- // Set broadcast channel declarations
54
+ // Set on broadcasts
46
55
ctx . onEvent = parseDeclarations ( {
47
56
type : DECLARATION_BROADCAST ,
48
57
declarations : config . onEvent ,
49
58
customValidateDeclaration : validateBroadcastDeclaration ,
50
59
customValidateSituation : null ,
51
60
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
53
72
} ) ;
54
73
55
- // Set state change declarations
74
+ // Run when state changed and the declaration finishes its execution (after reactions)
56
75
ctx . onStateChange = parseDeclarations ( {
57
- type : DECLARATION_SUBSCRIPTION ,
76
+ type : DECLARATION_SUB ,
58
77
declarations : config . onStateChange ,
59
78
customValidateDeclaration : validateSubscriptionDeclaration ,
60
79
customValidateSituation : null ,
61
80
customValidateReducer : null ,
62
- customValidateReaction : null ,
81
+ customValidateReaction : null
63
82
} ) ;
64
83
65
84
executeHooks ( { id : AFTER_START } ) ;
0 commit comments