witheve / Eve

Better tools for thought
http://witheve.com
Apache License 2.0
7.16k stars 257 forks source link

Watchers don't seem to fire #819

Closed yazz closed 7 years ago

yazz commented 7 years ago

I have written a small Eve program in javascript but I can't seem to get the watchers to fire:

    console.log("************************************************ ")
    console.log("******************************Doing Eve stuff ")
    console.log("************************************************ ")
    let eve = new witheve.Program("program name");

    let inputs = [];
    console.log(JSON.stringify(Object.keys(eve) , null, 2))
    var vv = 0
    setTimeout(function() {

        witheve.appendAsEAVs(inputs, {tag: "student", name: "Zubair " + vv});
        vv ++
    }, 1000)

    eve.watch("Export information about students", function(arg) {
        console.log("--Eve Export information ")
      // search for records tagged student
      let student = arg.find("student");
      let eav = arg.lookup(student);
      return [
          // Add these attributes and values to the student, creating a diff to which we can react
          student.add(eav.attribute, eav.value)
      ];

    }).asDiffs(function(diff)  {
          console.log("--Eve in adDiffs ")
          for(let [e, a, v] of diff.adds) {
            console.log("--Eve add ")
          }
          for(let [e, a, v] of diff.removes) {
              console.log("--Eve remove ")
          }
        });;
        witheve.appendAsEAVs(inputs, {tag: "student", name: "Archibald"});
        witheve.appendAsEAVs(inputs, {tag: "student", name: "Zubair"});
        eve.inputEAVs([0,"tag","student"],[0,"name","Archibald"])

    console.log("************************************************ ")
    console.log("******************************finished Eve stuff ")
    console.log("************************************************ ")
joshuafcole commented 7 years ago

Hey Zubair,

You're very close! There are just two issues here. The first is the appendAsEAVs doesn't insert anything into your program. Instead, it takes an object (e.g. {tag: "student", name: "Zubair"}) and inserts it as EAVs into it's first argument (e.g. ["<some id>", "tag", "student"], ["<some id>", "name", "Zubair"]). It's meant to be used in conjunction with inputEAVs(), like so:

let inputs = [];
witheve.appendAsEAVs(inputs, {tag: "student", name: "Archibald"});
witheve.appendAsEAVs(inputs, {tag: "student", name: "Zubair"});
eve.inputEAVS(inputs);

The second issue is that inputEAVs expects an array of EAVs, rather than EAVs as arguments, so this: eve.inputEAVs([0,"tag","student"],[0,"name","Archibald"]) should be this:

eve.inputEAVs([[0,"tag","student"],[0,"name","Archibald"]])

One last thing to note: While there's nothing wrong with having a console log inside of your watch block, it will not fire when you'd expect it to. The block as you've written it is executed once, as soon as you make it, to generate the actual internal representation of the block. The console logs in your asDiffs() handler will work just like you expected though.

joshuafcole commented 7 years ago

I'm going to mark this issue as closed. If you're still having any trouble don't hesitate to reply and we'll reopen it and see what we can do.

yazz commented 7 years ago

Ok great, that works :) THANKS