mirror of
https://github.com/gnh1201/welsonjs.git
synced 2025-02-06 15:04:58 +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;
|
||||
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;
|
||||
|
|
Loading…
Reference in New Issue
Block a user