Skip to content
This repository has been archived by the owner on Mar 5, 2018. It is now read-only.

Commit

Permalink
Test and refactor rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
pittst3r committed Mar 30, 2017
1 parent ab53a6d commit 84690ca
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 15 deletions.
39 changes: 26 additions & 13 deletions src/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,18 @@ export default class Application implements Owner {
private _registry: Registry;
private _container: Container;
private _renderResult: RenderResult;
/** Whether the initial render has completed. */
private _rendered: boolean;
private _afterRender: Option<() => void>;
/** Whether a re-render has been scheduled. */
private _scheduled: boolean;
private _scheduled: Option<Promise<void>> = null;
private _initializers: Initializer[] = [];
private _initialized = false;

constructor(options: ApplicationOptions) {
this.rootName = options.rootName;
this.resolver = options.resolver;
this._scheduled = new Promise<void>(resolve => {
this._afterRender = resolve;
});
}

/** @hidden */
Expand Down Expand Up @@ -136,13 +138,22 @@ export default class Application implements Owner {

this.env.commit();

this._rendered = true;
let { _afterRender: afterRender } = this;

this._afterRender = null;
this._scheduled = null;
this._renderResult = result.value;

afterRender();
}

renderComponent(component: string | ComponentDefinition<Component>, parent: Simple.Node, nextSibling: Option<Simple.Node>): void {
renderComponent(
component: string | ComponentDefinition<Component>,
parent: Simple.Node,
nextSibling: Option<Simple.Node> = null
): Promise<void> {
this._roots.push({ id: this._rootsIndex++, component, parent, nextSibling });
this.scheduleRerender();
return this.scheduleRerender();
}

/** @hidden */
Expand All @@ -153,13 +164,15 @@ export default class Application implements Owner {
}

/** @hidden */
scheduleRerender(): void {
if (this._scheduled || !this._rendered) { return; }

this._scheduled = true;
requestAnimationFrame(() => {
this._scheduled = false;
this.rerender();
scheduleRerender(): Promise<void> {
if (this._scheduled) return this._scheduled;

return this._scheduled = new Promise<void>(resolve => {
requestAnimationFrame(() => {
this._scheduled = null;
this.rerender();
resolve();
});
});
}

Expand Down
17 changes: 17 additions & 0 deletions test/render-component-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import buildApp from './test-helpers/test-app';

const { module, test } = QUnit;

module('renderComponent');

test('can render a component', async function(assert) {
let containerElement = document.createElement('div');

let app = buildApp()
.template('hello-world', `<h1>Hello Glimmer!</h1>`)
.boot();

await app.renderComponent('hello-world', containerElement);

assert.equal(containerElement.innerHTML, '<h1>Hello Glimmer!</h1>');
});
4 changes: 2 additions & 2 deletions test/test-helpers/test-app.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import Application from '../../src/application';
import Resolver, { BasicModuleRegistry } from '@glimmer/resolver';
import { Factory } from '@glimmer/di';

import { TestComponent, TestComponentManager } from './components';
import { precompile } from './compiler';

interface ComponentFactory {
export interface ComponentFactory {
create(injections: object): TestComponent;
}

Expand Down Expand Up @@ -52,6 +51,7 @@ export class AppBuilder {
constructor(name: string) {
this.rootName = name;
this.modules[`component-manager:/${this.rootName}/component-managers/main`] = TestComponentManager;
this.template('main', '<div />');
}

template(name: string, template: string) {
Expand Down

0 comments on commit 84690ca

Please sign in to comment.