Correct way to signal stream complete - gulp

I've created a function to pipe data into a file and output individual files for each line of data. However, there are 35K lines of data to compile and although the code below signals "done" after about 5 mins, the files are still processing. I can't start the next function until this one is completed so I need to know when it has actually finished.
function createLocations(done) {
if (GENERATE_LOCATIONS) {
for (var i = 0; i < locations.length; i++) {
var location = locations[i],
fileName = 'location-' + location.area.replace(/ +/g, '-').replace(/'+/g, '').replace(/&+/g, 'and').toLowerCase() + '-' + location.town.replace(/ +/g, '-').replace(/'+/g, '').replace(/`+/g, '').replace(/&+/g, 'and').toLowerCase();
gulp.src('./templates/location-template.html')
.pipe($.rename(fileName + ".html"))
.pipe(gulp.dest('./tmp/'));
}
}
done();
console.log('Creating '+locations.length+' Files Please Wait...');
}
The function runs for a further 15mins after it has signalled done. Appreciate any help to detect the actual completion of the function.
Many thanks!

Related

Inject AS3 or ABC to a swf file in runtime?

I've been seeing some tools for a browsergame that injects code to a swf file in order to automate some parts of the game.
I've been reading about swf flash format and I'm still don't knowing how that's possible, maybe the program runs the swf file with a custom flash player?
If you could guide me in this I would be very appreciated, this is very interesting.
Just a try:
var actionString:String = "methodAddOne";
var paramString:String = "1";
function methodAddOne(num:String):void{
trace(Number(num)+1);
}
this[actionString](paramString); //trace: 2
For a queue of params:
var actionString2:String="methodAdd";
var paramString2:String="1,2,3,-6";
function methodAdd(nums:String):void{
var params:Array= nums.split(",");
var out=0;
for each(var num:Number in params){
out += num;
}
trace(out);
}
this[actionString2](paramString2);// returns 0
All calculations, core-Methods,... you have to parse. Very complicated...
E.g. try to parse and calculate "1+2+3-6". Its possible of course, but hard to do with other Stuff and math-grammar.
So, the easier way is to provide a String-based Api with all methods, the
user can predefined use with params.
like this:
var stackString:String = "methodAddOne(100); methodAdd(1,2,3);";
function parseFunctionsString(str:String):void
{
//better use regExp at all the following
var f:String = str.substring(0,str.indexOf("("));
var p:String = str.substring(str.indexOf("(") + 1,str.indexOf(")"));
// clean spaces // also better regExp to validate some other user mistakes - just for this test in this way
f = f.replace(" ","");
p = p.replace(" ","");
//trace("** "+f+" ("+p+")");
if (this[f])
{
this[f](p);
}
else
{
trace("unknown function: "+f);
}
str = str.substr(str.indexOf(";") + 1,str.length - str.indexOf(";") + 1);
if (str.length > 2)
{
parseFunctionsString(str);
}
}
parseFunctionsString(stackString);
// trace:
// 101
// 6
As an idea... Greetings André

cc.textureCache.addImage blocking UI

I want to cache lot of textures in a running scene without blocking it, theoretically cc.textureCache.addImage do caching async if you call it the three arguments. This is not happening.
Code:
rainOfArrows: {
_animFrames:[],
loaded: function(){
console.log('>>>>>>>>>>>>>>LOAD!')
},
load: function(){
/* Load all animation arrow sprites in memory & generate animation */
var str = '';
for (var i = 0; i < 40; i++) {
str = 'arrow00' + (i < 10 ? ('0' + i) : i) + '.png';
cc.textureCache.addImage('res/SkilllsAnimations/arrowRain/'+str, this.loaded, this);
var spriteFrame = new cc.SpriteFrame(texture, cc.rect(0,0,winsize.width,winsize.height));
cc.spriteFrameCache.addSpriteFrame(spriteFrame, str);
this._animFrames.push(cc.spriteFrameCache.getSpriteFrame(str));
}
},
run: function():{ ----- }
}
The function loaded is executed and string '>>>>>>>>>>>>>>LOAD!' is printed async. But the hole scene freezes until all textures are loaded.
Before v3.0, this was done with addImageAsync and worked fine, now in 3.2 this is merged in addImage but I can not get it working, am I missing something?
Btw, i'm not using textures packed in one img and plist because they're too big.
A fix for this has been submitted by pandamicro and will be available in 3.4
Here it is in case you need it before 3.4: https://github.com/pandamicro/cocos2d-js/commit/2a124b5

HTML5 File Reader failed to add multiple files and only adding one file from the list

I want to add three files simulteneously.Files with extension kml,kmz and csv.
When I select all three files and click open I get all three files in FileList.
But only one file is getting added on map.If I add individual file i.e. one by one it works fine. reader onloadend event is fired 3 times.But all the time it adds only one of the three files.
function handleFileSelect(evt) {
var files = evt.target.files; // FileList
for (var i = 0; i < files.length; i++) {
f = files[i];
fileExtension = f.name.split('.').pop();
if(fileExtension != 'kml' && fileExtension !='kmz' && fileExtension != 'csv'){
alert('Unsupported file type ' + f.type + '(' + fileExtension + ')');
return;
}
var fileReaderkmlcsv = new FileReader();
fileReaderkmlcsv.readAsText(f);
fileReaderkmlcsv.onloadend =loadend;
} //- end for loop } //handleFileSelect
The problem was asynchronous code execution in javascript.
Here is the answer-
Asynchronous execution in javascript any solution to control execution?

How do I process one src file multiple ways using gulp?

I want to use one source file to generate multiple destination files with different characteristics. Here's a watered down version of what I want to do:
gulp.task('build', function() {
var src = gulp.src('myfile.txt')
for (var i = 0; i < 10; i++) {
src
.pipe(someplugin(i))
.pipe(rename('myfile-' + i + '.txt'))
.dest('./build')
}
});
Presumably, "someplugin" would change the file contents based on the index passed to it, so the code above would generate 10 files, each with slightly different content.
This doesn't work, and I didn't expect it to, but is there another way to achieve this?
I ended up creating a function that builds my tasks for me. Here's an example:
function taskBuilder(i) {
return function() {
gulp.src('*.mustache')
.pipe(someplugin(i))
.pipe(rename('myfile-' + i + '.txt'))
.dest('./build');
};
}
var tasks, task, i;
for (i = 0; i < 10; i++) {
taskName = 'tasks-' + i;
gulp.task(taskName, taskBuilder(i));
tasks.push(task);
}
gulp.task('default', tasks);
Maybe you should check this :
How to save a stream into multiple destinations with Gulp.js?
If it doesn't answer your question, I believe a simple solution would be to have many tasks all running one after the other. For example :
gulp.task('build', function() {
return gulp.src('*.txt')
.pipe(someplugin(i))
.pipe(rename('myfile-1.txt'))
.dest('./build')
}
});
gulp.task('build2', [build], function() {
return gulp.src('*.txt')
.pipe(someplugin(i))
.pipe(rename('myfile-2.txt'))
.dest('./build')
}
});
Running task 'build2' will run 'build' first, and then 'build2' when it's complete. If you don't have to wait for 'build2' to be finished, just remove the "return" and they will run at the same time.

Execution order of GS files in a Project

Where can I read documentation concerning the execution order rules for GS files?
To dimension the problem I created two trivial objects, each in their own file.
1_File.gs
var ObjB = new Object();
ObjB.sayName = "[" + ObjA.sayName + "]";
0_File.gs
var ObjA = new Object();
ObjA.sayName = " I'm A ";
A call such as ...
Logger.log(ObjA.sayName + " : " + ObjB.sayName);
... gets the error ...
TypeError: Cannot read property "sayName" from undefined.
If I move the code from 1_File.gs into 0_File.gs, and vice versa, then there is no error and the log shows correctly ...
I'm A : [ I'm A ]
Renaming 0_File.gs to 2_File.gs doesn't affect execution order either, so I assume that order depends on which file gets created first.
Is there no concept of "include" or "import" that would allow me to make order of execution explicit?
Where can I read documentation concerning the execution order rules for GS files?
There is no such documentation and I think will not be any time published. In similar way, an initialization order of the static variables in C++ is also undefined and depends on compiler/linker.
Is there no concept of "include" or "import" that would allow me to make order of execution explicit?
Yes, there is no "includes", "imports" and even "modules", but there are libraries.
Also there is a workaround by using a closure. Bellow is a sample code. By executing the test function the log contains c.d. The idea is to have in all gs files a function started with init. In these functions all global variables are instanced. The anonymous closure is executed during the Code.gs file instancing and calls all "init" functions of all gs files.
Code.gs
var c;
function callAllInits_() {
var keys = Object.keys(this);
for (var i = 0; i < keys.length; i++) {
var funcName = keys[i];
if (funcName.indexOf("init") == 0) {
this[funcName].call(this);
}
}
}
(function() {
callAllInits_();
c = { value : 'c.' + d.value };
})();
function test() {
Logger.log(c.value);
}
d.gs
var d;
function initD() {
d = { value : 'd' };
};
I tackled this problem by creating a class in each file and making sure that each class is instantiated in the original Code.gs (which I renamed to _init.gs). Instantiating each class acts as a form of include and makes sure everything is in place before executing anything.
_init.gs:
// These instances can now be referred to in all other files
var Abc = new _Abc();
var Menu = new _Menu();
var Xyz = new _Xyz();
var Etc = new _Etc();
// We need the global context (this) in order to dynamically add functions to it
Menu.createGlobalFunctions(this);
function onInstall(e) {
onOpen(e);
}
function onOpen(e) {
Menu.build();
}
And classes usually look like this:
menu.gs:
function _Menu() {
this.build = function() {
...
}
...
}
If you have more than one level of inheritance, you need to give the init functions names like init000Foo, init010Bar, and init020Baz, and then sort the init functions by name before executing. This will ensure init000Foo gets evaluated first, then Bar, then Baz.
function callAllInits() {
var keys = Object.keys(this);
var inits = new Array();
for (var i = 0; i < keys.length; i += 1) {
var funcName = keys[i];
if (funcName.indexOf("init") == 0) {
inits.push(funcName);
}
}
inits.sort();
for (var i = 0; i < inits.length; i += 1) {
// To see init order:
// Logger.log("Initializing " + inits[i]);
this[inits[i]].call(this);
}
}
The other answers (i.e., don't write any top-level code which references objects in other files) describe the ideal way to avoid this problem. However, if you've already written a lot of code and rewriting it is not feasible, there is a workaround:
Google App Script appears to load code files in the order they were created. The oldest file first, followed by the next, and the most recently created file last. This is the order displayed in the editor when "Sort files alphabetically" is unchecked.
Thus, if you have the files in this order:
Code.gs
1_File.gs (depends on 0_File.gs)
0_File.gs
An easy fix is to make a copy of 1_File.gs and then delete the original, effectively moving it to the end of the list.
Click the triangle next to 1_File.gs and select "Make a copy"
Code.gs
1_File.gs
0_File.gs
1_File copy.gs
Click the triangle next to 1_File.gs and select "Delete"
Code.gs
0_File.gs
1_File copy.gs
Click the triangle next to 1_File copy.gs and select "Rename", then remove the " copy" from the end.
Code.gs
0_File.gs
1_File.gs
Now 0_File.gs is loaded before 1_File.gs.
This works for me as of December 2021. Quite likely, the other answers are outdated.
You can easily fix this. When you look at the scripts in the "Files" section of the web editor, you see they have an order. Files are evaluated in the order they appear there. Clicking on the three dots to the right of a file name brings up a menu that allows you to move a file up or down.
There is no such order in Google Apps Script. It purely depends on where you have these objects declared and how your function is invoked.
Can you explain a bit about how and when your Logger.log() code will be invoked.
Also, when do you declare your objects objA and objB ?
These will help us provide a better answer
here is how I would do this...
main
function include(filename) {
return ContentService.createTextOutput(filename);
}
function main() {
include('Obj A');
include('Obj B');
Logger.log(ObjA.sayName + " : " + ObjB.sayName);
}
Obj A
var ObjA = new Object();
ObjA.sayName = " I'm A ";
Obj B
var ObjB = new Object();
ObjB.sayName = "[" + ObjA.sayName + "]";