Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangfisher committed Jan 26, 2024
1 parent 2ed2880 commit 226c5f3
Show file tree
Hide file tree
Showing 12 changed files with 235 additions and 217 deletions.
2 changes: 1 addition & 1 deletion docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
},
"dependencies": {
"color": "^4.2.3",
"helux": "^4.2.4",
"helux": "^4.2.5",
"helux-store": "workspace:^",
"speed-form": "workspace:^"
}
Expand Down
2 changes: 1 addition & 1 deletion example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"axios": "^1.6.2",
"classnames": "^2.3.2",
"color": "^4.2.3",
"helux": "^4.2.4",
"helux": "^4.2.5",
"helux-store": "workspace:^",
"react": "^18.2.0",
"react-dom": "^18.2.0",
Expand Down
2 changes: 1 addition & 1 deletion example/src/FormDemo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { useCallback } from "react";
import Card from "./components/Card"
import Network from './forms/network';
import JsonViewer from "./components/JsonViewer"
import { AsyncComputedObject, watch } from "helux-store";
import { AsyncComputedObject } from "helux-store";
import classnames from 'classnames';
import { ReactFC } from "./types";
import ColorBlock from "./components/ColorBlock";
Expand Down
14 changes: 5 additions & 9 deletions example/src/forms/network.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
import { ComputedScopeRef, Dict, computed } from "helux-store";
import { ComputedScopeRef, Dict, computed,watch } from "helux-store";
import { createForm,action } from "speed-form";
// import { Project, getProjects } from "../api/getProjects";
import { delay } from "flex-tools/async/delay";
import validator from "validator";
let count =1
// 声明表单数据
const formSchema = {
dirty: computed<boolean>(()=>{
return true
dirty:watch((value)=>{
return value
},(path)=>{
return path[path.length-1] ==='value'
}),
onlyReady: computed<boolean>(()=>{
return true
},{depends:[]}),
submiting: computed<boolean>(()=>{
return true
},{depends:[]}),
fields: {
asyncTitle: {
value: "React-Helux-Form",
Expand Down
2 changes: 1 addition & 1 deletion packages/form/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"@testing-library/react": "^14.1.2",
"@types/react": "^18.2.42",
"@vitejs/plugin-react": "^4.2.1",
"helux": "^4.2.4",
"helux": "^4.2.5",
"jsdom": "^23.0.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/store/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"@testing-library/react": "^14.1.2",
"@types/react": "^18.2.42",
"@vitejs/plugin-react": "^4.2.1",
"helux": "^4.2.4",
"helux": "^4.2.5",
"jsdom": "^23.0.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
Expand Down
52 changes: 27 additions & 25 deletions packages/store/src/computed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,16 +265,16 @@ export function computed<R = any,ExtraAttrs extends Dict = {}>( getter: any,depe

opts.async = isAsync;
opts.depends = deps;
const fn = () => {
const descriptor = () => {
return {
getter,
fn:getter,
options: opts,
};
};

// @ts-ignore
fn.__COMPUTED__ = isAsync ? 'async' : 'sync';
return fn //as ComputedDescriptor<R & ExtraAttrs>;
descriptor.__COMPUTED__ = isAsync ? 'async' : 'sync';
return descriptor //as ComputedDescriptor<R & ExtraAttrs>;
}

/**
Expand Down Expand Up @@ -317,11 +317,11 @@ function getComputedId(valuePath:string[],idArg:ComputedOptions['id']){
/**
* 为同步计算属性生成mutate
* @param stateCtx
* @param computedContext
* @param computedParams
*/
function createComputedMutate<Store extends StoreSchema<any>>(stateCtx: ISharedCtx<Store["state"]>, computedContext: IOperateParams,computeObjects:StoreExtendObjects['computedObjects'], storeOptions: Required<StoreOptions>) {
function createComputedMutate<Store extends StoreSchema<any>>(stateCtx: ISharedCtx<Store["state"]>, computedParams: IOperateParams,computeObjects:StoreExtendObjects['computedObjects'], storeOptions: Required<StoreOptions>) {

let { fullKeyPath:valuePath, value, parent } = computedContext;
const { fullKeyPath:valuePath, parent,value } = computedParams;
const { onCreateComputed } = storeOptions;
let { fn: getter, options: computedOptions } = value() as ComputedDescriptorParams<any>

Expand All @@ -344,8 +344,8 @@ function createComputedMutate<Store extends StoreSchema<any>>(stateCtx: ISharedC
fn: (draft, params) => {
const { input } = params;
// 1. 根据配置参数获取计算函数的上下文对象
const thisDraft = getComputedRefDraft(draft,{input,computedOptions,computedContext,storeOptions,type:"context"})
const scopeDraft= getComputedRefDraft(draft,{input,computedOptions,computedContext,storeOptions,type:"scope"})
const thisDraft = getComputedRefDraft(draft,{input,computedOptions,computedContext: computedParams,storeOptions,type:"context"})
const scopeDraft= getComputedRefDraft(draft,{input,computedOptions,computedContext: computedParams,storeOptions,type:"scope"})
// 2. 执行getter函数
let computedResult = computedOptions.initial;
try {
Expand All @@ -364,7 +364,7 @@ function createComputedMutate<Store extends StoreSchema<any>>(stateCtx: ISharedC
// 关闭死循环检测,信任开发者
checkDeadCycle: false,
});
computedContext.replaceValue(getVal(witness.snap, valuePath));
computedParams.replaceValue(getVal(witness.snap, valuePath));
return computeObjects[mutateId]
}

Expand Down Expand Up @@ -519,17 +519,18 @@ async function executeComputedGetter<R>(draft:any,getter:AsyncComputedGetter<R>,
/**
* 为异步计算属性生成mutate
* @param stateCtx
* @param computedContext
* @param computedParams
*/
function createAsyncComputedMutate<Store extends StoreSchema<any>>(stateCtx: ISharedCtx<Store["state"]>,computedContext: IOperateParams,computeObjects:IStore['computedObjects'],storeOptions: Required<StoreOptions>) {
const { fullKeyPath:valuePath, value, parent } = computedContext;
function createAsyncComputedMutate<Store extends StoreSchema<any>>(stateCtx: ISharedCtx<Store["state"]>,computedParams: IOperateParams,computeObjects:IStore['computedObjects'],storeOptions: Required<StoreOptions>) {
const { fullKeyPath:valuePath, parent ,value } = computedParams;
const { onCreateComputed } = storeOptions;

// 排除掉所有非own属性,例如valueOf等
if (parent && !Object.hasOwn(parent, valuePath[valuePath.length - 1])) {
return;
}
let { fn: getter, options: computedOptions } = value() as ComputedDescriptorParams<any>
if(!getter) debugger
computedOptions.async = true;


Expand All @@ -542,7 +543,7 @@ function createAsyncComputedMutate<Store extends StoreSchema<any>>(stateCtx: ISh
let isMutateRunning = false // 正在运行标志

// 根据配置读取计算函数的返回值以及状态等 应该更新到哪里
const computedResultPath:string[] =switchValue(toComputedResult,{
const computedResultPath:string[] = switchValue(toComputedResult,{
self:valuePath,
root: [],
parent: valuePath.slice(0,valuePath.length-2),
Expand Down Expand Up @@ -572,7 +573,7 @@ function createAsyncComputedMutate<Store extends StoreSchema<any>>(stateCtx: ISh
}
}
},
// @ts-ignore 此函数在依赖变化时执行,用来异步计算
// 此函数在依赖变化时执行,用来异步计算
task: async ({ draft, setState, input, extraArgs }) => {
// 当使用run方法时可以传入参数来覆盖默认的计算函数的配置参数
const finalComputedOptions = Object.assign({},computedOptions,extraArgs) as Required<ComputedOptions>
Expand All @@ -586,7 +587,7 @@ function createAsyncComputedMutate<Store extends StoreSchema<any>>(stateCtx: ISh
input,
computedResultPath,
computedOptions:finalComputedOptions,
computedContext,
computedContext: computedParams,
storeOptions,
setState
})
Expand All @@ -598,34 +599,35 @@ function createAsyncComputedMutate<Store extends StoreSchema<any>>(stateCtx: ISh
desc:mutateId,
checkDeadCycle: false,
});
computedContext.replaceValue(getVal(witness.snap, valuePath));
computedParams.replaceValue(getVal(witness.snap, valuePath));
computeObjects[mutateId] = witness
return computeObjects[mutateId]
}


export function installComputed<Store extends StoreSchema<any>>(options:StoreExtendContext<ISharedCtx<Store["state"]>>) {
const { stateCtx,fn,params,storeOptions,extendObjects} =options
const { stateCtx,params,storeOptions,extendObjects} = options
const descriptor = params.value
//@ts-ignore
if (fn.__COMPUTED__=='async') {
createAsyncComputedMutate<Store>(stateCtx, params,extendObjects.computedObjects, storeOptions);
if (descriptor.__COMPUTED__=='async') {
createAsyncComputedMutate<Store>(stateCtx,params,extendObjects.computedObjects, storeOptions);
//@ts-ignore
}else if (fn.__COMPUTED__=='sync') {
}else if (descriptor.__COMPUTED__=='sync') {
createComputedMutate<Store>(stateCtx, params,extendObjects.computedObjects, storeOptions);
}else if (isAsyncFunction(fn)) { // 简单的异步计算函数,没有通过computed函数创建,此时由于没有指定依赖,所以只会执行一次
}else if (isAsyncFunction(descriptor)) { // 简单的异步计算函数,没有通过computed函数创建,此时由于没有指定依赖,所以只会执行一次
params.value = () => ({
getter: fn,
fn: descriptor,
options: {
depends : [], // 未指定依赖
initial : undefined, // 也没有初始化值
immediate: true, // 立即执行
context : storeOptions.computedThis,
},
});
createAsyncComputedMutate<Store>(stateCtx, params,extendObjects.computedObjects, storeOptions);
createAsyncComputedMutate<Store>(stateCtx,params,extendObjects.computedObjects, storeOptions);
}else { // 简单的同步计算函数,没有通过computed函数创建
params.value = () => ({
getter: fn,
fn: descriptor,
options: {
initial : undefined,
context: storeOptions.computedThis,
Expand Down
7 changes: 3 additions & 4 deletions packages/store/src/extends.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
import { IOperateParams, ISharedCtx } from "helux";
import type { StoreExtendObjects, StoreOptions, StoreSchema } from "./store";
import { isSkipComputed } from "./utils";
import { ComputedDescriptor, installComputed } from "./computed";
import { installComputed } from "./computed";
import { installWatch } from "./watch";

export interface StoreExtendContext<Ctx>{
stateCtx:Ctx
extendObjects:StoreExtendObjects
storeOptions: Required<StoreOptions>
descriptor:Function | ComputedDescriptor
params:IOperateParams
}

Expand All @@ -34,11 +34,10 @@ export function installExtends<Store extends StoreSchema<any>>(stateCtx: IShared
stateCtx,
extendObjects,
storeOptions,
descriptor:value,
params
}
if(value.__COMPUTED__=='watch'){
// 安装watch函数扩展
//installWatch<Store>(ctx)
}else{ // 安装计算函数扩展
installComputed<Store>(ctx)
}
Expand Down
1 change: 1 addition & 0 deletions packages/store/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { ComputedOptions } from './computed';
import { deepClone } from "flex-tools/object/deepClone";
import { log } from "./utils";
import { installExtends } from "./extends"

export interface StoreSchema<State> extends Dict{
state:State
actions?:Actions<State>
Expand Down
2 changes: 1 addition & 1 deletion packages/store/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export function isSkipComputed(fn:Function){
}

export function log(message:any,level:'log' | 'error' | 'warn'='log'){
let msg = typeof(message) === 'function' ? message() : message
let msg = typeof(message) === 'function' ? message() : (message instanceof Error ? message.stack : message)
try{
console[level]("[helux-store] ",...Array.isArray(msg) ? msg : [msg])
}catch{}
Expand Down
Loading

0 comments on commit 226c5f3

Please sign in to comment.