Skip to content

Commit

Permalink
Fixes (#194)
Browse files Browse the repository at this point in the history
* fix(RuntimeCompilationResources): do not auto register

* fix(Observer): use identity instead of noop

* chore(Observer): fix test

* chore(lint): remove invalid-this rule

* fix(test): remove .only

* fix(CustomElement): flush at startup

* feat(Aurelia): self register to container

* fix(CustomElement): remove unnecessary flush

* fix(Observer): revert to noop

* fix(Renderer): remove unnecessary checks
  • Loading branch information
bigopon authored and EisenbergEffect committed Sep 28, 2018
1 parent 1683135 commit 5971d36
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 16 deletions.
16 changes: 13 additions & 3 deletions packages/runtime/src/aurelia.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DI, IContainer, IRegistry, PLATFORM } from '@aurelia/kernel';
import { DI, IContainer, IRegistry, PLATFORM, Registration } from '@aurelia/kernel';
import { BindingFlags } from './binding/binding-flags';
import { Lifecycle, LifecycleFlags } from './templating';
import { ICustomElement } from './templating/custom-element';
Expand All @@ -13,9 +13,14 @@ export class Aurelia {
private components: ICustomElement[] = [];
private startTasks: (() => void)[] = [];
private stopTasks: (() => void)[] = [];
private isStarted: boolean = false;
private isStarted = false;
private _root: ICustomElement = null;

constructor(private container: IContainer = DI.createContainer()) {}
constructor(private container: IContainer = DI.createContainer()) {
Registration
.instance(Aurelia, this)
.register(container, Aurelia);
}

public register(...params: (IRegistry | Record<string, Partial<IRegistry>>)[]): this {
this.container.register(...params);
Expand All @@ -27,6 +32,7 @@ export class Aurelia {

const startTask = () => {
if (!this.components.includes(component)) {
this._root = component;
this.components.push(component);
component.$hydrate(
this.container.get(IRenderingEngine),
Expand Down Expand Up @@ -64,6 +70,10 @@ export class Aurelia {
return this;
}

public root(): ICustomElement | null {
return this._root;
}

public start(): this {
this.startTasks.forEach(x => x());
this.isStarted = true;
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime/src/binding/property-observation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export class Observer implements Observer {
public propertyKey: string;
public currentValue: IIndexable | Primitive;

private callback: (oldValue: IIndexable | Primitive, newValue: IIndexable | Primitive) => IIndexable | Primitive;
private callback: (newValue: IIndexable | Primitive, oldValue: IIndexable | Primitive) => IIndexable | Primitive;

constructor(
instance: object,
Expand Down
9 changes: 1 addition & 8 deletions packages/runtime/src/templating/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,8 @@ export class Renderer implements IRenderer {
for (let i = 0, ii = childInstructions.length; i < ii; ++i) {
const current = childInstructions[i];
const currentType = current.type;
let realTarget;

if (currentType === TargetedInstructionType.stylePropertyBinding || currentType === TargetedInstructionType.listenerBinding) {
realTarget = target;
} else {
realTarget = component;
}

(this as any)[currentType](renderable, realTarget, current);
(this as any)[currentType](renderable, component, current);
}

renderable.$bindables.push(component);
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime/src/templating/rendering-engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ export class RuntimeCompilationResources implements IResourceDescriptions {

public find<TSource>(kind: IResourceKind<TSource>, name: string): ResourceDescription<TSource> | null {
const key = kind.keyFrom(name);
const resolver = this.context.getResolver<TSource>(key);
const resolver = this.context.getResolver<TSource>(key, false);

if (resolver !== null && resolver.getFactory) {
const factory = resolver.getFactory(this.context);
Expand Down
4 changes: 4 additions & 0 deletions packages/runtime/test/unit/aurelia.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ describe('Aurelia', () => {
sut = new Aurelia();
});

it('should initialize container directly', () => {
expect(sut['container'].get(Aurelia)).to.equal(sut);
});

it('should initialize correctly', () => {
expect(sut['components'].length).to.equal(0);
expect(sut['startTasks'].length).to.equal(0);
Expand Down
13 changes: 12 additions & 1 deletion packages/runtime/test/unit/binding/property-observation.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { spy } from 'sinon';
import { PLATFORM } from '../../../../kernel/src/index';
import { PrimitiveObserver, SetterObserver, ChangeSet, BindingFlags } from '../../../src/index';
import { PrimitiveObserver, SetterObserver, ChangeSet, BindingFlags, Observer } from '../../../src/index';
import { expect } from 'chai';
import { SpySubscriber } from '../util';

Expand Down Expand Up @@ -159,6 +159,17 @@ describe('SetterObserver', () => {
});
});

describe('Observer', () => {

it('use noop function as default callback', () => {
const values = createObjectArr();
values.forEach(value => {
const observer = new Observer({}, 'a', 'aChanged');
expect(observer['callback'](value, undefined)).to.be.undefined;
});
});
});

function createObjectArr(): any[] {
return [
{}, Object.create(null), new Number(), new Boolean(), new String(),
Expand Down
33 changes: 32 additions & 1 deletion packages/runtime/test/unit/templating/rendering-engine.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
import { RenderingEngine, CompiledTemplate, RuntimeCompilationResources, View, ViewFactory } from '../../../src/index';
import {
RenderingEngine,
CompiledTemplate,
RuntimeCompilationResources,
View,
ViewFactory,
IRenderContext,
ExposedContext,
CustomElementResource,
CustomAttributeResource,
BindingBehaviorResource,
ValueConverterResource,
RenderStrategyResource
} from '../../../src/index';
import { expect } from 'chai';
import { Container } from '../../../../kernel/src';
import { BindingCommandResource } from '../../../../jit/src';


describe('RenderingEngine', () => {
Expand All @@ -12,6 +27,22 @@ describe('CompiledTemplate', () => {

describe('RuntimeCompilationResources', () => {

it('does not register while finding resource', () => {
const container = new Container();
const resources = new RuntimeCompilationResources((container as any) as ExposedContext);

[
CustomElementResource,
CustomAttributeResource,
BindingBehaviorResource,
ValueConverterResource,
RenderStrategyResource,
BindingCommandResource
].forEach(r => {
resources.find(r, 'a');
expect(container.getResolver(r.keyFrom('a'), false)).to.be.null;
});
});
});

describe('View', () => {
Expand Down
2 changes: 1 addition & 1 deletion tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"no-increment-decrement": false,
"no-invalid-regexp": true,
"no-invalid-template-strings": true,
"no-invalid-this": true,
"no-invalid-this": false,
"no-jquery-raw-elements": true,
"no-misused-new": true,
"no-non-null-assertion": true,
Expand Down

0 comments on commit 5971d36

Please sign in to comment.