0

I have 2 soy.js and lib-dialogs.js files.

I need to make lib-dialogs pass the value of the lineCount variable to soy.js.

I was able to do this with localStorage but because it saves in a cookie it does not update the values correctly.

In lib-dialogs there is a function called BlocklyDialogs.congratulations that calls the necessary data.

FIle:lib-dialogs.js

    BlocklyDialogs.congratulations = function() {

  // Add the user's code.
  if (BlocklyGames.workspace) {
    var linesText = document.getElementById('dialogLinesText');
    linesText.textContent = '';
    // Line produces warning when compiling Puzzle since there is no JavaScript
    // generator.  But this function is never called in Puzzle, so no matter.
    var code = Blockly.JavaScript.workspaceToCode(BlocklyGames.workspace);
    code = BlocklyInterface.stripCode(code);
    var noComments = code.replace(/\/\/[^\n]*/g, '');  // Inline comments.
    noComments = noComments.replace(/\/\*.*\*\//g, '');  /* Block comments. */
    noComments = noComments.replace(/[ \t]+\n/g, '\n');  // Trailing spaces.
    noComments = noComments.replace(/\n+/g, '\n');  // Blank lines.
    noComments = noComments.trim();
    var lineCount = noComments.split('\n').length;
    var pre = document.getElementById('containerCode');
    pre.textContent = code;
    if (typeof prettyPrintOne == 'function') {
      code = pre.innerHTML;
      code = prettyPrintOne(code, 'js');
      pre.innerHTML = code;
    }
    if (lineCount == 1) {
      var text = BlocklyGames.getMsg('Games_linesOfCode1');
    } else {
      var text = BlocklyGames.getMsg('Games_linesOfCode2')
          .replace('%1', String(lineCount));
    }
    linesText.appendChild(document.createTextNode(text));
  }

FIle:soy.js

example "var count = BlocklyDialogs.congratulations(lineCount);"

In soy.js I need to receive the values of lineCount. I've already managed to do this using localStorage but I needed to do something more direct.

In testing I verified that the problem is in the lineCount variable because it is not passing a value to any variable even within the file itself. I created a variable outside the blocklyDialogs.congratulations function and entered a value of 5. I called the variable in the soy.js file and got it normally. I need to make the lineCount pass its value.

Nicson
  • 1
  • 2
  • `lineCount` is a block scoped `const`ant. It will not be available outside of that scope unless you change the scope, or provide the variable to the other logic in another way. – Taplar Mar 27 '20 at 17:21
  • I changed it to var, it still doesn't work. – Nicson Mar 27 '20 at 17:23
  • `var` are function scoped. https://stackoverflow.com/questions/500431/what-is-the-scope-of-variables-in-javascript – Taplar Mar 27 '20 at 17:23
  • I put the if (BlocklyGames.workspace) {...} out of BlocklyDialogs.congratulations = function () {...} He shows me the lineCount variable in the soy file but it arrives undefined – Nicson Mar 27 '20 at 17:39
  • I tried to create a function outside BlocklyDialogs.congratulations that way function nicson () {    let countLine = lineCount;    return countLine; } and in the soy.js file call the nicson function but it didn't work. – Nicson Mar 27 '20 at 18:29

1 Answers1

0

You can use event driven programming, pub-sub model.

    class EventManager {
      constructor(...listeners) {
        this.listeners = listeners;
      }
      register(fn) {
        const id = this.listeners.push(fn);
        return () => {
          this.listeners.splice(id - 1, 1);
        };
      }
      emit(data) {
        this.listeners.forEach(fn => fn(data));
      }
    }
    const pushEvents = new EventManager();
    
    // JS 1
    const unsubscribe1 = pushEvents.register(x => {
      console.log("event:", x);
    });
    pushEvents.register(x => {
      console.log("event:", x);
    });
    // JS 2
    pushEvents.emit("Tets data");
    
    //Output
    // event: Tets data
    // event: Tets data
    
    unsubscribe1();
    
    pushEvents.emit("Tets data2");
    //Output
    // event: Tets data2
.as-console-row {color: red!important}
xdeepakv
  • 7,835
  • 2
  • 22
  • 32