Skip to content

Commit 8128f37

Browse files
committed
allow apps to load without the editor and make the server database useful
Signed-off-by: Chris Granger <[email protected]>
1 parent dd1786c commit 8128f37

File tree

6 files changed

+208
-86
lines changed

6 files changed

+208
-86
lines changed

src/client.ts

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,7 @@ export class EveClient {
138138
showIDE:boolean = true;
139139
ide:IDE;
140140

141-
constructor(ide:IDE, url?:string) {
142-
this.ide = ide;
141+
constructor(url?:string) {
143142
let loc = url ? url : this.getUrl();
144143
let self = this;
145144

@@ -203,7 +202,10 @@ export class EveClient {
203202
onError() {
204203
this.localControl = true;
205204
this.localEve = true;
206-
this.ide.local = true;
205+
if(this.showIDE) {
206+
this.ide = new IDE();
207+
this.ide.local = true;
208+
}
207209
}
208210

209211
onOpen() {
@@ -265,30 +267,36 @@ export class EveClient {
265267

266268
_initProgram(data) {
267269
this.localEve = data.local;
270+
this.showIDE = data.withIDE;
268271
if(data.local) {
269272
browser.init(data.code);
270273
}
271274
if(this.showIDE) {
272-
initIDE(this.ide, this);
275+
this.ide = new IDE();
276+
initIDE(this);
273277
this.ide.render();
274278
this.ide.loadFile(data.path, data.code);
275279
}
276280
onHashChange({});
277281
}
278282

279283
_parse(data) {
284+
if(!this.showIDE) return;
280285
this.ide.loadDocument(data.generation, data.text, data.spans, data.extraInfo); // @FIXME
281286
}
282287

283288
_comments(data) {
289+
if(!this.showIDE) return;
284290
this.ide.injectSpans(data.spans, data.extraInfo);
285291
}
286292

287293
_findNode(data) {
294+
if(!this.showIDE) return;
288295
this.ide.attachView(data.recordId, data.spanId);
289296
}
290297

291298
_error(data) {
299+
if(!this.showIDE) return;
292300
this.ide.injectNotice("error", data.message);
293301
}
294302

@@ -347,9 +355,15 @@ function subscribeToTagDiff(tag:string, callback: (inserts: string[], removes: s
347355
});
348356
}
349357

350-
subscribeToTagDiff("editor", (inserts, removes, records) => ide.updateActions(inserts, removes, records));
358+
subscribeToTagDiff("editor", (inserts, removes, records) => {
359+
if(!client.showIDE) return;
360+
client.ide.updateActions(inserts, removes, records);
361+
});
351362

352-
subscribeToTagDiff("view", (inserts, removes, records) => ide.updateViews(inserts, removes, records));
363+
subscribeToTagDiff("view", (inserts, removes, records) => {
364+
if(!client.showIDE) return;
365+
client.ide.updateViews(inserts, removes, records)
366+
});
353367

354368
//---------------------------------------------------------
355369
// Communication helpers
@@ -391,10 +405,10 @@ function recordToEAVs(record) {
391405
//---------------------------------------------------------
392406
// Initialize an IDE
393407
//---------------------------------------------------------
394-
let ide = new IDE();
395-
export let client = new EveClient(ide);
408+
export let client = new EveClient();
396409

397-
function initIDE(ide:IDE, client:EveClient) {
410+
function initIDE(client:EveClient) {
411+
let ide = client.ide;
398412
ide.onChange = (ide:IDE) => {
399413
let generation = ide.generation;
400414
let md = ide.editor.toMarkdown();
@@ -430,6 +444,8 @@ function initIDE(ide:IDE, client:EveClient) {
430444
}
431445

432446
function changeDocument() {
447+
if(!client.showIDE) return;
448+
let ide = client.ide;
433449
let docId = "quickstart.eve";
434450
let path = "/" + location.hash.split('?')[0].split("#/")[1];
435451
console.log("PATH", path, location.hash);
@@ -447,14 +463,12 @@ function changeDocument() {
447463
ide.render();
448464
}
449465

450-
console.log(ide);
451-
452466
//---------------------------------------------------------
453467
// Handlers
454468
//---------------------------------------------------------
455469

456470
function onHashChange(event) {
457-
if(ide.loaded) changeDocument();
471+
if(client.ide && client.ide.loaded) changeDocument();
458472
let hash = window.location.hash.split("#/")[2];
459473
let queryParam = window.location.hash.split('?')[1];
460474

src/runtime/browser.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import * as builder from "./builder";
1010
import {ids} from "./id";
1111
import {RuntimeClient} from "./runtimeClient";
1212
import {HttpDatabase} from "./databases/http";
13+
import {BrowserViewDatabase, BrowserEditorDatabase, BrowserInspectorDatabase} from "./databases/browserSession";
1314

1415
//---------------------------------------------------------------------
1516
// Utils
@@ -34,7 +35,15 @@ class BrowserRuntimeClient extends RuntimeClient {
3435
client: EveClient;
3536

3637
constructor(client:EveClient) {
37-
super({"http": new HttpDatabase()}, client.showIDE);
38+
let dbs = {
39+
"http": new HttpDatabase()
40+
}
41+
if(client.showIDE) {
42+
dbs["view"] = new BrowserViewDatabase();
43+
dbs["editor"] = new BrowserEditorDatabase();
44+
dbs["inspector"] = new BrowserInspectorDatabase();
45+
}
46+
super(dbs);
3847
this.client = client;
3948
}
4049

src/runtime/databases/node/server.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,21 @@ import * as request from "request";
99

1010
export class ServerDatabase extends Database {
1111

12+
handling: boolean;
1213
receiving: boolean;
1314
requestId: number;
1415
requestToResponse: any;
1516

1617
constructor() {
1718
super();
19+
this.handling = false;
1820
this.requestId = 0;
1921
this.receiving = false;
2022
this.requestToResponse = {};
2123
}
2224

2325
handleHttpRequest(request, response) {
24-
if(!this.receiving) {
25-
// we need to 404
26-
response.writeHead(404, {"Content-Type": "text/plain"});
27-
return response.end("Nothing is listening to HTTP requests");
28-
}
26+
if(!this.receiving) return;
2927

3028
let scopes = ["server"];
3129
let requestId = `request|${this.requestId++}|${(new Date()).getTime()}`
@@ -72,7 +70,7 @@ export class ServerDatabase extends Database {
7270
}
7371
}
7472

75-
sendResponse(requestId, status, body) {
73+
sendResponse(evaluation, requestId, status, body) {
7674
let response = this.requestToResponse[requestId];
7775
response.statusCode = status;
7876
response.end(body);
@@ -90,17 +88,22 @@ export class ServerDatabase extends Database {
9088
handled[e] = true;
9189
if(index.lookup(e,"tag", "request") && !index.lookup(e, "tag", "sent")) {
9290
let responses = index.asValues(e, "response");
91+
if(responses || index.lookup(e, "tag", "handling")) this.handling = true;
9392
if(responses === undefined) continue;
9493
let [response] = responses;
9594
let {status, body} = index.asObject(response);
9695
actions.push(new InsertAction("server|sender", e, "tag", "sent", undefined, [name]));
97-
this.sendResponse(e, status[0], body[0]);
96+
this.sendResponse(evaluation, e, status[0], body[0]);
9897
}
9998
}
10099
}
101100
if(actions.length) {
102101
process.nextTick(() => {
103102
evaluation.executeActions(actions);
103+
// because this database is created per http request, we need to destroy this
104+
// evaluation once a response has been sent and we've dealt with any consequences
105+
// of the send.
106+
evaluation.close();
104107
})
105108
}
106109
}

src/runtime/runtime.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ export class Evaluation {
122122

123123
registerDatabase(name: string, db: Database) {
124124
if(this.nameToDatabase[name]) {
125-
throw new Error("Trying to register a database name that is already registered");
125+
throw new Error("Trying to register a database name that is already registered: " + name);
126126
}
127127
for(let database of this.databases) {
128128
db.analyze(this, database);

src/runtime/runtimeClient.ts

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,9 @@ import {ids} from "./id";
1919
export abstract class RuntimeClient {
2020
lastParse: any;
2121
evaluation: Evaluation;
22-
withIDE: boolean;
2322
extraDBs: any;
2423

25-
constructor(extraDBs:any = {}, withIDE = true) {
26-
this.withIDE = withIDE;
24+
constructor(extraDBs:any = {}) {
2725
this.extraDBs = extraDBs;
2826
}
2927

@@ -47,32 +45,32 @@ export abstract class RuntimeClient {
4745
let build = builder.buildDoc(parse);
4846
let {blocks, errors} = build;
4947
this.sendErrors(errors);
50-
analyzer.analyze(blocks.map((block) => block.parse), parse.spans, parse.extraInfo);
51-
let browser = new BrowserSessionDatabase(this);
52-
let event = new BrowserEventDatabase();
48+
// TODO: What is the right way to gate analysis? This seems hacky, but I'm not sure
49+
// that the RuntimeClient should really know/care about whether or not the editor is
50+
// hooked up. Maybe there should be a flag for analysis instead?
51+
if(this.extraDBs["editor"]) {
52+
analyzer.analyze(blocks.map((block) => block.parse), parse.spans, parse.extraInfo);
53+
}
54+
55+
let ev = new Evaluation();
5356
let session = new Database();
5457
session.blocks = blocks;
55-
// console.log(blocks);
56-
let ev = new Evaluation();
57-
5858
ev.registerDatabase("session", session);
59-
ev.registerDatabase("browser", browser);
60-
ev.registerDatabase("event", event);
6159

62-
if(this.withIDE) {
63-
let view = new BrowserViewDatabase();
64-
let editor = new BrowserEditorDatabase();
65-
let inspector = new BrowserInspectorDatabase();
66-
67-
ev.registerDatabase("view", view);
68-
ev.registerDatabase("editor", editor);
69-
ev.registerDatabase("inspector", inspector);
60+
let extraDBs = this.extraDBs;
61+
if(!extraDBs["browser"]) {
62+
ev.registerDatabase("browser", new BrowserSessionDatabase(this));
63+
}
64+
if(!extraDBs["event"]) {
65+
ev.registerDatabase("event", new BrowserEventDatabase());
7066
}
7167

72-
ev.registerDatabase("system", system.instance);
68+
if(!extraDBs["system"]) {
69+
ev.registerDatabase("system", system.instance);
70+
}
7371

7472
for(let dbName of Object.keys(this.extraDBs)) {
75-
let db = this.extraDBs[dbName];
73+
let db = extraDBs[dbName];
7674
ev.registerDatabase(dbName, db);
7775
}
7876

@@ -147,7 +145,9 @@ export abstract class RuntimeClient {
147145
let {blocks, errors} = build;
148146
let spans = [];
149147
let extraInfo = {};
150-
analyzer.analyze(blocks.map((block) => block.parse), spans, extraInfo);
148+
if(this.extraDBs["editor"]) {
149+
analyzer.analyze(blocks.map((block) => block.parse), spans, extraInfo);
150+
}
151151
this.sendErrors(errors);
152152
for(let block of blocks) {
153153
if(block.singleRun) block.dormant = true;
@@ -245,6 +245,8 @@ export abstract class RuntimeClient {
245245
}
246246
}
247247
this.evaluation.load(data.info.databases);
248+
} else {
249+
console.error("Unhandled message type: " + json);
248250
}
249251
}
250252
}

0 commit comments

Comments
 (0)