Fixing some bugs and changing subscribe behaviour

This commit is contained in:
Fabian 2019-10-01 18:24:26 +02:00
parent 405e589328
commit 518d46f410
4 changed files with 15 additions and 4 deletions

4
.env Normal file
View File

@ -0,0 +1,4 @@
PORT = 5000
ADMIN_KEY = test
ACCESS_LOG = true
DEV = true

View File

@ -1,5 +1,7 @@
import Logging from "@hibas123/nodelogging"; import Logging from "@hibas123/nodelogging";
import * as dotenv from "dotenv"; import * as dotenv from "dotenv";
dotenv.config()
interface IConfig { interface IConfig {
port: number; port: number;
@ -15,6 +17,4 @@ const config: IConfig = {
dev: (process.env.DEV || "").toLowerCase() === "true" dev: (process.env.DEV || "").toLowerCase() === "true"
} }
dotenv.config()
export default config; export default config;

View File

@ -4,6 +4,7 @@ import { DatabaseManager } from "./database/database";
import Logging from "@hibas123/logging"; import Logging from "@hibas123/logging";
import Query from "./database/query"; import Query from "./database/query";
import Session from "./database/session"; import Session from "./database/session";
import shortid = require("shortid");
type QueryTypes = "get" | "set" | "push" | "subscribe" | "unsubscribe"; type QueryTypes = "get" | "set" | "push" | "subscribe" | "unsubscribe";
@ -16,6 +17,7 @@ export class ConnectionManager {
} }
private static onConnection(socket: io.Socket) { private static onConnection(socket: io.Socket) {
Logging.debug("New Connection:", socket.id);
const reqMap = new Map<string, [number, number]>(); const reqMap = new Map<string, [number, number]>();
const stored = new Map<string, Query>(); const stored = new Map<string, Query>();
const session = new Session(); const session = new Session();
@ -62,14 +64,19 @@ export class ConnectionManager {
case "subscribe": case "subscribe":
if (!perms.read) if (!perms.read)
throw noperm; throw noperm;
let subscriptionID = shortid.generate();
query.subscribe(data, (data) => { query.subscribe(data, (data) => {
answer(id, data); socket.emit("event", subscriptionID, data);
}); });
stored.set(id, query); stored.set(id, query);
answer(id, subscriptionID);
return; return;
case "unsubscribe": case "unsubscribe":
query.unsubscribe(); query.unsubscribe();
stored.delete(id); stored.delete(id);
answer(id, true);
return; return;
} }

View File

@ -230,7 +230,7 @@ export default class Query {
if (type === change.type) { if (type === change.type) {
Logging.debug("Path", this.path, change.path); Logging.debug("Path", this.path, change.path);
if (this.path.length === change.path.length - (type === ChangeTypes.PUSH ? 1 : 0)) { if (this.path.length <= change.path.length - (type === ChangeTypes.PUSH ? 1 : 0)) {
let valid = true; let valid = true;
for (let i = 0; i < this.path.length; i++) { for (let i = 0; i < this.path.length; i++) {
if (this.path[i] !== change.path[i]) { if (this.path[i] !== change.path[i]) {