-
-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(runtime): remove ILifecycleTask
- Loading branch information
Showing
8 changed files
with
118 additions
and
286 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* eslint-disable @typescript-eslint/promise-function-async */ | ||
import { | ||
DI, | ||
IContainer, | ||
Key, | ||
Registration, | ||
Resolved, | ||
} from '@aurelia/kernel'; | ||
|
||
export type TaskSlot = ( | ||
'beforeCreate' | | ||
'beforeCompile' | | ||
'beforeCompileChildren' | | ||
'beforeActivate' | | ||
'afterActivate' | | ||
'beforeDeactivate' | | ||
'afterDeactivate' | ||
); | ||
|
||
export const IAppTask = DI.createInterface<IAppTask>('IAppTask').noDefault(); | ||
export interface IAppTask extends Pick< | ||
$AppTask, | ||
'slot' | | ||
'run' | | ||
'register' | ||
> {} | ||
|
||
export interface ICallbackSlotChooser<K extends Key> extends Pick< | ||
$AppTask<K>, | ||
'beforeCreate' | | ||
'beforeCompile' | | ||
'beforeCompileChildren' | | ||
'beforeActivate' | | ||
'afterActivate' | | ||
'beforeDeactivate' | | ||
'afterDeactivate' | ||
> {} | ||
|
||
export interface ICallbackChooser<K extends Key> extends Pick< | ||
$AppTask<K>, | ||
'call' | ||
> {} | ||
|
||
class $AppTask<K extends Key = Key> { | ||
public slot: TaskSlot = (void 0)!; | ||
public callback: (instance: unknown) => void | Promise<void> = (void 0)!; | ||
public container: IContainer = (void 0)!; | ||
|
||
private constructor( | ||
private readonly key: K, | ||
) {} | ||
|
||
public static with<K1 extends Key>(key: K1): ICallbackSlotChooser<K1> { | ||
return new $AppTask(key); | ||
} | ||
|
||
public beforeCreate(): ICallbackChooser<K> { | ||
return this.at('beforeCreate'); | ||
} | ||
|
||
public beforeCompile(): ICallbackChooser<K> { | ||
return this.at('beforeCompile'); | ||
} | ||
|
||
public beforeCompileChildren(): ICallbackChooser<K> { | ||
return this.at('beforeCompileChildren'); | ||
} | ||
|
||
public beforeActivate(): ICallbackChooser<K> { | ||
return this.at('beforeActivate'); | ||
} | ||
|
||
public afterActivate(): ICallbackChooser<K> { | ||
return this.at('afterActivate'); | ||
} | ||
|
||
public beforeDeactivate(): ICallbackChooser<K> { | ||
return this.at('beforeDeactivate'); | ||
} | ||
|
||
public afterDeactivate(): ICallbackChooser<K> { | ||
return this.at('afterDeactivate'); | ||
} | ||
|
||
public at(slot: TaskSlot): ICallbackChooser<K> { | ||
this.slot = slot; | ||
return this; | ||
} | ||
|
||
public call<K1 extends Key = K>(fn: (instance: Resolved<K1>) => void | Promise<void>): IAppTask { | ||
this.callback = fn as (instance: unknown) => void | Promise<void>; | ||
return this; | ||
} | ||
|
||
public register(container: IContainer): IContainer { | ||
return this.container = container.register(Registration.instance(IAppTask, this)); | ||
} | ||
|
||
public run(): void | Promise<void> { | ||
const callback = this.callback; | ||
const instance = this.container.get(this.key); | ||
return callback(instance); | ||
} | ||
} | ||
export const AppTask = $AppTask as { | ||
with<K extends Key>(key: K): ICallbackSlotChooser<K>; | ||
}; |
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.