Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
80381d1
Add transaction export, Exporter, Program.watch, and Watcher registry
joshuafcole Feb 3, 2017
357fb3e
Mode the index file to point at bootstrap.ts
joshuafcole Feb 3, 2017
2a9f18a
Less logging
joshuafcole Feb 3, 2017
6134ba0
Get basic rendering working
joshuafcole Feb 6, 2017
8df120e
Fix impossible removal bug
joshuafcole Feb 6, 2017
9acd6f9
Update example to test crossy parent
joshuafcole Feb 6, 2017
3fbf3bd
Sideport the dynamic add changes
joshuafcole Feb 6, 2017
f1a0107
Fix root removal + ignore used attrs
joshuafcole Feb 6, 2017
4c11db7
Fix diff ordering in exporter
joshuafcole Feb 6, 2017
7998640
Remove logging
joshuafcole Feb 7, 2017
90ecb7a
Simpler example of the jeff issue
joshuafcole Feb 7, 2017
8f5195b
Fix up type implementations for choose and union
ibdknox Feb 7, 2017
243a234
Dont ignore count for special attributes in html watcher
joshuafcole Feb 7, 2017
506d072
Propery crossy parent test
joshuafcole Feb 7, 2017
60f76ec
remove the completely wrong deduping stuff and fix the actual bug we …
ibdknox Feb 7, 2017
f204e0f
Remove extraneous reset stuff
joshuafcole Feb 7, 2017
f1d8359
Merge with upstream
joshuafcole Feb 7, 2017
ccc60a1
Change the exporter to be per block vs per tag
joshuafcole Feb 7, 2017
d7aed46
add() should always push into dynamic fields
joshuafcole Feb 7, 2017
ad36faa
Clean up cruft
joshuafcole Feb 7, 2017
1ec234d
Fix bug in maybeReverse that made 0 -> undefined
joshuafcole Feb 8, 2017
7cf13ab
Remove extraneous block
joshuafcole Feb 8, 2017
a798847
Add sorted insertion
joshuafcole Feb 8, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<script type="text/javascript" src="src/system.js"></script>
<script type="text/javascript" src="src/systemJSConfig.js"></script>
<script>
SystemJS.import("src/runtime/dsl");
SystemJS.import("src/bootstrap");
</script>

<!-- PRODUCTION ANALYTICS -->
Expand Down
95 changes: 95 additions & 0 deletions src/bootstrap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import {Program} from "./runtime/dsl";
import {Watcher} from "./watchers/watcher";
import "./watchers/html";

let prog = new Program("test");
Watcher.attach("html", prog);

prog
.block("simple block", ({find, record, lib}) => {
let person = find("P");
let potato = find("potato");
let nameElem;
return [
nameElem = record("html/element", {tagname: "span", text: person.name}),
record("html/element", {tagname: "section", potato}).add("child" + "ren", nameElem)
]
});

prog.test(0, [
[2, "tag", "html/element"],
[2, "tagname", "div"],
[2, "children", 3],
[2, "sort", 1],

[3, "tag", "html/element"],
[3, "tagname", "span"],
[3, "text", "Woo hoo!"],
[3, "style", 4],

[4, "color", "red"],
[4, "background", "pink"],

[5, "tag", "html/element"],
[5, "tagname", "div"],
[5, "style", 6],
[5, "children", 7],
[5, "sort", 3],

[6, "border", "3px solid green"],

[7, "tag", "html/element"],
[7, "tagname", "span"],
[7, "text", "meep moop"],
]);

prog.test(1, [
[3, "style", 4, 0, -1]
]);

prog.test(2, [
[3, "style", 4, 0, 1],
[4, "font-size", "4em"],
[4, "background", "pink", 0, -1]
]);

prog.test(3, [
[8, "tag", "html/element"],
[8, "tagname", "div"],
[8, "style", 4],
[8, "text", "Jeff (from accounting)"],
[8, "sort", 0]
]);

// prog
// .test(0, [
// [1, "tag", "P"],
// [1, "name", "Jeff"],

// [2, "tag", "P"],
// [2, "name", "KERY"],

// [3, "tag", "P"],
// [3, "name", "RAB"],

// [4, "tag", "potato"],
// [4, "kind", "idaho"],

// [5, "tag", "potato"],
// [5, "kind", "irish gold"],

// ]);

// prog
// .test(1, [
// [1, "tag", "P", 0, -1],
// [2, "tag", "P", 0, -1]

// ]);

// prog
// .test(2, [
// [1, "tag", "P", 0, 1],
// ]);

console.log(prog);
76 changes: 65 additions & 11 deletions src/runtime/dsl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ declare var Proxy:new (obj:any, proxy:any) => any;
declare var Symbol:any;

import {RawValue, Register, isRegister, GlobalInterner, Scan, IGNORE_REG, ID,
InsertNode, Node, Constraint, FunctionConstraint, Change, concatArray} from "./runtime";
InsertNode, WatchNode, Node, Constraint, FunctionConstraint, Change, concatArray} from "./runtime";
import * as runtime from "./runtime";
import * as indexes from "./indexes";

Expand All @@ -20,6 +20,11 @@ var CURRENT_ID = 0;
// Utils
//--------------------------------------------------------------------

function toArray<T>(x:T|T[]):T[] {
if(x.constructor === Array) return x as T[];
return [x as T];
}

