This repository has been archived by the owner on May 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from m00sey/feat/ops-cleanup
Feat/ops cleanup
- Loading branch information
Showing
4 changed files
with
77 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,27 @@ | ||
import { SignifyClient, Dict } from 'signify-ts'; | ||
import { SignifyClient } from 'signify-ts'; | ||
|
||
const DONE = 'done'; | ||
const NAME = 'name'; | ||
const RESPONSE = 'response'; | ||
|
||
export const getAgentOperationResult = async ( | ||
client: SignifyClient, | ||
op: any, | ||
log: boolean = false, | ||
time: number = 1000 | ||
) => { | ||
const ops = client.operations(); | ||
while (!op[DONE]) { | ||
op = await ops.get(op[NAME]); | ||
if (log) { | ||
console.log(op); | ||
export interface OperationsArgs { | ||
client: SignifyClient; | ||
op: any; | ||
time?: number; | ||
} | ||
|
||
namespace operations { | ||
export async function getResult({ | ||
client, | ||
op, | ||
time = 1000, | ||
}: OperationsArgs) { | ||
while (!op[DONE]) { | ||
op = await client.operations().get(op[NAME]); | ||
await new Promise((resolve) => setTimeout(resolve, time)); | ||
} | ||
await new Promise((resolve) => setTimeout(resolve, time)); | ||
return op[RESPONSE]; | ||
} | ||
return op[RESPONSE]; | ||
}; | ||
} | ||
|
||
export const sendAgentMessage = async ( | ||
client: SignifyClient, | ||
name: string, | ||
topic: string, | ||
sender: Dict<any>, | ||
route: string, | ||
payload: Dict<any>, | ||
embeds: Dict<any>, | ||
recipients: string[] | ||
) => { | ||
return await client | ||
.exchanges() | ||
.send(name, topic, sender, route, payload, embeds, recipients); | ||
}; | ||
export { operations }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { SignifyClient, Credentials, Operations } from 'signify-ts'; | ||
import { describe, expect, it } from '@jest/globals'; | ||
import { instance, mock, when } from 'ts-mockito'; | ||
import { operations } from '../src/operations'; | ||
|
||
describe('operations', () => { | ||
it('should complete', async () => { | ||
let mockedClient: SignifyClient = mock(SignifyClient); | ||
|
||
let c: Credentials = mock(Credentials); | ||
when(mockedClient.credentials()).thenReturn(instance(c)); | ||
|
||
let client = instance(mockedClient); | ||
|
||
let op = { done: true, name: 'my_op', response: 'yay'}; | ||
|
||
let resp = await operations.getResult({client: client, op: op}); | ||
expect(resp).toEqual('yay'); | ||
}); | ||
|
||
it('should retry', async () => { | ||
let mockedClient: SignifyClient = mock(SignifyClient); | ||
|
||
let c: Credentials = mock(Credentials); | ||
when(mockedClient.credentials()).thenReturn(instance(c)); | ||
|
||
let mockedOps: Operations = mock(Operations); | ||
when(mockedOps.get('my_op')).thenResolve({done: true, name: 'my_op', response: 'yay'}); | ||
when(mockedClient.operations()).thenReturn(instance(mockedOps)); | ||
|
||
let client = instance(mockedClient); | ||
|
||
let op = { done: false, name: 'my_op' }; | ||
let resp = await operations.getResult({ client: client, op: op }); | ||
expect(resp).toEqual('yay'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters