Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doc #157

Merged
merged 10 commits into from
Feb 28, 2024
25 changes: 25 additions & 0 deletions docs/docs/guide/derive.md
Original file line number Diff line number Diff line change
Expand Up @@ -389,3 +389,28 @@ witness.run();
// 呼叫 fnItem 配置的异步函数
witness.runTask();
```

### 注意事项

`derive`回调里的依赖必须提前声明,不能放到条件语句里,否则可能照成依赖丢失。

- 错误示例

```ts
const result = derive(() => {
if( state.x ) return 2;
return state.y + 1;
});
```

- 正确示例

```ts
const result = derive(() => {
const { x, y } = state;
if (x) return 2;
y + 1;
});
```


2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "helux",
"version": "4.2.4",
"version": "4.2.6",
"description": "A reactive atomic state engine for React like.",
"keywords": [],
"author": {
Expand Down
29 changes: 29 additions & 0 deletions packages/helux-core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,34 @@
# @helux/core

## 4.2.7

### Patch Changes

- db6c763: build(4.2.7): issue 156
- Updated dependencies [db6c763]
- @helux/[email protected]
- @helux/[email protected]
- @helux/[email protected]

## 4.2.6

### Patch Changes

- efeee46: build(4.2.6): refactor onRead
- Updated dependencies [efeee46]
- @helux/[email protected]
- @helux/[email protected]
- @helux/[email protected]

## 4.2.5

### Patch Changes

- d2dd4b9: build(4.2.5): optimize types
- @helux/[email protected]
- @helux/[email protected]
- @helux/[email protected]

## 4.2.4

### Patch Changes
Expand Down
4 changes: 2 additions & 2 deletions packages/helux-core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@helux/core",
"version": "4.2.4",
"version": "4.2.7",
"description": "A reactive atomic state engine for React like.",
"bugs": {
"url": "https://github.com/heluxjs/helux/issues"
Expand Down Expand Up @@ -35,7 +35,7 @@
"@helux/hooks-impl": "workspace:^",
"@helux/types": "workspace:^",
"@helux/utils": "workspace:^",
"limu": "^3.12.3"
"limu": "^3.12.4"
},
"devDependencies": {
"esbuild-copy-static-files": "^0.1.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/helux-core/src/consts/user.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { VER as limuVer } from 'limu';

export const VER = '4.2.4';
export const VER = '4.2.7';

export const LIMU_VER = limuVer;

Expand Down
2 changes: 0 additions & 2 deletions packages/helux-core/src/factory/common/ctor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ export function newMutateCtx(options: ISetFactoryOpts): IMutateCtx {
sn = genRenderSN(),
isFirstCall = false,
desc = '',
onRead,
} = options;
return {
fnKey: '',
Expand All @@ -78,7 +77,6 @@ export function newMutateCtx(options: ISetFactoryOpts): IMutateCtx {
sn,
isFirstCall,
desc,
onRead,
};
}

Expand Down
1 change: 0 additions & 1 deletion packages/helux-core/src/factory/createShared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,6 @@ export function createSharedLogic(innerOptions: IInnerOptions, createOptions?: a
setDraft,
setEnableMutate: (enabled: boolean) => setEnableMutate(enabled, internal),
getOptions: () => getOptions(internal),
setOnReadHook: (onRead: Fn) => (internal.onRead = onRead),
defineActions: (throwErr?: boolean) => (actionDict: Dict<ActionTask>) => defineActions({ ...acCommon, actionDict }, throwErr),
defineTpActions: (throwErr?: boolean) => (actionDict: Dict<Action>) =>
defineActions({ ...acCommon, actionDict, forTp: true }, throwErr),
Expand Down
5 changes: 3 additions & 2 deletions packages/helux-core/src/factory/creator/operateState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ export function handleOperate(opParams: IOperateParams, opts: { internal: TInter
// 1 减轻运行负担,
// 2 降低死循环可能性,例如在 watch 回调里调用顶层的 setState
if (mutateCtx.enableDep) {
// 支持对 draft 操作时可以收集到依赖: draft.a = draft.b + 1
// 来自实例 reactive 透传的 onRead
if (currReactive.onRead) {
// 来自顶层 reactive 透传的 onRead
currReactive.onRead(opParams);
} else {
// 支持对 draft 操作时可以收集到依赖: draft.a = draft.b + 1
if (getRunningFn().fnCtx) {
recordFnDepKeys([depKey], { sharedKey });
}
Expand All @@ -70,6 +70,7 @@ export function handleOperate(opParams: IOperateParams, opts: { internal: TInter
recordBlockDepKey([depKey]);
recordLastest(sharedKey, value, internal.sharedState, depKey, fullKeyPath);
}
internal.onRead?.(opParams);
}
}
return;
Expand Down
3 changes: 2 additions & 1 deletion packages/helux-core/src/factory/creator/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ export function parseOptions(innerOptions: IInnerOptions, options: ICreateOption
const rules = options.rules || [];
const before = options.before || noop;
const mutate = options.mutate || noop;
const onRead = options.onRead || undefined;
// 后续 parseRules 步骤会转 stopArrDep stopDepth 到 stopDepInfo 上
const stopArrDep = options.stopArrDep ?? true;
const stopDepth = options.stopDepth || STOP_DEPTH;
Expand Down Expand Up @@ -210,7 +211,7 @@ export function parseOptions(innerOptions: IInnerOptions, options: ICreateOption
before,
mutate,
mutateFnDict,
onRead: null as any, // 等待 setOnReadHook 写入
onRead,
enableMutate,
stateType,
recordLoading,
Expand Down
4 changes: 2 additions & 2 deletions packages/helux-core/src/factory/creator/reactive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ export function nextTickFlush(sharedKey: number, desc?: string) {
}

function buildMeta(internal: TInternal, options: IBuildReactiveOpts) {
const { from = REACTIVE, onRead } = options;
const { finish, draftRoot } = internal.setStateFactory({ isReactive: true, from, handleCbReturn: false, enableDep: true, onRead });
const { from = REACTIVE } = options;
const { finish, draftRoot } = internal.setStateFactory({ isReactive: true, from, handleCbReturn: false, enableDep: true });
const latestMeta = newReactiveMeta(draftRoot, options, finish);
latestMeta.key = getReactiveKey();
latestMeta.sharedKey = internal.sharedKey;
Expand Down
4 changes: 2 additions & 2 deletions packages/helux-core/src/types/api.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
|------------------------------------------------------------------------------------------------
| [email protected].4
| [email protected].7
| A state library core that integrates atom, signal, collection dep, derive and watch,
| it supports all react like frameworks ( including react 18 ).
|------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -68,7 +68,7 @@ import type {
} from './base';

export declare const cst: {
VER: '4.2.4';
VER: '4.2.7';
LIMU_VER: string;
EVENT_NAME: {
/** 共享状态创建时的事件 */
Expand Down
17 changes: 6 additions & 11 deletions packages/helux-core/src/types/base.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ export interface IMutateTaskParam<T = SharedState, P extends Arr = Arr, E extend
/** deps 返回的结果 */
input: P;
extraBound: IBoundStateInfo<E>;
/**
/**
* 额外参数,用来传递给 mutate 的fn和task函数
*/
extraArgs?: any;
Expand Down Expand Up @@ -528,10 +528,6 @@ export interface IMutateCtx {
* 修改描述
*/
desc: string;
/**
* useReactive 透传的 onRead,方便为每个实例单独收集依赖
*/
onRead?: OnOperate;
}

export interface IInnerSetStateOptions extends ISetStateOptions {
Expand Down Expand Up @@ -560,7 +556,6 @@ export interface ISetFactoryOpts extends IInnerSetStateOptions {
* 同时也减少不必要的运行时分析性能损耗
*/
enableDep?: boolean;
onRead?: OnOperate;
}

/**
Expand Down Expand Up @@ -856,10 +851,6 @@ export interface ISharedStateCtxBase<T = any, O extends ICreateOptions<T> = ICre
mutate: <P extends Arr = Arr>(fnItem: IMutateFnLooseItem<T, P> | MutateFn<T, P>) => IMutateWitness<T>;
runMutate: (descOrOptions: string | IRunMutateOptions) => T;
runMutateTask: (descOrOptions: string | IRunMutateOptions) => T;
/**
* 配置 onRead 钩子函数
*/
setOnReadHook: (onRead: OnRead) => void;
/**
* 是否禁止 mutate 再次执行( 首次一定执行,此函数只能禁止是否再次执行 )
* ```ts
Expand Down Expand Up @@ -1255,12 +1246,16 @@ export interface ICreateOptionsFull<T = SharedState> {
* default: true,是否允许 mutate 执行,可以创建 atom 时设置,也可以中途通过 setEnableMutate 反复设置
*/
enableMutate: boolean;
/**
* 任何读行为都会触发此函数
*/
onRead: OnRead;
}

