mirror of
https://github.com/Ylianst/MeshAgent
synced 2026-01-13 22:14:00 +00:00
Toaster-1 change
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -59,7 +59,7 @@ function Promise(promiseFunc)
|
||||
{
|
||||
this._ObjectID = 'promise';
|
||||
this.promise = this;
|
||||
this._internal = { _ObjectID: 'promise.internal', promise: this, func: promiseFunc, completed: false, errors: false, completedArgs: [], internalCount: 0, _up: null };
|
||||
this._internal = { _ObjectID: 'promise.internal', promise: this, completed: false, errors: false, completedArgs: [], internalCount: 0, _up: null };
|
||||
require('events').EventEmitter.call(this._internal);
|
||||
Object.defineProperty(this, "parentPromise",
|
||||
{
|
||||
@@ -132,52 +132,53 @@ function Promise(promiseFunc)
|
||||
eventCallback.apply(this, []);
|
||||
}
|
||||
}).internal);
|
||||
this._internal.resolver = (function _resolver()
|
||||
this._internal.resolver = function _resolver()
|
||||
{
|
||||
if (_resolver._self.completed) { return; }
|
||||
_resolver._self.errors = false;
|
||||
_resolver._self.completed = true;
|
||||
_resolver._self.completedArgs = [];
|
||||
if (this.completed) { return; }
|
||||
this.errors = false;
|
||||
this.completed = true;
|
||||
this.completedArgs = [];
|
||||
var args = ['resolved'];
|
||||
if (this.emit_returnValue && this.emit_returnValue('resolved') != null)
|
||||
{
|
||||
_resolver._self.completedArgs.push(this.emit_returnValue('resolved'));
|
||||
this.completedArgs.push(this.emit_returnValue('resolved'));
|
||||
args.push(this.emit_returnValue('resolved'));
|
||||
}
|
||||
else
|
||||
{
|
||||
for (var a in arguments)
|
||||
{
|
||||
_resolver._self.completedArgs.push(arguments[a]);
|
||||
this.completedArgs.push(arguments[a]);
|
||||
args.push(arguments[a]);
|
||||
}
|
||||
}
|
||||
if (args.length == 2 && args[1]!=null && typeof(args[1]) == 'object' && args[1]._ObjectID == 'promise')
|
||||
{
|
||||
var pr = getRootPromise(_resolver._self.promise);
|
||||
args[1]._XSLF = _resolver._self;
|
||||
var pr = getRootPromise(this.promise);
|
||||
args[1]._XSLF = this;
|
||||
args[1].then(return_resolved, return_rejected);
|
||||
}
|
||||
else
|
||||
{
|
||||
_resolver._self.emit.apply(_resolver._self, args);
|
||||
_resolver._self.emit('settled');
|
||||
this.emit.apply(this, args);
|
||||
this.emit('settled');
|
||||
}
|
||||
}).internal;
|
||||
this._internal.rejector = (function _rejector()
|
||||
};
|
||||
|
||||
this._internal.rejector = function _rejector()
|
||||
{
|
||||
if (_rejector._self.completed) { return; }
|
||||
_rejector._self.errors = true;
|
||||
_rejector._self.completed = true;
|
||||
_rejector._self.completedArgs = [];
|
||||
if (this.completed) { return; }
|
||||
this.errors = true;
|
||||
this.completed = true;
|
||||
this.completedArgs = [];
|
||||
var args = ['rejected'];
|
||||
for (var a in arguments)
|
||||
{
|
||||
_rejector._self.completedArgs.push(arguments[a]);
|
||||
this.completedArgs.push(arguments[a]);
|
||||
args.push(arguments[a]);
|
||||
}
|
||||
|
||||
var r = getRootPromise(_rejector._self.promise);
|
||||
var r = getRootPromise(this.promise);
|
||||
if ((r._internal.external == null || r._internal.external == false) && r._internal.uncaught == null)
|
||||
{
|
||||
r._internal.uncaught = setImmediate(function (a)
|
||||
@@ -186,10 +187,9 @@ function Promise(promiseFunc)
|
||||
}, arguments[0]);
|
||||
}
|
||||
|
||||
_rejector._self.emit.apply(_rejector._self, args);
|
||||
_rejector._self.emit('settled');
|
||||
}).internal;
|
||||
this._internal.rejector.internal = true;
|
||||
this.emit.apply(this, args);
|
||||
this.emit('settled');
|
||||
};
|
||||
|
||||
this.catch = function(func)
|
||||
{
|
||||
@@ -223,38 +223,35 @@ function Promise(promiseFunc)
|
||||
if(rv._ObjectID == 'promise')
|
||||
{
|
||||
rv.parentPromise = this;
|
||||
rv._internal.once('resolved', retVal._internal.resolver);
|
||||
rv._internal.once('rejected', retVal._internal.rejector);
|
||||
rv._internal.once('resolved', retVal._internal.resolver.bind(retVal._internal).internal);
|
||||
rv._internal.once('rejected', retVal._internal.rejector.bind(retVal._internal).internal);
|
||||
}
|
||||
else
|
||||
{
|
||||
retVal._internal.resolver(rv);
|
||||
retVal._internal.resolver.call(retVal._internal, rv);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
this._internal.once('resolved', retVal._internal.resolver);
|
||||
this._internal.once('rejected', retVal._internal.rejector);
|
||||
this._internal.once('resolved', retVal._internal.resolver.bind(retVal._internal).internal);
|
||||
this._internal.once('rejected', retVal._internal.rejector.bind(retVal._internal).internal);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
this._internal.once('resolved', retVal._internal.resolver);
|
||||
this._internal.once('rejected', retVal._internal.rejector);
|
||||
this._internal.once('resolved', retVal._internal.resolver.bind(retVal._internal).internal);
|
||||
this._internal.once('rejected', retVal._internal.rejector.bind(retVal._internal).internal);
|
||||
}
|
||||
|
||||
this.__childPromise = retVal;
|
||||
return(retVal);
|
||||
};
|
||||
|
||||
this._internal.resolver._self = this._internal;
|
||||
this._internal.rejector._self = this._internal;;
|
||||
|
||||
try
|
||||
{
|
||||
promiseFunc.call(this, this._internal.resolver, this._internal.rejector);
|
||||
promiseFunc.call(this, this._internal.resolver.bind(this._internal), this._internal.rejector.bind(this._internal));
|
||||
}
|
||||
catch(e)
|
||||
catch (e)
|
||||
{
|
||||
this._internal.errors = true;
|
||||
this._internal.completed = true;
|
||||
@@ -281,8 +278,6 @@ function Promise(promiseFunc)
|
||||
|
||||
this._internal.once('settled', (function ()
|
||||
{
|
||||
delete this.resolver._self;
|
||||
delete this.rejector._self;
|
||||
delete this.promise._up;
|
||||
delete this.promise.__childPromise;
|
||||
delete this.promise.promise;
|
||||
@@ -355,4 +350,5 @@ Promise.all = function all(promiseList)
|
||||
|
||||
module.exports = Promise;
|
||||
module.exports.event_switcher = event_switcher;
|
||||
module.exports.event_forwarder = event_forwarder;
|
||||
module.exports.event_forwarder = event_forwarder;
|
||||
module.exports.defaultInit = function defaultInit(res, rej) { this.resolve = res; this.reject = rej; }
|
||||
@@ -38,12 +38,25 @@ if (process.platform == 'linux' || process.platform == 'darwin' || process.platf
|
||||
}
|
||||
}
|
||||
|
||||
function stdouthelper(c)
|
||||
{
|
||||
if (c.toString().includes('<DISMISSED>')) { this.stdin.write('exit\n'); }
|
||||
}
|
||||
function exithelper()
|
||||
{
|
||||
if (this.timeout != null) { clearTimeout(this.timeout); }
|
||||
this.toast.resolve('DISMISSED');
|
||||
|
||||
this.toast.child = null;
|
||||
this.toast = null;
|
||||
}
|
||||
function Toaster()
|
||||
{
|
||||
this._ObjectID = 'toaster';
|
||||
this.Toast = function Toast(title, caption, tsid)
|
||||
{
|
||||
var retVal = new promise(function (res, rej) { this._res = res; this._rej = rej; });
|
||||
//var retVal = new promise(function (res, rej) { this._res = res; this._rej = rej; });
|
||||
var retVal = new promise(promise.defaultInit);
|
||||
if (title == 'MeshCentral') { try { title = require('MeshAgent').displayName; } catch (x) { } }
|
||||
|
||||
retVal.title = title;
|
||||
@@ -70,7 +83,7 @@ function Toaster()
|
||||
{
|
||||
if(tsid != null && cid != 0)
|
||||
{
|
||||
retVal._rej('Insufficient permission to display toast as uid: ' + tsid);
|
||||
retVal.reject('Insufficient permission to display toast as uid: ' + tsid);
|
||||
return (retVal);
|
||||
}
|
||||
retVal.options.type = require('child_process').SpawnTypes.USER;
|
||||
@@ -78,7 +91,7 @@ function Toaster()
|
||||
}
|
||||
catch (ee)
|
||||
{
|
||||
retVal._rej('Cannot display user notification when a user is not logged in');
|
||||
retVal.reject('Cannot display user notification when a user is not logged in');
|
||||
return (retVal);
|
||||
}
|
||||
|
||||
@@ -87,24 +100,26 @@ function Toaster()
|
||||
retVal.child.toast = retVal;
|
||||
retVal.child.stdout.stdin = retVal.child.stdin;
|
||||
retVal.child.stderr.stdin = retVal.child.stdin;
|
||||
retVal.child.stdout.on('data', function (c) { if (c.toString().includes('<DISMISSED>')) { this.stdin.write('exit\n'); } });
|
||||
retVal.child.stderr.once('data', function (c) { this.stdin.write('$objBalloon.dispose();exit\n'); });
|
||||
//retVal.child.stdout.on('data', function (c) { if (c.toString().includes('<DISMISSED>')) { this.stdin.write('exit\n'); } });
|
||||
//retVal.child.stderr.once('data', function (c) { this.stdin.write('$objBalloon.dispose();exit\n'); });
|
||||
retVal.child.stdout.on('data', stdouthelper);
|
||||
retVal.child.stdin.write('[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")\r\n');
|
||||
retVal.child.stdin.write('$objBalloon = New-Object System.Windows.Forms.NotifyIcon\r\n');
|
||||
retVal.child.stdin.write('$objBalloon.Icon = [System.Drawing.SystemIcons]::Information\r\n');
|
||||
retVal.child.stdin.write('$objBalloon.Visible = $True\r\n');
|
||||
retVal.child.stdin.write('Register-ObjectEvent -InputObject $objBalloon -EventName BalloonTipClosed -Action { $objBalloon.dispose();Write-Host "<`DISMISSED`>" }')
|
||||
retVal.child.stdin.write('$objBalloon.ShowBalloonTip(10000, $env:_title, $env:_caption, 0)\r\n');
|
||||
retVal.child.timeout = setTimeout(function (c)
|
||||
{
|
||||
c.timeout = null;
|
||||
c.stdin.write('$objBalloon.dispose();exit\n');
|
||||
}, 10000, retVal.child);
|
||||
retVal.child.on('exit', function ()
|
||||
{
|
||||
if (this.timeout != null) { clearTimeout(this.timeout); }
|
||||
this.toast._res('DISMISSED');
|
||||
});
|
||||
//retVal.child.timeout = setTimeout(function (c)
|
||||
//{
|
||||
// c.timeout = null;
|
||||
// c.stdin.write('$objBalloon.dispose();exit\n');
|
||||
//}, 10000, retVal.child);
|
||||
//retVal.child.on('exit', function ()
|
||||
//{
|
||||
// if (this.timeout != null) { clearTimeout(this.timeout); }
|
||||
// this.toast._res('DISMISSED');
|
||||
//});
|
||||
retVal.child.on('exit', exithelper);
|
||||
|
||||
return (retVal);
|
||||
}
|
||||
@@ -120,7 +135,7 @@ function Toaster()
|
||||
}
|
||||
catch (xxe)
|
||||
{
|
||||
retVal._rej(xxe);
|
||||
retVal.reject(xxe);
|
||||
return (retVal);
|
||||
}
|
||||
|
||||
@@ -171,7 +186,7 @@ function Toaster()
|
||||
// Timeout Supported
|
||||
retVal._mb = require('message-box').create(retVal.title, retVal.caption, 5, 1);
|
||||
retVal._mb.toast = retVal;
|
||||
retVal._mb.then(function () { this.toast._res('DISMISSED'); }, function (e) { this.toast._res('DISMISSED'); });
|
||||
retVal._mb.then(function () { this.toast.resolve('DISMISSED'); }, function (e) { this.toast.resolve('DISMISSED'); });
|
||||
return (retVal);
|
||||
}
|
||||
else
|
||||
@@ -196,7 +211,7 @@ function Toaster()
|
||||
retVal.child.on('exit', function (code)
|
||||
{
|
||||
if (this.timeout) { clearTimeout(this.timeout); }
|
||||
this.parent._res('DISMISSED');
|
||||
this.parent.resolve('DISMISSED');
|
||||
});
|
||||
}
|
||||
else
|
||||
@@ -208,7 +223,7 @@ function Toaster()
|
||||
var xdg = require('user-sessions').findEnv(retVal.consoleUid, 'XDG_RUNTIME_DIR'); if (xdg == null) { xdg = ''; }
|
||||
if (!retVal.xinfo || !retVal.xinfo.display || !retVal.xinfo.xauthority)
|
||||
{
|
||||
retVal._rej('Internal Error');
|
||||
retVal.reject('Internal Error');
|
||||
return (retVal);
|
||||
}
|
||||
|
||||
@@ -217,7 +232,7 @@ function Toaster()
|
||||
retVal._notify.parent = retVal;
|
||||
retVal._notify.stdout.on('data', function (chunk) { });
|
||||
retVal._notify.stderr.on('data', function (chunk) { });
|
||||
retVal._notify.on('exit', function (code) { this.parent._res('DISMISSED'); });
|
||||
retVal._notify.on('exit', function (code) { this.parent.resolve('DISMISSED'); });
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -242,11 +257,11 @@ function Toaster()
|
||||
{
|
||||
retVal._mb = require('message-box').create(title, caption, 5, 'OK');
|
||||
retVal._mb.ret = retVal;
|
||||
retVal._mb.then(function () { this.ret._res('DISMISSED'); }, function () { this.ret._res('DISMISSED'); });
|
||||
retVal._mb.then(function () { this.ret.resolve('DISMISSED'); }, function () { this.ret.resolve('DISMISSED'); });
|
||||
}
|
||||
else
|
||||
{
|
||||
retVal._rej('Zenity/KDialog/xmessage not found');
|
||||
retVal.reject('Zenity/KDialog/xmessage not found');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -255,7 +270,7 @@ function Toaster()
|
||||
case 'darwin':
|
||||
retVal._toast = require('message-box').notify(title, caption);
|
||||
retVal._toast.parent = retVal;
|
||||
retVal._toast.then(function (v) { this.parent._res(v); }, function (e) { this.parent._rej(e); });
|
||||
retVal._toast.then(function (v) { this.parent.resolve(v); }, function (e) { this.parent.reject(e); });
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user