Skip to content

Commit ba01956

Browse files
create _onFlushEffects event
1 parent 52c75b8 commit ba01956

File tree

4 files changed

+24
-13
lines changed

4 files changed

+24
-13
lines changed

src/useDynamicTabs/useDynamicTabs.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11

2-
import React, { useReducer, useLayoutEffect, useRef } from "react";
2+
import React, { useState, useReducer, useLayoutEffect, useRef } from "react";
33
function useDynamicTabs(getDeps, options = {}) {
44
const { reducer, getApiInstance, PanelList, TabList, ApiContext, StateContext, ForceUpdateContext } = getDeps();
55
const ref = useRef(null);
66
if (ref.current === null)
77
ref.current = { api: getApiInstance(options), TabListComponent: null, PanelListComponent: null };
88
const { current: { api } } = ref
99
, _ref = ref.current
10-
, [state, dispatch] = useReducer(reducer, api.getInitialState());
11-
api.updateStateRef(state).updateDispatchRef(dispatch);
10+
, [state, dispatch] = useReducer(reducer, api.getInitialState())
11+
, [flushState, setFlushState] = useState({});
12+
api.updateStateRef(state, dispatch).updateFlushState(setFlushState);
1213
useLayoutEffect(() => {
1314
api.trigger('onLoad', api.userProxy);
1415
return () => { api.trigger('onDestroy', api.userProxy); };
@@ -19,6 +20,9 @@ function useDynamicTabs(getDeps, options = {}) {
1920
useLayoutEffect(() => {
2021
api.trigger('onInit', api.userProxy);
2122
});
23+
useLayoutEffect(() => {
24+
api.trigger('_onFlushEffects', api.userProxy, { currentData: api.getCopyData() });
25+
}, [flushState]);
2226
useLayoutEffect(() => {
2327
const oldState = api.getCopyPerviousData()
2428
, [openedTabsId, closedTabsId] = api.helper.getArraysDiff(state.openTabIDs, oldState.openTabIDs)

src/utils/api/api.factory.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ const _apiProps = {
2626
_subscribeCallbacksOptions: function () {
2727
const op = this.optionsManager.options;
2828
Object.keys(this._publishers).map(eventName => {
29-
this.on(eventName, function () {
30-
op[eventName].apply(this, arguments);
31-
});
29+
if (eventName[0] !== '_')
30+
this.on(eventName, function () {
31+
op[eventName].apply(this, arguments);
32+
});
3233
});
3334
return this;
3435
},
@@ -46,7 +47,7 @@ const _apiProps = {
4647
isSelected: function (id = missingParamEr('isSelected')) { return this.stateRef.selectedTabID == id; },
4748
isOpen: function (id = missingParamEr('isOpen')) { return this.stateRef.openTabIDs.indexOf(id) >= 0; },
4849
_getOnChangePromise: function () {
49-
return new (Promise)(resolve => { this.one('onChange', () => { resolve.call(this.userProxy); }); });
50+
return new (Promise)(resolve => { this.one('_onFlushEffects', function () { resolve.apply(this, arguments); }); });
5051
},
5152
select: function (id = missingParamEr('select')) {
5253
const result = this._getOnChangePromise();

src/utils/api/baseApi.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,30 @@ function BaseApi(helper) {
66
this._initialState = null;
77
this._perviousState = null;// it is a pervious value of this._state
88
this._dispatch = null;
9+
this._setFlushState = null;
910
helper.setNoneEnumProps(this, {
1011
forceUpdateState: {},
1112
stateRef: {}// have a same reference with state . It will be updated in each execution of useDynamicTabs.js
1213
});
1314
}
14-
BaseApi.prototype._select = function (tabId) { this._dispatch({ type: actions.active, tabId }); };
15-
BaseApi.prototype._close = function (tabId) { this._dispatch({ type: actions.close, tabId }); };
16-
BaseApi.prototype._open = function (tabId) { this._dispatch({ type: actions.open, tabId }); };
15+
BaseApi.prototype._select = function (tabId) { this._dispatch({ type: actions.active, tabId }); this.__flushEffects(); };
16+
BaseApi.prototype._close = function (tabId) { this._dispatch({ type: actions.close, tabId }); this.__flushEffects(); };
17+
BaseApi.prototype._open = function (tabId) { this._dispatch({ type: actions.open, tabId }); this.__flushEffects(); };
1718
BaseApi.prototype._refresh = function () {
1819
this.forceUpdateState = {};
1920
this._dispatch({ type: actions.refresh });
21+
this.__flushEffects();
22+
};
23+
BaseApi.prototype.__flushEffects = function () {
24+
this._setFlushState({});
2025
};
2126
Helper.setNoneEnumProps(BaseApi.prototype, {
22-
updateStateRef: function (state) { this.stateRef = state; return this; },
23-
updateDispatchRef: function (dispatch) { this._dispatch = dispatch; return this; },
27+
updateStateRef: function (state, dispatch) { this.stateRef = state; this._dispatch = dispatch; return this; },
2428
updateState: function (state) {
2529
this._perviousState = this._helper.getCopyState(this._state || state);
2630
this._state = this._helper.getCopyState(state);
2731
return this;
28-
}
32+
},
33+
updateFlushState: function (setFlushState) { this._setFlushState = setFlushState; return this; }
2934
});
3035
export default BaseApi;

src/utils/api/pub_sub.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const Pub_Sub = function () {
88
, onClose: []
99
, onSelect: []
1010
, onInit: []
11+
, _onFlushEffects: []
1112
, onFirstSelect: []
1213
};
1314
};

0 commit comments

Comments
 (0)