Skip to content

Commit

Permalink
feat(redux): offer a method to get store instance
Browse files Browse the repository at this point in the history
  • Loading branch information
Genuifx committed Mar 1, 2020
1 parent f7665ab commit 8f7a71f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
11 changes: 11 additions & 0 deletions packages/wxa-redux/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ let mountRedux = function (originHook) {
connectState();
}
});
// 直接挂载一次数据,这样子onLoad阶段可以直接使用现有数据
connectState();
}
if (originHook) originHook.apply(this, args);
Expand All @@ -86,6 +87,14 @@ let unmountRedux = function (originUnmount) {
}
}

let _store;

export function getStore() {
if (_store == null) console.warn('%c[@wxa/redux] store is null, initial redux plugin first in app.wxa', 'font-size: 12px; color: red;');

return _store;
}

export const wxaRedux = (options = {}) => {
// get options.
let args = [];
Expand All @@ -111,6 +120,8 @@ export const wxaRedux = (options = {}) => {
// create Store directly;
// cause the reducer may be attached at subpackages.
let store = createStore.apply(null, args);
_store = store;

reducerRegistry.setChangeListener((reducer)=>{
let reducers = combine(reducer, userReducers);
if(debug) {
Expand Down
6 changes: 5 additions & 1 deletion packages/wxa-redux/test/wxa-redux.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {wxaRedux, combineReducers, reducerRegistry} from '../src/index'
import {wxaRedux, combineReducers, reducerRegistry, getStore} from '../src/index'
import fs from 'fs';
import path from 'path';

Expand Down Expand Up @@ -359,6 +359,10 @@ describe('while dispatch long long data', ()=>{
})
})

test('get store instance', () => {
expect(getStore()).not.toBeFalsy();
})

afterAll(()=>{
global.getApp = og;
})

0 comments on commit 8f7a71f

Please sign in to comment.