Skip to content

Commit

Permalink
fix(ConfigProvider): Isolate multiple context data
Browse files Browse the repository at this point in the history
Closes #43
  • Loading branch information
Macrox committed Nov 13, 2018
1 parent 86e0f83 commit 5afa5df
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 12 deletions.
17 changes: 11 additions & 6 deletions docs/dialog/demo/quick.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ The Dialog provides quick methods called alert and confirm, as well as a lower-l
---

````jsx
import { Button, Dialog } from '@alifd/next';
import { Button, Dialog, ConfigProvider } from '@alifd/next';


const popupAlert = () => {
Expand Down Expand Up @@ -47,9 +47,14 @@ const popupCustom = () => {
});
};

ReactDOM.render(<span>
<Button onClick={popupAlert}>Alert</Button> &nbsp;
<Button onClick={popupConfirm}>Confirm</Button> &nbsp;
<Button onClick={popupCustom}>Custom</Button>
</span>, mountNode);
ReactDOM.render(
<ConfigProvider locale={{ Dialog: { ok: 'OK', cancel: 'Cancel' } }}>
<span>
<Button onClick={popupAlert}>Alert</Button> &nbsp;
<Button onClick={popupConfirm}>Confirm</Button> &nbsp;
<Button onClick={popupCustom}>Custom</Button>
</span>
</ConfigProvider>,
mountNode
);
````
44 changes: 44 additions & 0 deletions src/config-provider/cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
class Cache {
constructor() {
this._root = null;
this._store = new Map();
}

empty() {
return this._store.size === 0;
}

has(key) {
return this._store.has(key)
}

get(key, defaultValue) {
const res = this.has(key) ? this._store.get(key) : this.root()
return (typeof res === 'undefined' || res === null) ?
defaultValue : res
}

add(key, value) {
const store = this._store;
if (this.empty()) {
this._root = key;
}
this._store.set(key, value);
}

update(key, value) {
if (this.has(key)) {
this._store.set(key, value)
}
}

remove(key) {
this._store.delete(key);
}

root() {
return this._store.get(this._root)
}
}

export default Cache;
19 changes: 13 additions & 6 deletions src/config-provider/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import PropTypes from 'prop-types';
import getContextProps from './get-context-props';
import { config, initLocales, setLanguage, setLocale, getLocale, getLanguage } from './config';
import Consumer from './consumer';
import Cache from './cache'

let childContextCache = {};
let childContextCache = new Cache();

/**
* ConfigProvider
Expand Down Expand Up @@ -62,7 +63,7 @@ class ConfigProvider extends Component {
* @returns {Object} 新的 context props
*/
static getContextProps = (props, displayName) => {
return getContextProps(props, childContextCache, displayName);
return getContextProps(props, childContextCache.root(), displayName);
};

static initLocales = initLocales;
Expand All @@ -73,7 +74,7 @@ class ConfigProvider extends Component {
static Consumer = Consumer;

static getContext = () => {
const { nextPrefix, nextLocale, nextPure, nextWarning } = childContextCache;
const { nextPrefix, nextLocale, nextPure, nextWarning } = childContextCache.root();

return {
prefix: nextPrefix,
Expand All @@ -99,7 +100,10 @@ class ConfigProvider extends Component {
}

componentDidMount() {
childContextCache = Object.assign({}, this.getChildContext(), childContextCache);
childContextCache.add(
this,
Object.assign({}, childContextCache.get(this, {}), this.getChildContext())
);
}

componentWillReceiveProps(nextProps) {
Expand All @@ -109,11 +113,14 @@ class ConfigProvider extends Component {
}

componentDidUpdate() {
childContextCache = this.getChildContext();
childContextCache.add(
this,
Object.assign({}, childContextCache.get(this, {}), this.getChildContext())
);
}

componentWillUnmount() {
childContextCache = {};
childContextCache.remove(this);
}

setMomentLocale(locale) {
Expand Down

0 comments on commit 5afa5df

Please sign in to comment.