Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve close() handling, now resolves and cleanly terminates process #33

Merged
merged 4 commits into from
Apr 12, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions src/Dialog/AbstractDialog.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Clue\React\Zenity\Dialog;

use React\Promise\Deferred;
use React\ChildProcess\Process;
use Clue\React\Zenity\Zen\BaseZen;

/**
Expand Down Expand Up @@ -204,14 +202,12 @@ protected function decamelize($name)
/**
* Create a dialog handler to process the results for this dialog.
*
* @param Deferred $deferred
* @param Process $process
* @return BaseZen
* @internal
*/
public function createZen(Deferred $deferred, Process $process)
public function createZen()
{
return new BaseZen($deferred, $process);
return new BaseZen();
}

/**
Expand Down
6 changes: 2 additions & 4 deletions src/Dialog/CalendarDialog.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

use Clue\React\Zenity\Dialog\AbstractTextDialog;
use DateTime;
use React\Promise\Deferred;
use React\ChildProcess\Process;
use Clue\React\Zenity\Zen\CalendarZen;

/**
Expand Down Expand Up @@ -86,8 +84,8 @@ public function setDateTime(DateTime $date)
return $this;
}

public function createZen(Deferred $deferred, Process $process)
public function createZen()
{
return new CalendarZen($deferred, $process);
return new CalendarZen();
}
}
6 changes: 2 additions & 4 deletions src/Dialog/ColorSelectionDialog.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
namespace Clue\React\Zenity\Dialog;

use Clue\React\Zenity\Dialog\AbstractDialog;
use React\Promise\Deferred;
use React\ChildProcess\Process;
use Clue\React\Zenity\Zen\ColorSelectionZen;

/**
Expand Down Expand Up @@ -43,8 +41,8 @@ public function setShowPalette($palette)
return $this;
}

public function createZen(Deferred $deferred, Process $process)
public function createZen()
{
return new ColorSelectionZen($deferred, $process);
return new ColorSelectionZen();
}
}
6 changes: 2 additions & 4 deletions src/Dialog/FileSelectionDialog.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
namespace Clue\React\Zenity\Dialog;

use Clue\React\Zenity\Dialog\AbstractDialog;
use React\Promise\Deferred;
use React\ChildProcess\Process;
use Clue\React\Zenity\Zen\FileSelectionZen;

/**
Expand Down Expand Up @@ -122,8 +120,8 @@ public function setFileFilter($filter)
return $this;
}

public function createZen(Deferred $deferred, Process $process)
public function createZen()
{
return new FileSelectionZen($deferred, $process, $this->multiple, $this->separator);
return new FileSelectionZen($this->multiple, $this->separator);
}
}
6 changes: 2 additions & 4 deletions src/Dialog/FormsDialog.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
namespace Clue\React\Zenity\Dialog;

use Clue\React\Zenity\Dialog\AbstractTextDialog;
use React\Promise\Deferred;
use React\ChildProcess\Process;
use Clue\React\Zenity\Zen\FormsZen;

/**
Expand Down Expand Up @@ -153,8 +151,8 @@ public function getArgs()
return array_merge(parent::getArgs(), $this->fields);
}

public function createZen(Deferred $deferred, Process $process)
public function createZen()
{
return new FormsZen($deferred, $process, $this->separator);
return new FormsZen($this->separator);
}
}
6 changes: 2 additions & 4 deletions src/Dialog/ListDialog.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
namespace Clue\React\Zenity\Dialog;

use Clue\React\Zenity\Dialog\AbstractTextDialog;
use React\Promise\Deferred;
use React\ChildProcess\Process;
use Clue\React\Zenity\Zen\ListZen;

/**
Expand Down Expand Up @@ -204,10 +202,10 @@ public function getArgs()
return array_merge(parent::getArgs(), $this->columns);
}

public function createZen(Deferred $deferred, Process $process)
public function createZen()
{
$single = (!$this->multiple && !$this->checklist);

return new ListZen($deferred, $process, $single, $this->separator);
return new ListZen($single, $this->separator);
}
}
6 changes: 2 additions & 4 deletions src/Dialog/NotificationDialog.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

use Clue\React\Zenity\Dialog\AbstractTextDialog;
use Clue\React\Zenity\Zen\NotificationZen;
use React\Promise\Deferred;
use React\ChildProcess\Process;

/**
* Use the --notification option to create a notification icon.
Expand All @@ -32,8 +30,8 @@ public function setListen($listen)
return $this;
}

public function createZen(Deferred $deferred, Process $process)
public function createZen()
{
return new NotificationZen($deferred, $process);
return new NotificationZen();
}
}
6 changes: 2 additions & 4 deletions src/Dialog/PasswordDialog.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
namespace Clue\React\Zenity\Dialog;

use Clue\React\Zenity\Dialog\AbstractDialog;
use React\Promise\Deferred;
use React\ChildProcess\Process;
use Clue\React\Zenity\Zen\PasswordZen;

/**
Expand Down Expand Up @@ -50,8 +48,8 @@ public function setUsername($username)
return $this;
}

public function createZen(Deferred $deferred, Process $process)
public function createZen()
{
return new PasswordZen($deferred, $process, $this->username);
return new PasswordZen($this->username);
}
}
6 changes: 2 additions & 4 deletions src/Dialog/ProgressDialog.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

use Clue\React\Zenity\Dialog\AbstractTextDialog;
use Clue\React\Zenity\Zen\ProgressZen;
use React\Promise\Deferred;
use React\ChildProcess\Process;

/**
* Use the --progress option to create a progress dialog.
Expand Down Expand Up @@ -79,8 +77,8 @@ public function setNoCancel($noc)
return $this;
}

public function createZen(Deferred $deferred, Process $process)
public function createZen()
{
return new ProgressZen($deferred, $process, $this->percentage);
return new ProgressZen($this->percentage);
}
}
6 changes: 2 additions & 4 deletions src/Dialog/ScaleDialog.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
namespace Clue\React\Zenity\Dialog;

use Clue\React\Zenity\Dialog\AbstractTextDialog;
use React\Promise\Deferred;
use React\ChildProcess\Process;
use Clue\React\Zenity\Zen\ScaleZen;

/**
Expand Down Expand Up @@ -90,8 +88,8 @@ public function setHideValue($toggle = true)
return $this;
}

public function createZen(Deferred $deferred, Process $process)
public function createZen()
{
return new ScaleZen($deferred, $process);
return new ScaleZen();
}
}
6 changes: 2 additions & 4 deletions src/Dialog/TextInfoDialog.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

use Clue\React\Zenity\Dialog\AbstractDialog;
use Clue\React\Zenity\Zen\TextInfoZen;
use React\Promise\Deferred;
use React\ChildProcess\Process;

/**
* Use the --text-info option to create a text information dialog.
Expand Down Expand Up @@ -81,8 +79,8 @@ public function addLine($line)
return $this;
}

public function createZen(Deferred $deferred, Process $process)
public function createZen()
{
return new TextInfoZen($deferred, $process);
return new TextInfoZen();
}
}
27 changes: 2 additions & 25 deletions src/Launcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,31 +46,8 @@ public function launch(AbstractDialog $dialog)
$process->stdin->write($inbuffer);
}

$deferred = new Deferred();

$result = null;
$process->stdout->on('data', function ($data) use (&$result) {
if ($data !== '') {
$result .= $data;
}
});

$zen = $dialog->createZen($deferred, $process);

$process->on('exit', function($code) use ($process, $zen, &$result, $deferred) {
if ($code !== 0) {
$deferred->reject($code);
} else {
if ($result === null) {
$result = true;
} else {
$result = $zen->parseValue(trim($result));
}
$deferred->resolve($result);
}

$zen->close();
});
$zen = $dialog->createZen();
$zen->go($process);

return $zen;
}
Expand Down
46 changes: 38 additions & 8 deletions src/Zen/BaseZen.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,66 @@
namespace Clue\React\Zenity\Zen;

use React\Promise\PromiseInterface;
use React\Promise\Deferred;
use React\ChildProcess\Process;
use React\Promise\Deferred;

class BaseZen implements PromiseInterface
{
private $deferred;
private $process;
protected $promise;
protected $deferred;
protected $process;

public function __construct(Deferred $deferred, Process $process)
/** @internal */
public function go(Process $process)
{
$this->deferred = $deferred;
$this->deferred = $deferred = new Deferred();
$this->process = $process;

$buffered = null;
$process->stdout->on('data', function ($data) use (&$buffered) {
if ($data !== '') {
$buffered .= $data;
}
});

$process->on('exit', function($code) use ($deferred) {
if ($code !== 0) {
$deferred->reject($code);
} else {
$deferred->resolve();
}
});

$that = $this;
$this->promise = $deferred->promise()->then(function () use (&$buffered, $that) {
if ($buffered === null) {
$buffered = true;
} else {
$buffered = $that->parseValue(trim($buffered));
}
return $buffered;
});
}

