Skip to content

Commit 553f8cc

Browse files
authored
Merge pull request #52 from bugwheels94/beta
Beta
2 parents d98c03c + d576fd9 commit 553f8cc

File tree

5 files changed

+16
-26
lines changed

5 files changed

+16
-26
lines changed

src/client.ts

+5-17
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import WebSocket from 'isomorphic-ws';
22
import HttpStatusCode from './statusCodes';
33
// import { MessageStore } from './messageStore';
4-
import crypto from 'crypto';
54
import EventEmitter from 'events';
65
import { AllowedType, DataMapping, JsonObject, Serialize } from './utils';
76
import { SoxtendServer } from '.';
8-
import { GROUPS_BY_CONNECTION_ID, SERVERS_HAVING_GROUP } from './constants';
97
import { nanoid } from 'nanoid';
108
export type ClientResponse = {
119
_id: number;
@@ -83,37 +81,27 @@ export class Socket<DataSentOverWire extends AllowedType = 'string'> extends Eve
8381

8482
public async joinGroup(groupId: string) {
8583
this.server.socketGroupStore.add(this, groupId);
86-
return Promise.all([
87-
this.server.distributor.addListItem(`${GROUPS_BY_CONNECTION_ID}${this.id}`, groupId),
88-
this.server.distributor.addListItem(`${SERVERS_HAVING_GROUP}${groupId}`, this.server.id),
89-
]);
9084
}
9185

9286
async joinGroups(groupdIds: Iterable<string>) {
9387
for (let groupId of groupdIds) {
9488
this.server.socketGroupStore.add(this, groupId);
95-
this.server.distributor.addListItem(`${SERVERS_HAVING_GROUP}${groupId}`, this.server.id);
9689
}
97-
this.server.distributor.addListItems(`${GROUPS_BY_CONNECTION_ID}${this.id}`, groupdIds);
9890
}
9991
async leaveGroup(groupId: string) {
10092
this.server.socketGroupStore.remove(this, groupId);
101-
102-
return this.server.distributor.removeListItem(`${GROUPS_BY_CONNECTION_ID}${this.id}`, groupId);
10393
}
10494
async leaveAllGroups() {
105-
const groups = await this.server.distributor.getListItems(`${GROUPS_BY_CONNECTION_ID}${this.id}`);
95+
const groups = this.server.socketGroupStore.myGroups.get(this.id);
96+
if (!groups) return;
10697
return this.leaveGroups(groups);
10798
}
108-
async leaveGroups(groups: string[]) {
109-
if (!groups.length) return;
99+
async leaveGroups(groups: Set<string | number>) {
110100
for (let group of groups) {
111101
this.server.socketGroupStore.remove(this, group);
112102
}
113-
114-
return Promise.all([this.server.distributor.removeListItems(`${GROUPS_BY_CONNECTION_ID}${this.id}`, groups)]);
115103
}
116-
async getAllGroups(socketId?: string) {
117-
return this.server.distributor.getListItems(`${GROUPS_BY_CONNECTION_ID}${socketId || this.id}`);
104+
async getAllGroups() {
105+
return this.server.socketGroupStore.myGroups.get(this.id);
118106
}
119107
}

src/constants.ts

-4
This file was deleted.

src/index.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import WebSocket from 'isomorphic-ws';
22
import { Socket } from './client';
33
import { ServerOptions } from 'ws';
4-
import crypto from 'crypto';
54
import { MessageDistributor } from './distributor';
65
// import { MessageStore } from './messageStore';
76
import EventEmitter from 'events';
87
import { IndividualSocketConnectionStore, SocketGroupStore } from './localStores';
98
import { AllowedType, DataMapping, Deserialize, JsonObject, Serialize } from './utils';
10-
import { SERVERS_HAVING_GROUP, SERVER_MESSAGES_GROUP_QUEUE } from './constants';
9+
import { nanoid } from 'nanoid';
1110
type SoxtendServerEvents = 'connection' | 'close';
1211

1312
declare global {
@@ -231,7 +230,7 @@ export class SoxtendServer<MessageType extends AllowedType = 'string'> extends E
231230
this.sendToGroup = this.sendMessageAsStringToGroup;
232231
}
233232
this.distributor = distributor;
234-
this.id = crypto.randomUUID();
233+
this.id = nanoid();
235234
this.individualSocketConnectionStore = new IndividualSocketConnectionStore();
236235
this.socketGroupStore = new SocketGroupStore<MessageType>();
237236
this.rawWebSocketServer = new WebSocket.Server(options);

src/localStores.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export interface LocalGroupStore {
1717
*/
1818
export class SocketGroupStore<DataSentOverWire extends AllowedType = 'string'> {
1919
store: Map<string | number, Set<Socket<DataSentOverWire>>> = new Map();
20-
20+
myGroups: Map<string, Set<string | number>> = new Map();
2121
add(socket: Socket<DataSentOverWire>, groupId: string | number) {
2222
// if (!groupId) {
2323
// return new SocketGroup(socketSet);
@@ -30,13 +30,20 @@ export class SocketGroupStore<DataSentOverWire extends AllowedType = 'string'> {
3030
const newClient = new Set<Socket<DataSentOverWire>>();
3131
newClient.add(socket);
3232
this.store.set(groupId, newClient);
33+
let set = this.myGroups.get(socket.id);
34+
if (!set) {
35+
set = new Set();
36+
this.myGroups.set(socket.id, set);
37+
}
38+
set.add(groupId);
3339
return newClient;
3440
}
3541
find(id: string | number) {
3642
return this.store.get(id);
3743
}
3844
remove(socket: Socket<DataSentOverWire>, groupId: string | number) {
3945
const group = this.store.get(groupId);
46+
this.myGroups.get(socket.id)?.delete(groupId);
4047
group?.delete(socket);
4148
}
4249
constructor() {

src/router.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ function createSelfResponse(instance: Socket, message: RouterRequest, server: So
9797
return instance.leaveGroup(groupId);
9898
},
9999
leaveGroups: async (groups: string[]) => {
100-
return instance.leaveGroups(groups);
100+
return instance.leaveGroups(new Set(groups));
101101
},
102102
leaveAllGroups: async () => {
103103
return instance.leaveAllGroups();

0 commit comments

Comments
 (0)