-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
279 additions
and
163 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,3 +1 @@ | ||
describe('SubscriptionManager', () => { | ||
|
||
}); | ||
describe("SubscriptionManager", () => {}) |
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,67 +1,70 @@ | ||
//subscriptionmanager | ||
|
||
enum SubscriptionTopic { | ||
EVENTS = 'events', | ||
BLOCKS = 'blocks', | ||
EVENTS = "events", | ||
BLOCKS = "blocks", | ||
} | ||
|
||
type SubscriptionTypes = { | ||
[SubscriptionTopic.EVENTS]: { | ||
args: { | ||
startBlock: number; | ||
endBlock: number; | ||
} | ||
response: { | ||
type: string; | ||
data: any; | ||
} | ||
}; | ||
[SubscriptionTopic.BLOCKS]: { | ||
args: { | ||
startBlock: number; | ||
endBlock: number; | ||
}, | ||
response: { | ||
type: string; | ||
data: any; | ||
} | ||
}; | ||
[SubscriptionTopic.EVENTS]: { | ||
args: { | ||
startBlock: number | ||
endBlock: number | ||
} | ||
response: { | ||
type: string | ||
data: any | ||
} | ||
} | ||
[SubscriptionTopic.BLOCKS]: { | ||
args: { | ||
startBlock: number | ||
endBlock: number | ||
} | ||
response: { | ||
type: string | ||
data: any | ||
} | ||
} | ||
} | ||
|
||
type Subscription = { | ||
unsubscribe: () => void; | ||
unsubscribe: () => void | ||
} | ||
|
||
type SubscriptionTransport = { | ||
subscribe: (topic: string, args: any, callback: (data: any) => void) => string; | ||
unsubscribe: (subscriptionId: string) => void; | ||
subscribe: (topic: string, args: any, callback: (data: any) => void) => string | ||
unsubscribe: (subscriptionId: string) => void | ||
} | ||
|
||
export class SubscriptionManager { | ||
private subscriptions: Subscription[] = []; | ||
private subscriptions: Subscription[] = [] | ||
|
||
constructor(private readonly transport: SubscriptionTransport) {} | ||
|
||
subscribe<T extends SubscriptionTopic>( | ||
topic: T, | ||
args: SubscriptionTypes[T]['args'], | ||
args: SubscriptionTypes[T]["args"], | ||
callback: (data: any) => void | ||
): () => void { | ||
const subscription = this.transport.subscribe(topic, args, (data) => { | ||
const decodedData = this.decode(topic, data); | ||
callback(decodedData); | ||
}); | ||
const subscription = this.transport.subscribe(topic, args, data => { | ||
const decodedData = this.decode(topic, data) | ||
callback(decodedData) | ||
}) | ||
|
||
return () => { | ||
const index = this.subscriptions.indexOf(subscription); | ||
if (index !== -1) { | ||
this.subscriptions.splice(index, 1); | ||
subscription.unsubscribe(); | ||
} | ||
const index = this.subscriptions.indexOf(subscription) | ||
if (index !== -1) { | ||
this.subscriptions.splice(index, 1) | ||
subscription.unsubscribe() | ||
} | ||
} | ||
} | ||
|
||
decode<T extends SubscriptionTopic>(topic: T, data: any): SubscriptionTypes[T]['response'] { | ||
return data; | ||
decode<T extends SubscriptionTopic>( | ||
topic: T, | ||
data: any | ||
): SubscriptionTypes[T]["response"] { | ||
return data | ||
} | ||
} | ||
} |
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,58 +1,63 @@ | ||
export interface BaseMessageRequest { | ||
action: ActionType | ||
action: ActionType | ||
} | ||
|
||
export interface BaseMessageResponse { | ||
action?: ActionType | ||
success: boolean | ||
error_message?: string | ||
action?: ActionType | ||
success: boolean | ||
error_message?: string | ||
} | ||
|
||
export interface ListSubscriptionsMessageRequest extends BaseMessageRequest { | ||
action: 'list_subscriptions' | ||
action: "list_subscriptions" | ||
} | ||
|
||
export interface ListSubscriptionsMessageResponse extends BaseMessageResponse { | ||
action: 'list_subscriptions' | ||
subscriptions?: SubscriptionEntry[] | ||
action: "list_subscriptions" | ||
subscriptions?: SubscriptionEntry[] | ||
} | ||
|
||
export interface SubscribeMessageRequest extends BaseMessageRequest { | ||
action: 'subscribe' | ||
topic: string | ||
arguments: Record<string, any> | ||
action: "subscribe" | ||
topic: string | ||
arguments: Record<string, any> | ||
} | ||
|
||
export interface SubscribeMessageResponse extends BaseMessageResponse { | ||
action: 'subscribe' | ||
topic: string | ||
id: string | ||
action: "subscribe" | ||
topic: string | ||
id: string | ||
} | ||
|
||
export interface UnsubscribeMessageRequest extends BaseMessageRequest { | ||
action: 'unsubscribe' | ||
id: string | ||
action: "unsubscribe" | ||
id: string | ||
} | ||
|
||
export type UnsubscribeMessageResponse = BaseMessageResponse & { | ||
action: 'unsubscribe' | ||
id: string | ||
action: "unsubscribe" | ||
id: string | ||
} | ||
|
||
export type SubscriptionEntry = { | ||
id: string | ||
topic: string | ||
arguments: Record<string, any> | ||
id: string | ||
topic: string | ||
arguments: Record<string, any> | ||
} | ||
|
||
export type ActionType = 'list_subscriptions' | 'subscribe' | 'unsubscribe' | ||
export type ActionType = "list_subscriptions" | "subscribe" | "unsubscribe" | ||
|
||
export type MessageRequest = ListSubscriptionsMessageRequest | SubscribeMessageRequest | UnsubscribeMessageRequest | ||
export type MessageRequest = | ||
| ListSubscriptionsMessageRequest | ||
| SubscribeMessageRequest | ||
| UnsubscribeMessageRequest | ||
|
||
export type MessageResponse = ListSubscriptionsMessageResponse | SubscribeMessageResponse | UnsubscribeMessageResponse | ||
export type MessageResponse = | ||
| ListSubscriptionsMessageResponse | ||
| SubscribeMessageResponse | ||
| UnsubscribeMessageResponse | ||
|
||
export type SubscriptionDataMessage = { | ||
id: string | ||
data: any | ||
id: string | ||
data: any | ||
} | ||
|
1 change: 1 addition & 0 deletions
1
packages/transport-http/src/subscriptions/sub-types/EventsSubscription.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 @@ | ||
export class EventsSubscription |
23 changes: 23 additions & 0 deletions
23
packages/transport-http/src/subscriptions/sub-types/SubscriptionType.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,23 @@ | ||
export abstract class SubscriptionType { | ||
id: number | ||
remoteId?: string | ||
topic: string | ||
checkpointArgs: any | ||
callback: (data: any) => void | ||
|
||
constructor(opts: { | ||
id: number | ||
remoteId?: string | ||
topic: string | ||
callback: (data: any) => void | ||
}) { | ||
this.id = opts.id | ||
this.remoteId = opts.remoteId | ||
this.topic = opts.topic | ||
this.callback = opts.callback | ||
} | ||
|
||
abstract handleMessage(data: any): void | ||
|
||
abstract connect(): void | ||
} |
4 changes: 1 addition & 3 deletions
4
packages/transport-http/src/subscriptions/subscription-manager.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 |
---|---|---|
@@ -1,3 +1 @@ | ||
describe('SubscriptionManager', () => { | ||
|
||
}); | ||
describe("SubscriptionManager", () => {}) |
Oops, something went wrong.