/**
* 目前api层面只暴露部分配置参数供用户查看
*/
export type CtxCreateOptions = Omit<ICreateOptionsFull, 'rules' | 'mutate' | 'before'>;
export type CtxCreateOptions = Omit<ICreateOptionsFull, 'rules' | 'mutate' | 'before' | 'onRead'>;

export interface IInnerCreateOptions<T = SharedState> extends ICreateOptionsFull<SharedState> {
forAtom: boolean;
Expand Down
2 changes: 1 addition & 1 deletion packages/helux-demo-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"dependencies": {
"@types/react": ">=16.0.0",
"@types/react-dom": ">=16.0.0",
"helux": "^4.2.4",
"helux": "^4.2.7",
"react": ">=16.10.2",
"react-dom": ">=16.10.2"
},
Expand Down
25 changes: 25 additions & 0 deletions packages/helux-hooks-impl/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
# @helux/hooks-impl

## 4.2.7

### Patch Changes

- db6c763: build(4.2.7): issue 156
- Updated dependencies [db6c763]
- @helux/[email protected]
- @helux/[email protected]

## 4.2.6

### Patch Changes

- efeee46: build(4.2.6): refactor onRead
- Updated dependencies [efeee46]
- @helux/[email protected]
- @helux/[email protected]

## 4.2.5

### Patch Changes

- @helux/[email protected]
- @helux/[email protected]

## 4.2.4

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/helux-hooks-impl/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@helux/hooks-impl",
"version": "4.2.4",
"version": "4.2.7",
"description": "helux hooks implement lib",
"bugs": {
"url": "https://github.com/heluxjs/helux/issues"
Expand Down
28 changes: 28 additions & 0 deletions packages/helux-hooks/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
# @helux/hooks

## 4.2.7

### Patch Changes

- db6c763: build(4.2.7): issue 156
- Updated dependencies [db6c763]
- @helux/[email protected]
- @helux/[email protected]
- @helux/[email protected]

## 4.2.6

### Patch Changes

- efeee46: build(4.2.6): refactor onRead
- Updated dependencies [efeee46]
- @helux/[email protected]
- @helux/[email protected]
- @helux/[email protected]

## 4.2.5

### Patch Changes

- @helux/[email protected]
- @helux/[email protected]
- @helux/[email protected]

## 4.2.4

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/helux-hooks/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@helux/hooks",
"version": "4.2.4",
"version": "4.2.7",
"description": "helux hooks lib for react",
"keywords": [
"helux",
Expand Down
23 changes: 23 additions & 0 deletions packages/helux-openinula/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
# @helux/openinula

## 4.2.7

### Patch Changes

- db6c763: build(4.2.7): issue 156
- Updated dependencies [db6c763]
- @helux/[email protected]

## 4.2.6

### Patch Changes

- efeee46: build(4.2.6): refactor onRead
- Updated dependencies [efeee46]
- @helux/[email protected]

## 4.2.5

### Patch Changes

- Updated dependencies [d2dd4b9]
- @helux/[email protected]

## 4.2.4

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/helux-openinula/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@helux/openinula",
"version": "4.2.4",
"version": "4.2.7",
"description": "State library for preact that integrates atom, signal, collection dep, derive and watch.",
"bugs": {
"url": "https://github.com/heluxjs/helux/issues"
Expand Down
14 changes: 14 additions & 0 deletions packages/helux-types/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
# @helux/types

## 4.2.7

### Patch Changes

- db6c763: build(4.2.7): issue 156

## 4.2.6

### Patch Changes

- efeee46: build(4.2.6): refactor onRead

## 4.2.5

## 4.2.4

## 4.2.3
Expand Down
2 changes: 1 addition & 1 deletion packages/helux-types/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@helux/types",
"version": "4.2.4",
"version": "4.2.7",
"description": "helux common types lib",
"keywords": [
"helux",
Expand Down
Loading
Loading