function maybeIntern(value:(RawValue|Register)):Register|ID {
if(value === undefined || value === null) throw new Error("Trying to intern an undefined");
if(isRegister(value)) return value;
Expand Down Expand Up @@ -210,7 +215,9 @@ class DSLRecord {
__output: boolean = false;
/** If a record is an output, it needs an id by default unless its modifying an existing record. */
__needsId: boolean = true;
__fields: any;

__fields: {[field:string]: (RawValue|DSLNode)[]};
__dynamicFields: [DSLVariable|string, DSLNode[]][] = [];
constructor(public __block:DSLBlock, tags:string[], initialAttributes:any, entityVariable?:DSLVariable) {
this.__id = CURRENT_ID++;
let fields:any = {tag: tags};
Expand Down Expand Up @@ -296,13 +303,18 @@ class DSLRecord {
})
}

add(attributeName:string, value:DSLNode) {
add(attributeName:string|DSLVariable, values:DSLNode|DSLNode[]) {
if(this.__block !== this.__block.program.contextStack[0]) {
throw new Error("Adds and removes may only happen in the root block.");
}
let record = new DSLRecord(this.__block, [], {[attributeName]: value}, this.__record);
values = toArray(values);

let record = new DSLRecord(this.__block, [], {}, this.__record);
record.__output = true;
this.__block.records.push(record);

record.__dynamicFields.push([attributeName, values]);

return this;
}

Expand Down Expand Up @@ -336,14 +348,32 @@ class DSLRecord {
}

toInserts() {
let program = this.__block.program;
let inserts:(Constraint|Node)[] = [];
let e = maybeIntern(toValue(this.__record));
let e = maybeIntern(this.__record.value);

for(let field in this.__fields) {
for(let dslValue of this.__fields[field]) {
let value = toValue(dslValue) as (RawValue | Register);
inserts.push(new InsertNode(e, maybeIntern(field), maybeIntern(value), maybeIntern("my-awesome-node")))
if(this.__block.watcher) {
inserts.push(new WatchNode(e, maybeIntern(field), maybeIntern(value), maybeIntern(program.nodeCount++), this.__block.__id))
} else {
inserts.push(new InsertNode(e, maybeIntern(field), maybeIntern(value), maybeIntern(program.nodeCount++)))
}
}
for(let [dslField, dslValues] of this.__dynamicFields) {
let field = toValue(dslField) as (RawValue | Register);
for(let dslValue of dslValues) {
let value = toValue(dslValue) as (RawValue | Register);
if(this.__block.watcher) {
inserts.push(new WatchNode(e, maybeIntern(field), maybeIntern(value), maybeIntern(program.nodeCount++), this.__block.__id))
} else {
inserts.push(new InsertNode(e, maybeIntern(field), maybeIntern(value), maybeIntern(program.nodeCount++)))
}
}
}
}

return inserts;
}

Expand Down Expand Up @@ -382,6 +412,7 @@ class DSLRecord {
//--------------------------------------------------------------------

type DSLCompilable = DSLRecord | DSLFunction;
export type BlockFunction = (block:DSLBlock) => any;

class DSLBlock {
__id:number;
Expand All @@ -401,7 +432,7 @@ class DSLBlock {

lib = this.generateLib();

constructor(public name:string, public creationFunction:(block:DSLBlock) => any, public readonly program:Program, mangle = true) {
constructor(public name:string, public creationFunction:BlockFunction, public readonly program:Program, mangle = true, public readonly watcher = false) {
this.__id = CURRENT_ID++;
let neueFunc = creationFunction;
if(mangle) {
Expand Down Expand Up @@ -1096,6 +1127,10 @@ export class Program {
blocks:DSLBlock[] = [];
runtimeBlocks:runtime.Block[] = [];
index:indexes.Index;
nodeCount = 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🗑

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

J/k, this tracks the nodeIds now that they aren't all awesome nodes.


protected _exporter?:runtime.Exporter;
protected _lastWatch?:number;

/** Represents the hierarchy of blocks currently being compiled into runtime nodes. */
contextStack:DSLBlock[] = [];
Expand All @@ -1104,22 +1139,41 @@ export class Program {
this.index = new indexes.HashIndex();
}

block(name:string, func:(block:DSLBlock) => any) {
block(name:string, func:BlockFunction) {
let block = new DSLBlock(name, func, this);
block.prepare();
this.blocks.push(block);
this.runtimeBlocks.push(block.block);

return this;
}

watch(name:string, func:BlockFunction) {
if(!this._exporter) this._exporter = new runtime.Exporter();
let block = new DSLBlock(name, func, this, true, true);
block.prepare();
this.blocks.push(block);
this.runtimeBlocks.push(block.block);
this._lastWatch = block.__id;
return this;
}

asDiffs(handler:runtime.DiffConsumer) {
if(!this._exporter || !this._lastWatch) throw new Error("Must have at least one watch block to export as diffs.");
this._exporter.triggerOnDiffs(this._lastWatch, handler);

return this;
}

input(changes:runtime.Change[]) {
let trans = new runtime.Transaction(changes[0].transaction, this.runtimeBlocks, changes);
let trans = new runtime.Transaction(changes[0].transaction, this.runtimeBlocks, changes, this._exporter && this._exporter.handle);
trans.exec(this.index);
return trans;
}

test(transaction:number, eavns:TestChange[]) {
let changes:Change[] = [];
let trans = new runtime.Transaction(transaction, this.runtimeBlocks, changes);
let trans = new runtime.Transaction(transaction, this.runtimeBlocks, changes, this._exporter && this._exporter.handle);
for(let [e, a, v, round = 0, count = 1] of eavns as EAVRCTuple[]) {
let change = Change.fromValues(e, a, v, "my-awesome-node", transaction, round, count);
if(round === 0) {
Expand All @@ -1129,7 +1183,7 @@ export class Program {
}
}
trans.exec(this.index);
console.log(trans.changes.map((change, ix) => ` <- ${change}`).join("\n"));
console.info(trans.changes.map((change, ix) => ` <- ${change}`).join("\n"));
return this;
}
}
Expand Down
Loading