refactor: sockets -> server

This commit is contained in:
cha0s 2024-01-05 14:55:16 -06:00
parent 8ae2aa1950
commit 061f5e53a1
3 changed files with 16 additions and 12 deletions

View File

@ -2,11 +2,11 @@ import {D} from '@flecks/core';
const debug = D('@flecks/socket/intercom');
export default function createIntercom(sockets, transport) {
export default function createIntercom(server, transport) {
return async function intercom(type, payload) {
debug('@flecks/socket.s: %s(%o)', transport, type, payload);
const responses = await new Promise((resolve, reject) => {
sockets.io.serverSideEmit(
server.io.serverSideEmit(
'@flecks/socket.intercom',
{payload, type},
(error, responses) => (error ? reject(error) : resolve(responses)),
@ -14,7 +14,7 @@ export default function createIntercom(sockets, transport) {
});
responses.push(
await new Promise((resolve) => {
sockets.localIntercom({payload, type}, resolve);
server.localIntercom({payload, type}, resolve);
}),
);
return responses;

View File

@ -1,19 +1,23 @@
import createIntercom from './create-intercom';
import Sockets from './sockets';
import SocketServer from './server';
const flecksServers = new WeakMap();
export const server = (flecks) => flecksServers.get(flecks);
export const hooks = {
'@flecks/web/server.request.socket': ({config: {'$flecks/socket.sockets': sockets}}) => (req, res, next) => {
req.intercom = createIntercom(sockets, 'web');
'@flecks/web/server.request.socket': (flecks) => (req, res, next) => {
req.intercom = createIntercom(server(flecks), 'web');
next();
},
'@flecks/web/server.up': async (httpServer, flecks) => {
const sockets = new Sockets(httpServer, flecks);
await sockets.connect();
flecks.set('$flecks/socket.sockets', sockets);
const server = new SocketServer(httpServer, flecks);
flecksServers.set(flecks, server);
await server.connect();
},
'@flecks/repl.context': (flecks) => ({
Packets: flecks.get('$flecks/socket.packets'),
sockets: flecks.get('$flecks/socket.sockets'),
socketServer: server(flecks),
}),
'@flecks/socket.server': ({config: {'@flecks/core': {id}}}) => ({
path: `/${id}/socket.io`,

View File

@ -6,7 +6,7 @@ import normalize from '../normalize';
import createIntercom from './create-intercom';
import ServerSocket from './socket';
const debug = D('@flecks/socket/server/sockets');
const debug = D('@flecks/socket/server');
const debugSilly = debug.extend('silly');
export default class SocketServer {
@ -72,7 +72,7 @@ export default class SocketServer {
const {req} = serverSocket;
req.flecks = this.flecks;
req.intercom = createIntercom(this, 'socket');
req.sockets = this;
req.server = this;
this.flecks.invokeSequentialAsync('@flecks/socket/server.connect', serverSocket);
}