Skip to content
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

Merged
merged 22 commits into from Jun 28, 2017
Merged

Add file and console libraries #858

merged 22 commits into from Jun 28, 2017

Conversation

cmontella
Copy link
Contributor

@cmontella cmontella commented Jun 26, 2017

No description provided.

);
me.inputEAVs(changes);
} else {
console.log(err);
Copy link
Contributor

@joshuafcole joshuafcole Jun 26, 2017

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.

})

// Console log watcher
me.watch("print to console log", ({find, record}) => {
Copy link
Contributor

@joshuafcole joshuafcole Jun 26, 2017

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.

});
})
})

Copy link
Contributor

@joshuafcole joshuafcole Jun 26, 2017

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.

@joshuafcole joshuafcole changed the base branch from fix/run-eve-headless to master Jun 27, 2017
import {Watcher} from "./watcher";
import {ID} from "../runtime/runtime";

class consoleWatcher extends Watcher {
Copy link
Contributor

@joshuafcole joshuafcole Jun 27, 2017

Choose a reason for hiding this comment

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

UpperCamel Case ConsoleWatcher

if(console) {
let {program:me} = this;

me.watch("print to console log", ({find, record}) => {
Copy link
Contributor

@joshuafcole joshuafcole Jun 27, 2017

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."

record({log, text: log.text})
]
})
me.asObjects<{log:ID, text:string}>(({adds, removes}) => {
Copy link
Contributor

@joshuafcole joshuafcole Jun 27, 2017

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);
    }
  })

}
}

Watcher.register("console", consoleWatcher);
Copy link
Contributor

@joshuafcole joshuafcole Jun 27, 2017

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.

me.watch("read a file", ({find, record}) => {
let file = find("file/read");
return [
record({file, path: file.path, encoding: file.encoding})
Copy link
Contributor

@joshuafcole joshuafcole Jun 27, 2017

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");

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){
Copy link
Contributor

@joshuafcole joshuafcole Jun 27, 2017

Choose a reason for hiding this comment

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

{encoding: encoding} => {encoding}

changes.push(
[file, "contents", contents, 1],
);
me.inputEAVs(changes);
Copy link
Contributor

@joshuafcole joshuafcole Jun 27, 2017

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.

me.inputEAVs(changes);
} else {
let id = `${file}|error`
let changes:RawEAVC[] = [];
Copy link
Contributor

@joshuafcole joshuafcole Jun 27, 2017

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.

let {file, path, contents, encoding} = adds[id];
fs.writeFile(path, contents, {encoding: encoding}, function(err){
if (!err) {
console.log(`Write file success: ${contents}`)
Copy link
Contributor

@joshuafcole joshuafcole Jun 27, 2017

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.

})
.asDiffs(({adds}) => {
for(let [log, _, text] of adds) {
console.log(text);
Copy link
Contributor

@joshuafcole joshuafcole Jun 27, 2017

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.

@cmontella cmontella changed the title Feature file watcher Add file and console libraries Jun 28, 2017
@joshuafcole joshuafcole merged commit 3c4e3e0 into master Jun 28, 2017
1 check passed
@cmontella cmontella deleted the feature/file-watcher branch Jun 28, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants