-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: ServiceManager and AbstractServiceController
- Loading branch information
1 parent
bcfa660
commit 284badc
Showing
14 changed files
with
177 additions
and
20 deletions.
There are no files selected for viewing
4 changes: 2 additions & 2 deletions
4
packages/daf-core/src/action-handler.ts → ...ges/daf-core/src/action/action-handler.ts
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
File renamed without changes.
4 changes: 2 additions & 2 deletions
4
packages/daf-core/src/graphql-core.ts → ...ages/daf-core/src/graphql/graphql-core.ts
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
4 changes: 2 additions & 2 deletions
4
.../daf-core/src/graphql-identity-manager.ts → ...e/src/graphql/graphql-identity-manager.ts
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
File renamed without changes.
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
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
packages/daf-core/src/message-validator.ts → ...daf-core/src/message/message-validator.ts
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
File renamed without changes.
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
71 changes: 71 additions & 0 deletions
71
packages/daf-core/src/service/__tests__/abstract-service-controller.test.ts
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,71 @@ | ||
import { AbstractServiceController, ServiceEventTypes } from '../abstract-service-controller' | ||
import { Issuer } from '../../identity/identity-manager' | ||
import { Resolver } from '../../core' | ||
import { Message } from '../../message/message' | ||
|
||
const msg1 = new Message({ raw: 'test1', meta: { type: 'test' } }) | ||
const msg2 = new Message({ raw: 'test2', meta: { type: 'test' } }) | ||
|
||
export class MockServiceController extends AbstractServiceController { | ||
static defaultServiceEndpoint: string = 'https://default.host/path' | ||
readonly type = 'mockService' | ||
private endPointUrl: string | ||
|
||
constructor(issuer: Issuer, didResolver: Resolver) { | ||
super(issuer, didResolver) | ||
this.endPointUrl = 'https://from-did-doc' | ||
} | ||
|
||
instanceId() { | ||
return { | ||
did: this.issuer.did, | ||
type: this.type, | ||
id: this.endPointUrl, | ||
} | ||
} | ||
|
||
async getMessagesSince(timestamp: number) { | ||
this.emit(ServiceEventTypes.NewMessages, [msg1, msg2]) | ||
return [msg1, msg2] | ||
} | ||
|
||
async listen() { | ||
this.emit(ServiceEventTypes.NewMessages, [msg1]) | ||
} | ||
} | ||
|
||
const mockIssuer: Issuer = { | ||
did: 'did:test:123', | ||
signer: async (data: string) => data, | ||
type: 'mock', | ||
} | ||
|
||
const mockResolver: Resolver = { | ||
resolve: async (did: string) => null, | ||
} | ||
|
||
it('should be possible to set configuration as a static property', async () => { | ||
expect(MockServiceController.defaultServiceEndpoint).toEqual('https://default.host/path') | ||
MockServiceController.defaultServiceEndpoint = 'https://custom.host/path' | ||
expect(MockServiceController.defaultServiceEndpoint).toEqual('https://custom.host/path') | ||
}) | ||
|
||
it('returns and emits an event with the same message array ', async () => { | ||
const controller = new MockServiceController(mockIssuer, mockResolver) | ||
spyOn(controller, 'emit') | ||
const messages = await controller.getMessagesSince(0) | ||
expect(controller.emit).toHaveBeenCalledWith(ServiceEventTypes.NewMessages, messages) | ||
}) | ||
|
||
it('emits events on listen', async () => { | ||
const controller = new MockServiceController(mockIssuer, mockResolver) | ||
spyOn(controller, 'emit') | ||
await controller.listen() | ||
expect(controller.emit).toHaveBeenCalledWith(ServiceEventTypes.NewMessages, [msg1]) | ||
}) | ||
|
||
it('instanceId is generated from state', async () => { | ||
const controller = new MockServiceController(mockIssuer, mockResolver) | ||
const instanceId = controller.instanceId() | ||
expect(instanceId).toEqual({ did: 'did:test:123', type: controller.type, id: 'https://from-did-doc' }) | ||
}) |
20 changes: 20 additions & 0 deletions
20
packages/daf-core/src/service/abstract-service-controller.ts
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,20 @@ | ||
import { EventEmitter } from 'events' | ||
import { Issuer } from '../identity/identity-manager' | ||
import { Resolver } from '../core' | ||
import { Message } from '../message/message' | ||
|
||
export enum ServiceEventTypes { | ||
NewMessages = 'NewMessages', | ||
} | ||
|
||
export abstract class AbstractServiceController extends EventEmitter { | ||
constructor(readonly issuer: Issuer, readonly didResolver: Resolver) { | ||
super() | ||
} | ||
abstract instanceId(): { did: string; type: string; id: string } | ||
abstract getMessagesSince(timestamp: number): Promise<Message[]> | ||
abstract listen(): void | ||
} | ||
|
||
type AbstractServiceControllerClass = typeof AbstractServiceController | ||
export interface ServiceControllerDerived extends AbstractServiceControllerClass {} |
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,66 @@ | ||
import { EventEmitter } from 'events' | ||
import { Resolver } from '../core' | ||
import { | ||
AbstractServiceController, | ||
ServiceControllerDerived, | ||
ServiceEventTypes, | ||
} from './abstract-service-controller' | ||
import { Issuer } from '../identity/identity-manager' | ||
import { Message } from '../message/message' | ||
|
||
interface Options { | ||
controllers: ServiceControllerDerived[] | ||
didResolver: Resolver | ||
} | ||
|
||
export interface LastMessageTimestampForInstance { | ||
timestamp: number | ||
did: string | ||
type: string | ||
id: string | ||
} | ||
|
||
export class ServiceManager extends EventEmitter { | ||
private controllerInstances: AbstractServiceController[] | ||
private controllers: ServiceControllerDerived[] | ||
private didResolver: Resolver | ||
|
||
constructor(options: Options) { | ||
super() | ||
this.controllerInstances = [] | ||
this.controllers = options.controllers | ||
this.didResolver = options.didResolver | ||
} | ||
|
||
async setupServices(issuers: Issuer[]) { | ||
for (const issuer of issuers) { | ||
for (const controller of this.controllers) { | ||
const instance = new controller(issuer, this.didResolver) | ||
instance.on(ServiceEventTypes.NewMessages, this.onNewMessages) | ||
this.controllerInstances.push(instance) | ||
} | ||
} | ||
} | ||
|
||
private onNewMessages(messages: Message[]) { | ||
this.emit(ServiceEventTypes.NewMessages, messages) | ||
} | ||
|
||
listen() { | ||
for (const instance of this.controllerInstances) { | ||
instance.listen() | ||
} | ||
} | ||
|
||
async getMessagesSince(ts: LastMessageTimestampForInstance[]): Promise<Message[]> { | ||
let result: Message[] = [] | ||
for (const instance of this.controllerInstances) { | ||
const { id, type, did } = instance.instanceId() | ||
const found = ts.find(i => i.did === did && i.id === id && i.type === type) | ||
let since = found ? found.timestamp : 0 | ||
const messages = await instance.getMessagesSince(since) | ||
result = result.concat(messages) | ||
} | ||
return result | ||
} | ||
} |