Conversation
…s' into feature/file-watcher
src/watchers/file.ts
Outdated
| ); | ||
| me.inputEAVs(changes); | ||
| } else { | ||
| console.log(err); |
There was a problem hiding this comment.
This should probably drop in an error message of some sort. We haven't fleshed out what the story for #error is yet, but it'd be perfectly fine to have readfile.error += [code: <err code>, name:, text] for the program to look for and handle.
src/watchers/file.ts
Outdated
| }) | ||
|
|
||
| // Console log watcher | ||
| me.watch("print to console log", ({find, record}) => { |
There was a problem hiding this comment.
As you mentioned, let's split this guy out.
src/watchers/file.ts
Outdated
| }); | ||
| }) | ||
| }) | ||
|
|
There was a problem hiding this comment.
let's add a #file/write too? Then I can use this with the websocket watcher for the server backing the editor.
src/watchers/console.ts
Outdated
| import {Watcher} from "./watcher"; | ||
| import {ID} from "../runtime/runtime"; | ||
|
|
||
| class consoleWatcher extends Watcher { |
There was a problem hiding this comment.
UpperCamel Case ConsoleWatcher
src/watchers/console.ts
Outdated
| if(console) { | ||
| let {program:me} = this; | ||
|
|
||
| me.watch("print to console log", ({find, record}) => { |
There was a problem hiding this comment.
Let's format these as sentences for consistency. E.g. "Print to console."
src/watchers/console.ts
Outdated
| record({log, text: log.text}) | ||
| ] | ||
| }) | ||
| me.asObjects<{log:ID, text:string}>(({adds, removes}) => { |
There was a problem hiding this comment.
asDiffs is a fair bit faster than asObjects. For cases where you only need an id and a single attribute, I recommend using asDiffs instead. E.g.
me
.watch("print to console log", ({find, record}) => {
let log = find("console/log");
return [log.add("text", log.text)];
})
.asDiffs(({adds}) => {
for(let [log, _, text] of adds) {
console.log(text);
}
})
src/watchers/console.ts
Outdated
| } | ||
| } | ||
|
|
||
| Watcher.register("console", consoleWatcher); No newline at end of file |
There was a problem hiding this comment.
Missing newline at end of file.
src/watchers/file.ts
Outdated
| me.watch("read a file", ({find, record}) => { | ||
| let file = find("file/read"); | ||
| return [ | ||
| record({file, path: file.path, encoding: file.encoding}) |
There was a problem hiding this comment.
I was going to suggest this. :) Let's give it utf-8 as a default though, e.g.
let encoding = choose(() => file.encoding, () => "utf-8");
src/watchers/file.ts
Outdated
| me.asObjects<{file:ID, path:string, encoding:string}>(({adds, removes}) => { | ||
| Object.keys(adds).forEach((id) => { | ||
| let {file, path, encoding} = adds[id]; | ||
| fs.readFile(path, {encoding: encoding}, function(err, contents){ |
There was a problem hiding this comment.
{encoding: encoding} => {encoding}
src/watchers/file.ts
Outdated
| changes.push( | ||
| [file, "contents", contents, 1], | ||
| ); | ||
| me.inputEAVs(changes); |
There was a problem hiding this comment.
This whole branch can be reduced to me.inputEAVs([[file, "contents", contents]]). The count is optional and defaults to 1.
src/watchers/file.ts
Outdated
| me.inputEAVs(changes); | ||
| } else { | ||
| let id = `${file}|error` | ||
| let changes:RawEAVC[] = []; |
There was a problem hiding this comment.
If you type this as RawEAV you can omit the counts.
src/watchers/file.ts
Outdated
| let {file, path, contents, encoding} = adds[id]; | ||
| fs.writeFile(path, contents, {encoding: encoding}, function(err){ | ||
| if (!err) { | ||
| console.log(`Write file success: ${contents}`) |
There was a problem hiding this comment.
Instead of printing, let's set status: "complete" on the file record or something similar.
src/watchers/console.ts
Outdated
| }) | ||
| .asDiffs(({adds}) => { | ||
| for(let [log, _, text] of adds) { | ||
| console.log(text); |
There was a problem hiding this comment.
Seems like these for loops have all lost their indentation.
No description provided.