Skip to content

Commit

Permalink
chore: bump 3.5.10
Browse files Browse the repository at this point in the history
  • Loading branch information
fantasticsoul committed Dec 16, 2023
1 parent 033c171 commit 3ab12b3
Show file tree
Hide file tree
Showing 14 changed files with 50 additions and 81 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Change Log

click helux-core [change log](./packages/helux-core/CHANGELOG.md) to see more details

[released] - 2023-11-24

- 接入 `vitest`
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": "helux",
"version": "3.5.9",
"version": "3.5.10",
"description": "A state library core that integrates atom, signal, collection dep, derive and watch, it supports all react like frameworks( including react 18 ).",
"keywords": [],
"author": {
Expand Down
3 changes: 1 addition & 2 deletions packages/helux-core/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { mutate, mutateDict, runMutate, runMutateTask } from './factory/createMu
import { atom, atomx, share, sharex } from './factory/createShared';
import { sync, syncer } from './factory/createSync';
import { watch } from './factory/createWatch';
import { currentDraftRoot, setAtomVal } from './factory/creator/current';
import { currentDraftRoot } from './factory/creator/current';
import { flush, reactiveDesc } from './factory/creator/reactive';
import { getDeriveLoading, runDerive, runDeriveTask } from './helpers/fnRunner';
import { getRawState, getSnap } from './helpers/state';
Expand Down Expand Up @@ -85,7 +85,6 @@ export {
reactiveDesc,
flush,
currentDraftRoot,
setAtomVal,
isAtom,
isDerivedAtom,
storeSrv,
Expand Down
3 changes: 3 additions & 0 deletions packages/helux-core/src/consts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ export { HAS_SYMBOL };

export const PROTO_KEY = '__proto__';

/** 提供给 sync 返回 undefined 时之用 */
export const UNDEFINED = createSymbol('HeluxUndefined');

/** 标识对象是一个 MutateFnItem */
export const MUTATE_FN_ITEM = createSymbol('HeluxMutateFnItem');

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 = '3.5.9';
export const VER = '3.5.10';

export const LIMU_VER = limuVer;

Expand Down
2 changes: 1 addition & 1 deletion packages/helux-core/src/factory/common/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export function createImmut(obj: Dict, onOperate: (op: IOperateParams) => void)
* 区分是 atom 还是 shared 返回的部分状态,atom 返回要自动装箱为 { val: T }
*/
export function wrapPartial(forAtom: boolean, val: any) {
if (val === undefined) return; // undefined 丢弃,如真需要赋值 undefined,调用 setAtomVal
if (val === undefined) return; // undefined 丢弃
if (forAtom) return { val };
if (isObj(val)) return val;
}
Expand Down
2 changes: 0 additions & 2 deletions packages/helux-core/src/factory/createShared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { derive } from './createDerived';
import { mutate, mutateDict, runMutate, runMutateTask } from './createMutate';
import { buildSharedObject } from './creator';
import type { TInternal } from './creator/buildInternal';
import { setAtomVal } from './creator/current';
import { getGlobalEmpty, initGlobalEmpty } from './creator/globalId';
import { getLoadingInfo, initGlobalLoading, initLoadingCtx } from './creator/loading';
import type { IInnerOptions } from './creator/parse';
Expand Down Expand Up @@ -121,7 +120,6 @@ export function createSharedLogic(innerOptions: IInnerOptions, createOptions?: a
useActionLoading: ldAction.useLoading,
sync,
syncer,
setAtomVal: forAtom ? setAtomVal : noop,
setOnReadHook,
sharedKey,
sharedKeyStr,
Expand Down
4 changes: 0 additions & 4 deletions packages/helux-core/src/factory/creator/current.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ let CURRENT_DEPS_CB: Fn = noop;

let CURRENT_FN_DEPS: string[] = [];

export function setAtomVal(val: any) {
CURRENT_DRAFT_ROOT.val = val;
}

export function currentDraftRoot() {
return CURRENT_DRAFT_ROOT;
}
Expand Down
14 changes: 7 additions & 7 deletions packages/helux-core/src/factory/creator/sync.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { isJsObj, setVal } from '@helux/utils';
import { FROM } from '../../consts';
import { FROM, UNDEFINED } from '../../consts';
import { createOneLevelOb } from '../../helpers/obj';
import type { Dict, Fn, InnerSetState } from '../../types/base';
import { createImmut, getDepKeyByPath } from '../common/util';
import type { TInternal } from './buildInternal';
import { DRAFT_ROOT, MUTATE_CTX } from './current';
import { DRAFT_ROOT } from './current';

export function getEventVal(e: any) {
let val = e;
Expand Down Expand Up @@ -37,11 +37,11 @@ function createSyncFn(innerSetState: InnerSetState, path: string[], before?: Fn)
(draft: any) => {
// 使用 draftRoot 做赋值,透传拆箱后的 draft 给用户( 如果是 atom )
const draftRoot = DRAFT_ROOT.current();
setVal(draftRoot, path, val);
// 刻意再次标记为 true,让 before 返回的结果生效(如用户未修改 draftRoot的话)
MUTATE_CTX.current().handleAtomCbReturn = true;
// 用户设置了想修改其他数据或自身数据的函数
return before?.(val, draft);
const isAtom = draft !== draftRoot;
const params = { draft, draftRoot, path, isAtom, UNDEFINED };
// before函数里用户还可以修改其他数据或返回 path 对应的数据新值
const newVal = before?.(val, params);
setVal(draftRoot, path, newVal !== undefined ? newVal : val);
},
{ from: FROM.SYNC, calledBy: 'sync' },
);
Expand Down
15 changes: 2 additions & 13 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].9
| [email protected].10
| 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 @@ -59,7 +59,7 @@ import type {
WatchOptionsType,
} from './base';

export declare const VER: '3.5.9';
export declare const VER: '3.5.10';

export declare const LIMU_VER: string;

Expand Down Expand Up @@ -587,17 +587,6 @@ export function addPlugin(plugin: IPlugin): void;
*/
export function action<T = any>(sharedState: T): <P = any>(fn: ActionFnDef<P, T>, desc?: string) => Action<P, T>;

/**
* get current draft root
* here pass state just for get return type
*/
export function currentDraftRoot<T = any>(state?: T): T extends Atom ? { val: T['val'] } : T;

/**
* setAtomVal('xx') 等效于 currentDraftRoot().val = 'xx';
*/
export function setAtomVal<T = any>(val?: T): T;

/**
* test if the input arg is a result returned by atom()
*/
Expand Down
31 changes: 22 additions & 9 deletions packages/helux-core/src/types/base.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -462,17 +462,31 @@ export type SyncerFn = (mayEvent: any, ...args: any[]) => void;

export type PathRecorder<T = SharedState, V = any> = (target: DraftType<T>) => V;

export type SyncBeforeFnParams<T = SharedState> = {
draft: DraftType<T>;
draftRoot: DraftRootType<T>;
path: string[];
/** 如需赋新值为 undefined,需返回这个 UNDEFINED 值表示 undefined */
UNDEFINED: symbol;
/** 当前对象是否由 atom 创建 */
isAtom: boolean;
};

// 此处用 V 约束 before 函数的返回类型
export type SyncFnBuilder<T = SharedState, V = any> = (
pathOrRecorder: string[] | PathRecorder<T>,
/** 在提交数据之前,还可以修改其他数据或自身数据的函数 */
before?: (eventNewVal: V, draft: DraftType<T>) => void,
/**
* 在提交数据之前,还可以修改其他数据或自身数据的函数
* 此函数也支持返回 path 对应的修改新值,如需修改为 undefined
* 需返回 params.UNDEFEIND 才有效,如果此函数不返回任何值或返回 undefined 均不会干预赋值操作
*/
before?: (eventNewVal: V, params: SyncBeforeFnParams<T>) => any,
) => SyncerFn;

export type Syncer<T = SharedState> = T extends Atom | ReadOnlyAtom
? T['val'] extends Primitive
? SyncerFn
: { [key in keyof T['val']]: SyncerFn }
? SyncerFn
: { [key in keyof T['val']]: SyncerFn }
: { [key in keyof T]: SyncerFn };

export type SafeLoading<T = SharedState, O extends ICreateOptions<T> = ICreateOptions<T>> = O['mutate'] extends MutateFnDict<T>
Expand All @@ -481,16 +495,16 @@ export type SafeLoading<T = SharedState, O extends ICreateOptions<T> = ICreateOp

type FnResultType<T extends PlainObject | DeriveFn> = T extends PlainObject
? T['fn'] extends Fn
? DerivedAtom<ReturnType<T['fn']>>
: DerivedAtom<any>
? DerivedAtom<ReturnType<T['fn']>>
: DerivedAtom<any>
: T extends DeriveFn
? DerivedAtom<ReturnType<T>>
: DerivedAtom<any>;

type FnResultValType<T extends PlainObject | DeriveFn> = T extends PlainObject
? T['fn'] extends Fn
? ReturnType<T['fn']>
: any
? ReturnType<T['fn']>
: any
: T extends DeriveFn
? ReturnType<T>
: any;
Expand Down Expand Up @@ -729,7 +743,6 @@ export interface ISharedCtx<T = SharedDict> extends ISharedStateCtxBase<T> {
export interface IAtomCtx<T = any> extends ISharedStateCtxBase<Atom<T>> {
state: ReadOnlyAtom<T>;
useState: (options?: IUseSharedStateOptions<T>) => [T, SetState<T>, IInsRenderInfo];
setAtomVal: (val: T) => void;
}

export interface BeforeFnParams<T = SharedState> {
Expand Down
36 changes: 3 additions & 33 deletions packages/helux/__tests__/atom/mutate.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { IPlugin } from '@helux/core';
import '@testing-library/jest-dom';
import { describe, expect, test } from 'vitest';
import { addPlugin, atom, currentDraftRoot, runMutate, setAtomVal } from '../helux';
import { addPlugin, atom, runMutate } from '../helux';

describe('create atom mutate', () => {
function runReturnLogic(cbLogic: any) {
Expand Down Expand Up @@ -29,40 +29,10 @@ describe('create atom mutate', () => {
runReturnLogic((numAtom) => numAtom.val + 10);
});

test('single mutate, return result ( currentDraftRoot )', async () => {
runReturnLogic((numAtom) => {
currentDraftRoot().val = numAtom.val + 10;
});
});

test('single mutate, return result ( setAtomVal )', async () => {
runReturnLogic((numAtom) => {
setAtomVal(numAtom.val + 10);
});
});

test('single mutate, return result after atom( cb has no {} )', async () => {
runReturnLogicAfterAtom((numAtom) => numAtom.val + 10);
});

test('single mutate, return result after atom( currentDraftRoot )', async () => {
runReturnLogicAfterAtom((numAtom) => {
currentDraftRoot().val = numAtom.val + 10;
});
});

test('single mutate, return result after atom( setAtomVal )', async () => {
runReturnLogicAfterAtom((numAtom) => {
setAtomVal(numAtom.val + 10);
});
});

test('single mutate, return result after atom( ctx.setAtomVal )', async () => {
runReturnLogicAfterAtom((numAtom, ctx) => {
ctx.setAtomVal(numAtom.val + 10);
});
});

test('single mutate, change draft', async () => {
const [numAtom, setAtom] = atom(1);
const [bAtom] = atom(0, {
Expand Down Expand Up @@ -222,7 +192,7 @@ describe('create atom mutate', () => {
});

test('multi mutate, watch self state with dead cycle', async () => {
window.alert = () => {};
window.alert = () => { };
let err: any = null;
const errPlugin: IPlugin = {
install(pluginCtx) {
Expand Down Expand Up @@ -255,7 +225,7 @@ describe('create atom mutate', () => {
});

test('multi mutate, watch self draft with dead cycle', async () => {
window.alert = () => {};
window.alert = () => { };
let err: any = null;
const errPlugin: IPlugin = {
install(pluginCtx) {
Expand Down
13 changes: 7 additions & 6 deletions packages/helux/__tests__/helux.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ window.alert = () => {};
// Learn more at https://reactjs.org/link/wrap-tests-with-act
const api = initHeluxContext({ heluxCtxKey: '__HELUX__', reactLib: React, act });

// 导出 core 所有方法,类型由 index.d.ts 提供(见 package.json的 types 配置)
// 导出 core 所有方法,类型由 index.d.ts 提供(见 package.json的 types 配置)
export const {
atom,
atomx,
share,
shareState,
shareAtom,
createShared,
sharex,
// derive api
derive,
deriveDict,
Expand All @@ -35,11 +35,12 @@ export const {
useMutable,
useMutateLoading,
useActionLoading,
useForceUpdate,
useEffect,
useLayoutEffect,
useStable,
useObject,
useLocalForceUpdate,
useAtomForceUpdate,
// action api
action,
// signal api
Expand All @@ -62,8 +63,8 @@ export const {
emit,
on,
// util api
currentDraftRoot,
setAtomVal,
reactiveDesc,
flush,
isAtom,
isDerivedAtom,
storeSrv,
Expand Down
2 changes: 0 additions & 2 deletions packages/helux/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ export const {
// util api
reactiveDesc,
flush,
currentDraftRoot,
setAtomVal,
isAtom,
isDerivedAtom,
storeSrv,
Expand Down

0 comments on commit 3ab12b3

Please sign in to comment.