Update std.js #52

This commit is contained in:
Namhyeon Go 2022-06-05 19:50:37 +09:00 committed by GitHub
parent 39f4db1966
commit 39ec21b9ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -364,47 +364,57 @@ function StdEventableObject() {
};
};
function AsyncFunction(f) {
this.htmlfile = null;
this.proxyWindow = null;
function AsyncFunction(f, owner) {
this.f = f;
this.owner = owner;
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 f = function() {
_this.f();
AsyncFunction.counter--; // decrease number of async functions
var _f = function() {
if (typeof _this.f === "function") _this.f();
AsyncFunction.counter--;
};
if (this.proxyWindow != null) {
try {
this.proxyWindow.setTimeout(1, f);
} catch (e) {
console.warn("AsyncFunction proxyWindow.setTimeout failed:", e.message);
sleep(1, f);
}
// CLI or Window?
if (typeof WScript !== "undefined") {
require("lib/shell").show(["cscript", "app.js", this.owner, f].concat(args));
} else {
sleep(1, f);
sleep(1, _f);
}
};
this.runSynchronously = function() {
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.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.sleep = sleep;