From 8f7a71f7188442102f96fcd9b25c8b12aa8dd242 Mon Sep 17 00:00:00 2001 From: Genuifx Date: Sun, 1 Mar 2020 20:28:52 +0800 Subject: [PATCH] feat(redux): offer a method to get store instance --- packages/wxa-redux/src/index.js | 11 +++++++++++ packages/wxa-redux/test/wxa-redux.test.js | 6 +++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/packages/wxa-redux/src/index.js b/packages/wxa-redux/src/index.js index 6bd8f510..587b4476 100644 --- a/packages/wxa-redux/src/index.js +++ b/packages/wxa-redux/src/index.js @@ -70,6 +70,7 @@ let mountRedux = function (originHook) { connectState(); } }); + // 直接挂载一次数据,这样子onLoad阶段可以直接使用现有数据 connectState(); } if (originHook) originHook.apply(this, args); @@ -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 = []; @@ -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) { diff --git a/packages/wxa-redux/test/wxa-redux.test.js b/packages/wxa-redux/test/wxa-redux.test.js index 0da1ca30..4727fd2c 100644 --- a/packages/wxa-redux/test/wxa-redux.test.js +++ b/packages/wxa-redux/test/wxa-redux.test.js @@ -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'; @@ -359,6 +359,10 @@ describe('while dispatch long long data', ()=>{ }) }) +test('get store instance', () => { + expect(getStore()).not.toBeFalsy(); +}) + afterAll(()=>{ global.getApp = og; }) \ No newline at end of file