0

(* As my previous question has been more or less answered, and the code has evolved, I open a new question. *)

I want to build a simple playground with MEAN stack that mimics plunker: we have a list of files and a textarea on the left hand, and a live preview on the right hand. Note that the files are saved in a temporary folder, and the live preview is an iframe injected by the files from that temporary folder.

enter image description here

********** what I have written **********

In the html file, I add one controller per file, so that I could track exactly which file is changed in the textarea. Then, I just need to save that file to the server, rather than all the files:

<div ng-repeat="file in files track by $index" ng-controller="fileCtrl">
    <a ng-click="go(file)">{{file.name}}</a>
</div>

the controller:

app.controller('fileCtrl', ['$scope', 'codeService', function ($scope, codeService) {
    $scope.$watch('file', function () {
        codeService.render($scope.files, $scope.file);
    }, true);
}]);

the codeService:

app.service('codeService', ['$http', function ($http) {
    this.render = function (files, changedFile) {
        $http.post('/writeFile', changedFile);
    };
}]);

the router:

router.post('/writeFile', function (req, res, next) {
    var file = req.body;
    var fs = require('fs');
    fs.writeFile("public/tmp/" + file.name, file.body, function (err) {
        if (err) { return console.log(err) };
    });
});

********** my tests **********

My tests show that the modification in the textarea is well caught, and the modified file can more or less be saved to the server: it works quite well for the 1st, 2nd writings, but it often has trouble for the writings that follow.

********** my questions **********

Could anyone help me re-structure the code to handle well:

  1. asynchronous calls of writings, such that all the writings are well (and fast) undertaken.
  2. writings with debounce, that means we could wait a little bit before each saving. But it's a little bit tricky when we have several files: assume we could switch among files very fast, how would debounce and asynchronous saving perform in that case?
Community
  • 1
  • 1
SoftTimur
  • 5,630
  • 38
  • 140
  • 292

1 Answers1

1


1. Here is plunker http://plnkr.co/edit/NerwghZaqcRuoswHYPlq?p=preview for you where you can play with debounce and asynchronous functions.
2. Once you switch between files very fast and type in different files, only the last result will be send to the server
3. Don't forget to handle errors in case you get it from BE (add catch() after each $http call to the server)
4. And on the Node Js side please add promise library that simplify your task. Personally I prefer Bluebird http://bluebirdjs.com/docs/api/promise.all.html

var files = [];
for (var i = 0; i < 100; ++i) {
    files.push(fs.writeFileAsync("file-" + i + ".txt", "", "utf-8"));
}
Promise.all(files).then(function() {
    console.log("all the files were created");
});

Hope it helps

Cassidy
  • 317
  • 3
  • 5
  • So you set one watcher for all the files, and save all the 3 items each time. If I save every time all the files even though there is only one that has been modified, it will be quite costly, right? How could I avoid this? Is there a quick way to detect which items have been changed? – SoftTimur Feb 21 '17 at 17:44
  • 1
    http://plnkr.co/edit/Ggee8sDzZmiFyH9V7dmG?p=preview yes, just add ng-change inside ng-repeat. check plunker for demo – Cassidy Feb 21 '17 at 17:49
  • I notice that you removed the global watcher and `debounce` when you add `ng-change`. As a result, it saves **every** change of a file rather than waiting a little bit. Is it possible to combine `debounce` and `ng-change`? – SoftTimur Feb 21 '17 at 18:02
  • Sure just add `
    ` ng-model option or wrap your save function` $scope.save =_.debounce(function _save(savedValue){ }, 3000) `
    – Cassidy Feb 21 '17 at 18:05
  • I see... here is http://plnkr.co/edit/HLD0meUFw27x997ZZKbI?p=preview . So now, we could 1) wait a little bit before saving; 2) save **all and only** the file(s) that have been changed... Thank you... – SoftTimur Feb 21 '17 at 18:12
  • You're welcome) please mark it as resolved if you don't have any questions. – Cassidy Feb 21 '17 at 18:18