Compare commits
5 Commits
v0.0.2-dev
...
v0.1.0-dev
Author | SHA1 | Date |
---|---|---|
semantic-release-bot | 3c1aed2f2f | |
Derrick Hammer | d312d1704d | |
Derrick Hammer | 296acba3b4 | |
Derrick Hammer | e56cbecbcf | |
Derrick Hammer | 39a91144de |
|
@ -1,3 +1,11 @@
|
|||
# [0.1.0-develop.1](https://git.lumeweb.com/LumeWeb/kernel-swarm/compare/v0.0.2-develop.10...v0.1.0-develop.1) (2023-07-22)
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* add socketListeners api call ([e56cbec](https://git.lumeweb.com/LumeWeb/kernel-swarm/commit/e56cbecbcf6641ea12059868829b9428a3c327c2))
|
||||
* track what modules are listening on a socket connection by their module id ([39a9114](https://git.lumeweb.com/LumeWeb/kernel-swarm/commit/39a91144de1641ec17ba02888409009310aca67a))
|
||||
|
||||
## [0.0.2-develop.10](https://git.lumeweb.com/LumeWeb/kernel-swarm/compare/v0.0.2-develop.9...v0.0.2-develop.10) (2023-07-22)
|
||||
|
||||
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
{
|
||||
"name": "@lumeweb/kernel-swarm",
|
||||
"version": "0.0.2-develop.10",
|
||||
"version": "0.1.0-develop.1",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "@lumeweb/kernel-swarm",
|
||||
"version": "0.0.2-develop.10",
|
||||
"version": "0.1.0-develop.1",
|
||||
"hasInstallScript": true,
|
||||
"dependencies": {
|
||||
"@lumeweb/hyperswarm-web": "0.0.2-develop.8",
|
||||
|
|
|
@ -38,7 +38,7 @@
|
|||
"patch-package": "^7.0.0",
|
||||
"presetter": "*"
|
||||
},
|
||||
"version": "0.0.2-develop.10",
|
||||
"version": "0.1.0-develop.1",
|
||||
"readme": "ERROR: No README data found!",
|
||||
"overrides": {
|
||||
"buffer": "^6.0.3"
|
||||
|
|
38
src/index.ts
38
src/index.ts
|
@ -24,6 +24,7 @@ interface SwarmConnection {
|
|||
swarm: number;
|
||||
conn: any;
|
||||
channels: Map<number, Protomux>;
|
||||
listeners: Map<string, number>;
|
||||
}
|
||||
|
||||
interface SwarmEvents {
|
||||
|
@ -78,6 +79,7 @@ addHandler("socketExists", handleSocketExists);
|
|||
addHandler("socketListenEvent", handleSocketListenEvent, {
|
||||
receiveUpdates: true,
|
||||
});
|
||||
addHandler("socketListeners", handleSocketListenersEvent);
|
||||
addHandler("socketWrite", handleWriteSocketEvent);
|
||||
addHandler("socketClose", handleCloseSocketEvent);
|
||||
addHandler("createProtomuxChannel", handleCreateProtomuxChannel, {
|
||||
|
@ -122,6 +124,7 @@ async function createSwarm(): Promise<number> {
|
|||
swarm: id,
|
||||
conn: peer,
|
||||
channels: new Map<number, Protomux>(),
|
||||
listeners: new Map<string, number>(),
|
||||
});
|
||||
|
||||
peer.once("close", () => {
|
||||
|
@ -155,6 +158,8 @@ function handleSocketListenEvent(aq: ActiveQuery) {
|
|||
return;
|
||||
}
|
||||
|
||||
const conn = connections.get(aq.callerInput.id) as SwarmConnection;
|
||||
|
||||
let responded = false;
|
||||
const respond = () => {
|
||||
if (responded) {
|
||||
|
@ -162,6 +167,15 @@ function handleSocketListenEvent(aq: ActiveQuery) {
|
|||
}
|
||||
|
||||
responded = true;
|
||||
let count = conn.listeners.get(aq.domain) as number;
|
||||
count--;
|
||||
|
||||
if (count > 0) {
|
||||
conn.listeners.set(aq.domain, count);
|
||||
} else {
|
||||
conn.listeners.delete(aq.domain);
|
||||
}
|
||||
|
||||
aq.respond();
|
||||
};
|
||||
|
||||
|
@ -182,6 +196,30 @@ function handleSocketListenEvent(aq: ActiveQuery) {
|
|||
socket.off(event, cb);
|
||||
respond();
|
||||
});
|
||||
|
||||
if (!conn.listeners.has(aq.domain)) {
|
||||
conn.listeners.set(aq.domain, 0);
|
||||
}
|
||||
|
||||
conn.listeners.set(aq.domain, (conn.listeners.get(aq.domain) as number) + 1);
|
||||
}
|
||||
|
||||
async function handleSocketListenersEvent(aq: ActiveQuery) {
|
||||
const socket = validateConnection(aq);
|
||||
|
||||
if (!socket) {
|
||||
return;
|
||||
}
|
||||
|
||||
const conn = connections.get(aq.callerInput.id) as SwarmConnection;
|
||||
|
||||
aq.respond(
|
||||
[...conn.listeners.entries()]
|
||||
.filter((item) => {
|
||||
return item[1] > 0;
|
||||
})
|
||||
.map((item) => item[0]),
|
||||
);
|
||||
}
|
||||
|
||||
async function handleSocketExists(aq: ActiveQuery) {
|
||||
|
|
Loading…
Reference in New Issue