public function then($fulfilledHandler = null, $errorHandler = null, $progressHandler = null)
{
return $this->deferred->then($fulfilledHandler, $errorHandler, $progressHandler);
return $this->promise->then($fulfilledHandler, $errorHandler, $progressHandler);
}

public function close()
{
if ($this->process !== null) {
$this->process->close();
$this->deferred->resolve();

if ($this->process !== null && $this->process->isRunning()) {
$this->process->terminate(SIGKILL);
}

return $this;
}

protected function writeln($line)
{
if ($this->process === null) return;
$this->process->stdin->write($line . PHP_EOL);
}

Expand Down
6 changes: 1 addition & 5 deletions src/Zen/FileSelectionZen.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,15 @@
namespace Clue\React\Zenity\Zen;

use Clue\React\Zenity\Zen\BaseZen;
use React\Promise\Deferred;
use React\ChildProcess\Process;
use SplFileInfo;

class FileSelectionZen extends BaseZen
{
private $multiple;
private $separator;

public function __construct(Deferred $deferred, Process $process, $multiple, $separator)
public function __construct($multiple, $separator)
{
parent::__construct($deferred, $process);

$this->multiple = $multiple;
$this->separator = $separator;
}
Expand Down
6 changes: 1 addition & 5 deletions src/Zen/FormsZen.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,13 @@
namespace Clue\React\Zenity\Zen;

use Clue\React\Zenity\Zen\BaseZen;
use React\Promise\Deferred;
use React\ChildProcess\Process;

class FormsZen extends BaseZen
{
private $separator;

public function __construct(Deferred $deferred, Process $process, $separator)
public function __construct($separator)
{
parent::__construct($deferred, $process);

$this->separator = $separator;
}

Expand Down
6 changes: 1 addition & 5 deletions src/Zen/ListZen.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,14 @@
namespace Clue\React\Zenity\Zen;

use Clue\React\Zenity\Zen\BaseZen;
use React\Promise\Deferred;
use React\ChildProcess\Process;

class ListZen extends BaseZen
{
private $single;
private $separator;

public function __construct(Deferred $deferred, Process $process, $single, $separator)
public function __construct($single, $separator)
{
parent::__construct($deferred, $process);

$this->single = $single;
$this->separator = $separator;
}
Expand Down
Loading