Thread loading

This commit is contained in:
2022-12-20 23:14:13 +00:00
parent c454f5c7aa
commit ff9de60b6b
10 changed files with 169 additions and 65 deletions

View File

@ -15,9 +15,9 @@ export class NostrSystem {
* @param {string} address
*/
ConnectToRelay(address) {
if(typeof this.Sockets[address] === "undefined") {
if (typeof this.Sockets[address] === "undefined") {
let c = new Connection(address);
for(let s of Object.values(this.Subscriptions)) {
for (let s of Object.values(this.Subscriptions)) {
c.AddSubscription(s);
}
this.Sockets[address] = c;
@ -25,22 +25,49 @@ export class NostrSystem {
}
AddSubscription(sub) {
for(let s of Object.values(this.Sockets)) {
for (let s of Object.values(this.Sockets)) {
s.AddSubscription(sub);
}
this.Subscriptions[sub.Id] = sub;
}
RemoveSubscription(subId) {
for(let s of Object.values(this.Sockets)) {
for (let s of Object.values(this.Sockets)) {
s.RemoveSubscription(subId);
}
delete this.Subscriptions[subId];
}
BroadcastEvent(ev) {
for(let s of Object.values(this.Sockets)) {
for (let s of Object.values(this.Sockets)) {
s.SendEvent(ev);
}
}
/**
* Request/Response pattern
* @param {Subscriptions} sub
* @returns {Array<any>}
*/
RequestSubscription(sub) {
return new Promise((resolve, reject) => {
let counter = 0;
let events = [];
sub.OnEvent = (ev) => {
if (!events.some(a => a.id === ev.id)) {
events.push(ev);
}
};
sub.OnEnd = (c) => {
c.RemoveSubscription(sub.Id);
if(--counter === 0) {
resolve(events);
}
};
for (let s of Object.values(this.Sockets)) {
s.AddSubscription(sub);
counter++;
}
});
}
}