Skip to content

Commit

Permalink
Merge pull request #100 from filefoxper/[email protected]
Browse files Browse the repository at this point in the history
  • Loading branch information
filefoxper authored Jan 1, 2025
2 parents b523f74 + 82ca7c3 commit 170e7cd
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 7 deletions.
30 changes: 30 additions & 0 deletions docs/react-effect/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,28 @@ It allows process callback running when a session is failed.
**If there are some Strategy.failure or Strategy.response.failure working in the strategy chain, only the first one runs process callback.**
```
Note: In 18.6.0, the `Strategy.faiure` will not disable other failure strategies after it. It only disables the previous failure strategies when it has nothing throw out. Experience this feature by adding `experience` property to GlobalConfig.
```
For experience:
```ts
const globalConfig = {
experience: 'next',
strategy: (workingStrategies: StrategyType[])=>{
return [
// put the final failure strategy at the top of the chain.
Strategy.failure((e) => {
console.log('failure', e);
}),
...workingStrategies
];
}
}
<ConfigProvider value={globalConfig}>{...}</ConfigProvider>
```
#### Parameters
* **process** - It is a callback to process when session is failed. This process callback accepts a error data from session.
Expand Down Expand Up @@ -360,6 +382,10 @@ It allows to process callback when a session execution is failed.
**If there are some Strategy.failure or Strategy.response.failure working in the strategy chain, only the first one runs process callback.**
```
Note: In 18.6.0, the `Strategy.response.faiure` will not be disabled by other failure strategies, like `Strategy.failure` or `Strategy.response.faiure`. Experience this feature by adding `experience` property to GlobalConfig.
```
#### Parameters
* **process** -It is a callback to process when a execution is failed. It accepts a session state error and the session state as parameters, returns void or a cleanup function.
Expand All @@ -370,11 +396,15 @@ It is a global config provider. It can be used for preseting common strategies a
```ts
type GlobalConfig = {
// For linking `unstable_batchedUpdates` from react-dom, and make running optmization.
batchUpdate?: (callback: () => void) => void;
/**
* @deprecated
**/
useGlobalFetching?: boolean;
// To experience the new features in the next middle version change.
experience?:'next';
// A callback to build a runtime strategy chains, it accepts a strategies array from a runtime `useQuery/useMutation`, returns a new strategies array to replace the original one.
strategy?:(
strategies:(StrategyType | undefined | null)[],
type: 'query' | 'mutation'
Expand Down
6 changes: 5 additions & 1 deletion docs/react-effect/log.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,8 @@

# v18.5.9

* fix the problem about `useLazyComponent` can not support `memo` Component.
* fix the problem about `useLazyComponent` can not support `memo` Component.

# v18.5.10

* add `experience` property to `GlobalConfig`.
26 changes: 26 additions & 0 deletions docs/zh/react-effect/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,28 @@ SWR 缓存策略。该策略可以为每次异步操作生成缓存键,并通

**如果在策略链中存在多个 Strategy.failureStrategy.response.failure 策略,只有第一个失败回调策略的回调函数会被执行。**

```
注意:在 18.6.0 版本开始后,Strategy.failure 策略将不再无条件阻止后续其他错误处理策略执行。Strategy.failure 策略回调函数会在未抛出异常的情况下阻止前置 Strategy.failure 策略运行。可通过添加 GlobalConfig.experience 字段获取体验特性。
```

体验特性:

```ts
const globalConfig = {
experience: 'next',
strategy: (workingStrategies: StrategyType[])=>{
return [
// 兜底异常处理策略,需要排在最前
Strategy.failure((e) => {
console.log('failure', e);
}),
...workingStrategies
];
}
}
<ConfigProvider value={globalConfig}>{...}</ConfigProvider>
```

#### 参数

* **process** - 回调函数,可接收异步函数执行失败时的错误信息为参数。
Expand Down Expand Up @@ -349,6 +371,10 @@ const globalConfig = {

**如果在策略链中存在多个 Strategy.failureStrategy.response.failure 策略,只有第一个失败回调策略的回调函数会被执行。**

```
注意:在 18.6.0 版本开始后,Strategy.response.failure 策略将不会使其他错误处理策略实效,同时也不再受其他错误处理策略的影响。可在全局配置 GlobalConfig 中设置 `experience` 字段获取体验特性。
```

#### 参数

* **process** - 会话执行失败后的回调函数。可接收执行失败后的会话状态错误和会话状态做参数,并可选性的返回一个副作用清理函数。
Expand Down
6 changes: 5 additions & 1 deletion docs/zh/react-effect/log.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,8 @@

## v18.5.9

* 修复 useLazyComponent 解析 memo 组件时报错的问题。
* 修复 useLazyComponent 解析 memo 组件时报错的问题。

## v18.5.10

* 为 GlobalConfig 增加了 `experience` 字段,用于体验下一个中间版本变化的特性。
3 changes: 3 additions & 0 deletions packages/@airma/react-effect/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ declare type QueryConfig<T, C extends PromiseCallback<T>> = {
variables?: Parameters<C>;
strategy?: StrategyCollectionType<T, Parameters<C>>;
manual?: boolean;
experience?: 'next';
};

declare type DefaultQueryConfig<T, C extends PromiseCallback<T>> = QueryConfig<
Expand All @@ -145,6 +146,7 @@ declare type MutationConfig<T, C extends PromiseCallback<T>> = {
triggerOn?: TriggerType[];
variables?: Parameters<C>;
strategy?: StrategyCollectionType<T, Parameters<C>>;
experience?: 'next';
};

declare type DefaultMutationConfig<
Expand Down Expand Up @@ -343,6 +345,7 @@ export declare const Provider: FC<

export declare type GlobalConfig = {
batchUpdate?: (callback: () => void) => void;
experience?: 'next';
strategy?: (
strategy: (StrategyType | null | undefined)[],
type: SessionType
Expand Down
2 changes: 1 addition & 1 deletion packages/@airma/react-effect/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"private": false,
"name": "@airma/react-effect",
"version": "18.5.9",
"version": "18.5.10",
"description": "This is a react async state management tool",
"license": "MIT",
"author": "Jimmy.Harding",
Expand Down
6 changes: 4 additions & 2 deletions packages/@airma/react-effect/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -528,13 +528,14 @@ export function useQuery<T, C extends PromiseCallback<T>>(
const manual = !deps && !variables ? true : man;
const triggerTypes: TriggerType[] = manual ? ['manual'] : triggerOn;
const scopeEffectConfig = useGlobalConfig() || {};
const { strategy: strategyCallback } = scopeEffectConfig;
const { strategy: strategyCallback, experience } = scopeEffectConfig;
const currentStrategies = toStrategies(strategy);
const strategies = strategyCallback
? strategyCallback(currentStrategies, 'query')
: currentStrategies;

const promiseConfig = {
experience,
...con,
triggerOn: triggerTypes,
strategy: strategies.concat(latest() as StrategyType | null | undefined)
Expand All @@ -560,13 +561,14 @@ export function useMutation<T, C extends PromiseCallback<T>>(
const { triggerOn: triggerTypes = ['manual'], strategy } = con;

const scopeEffectConfig = useGlobalConfig() || {};
const { strategy: strategyCallback } = scopeEffectConfig;
const { strategy: strategyCallback, experience } = scopeEffectConfig;
const currentStrategies = toStrategies(strategy);
const strategies = strategyCallback
? strategyCallback(currentStrategies, 'mutation')
: currentStrategies;

const promiseConfig = {
experience,
...con,
triggerOn: triggerTypes,
strategy: strategies.concat(block() as StrategyType | null | undefined)
Expand Down
3 changes: 3 additions & 0 deletions packages/@airma/react-effect/src/libs/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ export type QueryConfig<T, C extends PromiseCallback<T>> = {
strategy?: StrategyCollectionType<T>;
loaded?: boolean;
manual?: boolean;
experience?: 'next';
};

export type MutationConfig<T, C extends PromiseCallback<T>> = {
Expand All @@ -181,10 +182,12 @@ export type MutationConfig<T, C extends PromiseCallback<T>> = {
variables?: Parameters<C>;
strategy?: StrategyCollectionType<T>;
loaded?: boolean;
experience?: 'next';
};

export type GlobalConfig = {
batchUpdate?: (callback: () => void) => void;
experience?: 'next';
strategy?: (
strategy: (StrategyType | null | undefined)[],
type: 'query' | 'mutation'
Expand Down
31 changes: 29 additions & 2 deletions packages/@airma/react-effect/src/strategies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,32 @@ function failure(
option?: { withAbandoned?: boolean }
): StrategyType {
const { withAbandoned } = option || {};
return function er(value) {
const next: StrategyType = function next(value) {
const { runner, executeContext: runtimeCache, localCache: store } = value;
return runner().then(d => {
const isAllProcessed = runtimeCache.get(failure) as boolean | undefined;
if (d.isError && !isAllProcessed && (!d.abandon || withAbandoned)) {
try {
process(d.error, d);
runtimeCache.set(failure, true);
} catch (e) {
runtimeCache.set(failure, false);
}
}
return d;
});
};
return function er(value) {
const {
runner,
executeContext: runtimeCache,
localCache: store,
config
} = value;
const { experience } = config;
if (experience === 'next') {
return next(value);
}
const hasHigherErrorProcessor = runtimeCache.get(error);
runtimeCache.set(error, true);
store.current = process;
Expand Down Expand Up @@ -448,7 +472,10 @@ response.failure = function responseFailure<T>(
process: (e: unknown, sessionData: SessionState) => void | (() => void)
): StrategyType {
const sc: StrategyType = function sc(value) {
const { runner, executeContext: runtimeCache } = value;
const { runner, executeContext: runtimeCache, config } = value;
if (config.experience === 'next') {
return runner();
}
runtimeCache.set(error, true);
return runner();
};
Expand Down

0 comments on commit 170e7cd

Please sign in to comment.