diff --git a/.gitignore b/.gitignore index 3c3629e..a8d7c85 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,4 @@ +*.log +.DS_Store node_modules +dist \ No newline at end of file diff --git a/dist/index.d.ts b/dist/index.d.ts deleted file mode 100644 index 2d1cf84..0000000 --- a/dist/index.d.ts +++ /dev/null @@ -1,25 +0,0 @@ -declare type Event = "connect" | "disconnect" | "message" | "verify"; -export default class Syncr { - url: string; - ready: boolean; - ws?: WebSocket; - pingInterval?: number; - pending: Map any; - reject: (a: any) => any; - }>; - message_timeout: number; - connection_verified: boolean; - private onEventFunctions; - private onNextEventFunctions; - constructor(url: string); - verify(): void; - connect(): Promise; - on(event: Event, f: Function): void; - onNext(event: Event, f: Function): void; - private trigger; - cleanup(): void; - ping(): void; - send(message: any, timeout?: number): Promise; -} -export {}; diff --git a/dist/index.js b/dist/index.js deleted file mode 100644 index f5bb92b..0000000 --- a/dist/index.js +++ /dev/null @@ -1,146 +0,0 @@ -"use strict"; -var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { - function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } - return new (P || (P = Promise))(function (resolve, reject) { - function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } - function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } - function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } - step((generator = generator.apply(thisArg, _arguments || [])).next()); - }); -}; -var __importDefault = (this && this.__importDefault) || function (mod) { - return (mod && mod.__esModule) ? mod : { "default": mod }; -}; -Object.defineProperty(exports, "__esModule", { value: true }); -const sleep_1 = __importDefault(require("./sleep")); -const uuid_1 = require("uuid"); -class Syncr { - constructor(url) { - this.url = url; - this.ready = false; - this.ws = undefined; - this.pingInterval = undefined; - this.message_timeout = 10000; - this.connection_verified = false; - this.pending = new Map(); // key: uuid, value: promise - this.onEventFunctions = { - connect: [], - disconnect: [], - message: [], - verify: [], - }; - this.onNextEventFunctions = { - connect: [], - disconnect: [], - message: [], - verify: [], - }; - this.connect(); - } - verify() { - this.connection_verified = true; - this.trigger("verify"); - } - connect() { - return __awaiter(this, void 0, void 0, function* () { - this.ws = new WebSocket(this.url); - this.ws.onopen = () => { - this.ready = true; - clearInterval(this.pingInterval); - this.pingInterval = window.setInterval(() => this.ping(), 5000); - this.trigger("connect"); - }; - this.ws.onclose = (e) => __awaiter(this, void 0, void 0, function* () { - if (this.ready) { - this.pending.forEach((promise) => promise.reject("disconnect")); - this.trigger("disconnect", e); - } - this.connection_verified = false; - this.cleanup(); - yield (0, sleep_1.default)(9000 * Math.random() + 1000); - this.connect(); - }); - this.ws.onerror = (err) => { }; //console.error("websocket err", err) - this.ws.onmessage = (event) => { - const msg = JSON.parse(event.data); - console.log("<--- server", msg.type); - if (msg.key) { - const promise = this.pending.get(msg.key); - if (promise === undefined) { - console.error("mesage not found in pending - will not process"); - return; - } - if (msg.type === "failure") { - promise.reject(msg.payload); - } - else { - promise.resolve(msg.payload); - } - this.pending.delete(msg.key); - } - else { - this.trigger("message", msg); - } - }; - }); - } - on(event, f) { - this.onEventFunctions[event].push(f); - } - onNext(event, f) { - this.onNextEventFunctions[event].push(f); - } - trigger(event, ...args) { - this.onEventFunctions[event].forEach((f) => f(...args)); - this.onNextEventFunctions[event].forEach((f) => f(...args)); - this.onNextEventFunctions[event] = []; - } - cleanup() { - this.ready = false; - clearInterval(this.pingInterval); - this.ws = undefined; - } - ping() { - if (this.ready) { - try { - this.ws && this.ws.send("ping"); - } - catch (e) { - console.error(e); - } - } - } - send(message, timeout) { - return __awaiter(this, void 0, void 0, function* () { - if (!this.ready) { - throw new Error("not ready"); - } - // make a key - // create promise, put in map - // when its returned, trigger it. - console.log("server --->", message); - const key = (0, uuid_1.v4)(); - return new Promise((resolve, reject) => { - this.pending.set(key, { resolve, reject }); - if (!this.ws) { - reject("ws is undefined"); - } - else { - this.ws.send(JSON.stringify({ - key, - payload: message, - })); - } - setTimeout(() => { - const p = this.pending.get(key); - if (p) { - console.log("MSG TIMEOUT!!!"); - p.reject("timeout"); - } - this.pending.delete(key); - }, timeout || this.message_timeout); - }); - }); - } -} -exports.default = Syncr; diff --git a/dist/sleep.d.ts b/dist/sleep.d.ts deleted file mode 100644 index 738983b..0000000 --- a/dist/sleep.d.ts +++ /dev/null @@ -1,2 +0,0 @@ -declare const _default: (ms: number) => Promise; -export default _default; diff --git a/dist/sleep.js b/dist/sleep.js deleted file mode 100644 index bab9f4e..0000000 --- a/dist/sleep.js +++ /dev/null @@ -1,5 +0,0 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -exports.default = (ms) => new Promise((resolve) => { - setTimeout(() => resolve(ms), ms); -});