mirror of
https://github.com/gnh1201/welsonjs.git
synced 2025-05-13 05:01:03 +00:00
Update std.js #52
This commit is contained in:
parent
39f4db1966
commit
39ec21b9ba
60
lib/std.js
60
lib/std.js
|
@ -364,47 +364,57 @@ function StdEventableObject() {
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
function AsyncFunction(f) {
|
|
||||||
this.htmlfile = null;
|
function AsyncFunction(f, owner) {
|
||||||
this.proxyWindow = null;
|
|
||||||
this.f = f;
|
this.f = f;
|
||||||
|
this.owner = owner;
|
||||||
|
|
||||||
this.run = function() {
|
this.run = function() {
|
||||||
AsyncFunction.counter++; // increase number of async functions
|
var args = Array.from(arguments);
|
||||||
|
|
||||||
|
// increase number of async functions
|
||||||
|
AsyncFunction.counter++;
|
||||||
|
|
||||||
|
// decrease number of async functions
|
||||||
var _this = this;
|
var _this = this;
|
||||||
var f = function() {
|
var _f = function() {
|
||||||
_this.f();
|
if (typeof _this.f === "function") _this.f();
|
||||||
AsyncFunction.counter--; // decrease number of async functions
|
AsyncFunction.counter--;
|
||||||
};
|
};
|
||||||
|
|
||||||
if (this.proxyWindow != null) {
|
// CLI or Window?
|
||||||
try {
|
if (typeof WScript !== "undefined") {
|
||||||
this.proxyWindow.setTimeout(1, f);
|
require("lib/shell").show(["cscript", "app.js", this.owner, f].concat(args));
|
||||||
} catch (e) {
|
|
||||||
console.warn("AsyncFunction proxyWindow.setTimeout failed:", e.message);
|
|
||||||
sleep(1, f);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
sleep(1, f);
|
sleep(1, _f);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
this.runSynchronously = function() {
|
this.runSynchronously = function() {
|
||||||
return this.f.apply(null, arguments);
|
return this.f.apply(null, arguments);
|
||||||
};
|
};
|
||||||
|
|
||||||
// https://qiita.com/abcang/items/80b5733b5a96eeff9945
|
|
||||||
if (typeof WScript !== "undefined") {
|
|
||||||
try {
|
|
||||||
this.htmlfile = CreateObject("htmlfile");
|
|
||||||
this.proxyWindow = this.htmlfile.parentWindow;
|
|
||||||
} catch (e) {
|
|
||||||
console.warn("AsyncFunction require HTMLFILE object in WScript");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
AsyncFunction.counter = 0; // static variable
|
AsyncFunction.counter = 0; // static variable
|
||||||
|
AsyncFunction.bind = function(exports, args) {
|
||||||
|
var result = false;
|
||||||
|
|
||||||
|
if (args.length > 0) {
|
||||||
|
var f = '_async_' + args[0];
|
||||||
|
|
||||||
|
if (f in exports && typeof exports[f] === "function") {
|
||||||
|
try {
|
||||||
|
exports[f](args);
|
||||||
|
result = true;
|
||||||
|
} catch(e) {
|
||||||
|
console.error("AsyncFunction.bind exception", e.message);
|
||||||
|
result = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
};
|
||||||
|
|
||||||
global.GetResource = GetResource;
|
global.GetResource = GetResource;
|
||||||
global.sleep = sleep;
|
global.sleep = sleep;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user