Skip to content

Commit

Permalink
Release/1.4.1 (#116)
Browse files Browse the repository at this point in the history
* docs: language link for migration

* docs: add link for upgrade guidelines

* fix: immer error (#117)

* feat: set error & loading as blacklist for immer pulgin

* chore: lint

* chore: notice
  • Loading branch information
alvinhui authored May 19, 2020
1 parent 4fafc3e commit bba4e12
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 28 deletions.
2 changes: 2 additions & 0 deletions docs/migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ id: migration
title: Migration
---

English | [简体中文](./migration.zh-CN.md)

## Migrating From Redux

We provide a gradual solution to allow your project to be partially migrated from Redux to icestore.
Expand Down
2 changes: 2 additions & 0 deletions docs/migration.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ id: migration
title: Migration
---

[English](./migration.md) | 简体中文

## 从 Redux 迁移

我们提供了渐进式的方案使得您的项目可以局部从 Redux 迁移到 icestore。
Expand Down
6 changes: 3 additions & 3 deletions docs/upgrade-guidelines.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ title: Upgrade Guidelines

## 从 1.2.0 升级到 1.3.0

1.3.0 是完全向下兼容的,但是我们推荐您在新增代码中使用最新的 API。
1.3.0 是完全向下兼容的,但是我们推荐您在新增代码中使用最新的 API。1.2.x 有一些已知的[功能不完备](https://github.com/alibaba/ice/issues/3037),您必须知晓。

我们会在未来的版本中删除标记为「已过期」的 API。

Expand Down Expand Up @@ -128,7 +128,7 @@ export default withModelDispatchers('todos')(TodoList);

## 从 1.0.0 升级到 1.3.0

1.3.0 是完全向下兼容的,但是我们推荐您在新增代码中使用最新的 API。
1.3.0 是完全向下兼容的,但是我们推荐您在新增代码中使用最新的 API。1.0.x 有一些已知的[功能不完备](https://github.com/alibaba/ice/issues/3037),您必须知晓。

我们会在未来的版本中删除标记为「已过期」的 API。

Expand Down Expand Up @@ -246,7 +246,7 @@ export default withModelEffectsState('todos')(TodoList);

从 0.x.x 到 1.x.x 是不兼容的。您可以选择性地进行升级。

但 0.x.x 有一些已知的缺陷,您必须知晓:https://github.com/alibaba/ice/issues/3037
但 0.x.x 有一些已知的[缺陷](https://github.com/alibaba/ice/issues/3037),您必须知晓。

### Define Model

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ice/store",
"version": "1.4.0",
"version": "1.4.1",
"description": "Simple and friendly state for React",
"main": "lib/index.js",
"files": [
Expand Down
18 changes: 10 additions & 8 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,19 @@ export const createStore = <M extends T.Models, C extends T.CreateStoreConfig<M>
plugins.push(createReduxHooksPlugin({context}));
plugins.push(createModelApisPlugin());

const loading = createLoadingPlugin();
const error = createErrorPlugin();
const immer = createImmerPlugin();
if (!disableImmer) {
plugins.push(immer);
}
// https://github.com/ice-lab/icestore/issues/94
// TODO: fix error & loading plugin immer problem
const immerBlacklist = [];
if (!disableLoading) {
plugins.push(loading);
plugins.push(createLoadingPlugin());
immerBlacklist.push('loading');
}
if (!disableError) {
plugins.push(error);
plugins.push(createErrorPlugin());
immerBlacklist.push('error');
}
if (!disableImmer) {
plugins.push(createImmerPlugin({ blacklist: immerBlacklist }));
}

// compatibility handling
Expand Down
38 changes: 22 additions & 16 deletions src/plugins/immer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,35 @@ import produce from 'immer';
import { combineReducers, ReducersMapObject } from 'redux';
import * as T from '../types';

function combineReducersWithImmer(reducers: ReducersMapObject) {
const reducersWithImmer: ReducersMapObject<any, T.Action<any>> = {};
// reducer must return value because literal don't support immer
export interface ImmerConfig {
blacklist?: string[];
}

function createCombineReducersWithImmer(blacklist: string[] = []) {
return function(reducers: ReducersMapObject) {
const reducersWithImmer: ReducersMapObject<any, T.Action<any>> = {};
// reducer must return value because literal don't support immer

Object.keys(reducers).forEach((key) => {
const reducerFn = reducers[key];
reducersWithImmer[key] = (state, payload) =>
typeof state === 'object'
? produce(state, (draft: T.Models) => {
const next = reducerFn(draft, payload);
if (typeof next === 'object') return next;
})
: reducerFn(state, payload);
});
Object.keys(reducers).forEach((key) => {
const reducerFn = reducers[key];
reducersWithImmer[key] = (state, payload) =>
typeof state === 'object' && !blacklist.includes(key)
? produce(state, (draft: T.Models) => {
const next = reducerFn(draft, payload);
if (typeof next === 'object') return next;
})
: reducerFn(state, payload);
});

return combineReducers(reducersWithImmer);
return combineReducers(reducersWithImmer);
};
}

// icestore plugin
const immerPlugin = (): T.Plugin => ({
const immerPlugin = (config: ImmerConfig = {}): T.Plugin => ({
config: {
redux: {
combineReducers: combineReducersWithImmer,
combineReducers: createCombineReducersWithImmer(config.blacklist),
},
},
});
Expand Down

0 comments on commit bba4e12

Please sign in to comment.