Skip to content

Commit

Permalink
Merge pull request #52 from bugwheels94/beta
Browse files Browse the repository at this point in the history
Beta
  • Loading branch information
bugwheels94 authored May 6, 2024
2 parents d98c03c + d576fd9 commit 553f8cc
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 26 deletions.
22 changes: 5 additions & 17 deletions src/client.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import WebSocket from 'isomorphic-ws';
import HttpStatusCode from './statusCodes';
// import { MessageStore } from './messageStore';
import crypto from 'crypto';
import EventEmitter from 'events';
import { AllowedType, DataMapping, JsonObject, Serialize } from './utils';
import { SoxtendServer } from '.';
import { GROUPS_BY_CONNECTION_ID, SERVERS_HAVING_GROUP } from './constants';
import { nanoid } from 'nanoid';
export type ClientResponse = {
_id: number;
Expand Down Expand Up @@ -83,37 +81,27 @@ export class Socket<DataSentOverWire extends AllowedType = 'string'> extends Eve

public async joinGroup(groupId: string) {
this.server.socketGroupStore.add(this, groupId);
return Promise.all([
this.server.distributor.addListItem(`${GROUPS_BY_CONNECTION_ID}${this.id}`, groupId),
this.server.distributor.addListItem(`${SERVERS_HAVING_GROUP}${groupId}`, this.server.id),
]);
}

async joinGroups(groupdIds: Iterable<string>) {
for (let groupId of groupdIds) {
this.server.socketGroupStore.add(this, groupId);
this.server.distributor.addListItem(`${SERVERS_HAVING_GROUP}${groupId}`, this.server.id);
}
this.server.distributor.addListItems(`${GROUPS_BY_CONNECTION_ID}${this.id}`, groupdIds);
}
async leaveGroup(groupId: string) {
this.server.socketGroupStore.remove(this, groupId);

return this.server.distributor.removeListItem(`${GROUPS_BY_CONNECTION_ID}${this.id}`, groupId);
}
async leaveAllGroups() {
const groups = await this.server.distributor.getListItems(`${GROUPS_BY_CONNECTION_ID}${this.id}`);
const groups = this.server.socketGroupStore.myGroups.get(this.id);
if (!groups) return;
return this.leaveGroups(groups);
}
async leaveGroups(groups: string[]) {
if (!groups.length) return;
async leaveGroups(groups: Set<string | number>) {
for (let group of groups) {
this.server.socketGroupStore.remove(this, group);
}

return Promise.all([this.server.distributor.removeListItems(`${GROUPS_BY_CONNECTION_ID}${this.id}`, groups)]);
}
async getAllGroups(socketId?: string) {
return this.server.distributor.getListItems(`${GROUPS_BY_CONNECTION_ID}${socketId || this.id}`);
async getAllGroups() {
return this.server.socketGroupStore.myGroups.get(this.id);
}
}
4 changes: 0 additions & 4 deletions src/constants.ts

This file was deleted.

5 changes: 2 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import WebSocket from 'isomorphic-ws';
import { Socket } from './client';
import { ServerOptions } from 'ws';
import crypto from 'crypto';
import { MessageDistributor } from './distributor';
// import { MessageStore } from './messageStore';
import EventEmitter from 'events';
import { IndividualSocketConnectionStore, SocketGroupStore } from './localStores';
import { AllowedType, DataMapping, Deserialize, JsonObject, Serialize } from './utils';
import { SERVERS_HAVING_GROUP, SERVER_MESSAGES_GROUP_QUEUE } from './constants';
import { nanoid } from 'nanoid';
type SoxtendServerEvents = 'connection' | 'close';

declare global {
Expand Down Expand Up @@ -231,7 +230,7 @@ export class SoxtendServer<MessageType extends AllowedType = 'string'> extends E
this.sendToGroup = this.sendMessageAsStringToGroup;
}
this.distributor = distributor;
this.id = crypto.randomUUID();
this.id = nanoid();
this.individualSocketConnectionStore = new IndividualSocketConnectionStore();
this.socketGroupStore = new SocketGroupStore<MessageType>();
this.rawWebSocketServer = new WebSocket.Server(options);
Expand Down
9 changes: 8 additions & 1 deletion src/localStores.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export interface LocalGroupStore {
*/
export class SocketGroupStore<DataSentOverWire extends AllowedType = 'string'> {
store: Map<string | number, Set<Socket<DataSentOverWire>>> = new Map();

myGroups: Map<string, Set<string | number>> = new Map();
add(socket: Socket<DataSentOverWire>, groupId: string | number) {
// if (!groupId) {
// return new SocketGroup(socketSet);
Expand All @@ -30,13 +30,20 @@ export class SocketGroupStore<DataSentOverWire extends AllowedType = 'string'> {
const newClient = new Set<Socket<DataSentOverWire>>();
newClient.add(socket);
this.store.set(groupId, newClient);
let set = this.myGroups.get(socket.id);
if (!set) {
set = new Set();
this.myGroups.set(socket.id, set);
}
set.add(groupId);
return newClient;
}
find(id: string | number) {
return this.store.get(id);
}
remove(socket: Socket<DataSentOverWire>, groupId: string | number) {
const group = this.store.get(groupId);
this.myGroups.get(socket.id)?.delete(groupId);
group?.delete(socket);
}
constructor() {
Expand Down
2 changes: 1 addition & 1 deletion src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ function createSelfResponse(instance: Socket, message: RouterRequest, server: So
return instance.leaveGroup(groupId);
},
leaveGroups: async (groups: string[]) => {
return instance.leaveGroups(groups);
return instance.leaveGroups(new Set(groups));
},
leaveAllGroups: async () => {
return instance.leaveAllGroups();
Expand Down

0 comments on commit 553f8cc

Please sign in to comment.