flow
This commit is contained in:
parent
6f16de6887
commit
03424b4db1
|
@ -6,10 +6,18 @@ const capitalize = (token) => token.substring(0, 1).toUpperCase() + token.substr
|
|||
const debug = D('@latus/core/gather');
|
||||
|
||||
export const gatherWithLatus = (context) => (latus) => (
|
||||
Object.fromEntries(context.keys().map((path) => [
|
||||
Object.fromEntries(context.keys().map((path) => {
|
||||
const M = context(path).default;
|
||||
if ('function' !== typeof M) {
|
||||
throw new ReferenceError(
|
||||
`gatherWithLatus: require(${path}).default is not a function (from: ${context.id})`,
|
||||
);
|
||||
}
|
||||
return [
|
||||
basename(path, extname(path)).split('-').map(capitalize).join(''),
|
||||
context(path).default(latus),
|
||||
]))
|
||||
M(latus),
|
||||
];
|
||||
}))
|
||||
);
|
||||
|
||||
export default (latus, type, idAttribute, typeAttribute, check = () => {}) => {
|
||||
|
|
|
@ -3,6 +3,10 @@ import {render} from 'react-dom';
|
|||
|
||||
import LatusContext from './context';
|
||||
|
||||
export {default as React} from 'react';
|
||||
export {default as LatusContext} from './context';
|
||||
export {default as useLatus} from './hooks/use-latus';
|
||||
|
||||
export default {
|
||||
hooks: {
|
||||
'@latus/http/client/up': async (latus) => {
|
||||
|
|
5
packages/react/src/hooks/use-latus.js
Normal file
5
packages/react/src/hooks/use-latus.js
Normal file
|
@ -0,0 +1,5 @@
|
|||
import {useContext} from 'react';
|
||||
|
||||
import LatusContext from '../context';
|
||||
|
||||
export default () => useContext(LatusContext);
|
|
@ -1,7 +1,5 @@
|
|||
export {default as React} from 'react';
|
||||
|
||||
export {default as LatusContext} from './context';
|
||||
|
||||
export default {
|
||||
hooks: {
|
||||
'@latus/core/config': () => ({
|
||||
|
|
|
@ -75,10 +75,10 @@ export default class SocketClient extends decorate(Class) {
|
|||
for (let i = 0; i < Packets.length; i++) {
|
||||
const Packet = Packets[i];
|
||||
const {id} = Packet;
|
||||
debug('Registering packet %s(id: %s)', Packet.name, id);
|
||||
debug('Registering packet %s(id: %s)', Packet.type, id);
|
||||
this.socket.on(id, (data, fn) => {
|
||||
const packet = new Packet(data);
|
||||
debug('recieved packet %s(%o)', Packet.name, packet.data);
|
||||
debug('recieved packet %s(%j)', Packet.type, packet.data);
|
||||
this.emit('packet', packet, fn);
|
||||
});
|
||||
}
|
||||
|
@ -94,7 +94,7 @@ export default class SocketClient extends decorate(Class) {
|
|||
|
||||
static send(latus, socket, packetOrDehydrated, method) {
|
||||
const packet = normalize(latus, packetOrDehydrated);
|
||||
debug('sending packet %o', packet);
|
||||
debug('sending packet %s(%j)', packet.constructor.type, packet.data);
|
||||
const {id} = packet.constructor;
|
||||
return socket[method](id, packet.data);
|
||||
}
|
||||
|
|
|
@ -4,7 +4,7 @@ import useSocket from './use-socket';
|
|||
|
||||
const {useEffect} = React;
|
||||
|
||||
export default function useSocketPacket(fn, deps) {
|
||||
export default function useSocketPacket(fn, deps = []) {
|
||||
const socket = useSocket();
|
||||
useEffect(() => {
|
||||
socket.on('packet', fn);
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import {LatusContext, React} from '@latus/react';
|
||||
import {LatusContext, React} from '@latus/react/client';
|
||||
|
||||
import {socket} from '../client/socket';
|
||||
|
||||
|
|
|
@ -4,6 +4,7 @@ import badPacketsCheck from './packet/bad-packets-check';
|
|||
import Bundle from './packet/bundle';
|
||||
import Refresh from './packet/refresh';
|
||||
|
||||
export * from './hooks';
|
||||
export {default as normalize} from './normalize';
|
||||
export {default as Packet, ValidationError} from './packet';
|
||||
export {default as packets} from './packet/packets';
|
||||
|
|
|
@ -12,7 +12,7 @@ export default (socket) => async (packet, fn) => {
|
|||
if (error instanceof Error) {
|
||||
fn({
|
||||
code: error.code || 500,
|
||||
reason: 'production' === process.env.NODE_ENV ? 'Error' : error.message,
|
||||
reason: 'production' === process.env.NODE_ENV ? 'error' : error.message,
|
||||
...('production' === process.env.NODE_ENV ? {} : {stack: error.stack}),
|
||||
});
|
||||
return;
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
import normalize from '../normalize';
|
||||
import Packet from './packet';
|
||||
import packets from './packets';
|
||||
|
||||
export default (latus) => class BundlePacket extends Packet {
|
||||
|
||||
|
@ -16,7 +18,7 @@ export default (latus) => class BundlePacket extends Packet {
|
|||
// Pack up all the packets.
|
||||
const packedPackets = new Array(packets.length);
|
||||
for (let i = 0; i < packets.length; i++) {
|
||||
const packet = packets[i];
|
||||
const packet = normalize(latus, packets[i]);
|
||||
const Packet_ = packet.constructor;
|
||||
const {id} = Packet_;
|
||||
packedPackets[i] = Packet_.pack({
|
||||
|
@ -47,32 +49,32 @@ export default (latus) => class BundlePacket extends Packet {
|
|||
return this.unpack(buffer).data;
|
||||
}
|
||||
|
||||
static unpackData(data) {
|
||||
const packets = [];
|
||||
static unpackData(buffer) {
|
||||
const res = [];
|
||||
let caret = 0;
|
||||
while (caret < data.length) {
|
||||
while (caret < buffer.length) {
|
||||
// Read packed length.
|
||||
const length = data.readUInt32LE(caret);
|
||||
const length = buffer.readUInt32LE(caret);
|
||||
caret += 4;
|
||||
// Read packed data. TODO: manual blitting sucks...
|
||||
const packedPacket = Buffer.allocUnsafe(length);
|
||||
let i = 0;
|
||||
while (i < length) {
|
||||
packedPacket.writeUInt8(data.readUInt8(caret++), i++);
|
||||
packedPacket.writeUInt8(buffer.readUInt8(caret++), i++);
|
||||
}
|
||||
// Lookup packet.
|
||||
const packetId = packedPacket.readUInt8(0);
|
||||
const {default: Packet} = packets(latus).fromId[packetId];
|
||||
const {fromId: {[packetId]: Packet_}} = packets(latus);
|
||||
// Unpack and instantiate the packet.
|
||||
const unpacked = Packet.unpack(packedPacket);
|
||||
packets.push(new Packet(unpacked.data));
|
||||
const unpacked = Packet_.unpack(packedPacket);
|
||||
res.push(new Packet_(unpacked.data));
|
||||
}
|
||||
return packets;
|
||||
return res;
|
||||
}
|
||||
|
||||
static respond({data: packets}, socket) {
|
||||
for (let i = 0; i < packets.length; i++) {
|
||||
socket.emit('packet', packets[i]);
|
||||
socket.emit('packet', packets[i], () => {});
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -44,7 +44,7 @@ export default class Packet {
|
|||
|
||||
static unpack(buffer) {
|
||||
const unpacked = this.builder.decode(buffer);
|
||||
unpacked.data = Packet.unpackData(unpacked.data);
|
||||
unpacked.data = this.unpackData(unpacked.data);
|
||||
return unpacked;
|
||||
}
|
||||
|
||||
|
|
|
@ -47,7 +47,7 @@ const Encoder = (latus) => class Encoder {
|
|||
}
|
||||
catch (error) {
|
||||
const next = new Error(`Couldn't pack ${
|
||||
Packet.name
|
||||
Packet.type
|
||||
}(${
|
||||
JSON.stringify(packet.data[1], null, 2)
|
||||
}): ${
|
||||
|
@ -95,7 +95,7 @@ const Decoder = (latus) => class Decoder extends decorate(class {}) {
|
|||
});
|
||||
}
|
||||
catch (error) {
|
||||
const next = new Error(`Couldn't unpack ${Packet.name}(${view}): ${error.message}`);
|
||||
const next = new Error(`Couldn't unpack ${Packet.type}(${view}): ${error.message}`);
|
||||
// eslint-disable-next-line no-console
|
||||
console.error(next);
|
||||
throw next;
|
||||
|
|
|
@ -24,12 +24,12 @@ export default class ServerSocket extends decorate(Class) {
|
|||
this.socket.leavePromise = promisify(this.socket.leave.bind(this.socket));
|
||||
const Packets = Object.entries(packets(latus).fromType);
|
||||
for (let i = 0; i < Packets.length; i++) {
|
||||
const [name, Packet] = Packets[i];
|
||||
const [type, Packet] = Packets[i];
|
||||
const {id} = Packet;
|
||||
debug('Registering packet %s(id: %s)', name, id);
|
||||
debug('Registering packet %s(id: %s)', type, id);
|
||||
this.socket.on(id, (data, fn) => {
|
||||
const packet = new Packet(data);
|
||||
debug('recieved packet %s(%o)', name, data);
|
||||
debug('recieved packet %s(%j)', type, data);
|
||||
this.emit('packet', packet, fn);
|
||||
});
|
||||
}
|
||||
|
@ -67,7 +67,7 @@ export default class ServerSocket extends decorate(Class) {
|
|||
|
||||
static send(latus, socket, packetOrDehydrated, method) {
|
||||
const packet = normalize(latus, packetOrDehydrated);
|
||||
debug('sending packet %s(%o)', packet.constructor.type, packet.data);
|
||||
debug('sending packet %s(%j)', packet.constructor.type, packet.data);
|
||||
const {id} = packet.constructor;
|
||||
return socket[method](id, packet.data);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user