@@ -28,6 +28,15 @@ export type Func<This = any, Arguments extends readonly unknown[] = any[], Retur
2828 : ( this : This , ...args : Arguments ) => Return ;
2929export namespace Func {
3030 export type Return < F > = F extends Func ? ReturnType < F > : never ;
31+ export type AwaitedResult < F > =
32+ | {
33+ type : "resolved" ;
34+ result : Awaited < Return < F > > ;
35+ }
36+ | {
37+ type : "rejected" ;
38+ error : unknown ;
39+ } ;
3140 export type Args < F > = F extends Func ? Parameters < F > : never ;
3241 export type This < F > = F extends Func < infer T > ? T : never ;
3342 export type SetReturn < T , R > = Func < This < T > , Args < T > , R > ;
@@ -38,6 +47,7 @@ export type FuncRemember<F extends Func, K extends KeyFun<F> | void = void> = F
3847 readonly key : Func . Return < K > | undefined ;
3948 readonly runned : boolean ;
4049 readonly returnValue : Func . Return < F > | undefined ;
50+ readonly awaitedReturnValue : Func . AwaitedResult < F > | undefined ;
4151 reset ( ) : void ;
4252 rerun ( ...args : Parameters < F > ) : Func . Return < F > ;
4353} ;
@@ -50,27 +60,48 @@ export namespace func_remember {
5060 * @returns
5161 */
5262/*@__NO_SIDE_EFFECTS__ */
53- export const func_remember = < F extends Func , K extends Func < ThisParameterType < F > , Parameters < F > > | void | void > ( func : F , key ?: K ) : FuncRemember < F , K > => {
63+ export const func_remember = < F extends Func , K extends Func < ThisParameterType < F > , Parameters < F > > | void | void > ( func : F , key ?: K , cacheAwaited ?: boolean ) : FuncRemember < F , K > => {
5464 let result :
5565 | {
5666 key : Func . Return < K > ;
5767 res : Func . Return < F > ;
68+ awaitedRes : Func . AwaitedResult < F > | undefined ;
5869 }
5970 | undefined ;
6071
6172 const once_fn = function ( this : ThisParameterType < F > , ...args : Parameters < F > ) {
6273 const newKey = key ?. apply ( this , args ) ;
6374 if ( result === undefined || newKey !== result . key ) {
75+ const res = func . apply ( this , args ) ;
76+
6477 result = {
6578 key : newKey ,
66- res : func . apply ( this , args ) ,
79+ res : res ,
80+ awaitedRes : undefined ,
6781 } ;
82+ if ( cacheAwaited ) {
83+ if ( typeof res === "object" && res && "then" in res && typeof res . then === "function" ) {
84+ res . then (
85+ ( r : any ) => {
86+ if ( result && res === result . res ) {
87+ result . awaitedRes = { type : "resolved" , result : r } ;
88+ }
89+ } ,
90+ ( e : any ) => {
91+ if ( result && res === result . res ) {
92+ result . awaitedRes = { type : "rejected" , error : e } ;
93+ }
94+ } ,
95+ ) ;
96+ } else {
97+ result . awaitedRes = res ;
98+ }
99+ }
68100 }
69101 return result . res ;
70102 } ;
71103
72- const once_fn_mix = Object . assign ( once_fn as F , {
73- /// 注意,这的get
104+ const once_fn_mix = obj_assign_props ( once_fn as F , {
74105 get source ( ) {
75106 return func ;
76107 } ,
@@ -83,6 +114,9 @@ export const func_remember = <F extends Func, K extends Func<ThisParameterType<F
83114 get returnValue ( ) {
84115 return result ?. res ;
85116 } ,
117+ get awaitedReturnValue ( ) {
118+ return result ?. awaitedRes ;
119+ } ,
86120 reset ( ) {
87121 result = undefined ;
88122 } ,
@@ -91,12 +125,7 @@ export const func_remember = <F extends Func, K extends Func<ThisParameterType<F
91125 return once_fn_mix ( ...args ) as Func . Return < F > ;
92126 } ,
93127 } ) ;
94- Object . defineProperties ( once_fn_mix , {
95- source : { value : func , writable : false , configurable : true , enumerable : true } ,
96- key : { get : ( ) => result ?. key , configurable : true , enumerable : true } ,
97- runned : { get : ( ) => result != null , configurable : true , enumerable : true } ,
98- returnValue : { get : ( ) => result ?. res , configurable : true , enumerable : true } ,
99- } ) ;
128+
100129 return once_fn_mix ;
101130} ;
102131
@@ -308,6 +337,7 @@ export const func_parallel_limit = <T extends Func<void>>(
308337 return ;
309338 }
310339 const func = next . value ;
340+ console . log ( "qaq" , func ) ;
311341 //@ts -ignore
312342 const result = ( await func_catch ( func ) ( ) ) as FuncCatch . Return < unknown , Func . Return < T > > ;
313343 void returns . emit ( { source : func , result} ) ;
@@ -317,3 +347,29 @@ export const func_parallel_limit = <T extends Func<void>>(
317347
318348 return obj_assign_props ( returns , { then : done . promise . then . bind ( done . promise ) } ) ;
319349} ;
350+
351+ // const createTask = (ms: number, log: string) => {
352+ // return Object.assign(
353+ // async () => {
354+ // console.log(log, "start");
355+ // await delay(ms);
356+ // console.log(log, "done");
357+ // },
358+ // {
359+ // id: log,
360+ // },
361+ // );
362+ // };
363+ // await func_parallel_limit(
364+ // [
365+ // //
366+ // createTask(1000, "task1"),
367+ // createTask(1000, "task2"),
368+ // createTask(1000, "task3"),
369+ // createTask(1000, "task4"),
370+ // createTask(1000, "task5"),
371+ // createTask(1000, "task6"),
372+ // ],
373+ // 2,
374+ // );
375+ // console.log("all done");
0 commit comments