Skip to content
This repository has been archived by the owner on May 22, 2020. It is now read-only.

Commit

Permalink
RUN-4730 listener callback not implemented in launchExternalProcess (#…
Browse files Browse the repository at this point in the history
…204)

* RUN-4730 add listener handling codes

* RUN-4730 Use promise to reject error and update the test

* RUN-4730 fix linting issue
  • Loading branch information
licui3936 authored and rdepena committed Nov 29, 2018
1 parent f592f86 commit 60e8ff4
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 15 deletions.
42 changes: 39 additions & 3 deletions src/api/system/system.ts
Expand Up @@ -12,7 +12,8 @@ import { RVMInfo } from './rvm';
import { RuntimeInfo } from './runtime-info';
import { Entity, EntityInfo } from './entity';
import { HostSpecs } from './host-specs';
import { ExternalProcessRequestType , TerminateExternalRequestType, ExternalConnection, ExternalProcessInfo } from './external-process';
import { ExternalProcessRequestType , TerminateExternalRequestType, ExternalConnection, ExitCode,
ExternalProcessInfo } from './external-process';
import Transport from '../../transport/transport';
import { CookieInfo, CookieOption } from './cookie';
import { RegistryInfo } from './registry-info';
Expand All @@ -21,6 +22,7 @@ import { RuntimeError, NotSupportedError } from '../../transport/transport-error
import { ClearCacheOption } from './clearCacheOption';
import { CrashReporterOption } from './crashReporterOption';
import { SystemEvents } from '../events/system';
import { _Window } from '../window/window';

/**
* AppAssetInfo interface
Expand Down Expand Up @@ -592,8 +594,42 @@ export default class System extends EmitterBase<SystemEvents> {
* @tutorial System.launchExternalProcess
*/
public launchExternalProcess(options: ExternalProcessRequestType): Promise<Identity> {
return this.wire.sendAction('launch-external-process', options)
.then(({ payload }) => payload.data);
return new Promise((resolve, reject) => {
const exitEventKey: string = 'external-process-exited';
let processUuid: string;
let externalProcessExitHandler: (payload: any) => void;
let ofWindow: _Window;
if (typeof options.listener === 'function') {
externalProcessExitHandler = (payload: any) => {
const data: any = payload || {};
const exitPayload: ExitCode = {
topic: 'exited',
uuid: data.processUuid || '',
exitCode: data.exitCode || 0
};
if (processUuid === payload.processUuid) {
options.listener(exitPayload);
ofWindow.removeListener(exitEventKey, externalProcessExitHandler);
}
};
// window constructor expects the name is not undefined
if (!this.wire.me.name) {
this.wire.me.name = this.wire.me.uuid;
}
ofWindow = new _Window(this.wire, this.wire.me);
ofWindow.on(exitEventKey, externalProcessExitHandler);
}
this.wire.sendAction('launch-external-process', options)
.then(({ payload }) => {
processUuid = payload.data.uuid;
resolve(payload.data);
}).catch((err: Error) => {
if (ofWindow) {
ofWindow.removeListener(exitEventKey, externalProcessExitHandler);
}
reject(err);
});
});
}

/**
Expand Down
27 changes: 15 additions & 12 deletions test/system.test.ts
Expand Up @@ -2,6 +2,7 @@ import { conn } from './connect';
import { Fin } from '../src/main';
import * as assert from 'assert';
import { cleanOpenRuntimes } from './multi-runtime-utils';
import { ExternalProcessRequestType, ExitCode } from '../src/api/system/external-process';

describe('System.', function () {
let fin: Fin;
Expand Down Expand Up @@ -207,19 +208,21 @@ describe('System.', function () {
}));
});

describe.skip('launchExternalProcess()', () => {
const processOptions = {
path: 'notepad',
arguments: '',
listener: (code: any) => { code = 'a'; }
};

describe('launchExternalProcess()', () => {
it('Fulfilled', (done) => {
fin.System.launchExternalProcess(processOptions)
.then(() => {
assert(true);
return done();
});
const processOptions: ExternalProcessRequestType = {
path: 'notepad',
arguments: '',
listener: (result: ExitCode) => {
assert(result.topic === 'exited', 'Expected topic is "exited"');
assert(result.exitCode === 0);
done();
}
};

fin.System.launchExternalProcess(processOptions).then(identity => {
fin.System.terminateExternalProcess({ uuid: identity.uuid, timeout: 1000, killTree: true});
});
});
});

Expand Down

0 comments on commit 60e8ff4

Please sign in to comment.