Permalink
Browse files

Expose the iterator to the outside of the template

  • Loading branch information...
chadhietala authored and krisselden committed Feb 18, 2017
1 parent da7d429 commit 90d7366de2939aa8435ae4250dc301e121f0b21c
@@ -38,7 +38,13 @@ function commonSetup() {
function render<T>(template: Template<T>, self: any) {
let result;
env.begin();
result = template.render(new UpdatableReference(self), root, new TestDynamicScope());
let vm = template.render(new UpdatableReference(self), root, new TestDynamicScope());

do {
result = vm.next();
} while (!result.done);

result = result.value;
env.commit();
return result;
}
@@ -76,7 +76,7 @@ export {
Layout
} from './lib/scanner';

export { PublicVM as VM, UpdatingVM, RenderResult } from './lib/vm';
export { PublicVM as VM, UpdatingVM, RenderResult, IteratorResult } from './lib/vm';

export { SafeString, isSafeString } from './lib/upsert';

@@ -8,8 +8,6 @@ import { SymbolTable } from '@glimmer/interfaces';
import { Environment, DynamicScope } from './environment';
import { ElementStack } from './builder';
import { VM } from './vm';
import { IteratorResult } from './vm/append';
import RenderResult from './vm/render-result';
import Scanner, {
EntryPoint,
PartialBlock,
@@ -35,7 +33,7 @@ export interface Template<T> {
/**
* Helper to render template as root entry point.
*/
render(self: PathReference<any>, appendTo: Simple.Element, dynamicScope: DynamicScope): RenderResult;
render(self: PathReference<any>, appendTo: Simple.Element, dynamicScope: DynamicScope): VM;

// internal casts, these are lazily created and cached
asEntryPoint(): EntryPoint;
@@ -113,15 +111,8 @@ function template<T>(block: SerializedTemplateBlock, id: string, meta: T, env: E
let render = (self: PathReference<any>, appendTo: Simple.Element, dynamicScope: DynamicScope) => {
let elementStack = ElementStack.forInitialRender(env, appendTo, null);
let compiled = asEntryPoint().compile(env);
let vm = VM.initial(env, self, dynamicScope, elementStack, compiled.symbols);
vm.prepare(compiled.start, compiled.end);
let result: IteratorResult<RenderResult>;

do {
result = vm.next();
} while (!result.done);

return result.value as RenderResult;
let vm = VM.initial(env, self, dynamicScope, elementStack, compiled);
return vm;
};

return { id, meta, _block: block, asEntryPoint, asLayout, asPartial, render };
@@ -1,3 +1,3 @@
export { default as VM, PublicVM } from './vm/append';
export { default as VM, PublicVM, IteratorResult } from './vm/append';
export { default as UpdatingVM } from './vm/update';
export { default as RenderResult } from './vm/render-result';
@@ -2,7 +2,7 @@ import { Scope, DynamicScope, Environment, Opcode } from '../environment';
import { ElementStack } from '../builder';
import { Option, Destroyable, Stack, LinkedList, ListSlice, Opaque, assert, expect } from '@glimmer/util';
import { PathReference, combineSlice } from '@glimmer/reference';
import { CompiledBlock } from '../compiled/blocks';
import { CompiledBlock, CompiledProgram } from '../compiled/blocks';
import { InlineBlock, PartialBlock } from '../scanner';
import { CompiledExpression } from '../compiled/expressions';
import { CompiledArgs, EvaluatedArgs } from '../compiled/expressions/args';
@@ -47,10 +47,13 @@ export default class VM implements PublicVM {
self: PathReference<Opaque>,
dynamicScope: DynamicScope,
elementStack: ElementStack,
size: number
compiledProgram: CompiledProgram
) {
let { symbols: size, start, end } = compiledProgram;
let scope = Scope.root(self, size);
return new VM(env, scope, dynamicScope, elementStack);
let vm = new VM(env, scope, dynamicScope, elementStack);
vm.prepare(start, end);
return vm;
}

constructor(
@@ -282,7 +285,7 @@ export default class VM implements PublicVM {
return result.value as RenderResult;
}

prepare(start: number, end: number, initialize?: (vm: VM) => void): void {
private prepare(start: number, end: number, initialize?: (vm: VM) => void): void {
let { elementStack, frame, updatingOpcodeStack } = this;

elementStack.pushSimpleBlock();
@@ -1,13 +1,15 @@
import { Simple, Template, RenderResult, readDOMAttr } from "../index";
import { TestEnvironment, TestDynamicScope, equalTokens } from "@glimmer/test-helpers";
import { IteratorResult } from '@glimmer/runtime';

import { PathReference } from "@glimmer/reference";
import { UpdatableReference } from "@glimmer/object-reference";
import { Opaque } from "@glimmer/util";

let root: Simple.Element;
let env: TestEnvironment;
let self: UpdatableReference<any>;
let result: RenderResult;
let result: IteratorResult<RenderResult>;

function compile(template: string) {
return env.compile(template);
@@ -26,7 +28,13 @@ function commonSetup() {
function render<T>(template: Template<T>, context = {}, view: PathReference<Opaque> = null) {
self = new UpdatableReference(context);
env.begin();
result = template.render(self, root, new TestDynamicScope());
let vm = template.render(self, root, new TestDynamicScope());

do {
result = vm.next();
} while (!result.done);

result = result.value;
env.commit();
assertInvariants(result);
return result;
@@ -1,9 +1,9 @@
import { Simple, Template, RenderResult } from "@glimmer/runtime";
import { Simple, Template, RenderResult, IteratorResult } from "@glimmer/runtime";
import { BasicComponent, TestEnvironment, TestDynamicScope, equalTokens } from "@glimmer/test-helpers";
import { UpdatableReference } from "@glimmer/object-reference";
import { Opaque } from '@glimmer/util';

let env: TestEnvironment, root: Simple.Element, result: RenderResult, self: UpdatableReference<Opaque>;
let env: TestEnvironment, root: Simple.Element, result: IteratorResult<RenderResult>, self: UpdatableReference<Opaque>;

function rootElement() {
return env.getDOM().createElement('div');
@@ -22,7 +22,13 @@ function commonSetup() {
function render<T>(template: Template<T>, context={}) {
self = new UpdatableReference(context);
env.begin();
result = template.render(self, root, new TestDynamicScope());
let vm = template.render(self, root, new TestDynamicScope());

do {
result = vm.next();
} while (!result.done);

result = result.value;
env.commit();
assertInvariants(result);
return result;
@@ -4,7 +4,8 @@ import {
RenderResult,
setDebuggerCallback,
resetDebuggerCallback,
debugCallback
debugCallback,
IteratorResult
} from "@glimmer/runtime";
import {
BasicComponent,
@@ -19,7 +20,7 @@ import {
import { UpdatableReference } from "@glimmer/object-reference";
import { Opaque } from '@glimmer/util';

let env: TestEnvironment, root: Simple.Element, result: RenderResult, self: UpdatableReference<Opaque>;
let env: TestEnvironment, root: Simple.Element, result: IteratorResult<RenderResult>, self: UpdatableReference<Opaque>;

function rootElement() {
return env.getDOM().createElement('div');
@@ -37,7 +38,13 @@ function commonSetup() {
function render<T>(template: Template<T>, context={}) {
self = new UpdatableReference(context);
env.begin();
result = template.render(self, root, new TestDynamicScope());
let vm = template.render(self, root, new TestDynamicScope());

do {
result = vm.next();
} while (!result.done);

result = result.value;
env.commit();
return result;
}
@@ -37,7 +37,15 @@ export class EmberishRootView extends EmberObject {
appendTo(selector: string) {
let element = this.parent = document.querySelector(selector);
let self = new UpdatableReference(this);
this._result = this.template.render(self, element, new TestDynamicScope());
let vm = this.template.render(self, element, new TestDynamicScope());

let result;
do {
result = vm.next();
} while (!result.done);

this._result = result.value;

this.element = element.firstElementChild;
}

@@ -31,7 +31,13 @@ function commonSetup(customEnv = new TestEnvironment()) {
function render<T>(template: Template<T>, self: any) {
let result;
env.begin();
result = template.render(new UpdatableReference(self), root, new TestDynamicScope());
let vm = template.render(new UpdatableReference(self), root, new TestDynamicScope());

do {
result = vm.next();
} while (!result.done);

result = result.value;
env.commit();
return result;
}
@@ -1,4 +1,4 @@
import { Simple, Template, RenderResult } from "@glimmer/runtime";
import { Simple, Template, RenderResult, IteratorResult } from "@glimmer/runtime";
import {
BasicComponent,
EmberishCurlyComponent,
@@ -12,7 +12,7 @@ import {
import { UpdatableReference } from "@glimmer/object-reference";
import { Opaque } from '@glimmer/util';

let env: TestEnvironment, root: Simple.Element, result: RenderResult, self: UpdatableReference<Opaque>;
let env: TestEnvironment, root: Simple.Element, result: IteratorResult<RenderResult>, self: UpdatableReference<Opaque>;

function rootElement() {
return env.getDOM().createElement('div');
@@ -30,7 +30,13 @@ function commonSetup() {
function render<T>(template: Template<T>, context={}) {
self = new UpdatableReference(context);
env.begin();
result = template.render(self, root, new TestDynamicScope());
let vm = template.render(self, root, new TestDynamicScope());

do {
result = vm.next();
} while (!result.done);

result = result.value;
env.commit();
assertInvariants(result);
return result;
@@ -1,4 +1,4 @@
import { UNDEFINED_REFERENCE, EvaluatedArgs, Template, RenderResult, SafeString, PrimitiveReference, VM } from "@glimmer/runtime";
import { UNDEFINED_REFERENCE, EvaluatedArgs, Template, RenderResult, SafeString, PrimitiveReference, VM, IteratorResult } from "@glimmer/runtime";
import { BasicComponent, TestEnvironment, TestDynamicScope, TestModifierManager, equalTokens, stripTight, trimLines } from "@glimmer/test-helpers";
import { ConstReference, PathReference } from "@glimmer/reference";
import { UpdatableReference } from "@glimmer/object-reference";
@@ -23,7 +23,7 @@ const serializesNSAttributesCorrectly = (function() {
let hooks, root: Element;
let env: TestEnvironment;
let self: UpdatableReference<any>;
let result: RenderResult;
let result: IteratorResult<RenderResult>;

function compile(template: string) {
return env.compile(template);
@@ -42,7 +42,13 @@ function commonSetup() {
function render<T>(template: Template<T>, context = {}, view: PathReference<Opaque> = null) {
self = new UpdatableReference(context);
env.begin();
result = template.render(self, root, new TestDynamicScope());
let vm = template.render(self, root, new TestDynamicScope());

do {
result = vm.next();
} while (!result.done);

result = result.value;
env.commit();
assertInvariants(result);
return result;
@@ -102,7 +102,15 @@ export class RenderingTest {
let root = new SimpleRootReference(rootObject);

this.context = rootObject;
this.result = this.template.render(root, appendTo, dynamicScope);

let vm = this.template.render(root, appendTo, new TestDynamicScope());

let result;
do {
result = vm.next();
} while (!result.done);

this.result = result.value;
this.env.commit();
this.element = document.getElementById('qunit-fixture').firstChild;
}
@@ -70,7 +70,14 @@ export function start() {
let output = document.getElementById('output');
let self = new UpdatableReference(data);
let template: Template<{}> = TEMPLATES['application'];
let result = template.render(self, output, new TestDynamicScope());
let vm = template.render(self, output, new TestDynamicScope());

let result;
do {
result = vm.next();
} while (!result.done);

result = result.value;

window.addEventListener("resize", function() {
data.width = window.innerWidth;

0 comments on commit 90d7366

Please sign in to comment.