Skip to content

Commit

Permalink
refactor(runtime): remove ILifecycleTask
Browse files Browse the repository at this point in the history
  • Loading branch information
fkleuver committed Oct 12, 2020
1 parent 655a5e5 commit 69f5fac
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 286 deletions.
2 changes: 1 addition & 1 deletion packages/__tests__/i18n/i18n.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('I18N', function () {
eaSpy.getMock(new EventAggregator()),
mockSignaler
);
await sut.task.wait();
await sut.initPromise;
await sut.setLocale('en');
return { i18nextSpy, sut, eaSpy, mockSignaler };
}
Expand Down
2 changes: 1 addition & 1 deletion packages/i18n/src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function coreComponents(options: I18nConfigurationOptions) {
register(container: IContainer) {
return container.register(
Registration.callback(I18nInitOptions, () => options.initOptions),
AppTask.with(I18N).beforeActivate().call(i18n => i18n.task.wait() as Promise<void>),
AppTask.with(I18N).beforeActivate().call(i18n => i18n.initPromise),
Registration.singleton(I18nWrapper, I18nextWrapper),
Registration.singleton(I18N, I18nService),

Expand Down
10 changes: 5 additions & 5 deletions packages/i18n/src/i18n.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DI, IEventAggregator, PLATFORM } from '@aurelia/kernel';
import { ILifecycleTask, ISignaler, PromiseTask } from '@aurelia/runtime';
import { ISignaler } from '@aurelia/runtime';
import i18nextCore from 'i18next';
import { I18nInitOptions } from './i18n-configuration-options';
import { I18nextWrapper, I18nWrapper } from './i18next-wrapper';
Expand Down Expand Up @@ -36,7 +36,7 @@ export class I18nKeyEvaluationResult {

export interface I18N {
i18next: i18nextCore.i18n;
readonly task: ILifecycleTask;
readonly initPromise: Promise<void>;
/**
* Evaluates the `keyExpr` to translated values.
* For a single key, `I18nService#tr` method can also be easily used.
Expand Down Expand Up @@ -100,15 +100,15 @@ export class I18nService implements I18N {
public i18next: i18nextCore.i18n;
/**
* This is used for i18next initialization and awaited for before the bind phase.
* If need be (usually there is none), this task can be awaited for explicitly in client code.
* If need be (usually there is none), this can be awaited for explicitly in client code.
*/
public readonly task: ILifecycleTask;
public readonly initPromise: Promise<void>;
private options!: I18nInitOptions;
private readonly intl: typeof Intl;

public constructor(@I18nWrapper i18nextWrapper: I18nextWrapper, @I18nInitOptions options: I18nInitOptions, @IEventAggregator private readonly ea: IEventAggregator, @ISignaler private readonly signaler: ISignaler) {
this.i18next = i18nextWrapper.i18next;
this.task = new PromiseTask(this.initializeI18next(options), null, this);
this.initPromise = this.initializeI18next(options);
this.intl = PLATFORM.global.Intl;
}

Expand Down
8 changes: 2 additions & 6 deletions packages/router/src/task-queue.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IScheduler, ITask, ILifecycleTask } from '@aurelia/runtime';
import { IScheduler, ITask } from '@aurelia/runtime';
import { bound } from '@aurelia/kernel';

/**
Expand All @@ -15,7 +15,7 @@ export type QueueableFunction = ((task: QueueTask<void>) => void | Promise<void>
/**
* @internal - Shouldn't be used directly
*/
export class QueueTask<T> implements ILifecycleTask {
export class QueueTask<T> {
public done: boolean = false;
private readonly promise: Promise<void>;

Expand Down Expand Up @@ -47,10 +47,6 @@ export class QueueTask<T> implements ILifecycleTask {
public wait(): Promise<void> {
return this.promise;
}
public canCancel(): boolean {
return false;
}
public cancel(): void { return; }
}

export interface ITaskQueueOptions {
Expand Down
107 changes: 107 additions & 0 deletions packages/runtime/src/app-task.ts
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>;
};
2 changes: 1 addition & 1 deletion packages/runtime/src/aurelia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
ILifecycle,
ICustomElementController,
} from './lifecycle';
import { IAppTask, TaskSlot } from './lifecycle-task';
import { IAppTask, TaskSlot } from './app-task';
import { CustomElement, CustomElementDefinition } from './resources/custom-element';
import { Controller } from './templating/controller';
import { HooksDefinition } from './definitions';
Expand Down
11 changes: 1 addition & 10 deletions packages/runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -482,19 +482,10 @@ export {
IComponentFactory,
} from './templating/render-context';
export {
PromiseOrTask,
MaybePromiseOrTask,
AggregateContinuationTask,
TerminalTask,
AggregateTerminalTask,
ContinuationTask,
ILifecycleTask,
LifecycleTask,
PromiseTask,
TaskSlot,
AppTask,
IAppTask,
} from './lifecycle-task';
} from './app-task';
export {
AccessorOrObserver,
AccessorType,
Expand Down
Loading

0 comments on commit 69f5fac

Please sign in to comment.