Skip to content

Commit

Permalink
chore: bump 3.5.12
Browse files Browse the repository at this point in the history
  • Loading branch information
fantasticsoul committed Dec 16, 2023
1 parent 5455071 commit d92636c
Show file tree
Hide file tree
Showing 26 changed files with 299 additions and 132 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Change Log

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

[released] - 2023-11-24

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.11",
"version": "3.5.12",
"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
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.11';
export const VER = '3.5.13';

export const LIMU_VER = limuVer;

Expand Down
4 changes: 4 additions & 0 deletions packages/helux-core/src/factory/common/fnScope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ export function getFnCtx(fnKey: string) {
return map.get(fnKey);
}

export function getSafeFnCtx(fnKey: string) {
return getCtxMap(fnKey).get(fnKey) as IFnCtx;
}

export function getFnCtxByObj<T = Dict>(obj: T) {
const fnKey = getFnKey(obj);
return getFnCtx(fnKey) || null;
Expand Down
3 changes: 1 addition & 2 deletions packages/helux-core/src/factory/common/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,9 @@ export function getRootValDepKeyInfo(internal: TInternal) {

export function getDepKeyByPath(fullKeyPath: string[], sharedKey: number) {
try {
// console.trace(fullKeyPath);
return prefixValKey(fullKeyPath.join(KEY_SPLITER), sharedKey);
} catch (err) {
console.trace('found Symbol key in your path :', fullKeyPath);
console.warn('found Symbol key in your path :', fullKeyPath);
return `${sharedKey}`;
}
}
Expand Down
13 changes: 7 additions & 6 deletions packages/helux-core/src/factory/createAction.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { FROM } from '../consts';
import { getStatusKey, setLoadStatus } from '../factory/creator/loading';
import type { Fn, SharedState } from '../types/base';
import { checkSharedStrict } from './common/check';
import { newFakeFnItem } from './creator/fake';
import { callAsyncMutateFnLogic } from './creator/mutateFn';

const Action = 'Action';
const { ACTION } = FROM;

/**
* 内部统一封装,shared 和 atom 走统一的入口,上层接口自己标记对应类型
Expand All @@ -16,17 +18,16 @@ function innerCreate<T = SharedState>(state: T, options: { task: Fn; desc: strin
const action = (payload: any, throwFnErr?: boolean) => {
// 用户调用 action 独立定义的 throwErr 优先级高于 创建 action 函数时预设的 throwErr
const throwErrVar = throwFnErr ?? throwErr;
const fnItem = newFakeFnItem({ desc, task, depKeys: [] });
return callAsyncMutateFnLogic(state, {
desc,
task,
from: Action,
depKeys: [],
fnItem,
from: ACTION,
throwErr: throwErrVar,
getArgs: ({ draft, draftRoot, setState, desc, flush }) => [{ draft, draftRoot, setState, desc, payload, flush }],
});
};
// 提前记录一个值,方便用户使用 getLoading 时可收集到依赖
setLoadStatus(internal, getStatusKey(Action, desc), { loading: false, ok: true, err: null });
setLoadStatus(internal, getStatusKey(ACTION, desc), { loading: false, ok: true, err: null });
action.__sharedKey = internal.sharedKey;
action.__fnName = desc;
return action;
Expand Down
10 changes: 7 additions & 3 deletions packages/helux-core/src/factory/createMutate.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { SINGLE_MUTATE } from '../consts';
import { FROM, SINGLE_MUTATE } from '../consts';
import { getInternal } from '../helpers/state';
import type { Dict, IRunMutateOptions, MutateFn, MutateFnDict, MutateFnLooseItem, MutateWitness, SharedState } from '../types/base';
import { checkShared, checkSharedStrict } from './common/check';
import type { TInternal } from './creator/buildInternal';
import { callMutateFn, watchAndCallMutateDict } from './creator/mutateFn';
import { callAsyncMutateFnLogic, callMutateFnLogic, watchAndCallMutateDict } from './creator/mutateFn';
import { parseCreateMutateOpt, parseMutate, parseMutateFn } from './creator/parse';

interface ILogicOptions {
Expand All @@ -25,8 +25,12 @@ function runMutateFnItem<T = SharedState>(options: { target: T; desc?: string; f
// 指定了 task 但未配置 task,返回最近一次修改结果的快照
if (forTask && !item.task) return [snap, new Error(`mutate task ${desc} not defined`)] as [any, Error | null];

const baseOpts = { sn: 0, fnItem: item, from: FROM.MUTATE };
// 调用 desc 对应的函数
return callMutateFn(target, { ...item, forTask });
if (forTask) {
return callAsyncMutateFnLogic(target, baseOpts);
}
return callMutateFnLogic(target, baseOpts);
}

function makeWitness(target: SharedState, desc: string, oriDesc: string, internal: TInternal) {
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
@@ -1,4 +1,3 @@
import { noop } from '@helux/utils';
import { FROM, STATE_TYPE } from '../consts';
import { runDerive, runDeriveTask } from '../helpers/fnRunner';
import { useAtom, useAtomForceUpdate, useDerived, useLocalForceUpdate, useMutable, useReactive } from '../hooks';
Expand Down
10 changes: 10 additions & 0 deletions packages/helux-core/src/factory/creator/current.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,20 @@ let CURRENT_DEPS_CB: Fn = noop;

let CURRENT_FN_DEPS: string[] = [];

/** 被触发运行的 watch 函数 */
let CURRENT_TRIGGERED_WATCH = '';

export function currentDraftRoot() {
return CURRENT_DRAFT_ROOT;
}

/** 当前正在被触发运行的 watch 函数 key */
export const TRIGGERED_WATCH = {
current: () => CURRENT_TRIGGERED_WATCH,
set: (val: any) => (CURRENT_TRIGGERED_WATCH = val),
del: () => 0,
};

export const DEPS_CB = {
current: () => CURRENT_DEPS_CB,
set: (cb: Fn) => (CURRENT_DEPS_CB = cb),
Expand Down
41 changes: 25 additions & 16 deletions packages/helux-core/src/factory/creator/deadCycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
import { includeOne, nodupPush, noop, safeMapGet, tryAlert } from '@helux/utils';
import { fmtDepKeys } from '../../helpers/debug';
import type { IFnCtx } from '../../types/base';
import type { Fn, IFnCtx } from '../../types/base';
import { TInternal } from './buildInternal';

type Log = { sn: number; descs: string[]; timer: any; errs: any[]; cycle: string[] };
Expand Down Expand Up @@ -43,13 +43,15 @@ export function depKeyDcError(internal: TInternal, fnCtx: IFnCtx, depKeys: strin
const descStr = desc ? `(${desc})` : '';
const dcInfo =
`DEAD_CYCLE: found reactive object in ${tip}${descStr} cb`
+ ` is changing module(${internal.usefulName})'s keys(${fmtDepKeys(depKeys, false, '.')}) by its self, `
+ 'but some of these keys are also the watched dep keys, it will cause a infinity loop call!';
+ ` is changing module(${internal.usefulName})'s some of these dep keys(${fmtDepKeys(depKeys, false, '.')}), `
+ 'it will cause a infinity loop call!';

const mutateFn = task || fn;
const targetFn = mutateFn === noop ? fnCtx.fn : mutateFn;
console.error(` ${dcInfo} open the stack to find the below fn: \n`, targetFn);
return new Error(`[only-dev-mode alert] ${dcInfo}`);
return {
err: new Error(`[only-dev-mode alert] ${dcInfo}`),
tipFn: () => console.error(` ${dcInfo} open the stack to find the below fn: \n`, targetFn),
};
}

/**
Expand Down Expand Up @@ -80,13 +82,22 @@ export function probeFnDeadCycle(internal: TInternal, sn: number, desc: string)
}
}

export function alertDepKeyDeadCycleErr(internal: TInternal, dcErrorInfo: { err: Error | null; tipFn: Fn }) {
dcErrorInfo.tipFn();
tryAlert(dcErrorInfo.err, {
logErr: false,
throwErr: false,
alertErr: internal.alertDeadCycleErr,
});
}

/**
* 发现类似
* 1 watch(()=>{ reactive.a = 1 }, ()=>[reactive.a])
* 2 matate(draft=>draft+=1)
* 等场景的死循环
*/
export function probeDepKeyDeadCycle(internal: TInternal, fnCtx: IFnCtx, changedDepKeys: string[]): boolean {
export function probeDepKeyDeadCycle(internal: TInternal, fnCtx: IFnCtx, changedDepKeys: string[]): { err: Error | null; tipFn: Fn } {
const { depKeys, subFnInfo } = fnCtx;
let shortArr = fnCtx.depKeys;
let longArr = changedDepKeys;
Expand All @@ -95,16 +106,14 @@ export function probeDepKeyDeadCycle(internal: TInternal, fnCtx: IFnCtx, changed
longArr = depKeys;
}

const foundDc = includeOne(shortArr, longArr);
if (foundDc) {
let dcErrorInfo: any = { err: null, tipFn: noop };
// found dc error
if (includeOne(shortArr, longArr)) {
const cbType: CbType = subFnInfo.desc ? cbTypes.MUTATE : cbTypes.WATCH;
tryAlert(depKeyDcError(internal, fnCtx, changedDepKeys, cbType), {
logErr: false,
throwErr: false,
alertErr: internal.alertDeadCycleErr,
});
dcErrorInfo = depKeyDcError(internal, fnCtx, changedDepKeys, cbType);
alertDepKeyDeadCycleErr(internal, dcErrorInfo);
}
return foundDc;
return dcErrorInfo;
}

/**
Expand All @@ -121,7 +130,7 @@ export function inDeadCycle(usefulName: string, desc: string) {
/**
* 分析错误日志
*/
export function analyzeErrLog(usefulName: string, err: any) {
export function analyzeErrLog(usefulName: string, err: any, alertErr = true) {
const log = logMap.get(usefulName);
if (!log) return;
const { timer, errs } = log;
Expand All @@ -138,7 +147,7 @@ export function analyzeErrLog(usefulName: string, err: any) {
}
}
if (targetErr) {
tryAlert(targetErr);
tryAlert(targetErr, { alertErr });
}
errs.length = 0;
}, 0);
Expand Down
19 changes: 18 additions & 1 deletion packages/helux-core/src/factory/creator/fake.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { noop } from '@helux/utils';
import { noop, noopArr } from '@helux/utils';
import { FROM } from '../../consts';
import type { MutateFnStdItem } from '../../types/base';
import type { IReactiveMeta } from '../../types/inner';
Expand All @@ -12,6 +12,7 @@ export const fakeMutateCtx = newMutateCtx({});
export const fakeReativeMeta: IReactiveMeta = {
isReactive: false,
key: '',
fnKey: '',
sharedKey: 0,
moduleName: '',
writeKeys: [],
Expand All @@ -26,6 +27,22 @@ export const fakeMutateFnItem: MutateFnStdItem = {
depKeys: [],
oriDesc: '',
desc: '',
watchKey: '',
};

export const fakeInternal = buildInternal({ rawState: {}, forAtom: false, usefulName: '' } as any, {} as any);

export const newFakeFnItem = (partial: any) => {
const { desc = '', fn = noop, task = noop, depKeys = [], deps = noopArr } = partial;
return {
fn,
task,
deps,
oriDesc: '',
desc,
depKeys,
checkDeadCycle: undefined,
watchKey: '',
...partial,
};
};
2 changes: 1 addition & 1 deletion packages/helux-core/src/factory/creator/loading.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const { GLOGAL_LOADING, PRIVATE_LOADING } = STATE_TYPE;
const { PRIVATE, GLOBAL } = RECORD_LOADING;
const fakeExtra: Dict = {};
const fakeLoading: Dict = {};
const fakeRenderInfo: IRenderInfo = { sn: 0, getDeps: noopArr, insKey: 0 };
const fakeRenderInfo: IRenderInfo = { time: 0, sn: 0, getDeps: noopArr, getPrevDeps: noopArr, insKey: 0 };
const fakeTuple = [createSafeLoading(fakeExtra, fakeLoading, MUTATE), noop, fakeRenderInfo];

interface IInitLoadingCtxOpt {
Expand Down
Loading

0 comments on commit d92636c

Please sign in to comment.