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

Commit

Permalink
hold
Browse files Browse the repository at this point in the history
  • Loading branch information
tgoc99 committed Aug 22, 2018
1 parent ceadf81 commit 8c60cb2
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 35 deletions.
2 changes: 1 addition & 1 deletion html/client.html
Expand Up @@ -11,7 +11,7 @@ <h3>Service Client</h3>
<script>
fin.desktop.main(async () => {
fin.desktop.InterApplicationBus.subscribe("*", "start", async function(message, uuid, name) {
const client = await fin.InterApplicationBus.Channel.connect({uuid});
const client = await fin.InterApplicationBus.Channel.connect(message);
client.register('multi-runtime-test', res => {
fin.desktop.InterApplicationBus.publish("multi-runtime-test-return", res);
});
Expand Down
2 changes: 1 addition & 1 deletion html/service.html
Expand Up @@ -10,7 +10,7 @@ <h3>Service Provider</h3>

<script>
fin.desktop.main(async () => {
const provider = await fin.InterApplicationBus.Channel.create('test-channel');
const provider = await fin.InterApplicationBus.Channel.create('mrtest');
provider.register('test', (payload, id) => {
provider.dispatch(id, 'multi-runtime-test', 'return-mrt');
return 'return-test';
Expand Down
42 changes: 17 additions & 25 deletions src/api/interappbus/channel/index.ts
Expand Up @@ -45,32 +45,27 @@ export class Channel extends EmitterBase {
await this.on('disconnected', listener);
}

// DOCS - if want to send payload, put payload in options
public async connect(options: Options): Promise<ChannelClient> {
const { uuid, name, channelName } = options;
let resolver: any;
let listener: any;
//@ts-ignore
// tslint:disable-next-line
public async connect(channelName: string, options?: Options): Promise<ChannelClient> {
if (!channelName || typeof channelName !== 'string') {
throw new Error('Please provide a channelName string to connect to a channel.');
}
const opts: any = options || {};
let resolver: (arg?: any) => void;
let listener: EventListener;
const waitResponse: Promise<ChannelClient> = new Promise(resolve => {
resolver = resolve;
listener = (payload: any) => {
if (uuid === payload.uuid) {
if (channelName && channelName !== payload.channelName) {
console.warn(`Channel created by ${uuid}: ${payload}
Still waiting for channelName ${channelName}.`);
return;
}
if (channelName === payload.channelName) {
this.removeListener('connected', listener);
this.connect(options).then(response => {
this.connect(channelName, opts).then(response => {
resolve(response);
});
}
};
this.on('connected', listener);
});
try {
const { payload: { data: providerIdentity } } = await this.wire.sendAction('connect-to-channel', options);
const { payload: { data: providerIdentity } } = await this.wire.sendAction('connect-to-channel', { channelName, ...opts});
// If there isn't a matching channel, the above sendAction call will error out and go to catch, skipping the logic below
if (resolver) {
resolver();
Expand All @@ -81,20 +76,23 @@ export class Channel extends EmitterBase {
this.channelMap.set(key, channel);
return channel;
} catch (e) {
const shouldWait: boolean = Object.assign({ wait: true }, options).wait;
const shouldWait: boolean = Object.assign({ wait: true }, opts).wait;
const internalNackMessage = 'internal-nack';
if (shouldWait && e.message === internalNackMessage) {
console.warn(`Channel not found for uuid: ${uuid}, waiting for channel creation.`);
console.warn(`Channel not found for channelName: ${channelName}, waiting for channel creation.`);
return await waitResponse;
} else if (e.message === internalNackMessage) {
throw new Error('No channel found');
throw new Error(`No channel found for channelName: ${channelName}`);
} else {
throw new Error(e);
}
}
}

public async create(channelName?: string): Promise<ChannelProvider> {
public async create(channelName: string): Promise<ChannelProvider> {
if (!channelName) {
throw new Error('Please provide a channelName to create a channel');
}
const { payload: { data: providerIdentity } } = await this.wire.sendAction('create-channel', {channelName});
const channel = new ChannelProvider(providerIdentity, this.wire.sendAction.bind(this.wire));
const key = providerIdentity.channelId;
Expand Down Expand Up @@ -151,9 +149,3 @@ export class Channel extends EmitterBase {
}
}
}

interface PluginSubscribeSuccess {
uuid: string;
name: string;
serviceName: string;
}
7 changes: 3 additions & 4 deletions test/external-channel.test.ts
Expand Up @@ -48,7 +48,7 @@ describe ('External Channel Provider', function() {
async function test () {
const spy = sinon.spy();
const finA = await launchAndConnect();
const provider = await finA.InterApplicationBus.Channel.create('test');
const provider = await finA.InterApplicationBus.Channel.create('ext-test');
provider.register('test', () => {
spy();
return 'return-test';
Expand All @@ -65,7 +65,7 @@ describe ('External Channel Provider', function() {
};
await finA.InterApplicationBus.subscribe({uuid: 'channel-client-test'}, 'return', listener);
await delayPromise(DELAY_MS);
await finA.InterApplicationBus.publish('start', 'hi');
await finA.InterApplicationBus.publish('start', 'ext-test');
await delayPromise(DELAY_MS);
}
test();
Expand Down Expand Up @@ -95,8 +95,7 @@ describe ('External Channel Provider', function() {
const finA = await launchAndConnect();
const service = await finA.Application.create(serviceConfig);
await service.run();
const providerIdentity = {uuid: 'channel-provider-test', name: 'channel-provider-test'};
const client = await finA.InterApplicationBus.Channel.connect(providerIdentity);
const client = await finA.InterApplicationBus.Channel.connect('ext-test');
client.dispatch('test').then(res => {
assert(res === 'return-test');
done();
Expand Down
8 changes: 4 additions & 4 deletions test/multi-runtime-channel.test.ts
Expand Up @@ -46,7 +46,7 @@ describe ('Multi Runtime Channels', function() {
async function test () {
const spy = sinon.spy();
const [finA, finB] = await Promise.all([launchAndConnect(), launchAndConnect()]);
const provider = await finB.InterApplicationBus.Channel.create();
const provider = await finB.InterApplicationBus.Channel.create('test');
provider.register('test', () => {
spy();
return 'return-test';
Expand Down Expand Up @@ -91,7 +91,7 @@ describe ('Multi Runtime Channels', function() {
const service = await finA.Application.create(serviceConfig);
await service.run();
await delayPromise(DELAY_MS);
const client = await finB.InterApplicationBus.Channel.connect({uuid: 'channel-provider-test'});
const client = await finB.InterApplicationBus.Channel.connect('test');
client.register('multi-runtime-test', (r: string) => {
assert.equal(r, 'return-mrt', 'wrong payload sent from service');
done();
Expand Down Expand Up @@ -156,13 +156,13 @@ describe ('Multi Runtime Channels', function() {

async function test() {
const [finA, finB] = await Promise.all([launchAndConnect(), launchAndConnect()]);
finB.InterApplicationBus.Channel.connect({uuid: 'channel-provider-mrtest'})
finB.InterApplicationBus.Channel.connect('mrtest')
.then((c) => {
c.register('multi-runtime-test', (r: string) => {
assert.equal(r, 'return-mrt', 'wrong payload sent from service');
done();
});
c.dispatch('test').then(res => {
c.dispatch('test').then((res: any) => {
assert.equal(res, 'return-test', 'wrong return payload from service');
});
});
Expand Down

0 comments on commit 8c60cb2

Please sign in to comment.