Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangfisher committed Aug 14, 2024
1 parent df6b672 commit 7f92d0c
Show file tree
Hide file tree
Showing 12 changed files with 47 additions and 47 deletions.
6 changes: 3 additions & 3 deletions packages/core/src/action.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@
import { ReactNode, useCallback, useRef, RefObject,useState} from "react";
import React from "react";
import type { RequiredFormOptions } from "./form";
import { AsyncComputedGetter, AsyncComputedObject, ComputedDescriptorInfo, ComputedOptions, ComputedParams, Dict, IStore, RuntimeComputedOptions, computed, getValueByPath} from '@speedform/reactive';
import { AsyncComputedGetter, AsyncComputedObject, ComputedDescriptorDefine, ComputedOptions, ComputedParams, Dict, IStore, RuntimeComputedOptions, computed, getValueByPath} from '@speedform/reactive';
import { omit } from "flex-tools/object/omit";
import { getFormData } from "./serialize";
import { getId } from "./utils";
import { FIELDS_STATE_KEY } from "./consts";

export type ActionComputedAttr<R=unknown,Fields=any> = ((fields:Fields)=>R)
| ((fields:Fields)=>Promise<R>)
| ComputedDescriptorInfo<R>
| ComputedDescriptorDefine<R>
| R


Expand Down Expand Up @@ -95,7 +95,7 @@ export interface FormActionExtra{


// 经过创建表单后的动作对象, execute
export type FormAction<T extends FormActionDefine> =ComputedDescriptorInfo<{
export type FormAction<T extends FormActionDefine> =ComputedDescriptorDefine<{
[Key in keyof T]: Key extends 'execute' ? never : T[Key]
}>

Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -407,8 +407,8 @@ function createFormComponent<State extends Dict = Dict>(store: IStore<State>,for


const form = createForm({
title:1,
// dirty:computed<number>(()=>1),
title:computed<string>(async ()=>"true"),
// dirty:computed<boolean>(()=>true),
// validate: computed(async ()=>{
// return true
// },["username"]),
Expand Down
34 changes: 17 additions & 17 deletions packages/reactive/src/__tests__/async/async.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,26 +306,26 @@ describe("执行分组计算",()=>{



type FormDefine = {
price:ComputedAttr<number>
count:ComputedAttr<number>
total:ComputedAttr<number>
}
// type FormDefine = {
// price:ComputedAttr<number>
// count:ComputedAttr<number>
// total:ComputedAttr<number>
// }

function createForm<S extends FormDefine>(state:S){
return createStore(state)
}
// function createForm<S extends FormDefine>(state:S){
// return createStore(state)
// }


const fm = createForm({
price:computed<number>(async ()=>1,[]),
count:computed(()=>1),
total:computed(async (scope)=>{
return scope.price * scope.count
},['price','count'])
})
// const fm = createForm({
// price:computed<number>(async ()=>1,[]),
// count:computed(()=>1),
// total:computed(async (scope)=>{
// return scope.price * scope.count
// },['price','count'])
// })

fm.state.price
fm.state.count
// fm.state.price
// fm.state.count


4 changes: 2 additions & 2 deletions packages/reactive/src/computed/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { delay } from 'flex-tools/async/delay';
import { OBJECT_PATH_DELIMITER } from '../consts';
import { getComputedScope } from '../context';
import { AsyncComputedObject, ComputedOptions, ComputedParams, ComputedProgressbar } from './types';
import type { ComputedDescriptorInfo, ComputedRunContext } from './types';
import type { ComputedDescriptorDefine, ComputedRunContext } from './types';
import { IReactiveReadHookParams } from '../reactives/types';
import { ComputedObject } from './computedObject';
import { createAsyncComputedObject, executeStoreHooks } from './utils';
Expand Down Expand Up @@ -242,7 +242,7 @@ export function createAsyncComputedMutate<T extends Dict,R=any>(computedParams:
// }

// 2. 获取到计算属性描述信息: 包括getter和配置。 此时value是一个函数
let { getter, options: computedOptions } = value() as ComputedDescriptorInfo<any>
let { getter, options: computedOptions } = value() as ComputedDescriptorDefine<any>
computedOptions.async = true;

// 3.运行Hook: 用来在创建computed前运行,允许拦截更改计算函数的依赖,上下文,以及getter等
Expand Down
6 changes: 3 additions & 3 deletions packages/reactive/src/computed/computed.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isAsyncFunction } from "flex-tools/typecheck/isAsyncFunction";
import type { ComputedDescriptorCreator, Dict } from "../types";
import type { ComputedDescriptor, Dict } from "../types";
import { AsyncComputedGetter, ComputedDepends, ComputedGetter, ComputedOptions } from "./types";
import { normalizeDeps } from "../utils/normalizeDeps";

Expand All @@ -19,7 +19,7 @@ import { normalizeDeps } from "../utils/normalizeDeps";
* @returns
*
*/
export function computed<R = any,ExtraAttrs extends Dict = {}>( getter: AsyncComputedGetter<R>,depends?:ComputedDepends,options?: ComputedOptions<R,ExtraAttrs>): ComputedDescriptorCreator<R>;
export function computed<R = any,ExtraAttrs extends Dict = {}>( getter: AsyncComputedGetter<R>,depends?:ComputedDepends,options?: ComputedOptions<R,ExtraAttrs>): ComputedDescriptor<R>;
export function computed<R = any,ExtraAttrs extends Dict = {}>( getter: ComputedGetter<R>, options?: ComputedOptions<R,ExtraAttrs>): R
export function computed<R = any,ExtraAttrs extends Dict = {}>( getter: any,depends?:any, options?: ComputedOptions<R,ExtraAttrs>):any {

Expand Down Expand Up @@ -61,7 +61,7 @@ export function computed<R = any,ExtraAttrs extends Dict = {}>( getter: any,depe
opts.async = isAsync;
opts.depends = normalizeDeps(deps) ;

const descriptor:ComputedDescriptorCreator<R> = () => {
const descriptor:ComputedDescriptor<R> = () => {
return {
getter,
options: opts,
Expand Down
12 changes: 6 additions & 6 deletions packages/reactive/src/computed/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import { IOperateParams, ISharedCtx } from "helux";
import type { ComputedScope, ITargetState } from "../store/types";
import { Dict } from "../types"
import { WatchDescriptorCreator } from "../watch";
import { WatchDescriptor } from "../watch";
import { Reactiveable } from "../reactives/types";


Expand All @@ -21,8 +21,8 @@ export type AsyncReturnType<T extends (...args: any) => any> = T extends (...arg
T extends (...args: any) => infer R ? R : any)


export type PickComputedResult<T> = T extends ComputedDescriptorCreator<infer X> ? AsyncComputedObject<X> :
( T extends WatchDescriptorCreator<any,infer X> ? X :
export type PickComputedResult<T> = T extends ComputedDescriptor<infer X> ? AsyncComputedObject<X> :
( T extends WatchDescriptor<any,infer X> ? X :
( T extends ComputedSyncReturns<infer X> ? X:
(T extends AsyncComputed<infer X> ? AsyncComputedObject<X>:
(T extends Computed<infer R> ? R : T)
Expand Down Expand Up @@ -217,14 +217,14 @@ export interface StateValueDescriptorParams<Fn extends Function,Options extends



export type ComputedDescriptorInfo<R=any> = {
export type ComputedDescriptorDefine<R=any> = {
getter: AsyncComputedGetter<R> | ComputedGetter<R>;
options: ComputedOptions<R>;
}


export interface ComputedDescriptorCreator<R=any> {
():ComputedDescriptorInfo<R>
export interface ComputedDescriptor<R=any> {
():ComputedDescriptorDefine<R>
__COMPUTED__: 'sync' | 'async' | 'watch'
}

Expand Down
4 changes: 2 additions & 2 deletions packages/reactive/src/watch/install.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { OBJECT_PATH_DELIMITER } from "../consts"
import { IStore } from "../store/types"
import { setVal } from "../utils"
import { WatchDescriptor } from "./types"
import { WatchDescriptorDefine } from "./types"
import { IReactiveReadHookParams } from "../reactives/types"
import { Dict } from "../types"

Expand All @@ -15,7 +15,7 @@ export function installWatch<T extends Dict>(params:IReactiveReadHookParams,stor

store.options.log(`install watch for <${params.path.length==0 ? "Dynamic" : params.path.join(OBJECT_PATH_DELIMITER)}>`)

const watchDescriptor = params.value() as WatchDescriptor
const watchDescriptor = params.value() as WatchDescriptorDefine

watchDescriptor.options.path = params.path

Expand Down
6 changes: 3 additions & 3 deletions packages/reactive/src/watch/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ export type WatchListenerOptions<Result=any> = {getSelfValue:()=>Result ,selfPat
export type WatchListener<Value=any, Result= Value> = (path:string[],value:Value,watchObject:WatchObject<any>)=>(Exclude<Result,Promise<any>> | undefined)


export type WatchDescriptor<Value=any, Result=Value> = {
export type WatchDescriptorDefine<Value=any, Result=Value> = {
listener : WatchListener<Value,Result>;
options : WatchOptions<Result>;
}

export interface WatchDescriptorCreator<Value = any,Result=Value> {
():WatchDescriptor<Value,Result>;
export interface WatchDescriptor<Value = any,Result=Value> {
():WatchDescriptorDefine<Value,Result>;
__COMPUTED__: 'sync' | 'async' | 'watch'
}

Expand Down
4 changes: 2 additions & 2 deletions packages/reactive/src/watch/useWatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useEffect } from "react"
import { ComputedScopeRef, IStore, Dict } from "../types"
import { sharex } from "helux"
import { installWatch } from "./install"
import { WatchDescriptor, WatchDependParams, WatchListener, WatchOptions } from "./types"
import { WatchDescriptorDefine, WatchDependParams, WatchListener, WatchOptions } from "./types"
import { normalizedWatchFilter } from "./utils"
import { IReactiveReadHookParams } from "../reactives/types"
/**
Expand Down Expand Up @@ -35,7 +35,7 @@ export function createUseWatch<T extends Dict>(store:IStore<T>){
enable : true,
scope : ComputedScopeRef.Depends
},options)
} as WatchDescriptor
} as WatchDescriptorDefine
return descr
}
} as unknown as IReactiveReadHookParams
Expand Down
6 changes: 3 additions & 3 deletions packages/reactive/src/watch/watch.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { WatchDependParams, WatchDescriptorCreator, WatchListener, WatchOptions } from "./types";
import { WatchDependParams, WatchDescriptor, WatchListener, WatchOptions } from "./types";
import { normalizedWatchFilter } from "./utils";

/*
Expand All @@ -21,7 +21,7 @@ import { normalizedWatchFilter } from "./utils";
* @param options
* @returns
*/
export function watch<Value =any,Result=Value>(listener:WatchListener<Value,Result>,depends?:WatchDependParams<Value>,options?:WatchOptions<Result>):WatchDescriptorCreator<Value,Result>{
export function watch<Value =any,Result=Value>(listener:WatchListener<Value,Result>,depends?:WatchDependParams<Value>,options?:WatchOptions<Result>):WatchDescriptor<Value,Result>{
const opts : WatchOptions<Result> = Object.assign({
depends:normalizedWatchFilter(depends),
enable:true
Expand All @@ -31,7 +31,7 @@ import { normalizedWatchFilter } from "./utils";
listener,
options: opts,
};
}) as WatchDescriptorCreator<Value,Result>
}) as WatchDescriptor<Value,Result>
descriptor.__COMPUTED__ = 'watch'
return descriptor
}
4 changes: 2 additions & 2 deletions packages/reactive/src/watch/watchObject.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getSnap } from "helux"
import { setVal } from "../utils/setVal"
import { WatchListener, WatchOptions, WatchDescriptor } from './types';
import { WatchListener, WatchOptions, WatchDescriptorDefine } from './types';
import { Dict, IStore } from "../types"
import { getVal } from "../utils/getVal"
import { OBJECT_PATH_DELIMITER } from "../consts"
Expand All @@ -13,7 +13,7 @@ export class WatchObject<T extends Dict> {
private _cache?: Dict
private _listener:WatchListener
private _options: Required<WatchOptions>
constructor(public store:IStore<T>,descriptor:WatchDescriptor){
constructor(public store:IStore<T>,descriptor:WatchDescriptorDefine){
this._options = Object.assign({
enable : true,
selfPath: [],
Expand Down
4 changes: 2 additions & 2 deletions packages/reactive/src/watch/watchObjects.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { watch as heluxWatch } from "helux"
import { WatchDescriptor } from './types';
import { WatchDescriptorDefine } from './types';
import { IStore, Dict } from "../types"
import { WatchObject } from "./watchObject";
import { getVal } from "../utils";
Expand Down Expand Up @@ -56,7 +56,7 @@ export class WatchObjects<T extends Dict> extends Map<string,WatchObject<T>>{
* @param watchTo 侦听结果写到处下载
* @returns
*/
add(descriptor:WatchDescriptor){
add(descriptor:WatchDescriptorDefine){
const watchObject = new WatchObject(this.store,descriptor)
this.set(watchObject.id,watchObject)
return watchObject
Expand Down

0 comments on commit 7f92d0c

Please sign in to comment.