-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwaku.ts
140 lines (111 loc) · 4.07 KB
/
waku.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import { v4 as uuid } from "uuid";
import {
PUBSUB_TOPIC,
} from "@/constants";
import { http } from "@/utils/http";
export type Message = {
payload: string,
contentTopic: string,
version: number,
timestamp: number
};
type EventListener = (event: CustomEvent) => void;
const SECOND = 1000;
const LOCAL_NODE = "http://127.0.0.1:8645/";
const FILTER_URL = "/filter/v2/";
const LIGHT_PUSH = "/lightpush/v1/";
class Filter {
private readonly internalEmitter = new EventTarget();
private readonly subscriptionsEmitter = new EventTarget();
private contentTopicToRequestID: Map<string, string> = new Map();
private contentTopicListeners: Map<string, number> = new Map();
// only one content topic subscriptions is possible now
private subscriptionRoutine: undefined | number;
constructor() {
this.internalEmitter.addEventListener("subscribed", this.handleSubscribed.bind(this));
this.internalEmitter.addEventListener("unsubscribed", this.handleUnsubscribed.bind(this));
}
private async handleSubscribed(_e: Event) {
const event = _e as CustomEvent;
const contentTopic = event.detail;
const numberOfListeners = this.contentTopicListeners.get(contentTopic);
// if nwaku node already subscribed to this content topic
if (numberOfListeners) {
this.contentTopicListeners.set(contentTopic, numberOfListeners + 1);
return;
}
const requestId = uuid();
await http.post(`${LOCAL_NODE}/${FILTER_URL}/subscriptions`, {
requestId,
contentFilters: [contentTopic],
pubsubTopic: PUBSUB_TOPIC
});
this.subscriptionRoutine = window.setInterval(async () => {
await this.fetchMessages();
}, SECOND);
this.contentTopicToRequestID.set(contentTopic, requestId);
this.contentTopicListeners.set(contentTopic, 1);
}
private async handleUnsubscribed(_e: Event) {
const event = _e as CustomEvent;
const contentTopic = event.detail;
const requestId = this.contentTopicToRequestID.get(contentTopic);
const numberOfListeners = this.contentTopicListeners.get(contentTopic);
if (!numberOfListeners || !requestId) {
return;
}
if (numberOfListeners - 1 > 0) {
this.contentTopicListeners.set(contentTopic, numberOfListeners - 1);
return;
}
await http.delete(`${LOCAL_NODE}/${FILTER_URL}/subscriptions`, {
requestId,
contentFilters: [contentTopic],
pubsubTopic: PUBSUB_TOPIC
});
clearInterval(this.subscriptionRoutine);
this.contentTopicListeners.delete(contentTopic);
this.contentTopicToRequestID.delete(contentTopic);
}
private async fetchMessages(): Promise<void> {
const contentTopic = Object.keys(this.contentTopicListeners)[0];
if (!contentTopic) {
return;
}
const response = await http.get(`${LOCAL_NODE}/${FILTER_URL}/${encodeURIComponent(contentTopic)}`);
const body: Message[] = await response.json();
if (!body || !body.length) {
return;
}
this.subscriptionsEmitter.dispatchEvent(
new CustomEvent(contentTopic, { detail: body })
);
}
public addEventListener(contentTopic: string, fn: EventListener) {
this.emitSubscribedEvent(contentTopic);
return this.subscriptionsEmitter.addEventListener(contentTopic, fn as any);
}
public removeEventListener(contentTopic: string, fn: EventListener) {
this.emitUnsubscribedEvent(contentTopic);
return this.subscriptionsEmitter.removeEventListener(contentTopic, fn as any);
}
private emitSubscribedEvent(contentTopic: string) {
this.internalEmitter.dispatchEvent(new CustomEvent("subscribed", { detail: contentTopic }));
}
private emitUnsubscribedEvent(contentTopic: string) {
this.internalEmitter.dispatchEvent(new CustomEvent("unsubscribed", { detail: contentTopic }));
}
}
class LightPush {
constructor() {}
public async send(message: Message): Promise<void> {
await http.post(`${LOCAL_NODE}/${LIGHT_PUSH}/message`, {
pubsubTopic: PUBSUB_TOPIC,
message,
});
}
}
export const waku = {
filter: new Filter(),
lightPush: new LightPush(),
};