-
Notifications
You must be signed in to change notification settings - Fork 257
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add file and console libraries #858
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As you mentioned, let's split this guy out.
src/watchers/file.ts
Outdated
}); | ||
}) | ||
}) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
{encoding: encoding}
=> {encoding}
src/watchers/file.ts
Outdated
changes.push( | ||
[file, "contents", contents, 1], | ||
); | ||
me.inputEAVs(changes); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like these for loops have all lost their indentation.
No description provided.