*add dist
This commit is contained in:
parent
afc332b75e
commit
1fabdc7e4d
|
@ -0,0 +1,21 @@
|
||||||
|
/// <reference types="node" />
|
||||||
|
/// <reference types="node" />
|
||||||
|
import { EventEmitter } from "events";
|
||||||
|
import { ErrTuple } from "libskynet";
|
||||||
|
import { Buffer } from "buffer";
|
||||||
|
export declare class DHT {
|
||||||
|
connect(pubkey: string): Promise<Socket>;
|
||||||
|
ready(): Promise<ErrTuple>;
|
||||||
|
}
|
||||||
|
export declare class Socket extends EventEmitter {
|
||||||
|
private id;
|
||||||
|
private eventUpdates;
|
||||||
|
constructor(id: number);
|
||||||
|
on(eventName: string, listener: (...args: any[]) => void): this;
|
||||||
|
off(type: string, listener: any): this;
|
||||||
|
write(message: string | Buffer): void;
|
||||||
|
end(): void;
|
||||||
|
private ensureEvent;
|
||||||
|
private trackEvent;
|
||||||
|
}
|
||||||
|
//# sourceMappingURL=index.d.ts.map
|
|
@ -0,0 +1 @@
|
||||||
|
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;AAQA,OAAO,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AACtC,OAAO,EAAU,QAAQ,EAAE,MAAM,WAAW,CAAC;AAC7C,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAehC,qBAAa,GAAG;IACC,OAAO,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAQ/C,KAAK,IAAI,OAAO,CAAC,QAAQ,CAAC;CAGnC;AAED,qBAAa,MAAO,SAAQ,YAAY;IACpC,OAAO,CAAC,EAAE,CAAS;IACnB,OAAO,CAAC,YAAY,CAAqC;gBAE7C,EAAE,EAAE,MAAM;IAKtB,EAAE,CAAC,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,GAAG,IAAI;IAkB/D,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,GAAG,GAAG,IAAI;IAStC,KAAK,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI;IAIrC,GAAG,IAAI,IAAI;IAIX,OAAO,CAAC,WAAW;IAMnB,OAAO,CAAC,UAAU;CAIrB"}
|
|
@ -0,0 +1,66 @@
|
||||||
|
import { callModule as callModuleKernel, connectModule as connectModuleKernel, } from "libkernel";
|
||||||
|
import { callModule as callModuleModule, connectModule as connectModuleModule, } from "libkmodule";
|
||||||
|
import { EventEmitter } from "events";
|
||||||
|
const DHT_MODULE = "AQD1IgE4lTZkq1fqdoYGojKRNrSk0YQ_wrHbRtIiHDrnow";
|
||||||
|
let callModule, connectModule;
|
||||||
|
if (window.document) {
|
||||||
|
callModule = callModuleKernel;
|
||||||
|
connectModule = connectModuleKernel;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
callModule = callModuleModule;
|
||||||
|
connectModule = connectModuleModule;
|
||||||
|
}
|
||||||
|
export class DHT {
|
||||||
|
async connect(pubkey) {
|
||||||
|
const [resp, err] = await callModule(DHT_MODULE, "connect", { pubkey });
|
||||||
|
if (err) {
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
return new Socket(resp.id);
|
||||||
|
}
|
||||||
|
async ready() {
|
||||||
|
return callModule(DHT_MODULE, "ready");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
export class Socket extends EventEmitter {
|
||||||
|
id;
|
||||||
|
eventUpdates = {};
|
||||||
|
constructor(id) {
|
||||||
|
super();
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
on(eventName, listener) {
|
||||||
|
const [update, promise] = connectModule(DHT_MODULE, "listenSocketEvent", { id: this.id, event: eventName }, (data) => {
|
||||||
|
this.emit(eventName, data);
|
||||||
|
});
|
||||||
|
this.trackEvent(eventName, update);
|
||||||
|
promise.then(() => {
|
||||||
|
this.off(eventName, listener);
|
||||||
|
});
|
||||||
|
return super.on(eventName, listener);
|
||||||
|
}
|
||||||
|
off(type, listener) {
|
||||||
|
const updates = [...this.eventUpdates[type]];
|
||||||
|
this.eventUpdates[type] = [];
|
||||||
|
for (const func of updates) {
|
||||||
|
func({ action: "off" });
|
||||||
|
}
|
||||||
|
return super.off(type, listener);
|
||||||
|
}
|
||||||
|
write(message) {
|
||||||
|
callModule(DHT_MODULE, "write", { id: this.id, message });
|
||||||
|
}
|
||||||
|
end() {
|
||||||
|
callModule(DHT_MODULE, "close", { id: this.id });
|
||||||
|
}
|
||||||
|
ensureEvent(event) {
|
||||||
|
if (!(event in this.eventUpdates)) {
|
||||||
|
this.eventUpdates[event] = [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
trackEvent(event, update) {
|
||||||
|
this.ensureEvent(event);
|
||||||
|
this.eventUpdates[event].push(update);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue