-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cef4396
commit fff7e0b
Showing
8 changed files
with
3,300 additions
and
2,131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { tracked } from '@glimmer/tracking'; | ||
import { isDestroyed, isDestroying } from '@ember/destroyable'; | ||
import { waitForPromise } from '@ember/test-waiters'; | ||
|
||
import { LifecycleResource } from './lifecycle'; | ||
|
||
import type { ArgsWrapper } from '../types'; | ||
|
||
export const FUNCTION_TO_RUN = Symbol('FUNCTION TO RUN'); | ||
|
||
// type UnwrapAsync<T> = T extends Promise<infer U> ? U : T; | ||
// type GetReturn<T extends () => unknown> = UnwrapAsync<ReturnType<T>>; | ||
export type ResourceFn<Return = unknown, Args extends unknown[] = unknown[]> = ( | ||
previous: Return | undefined, | ||
...args: Args | ||
) => Return | Promise<Return>; | ||
|
||
export interface BaseArgs<FnArgs extends unknown[]> extends ArgsWrapper { | ||
positional: FnArgs; | ||
} | ||
|
||
export class FunctionRunner< | ||
Return = unknown, | ||
Args extends unknown[] = unknown[], | ||
Fn extends ResourceFn<Return, Args> = ResourceFn<Return, Args> | ||
> extends LifecycleResource<BaseArgs<Args>> { | ||
// Set when using useResource | ||
declare [FUNCTION_TO_RUN]: Fn; | ||
|
||
@tracked _asyncValue: Return | undefined; | ||
declare _syncValue: Return | undefined; | ||
|
||
get value(): Return | undefined { | ||
return this._asyncValue || this._syncValue; | ||
} | ||
|
||
get funArgs() { | ||
return this.args.positional; | ||
} | ||
|
||
setup() { | ||
this.update(); | ||
} | ||
|
||
update() { | ||
/** | ||
* All positional args are consumed | ||
*/ | ||
let result = this[FUNCTION_TO_RUN](this.value, ...this.funArgs); | ||
|
||
if (typeof result === 'object') { | ||
if ('then' in result) { | ||
const recordValue = (value: Return) => { | ||
if (isDestroying(this) || isDestroyed(this)) { | ||
return; | ||
} | ||
|
||
this._asyncValue = value; | ||
}; | ||
|
||
waitForPromise(result); | ||
|
||
result.then(recordValue); | ||
|
||
return; | ||
} | ||
} | ||
|
||
this._syncValue = result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.