Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
9a142175aa | |||
09b593e192 | |||
c27fc147b5 | |||
ddde21925a | |||
bd849d347d | |||
f2a85d4719 | |||
4e7c28ac83 | |||
243f1a70e9 |
2
package-lock.json
generated
2
package-lock.json
generated
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartuniverse",
|
"name": "@pushrocks/smartuniverse",
|
||||||
"version": "1.0.82",
|
"version": "1.0.86",
|
||||||
"lockfileVersion": 1,
|
"lockfileVersion": 1,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartuniverse",
|
"name": "@pushrocks/smartuniverse",
|
||||||
"version": "1.0.82",
|
"version": "1.0.86",
|
||||||
"private": false,
|
"private": false,
|
||||||
"description": "messaging service for your micro services",
|
"description": "messaging service for your micro services",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
|
31
test/test.ts
31
test/test.ts
@ -86,6 +86,37 @@ tap.test('should receive a message correctly', async (tools) => {
|
|||||||
await done.promise;
|
await done.promise;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
interface IDemoReqRes {
|
||||||
|
method: 'demo',
|
||||||
|
request: {
|
||||||
|
wowso: string;
|
||||||
|
};
|
||||||
|
response: {
|
||||||
|
hereso: string;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
tap.test('ReactionRequest and ReactionResponse should work', async () => {
|
||||||
|
const reactionResponse = new smartuniverse.ReactionResponse<IDemoReqRes>({
|
||||||
|
channels: [testUniverse.getChannel(testChannelData.channelName)],
|
||||||
|
funcDef: async reqData => {
|
||||||
|
console.log(reqData);
|
||||||
|
return {
|
||||||
|
hereso: 'Hello there'
|
||||||
|
};
|
||||||
|
},
|
||||||
|
method: 'demo'
|
||||||
|
});
|
||||||
|
const reactionRequest = new smartuniverse.ReactionRequest<IDemoReqRes>({
|
||||||
|
method: 'demo'
|
||||||
|
});
|
||||||
|
const reactionResult = await reactionRequest.fire([testClientUniverse2.getChannel(testChannelData.channelName)], {
|
||||||
|
wowso: 'wowza'
|
||||||
|
});
|
||||||
|
const result = await reactionResult.getFirstResult();
|
||||||
|
console.log(result);
|
||||||
|
});
|
||||||
|
|
||||||
tap.test('should disconnect the client correctly', async () => {
|
tap.test('should disconnect the client correctly', async () => {
|
||||||
await testClientUniverse.stop();
|
await testClientUniverse.stop();
|
||||||
});
|
});
|
||||||
|
@ -28,7 +28,7 @@ export class ReactionRequest<T extends plugins.typedrequestInterfaces.ITypedRequ
|
|||||||
this.method = optionsArg.method;
|
this.method = optionsArg.method;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async fire(channelsArg: Array<UniverseChannel | ClientUniverseChannel>, timeoutMillisArg=60000) {
|
public async fire(channelsArg: Array<UniverseChannel | ClientUniverseChannel>, requestDataArg: T['request'], timeoutMillisArg=5000) {
|
||||||
const subscriptionMap = new plugins.lik.Objectmap<plugins.smartrx.rxjs.Subscription>();
|
const subscriptionMap = new plugins.lik.Objectmap<plugins.smartrx.rxjs.Subscription>();
|
||||||
const reactionResult = new ReactionResult<T>();
|
const reactionResult = new ReactionResult<T>();
|
||||||
const requestId = plugins.smartunique.shortId();
|
const requestId = plugins.smartunique.shortId();
|
||||||
@ -42,6 +42,18 @@ export class ReactionRequest<T extends plugins.typedrequestInterfaces.ITypedRequ
|
|||||||
reactionResult.pushReactionResponse(payload.typedRequestPayload.response);
|
reactionResult.pushReactionResponse(payload.typedRequestPayload.response);
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
|
const payload: ICombinatorPayload<T> = {
|
||||||
|
id: requestId,
|
||||||
|
typedRequestPayload: {
|
||||||
|
method: this.method,
|
||||||
|
request: requestDataArg,
|
||||||
|
response: null
|
||||||
|
}
|
||||||
|
};
|
||||||
|
channel.sendMessage({
|
||||||
|
messageText: 'reactionRequest',
|
||||||
|
payload
|
||||||
|
});
|
||||||
}
|
}
|
||||||
plugins.smartdelay.delayFor(timeoutMillisArg).then(async () => {
|
plugins.smartdelay.delayFor(timeoutMillisArg).then(async () => {
|
||||||
await subscriptionMap.forEach(subscriptionArg => {
|
await subscriptionMap.forEach(subscriptionArg => {
|
||||||
|
@ -2,7 +2,7 @@ import * as plugins from './smartuniverse.plugins';
|
|||||||
import { ReactionResponse } from './smartuniverse.classes.reactionresponse';
|
import { ReactionResponse } from './smartuniverse.classes.reactionresponse';
|
||||||
|
|
||||||
export class ReactionResult<T extends plugins.typedrequestInterfaces.ITypedRequest> {
|
export class ReactionResult<T extends plugins.typedrequestInterfaces.ITypedRequest> {
|
||||||
private resultSubject = new plugins.smartrx.rxjs.Subject<T['response']>();
|
private resultReplaySubject = new plugins.smartrx.rxjs.ReplaySubject<T['response']>();
|
||||||
private endResult: Array<T['response']> = [];
|
private endResult: Array<T['response']> = [];
|
||||||
private completeDeferred = plugins.smartpromise.defer<Array<T['response']>>();
|
private completeDeferred = plugins.smartpromise.defer<Array<T['response']>>();
|
||||||
|
|
||||||
@ -13,19 +13,34 @@ export class ReactionResult<T extends plugins.typedrequestInterfaces.ITypedReque
|
|||||||
}
|
}
|
||||||
|
|
||||||
public resultSubscribe(observerArg: (responseArg: T['response']) => void) {
|
public resultSubscribe(observerArg: (responseArg: T['response']) => void) {
|
||||||
return this.resultSubject.subscribe(observerArg);
|
return this.resultReplaySubject.subscribe(observerArg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* gets the end result as an array of all results
|
||||||
|
*/
|
||||||
public async getEndResult() {
|
public async getEndResult() {
|
||||||
const result = await this.completeDeferred.promise;
|
const result = await this.completeDeferred.promise;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* if there is a single respondant, or you are only interested in the first result
|
||||||
|
*/
|
||||||
|
public async getFirstResult() {
|
||||||
|
const done = plugins.smartpromise.defer<T['response']>();
|
||||||
|
const subscription = this.resultReplaySubject.subscribe(result => {
|
||||||
|
done.resolve(result);
|
||||||
|
subscription.unsubscribe();
|
||||||
|
});
|
||||||
|
return await done.promise;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* push a reactionResponse
|
* push a reactionResponse
|
||||||
*/
|
*/
|
||||||
public async pushReactionResponse(responseArg: T['response']) {
|
public async pushReactionResponse(responseArg: T['response']) {
|
||||||
this.resultSubject.next(responseArg);
|
this.resultReplaySubject.next(responseArg);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -68,7 +68,7 @@ export class Universe {
|
|||||||
/**
|
/**
|
||||||
* returns a channel
|
* returns a channel
|
||||||
*/
|
*/
|
||||||
public getChannelByName(channelNameArg: string) {
|
public getChannel(channelNameArg: string) {
|
||||||
return this.universeCache.channelMap.find(channelArg => {
|
return this.universeCache.channelMap.find(channelArg => {
|
||||||
return channelArg.name === channelNameArg;
|
return channelArg.name === channelNameArg;
|
||||||
});
|
});
|
||||||
|
Reference in New Issue
Block a user