ws-promisified
特徴
scrapbox用code
$ esbuild mod.ts --bundle --minify --charset=utf8 --sourcemap=inline
code:mod.js
function l(t,s){return new Promise((c,i)=>{let e=new WebSocket(t,s);o(e,"open",()=>c({close:()=>new Promise((r,a)=>{e.close(),o(e,"close",n=>r(n)),o(e,"error",n=>a(n))}),send:r=>{if(e.readyState===2)throw Error("The Web Socket is closing");if(e.readyState===3)throw Error("The Web Socket is already closed");e.send(r)},receive:async function*(){for(;;)yield await new Promise((a,n)=>{o(e,"message",v=>a(v)),o(e,"error",v=>n(v))})}})),o(e,"error",r=>i(r))})}function o(t,s,c){let i=e=>{c(e),t.removeEventListener(s,i)};t.addEventListener(s,i)}export{l as openWS};
//# sourceMappingURL=data:application/json;base64,ewogICJ2ZXJzaW9uIjogMywKICAic291cmNlcyI6IFsibW9kLnRzIl0sCiAgInNvdXJjZXNDb250ZW50IjogWyJleHBvcnQgaW50ZXJmYWNlIFJlc3VsdCB7XG4gIC8qKiBjbG9zZSB3ZWIgc29ja2V0ICovXG4gIGNsb3NlKCk6IFByb21pc2U8Q2xvc2VFdmVudD47XG4gIC8qKiBzZW5kIGRhdGEgdGhyb3VnaCB3ZWIgc29ja2V0ICovXG4gIHNlbmQoZGF0YTogc3RyaW5nIHwgQXJyYXlCdWZmZXJMaWtlIHwgQmxvYiB8IEFycmF5QnVmZmVyVmlldyk6IHZvaWQ7XG4gIC8qKiByZWNlaXZlIGRhdGEgYXMgYW4gYXN5bmMgZ2VuZXJhdG9yICovXG4gIHJlY2VpdmUoKTogQXN5bmNHZW5lcmF0b3I8TWVzc2FnZUV2ZW50LCBuZXZlciwgdm9pZD47XG59XG5cbi8qKiBjcmVhdGUgYW5kIG9wZW4gYSB3ZWIgc29ja2V0IHdyYXBwZWQgYnkgUHJvbWlzZSAqL1xuZXhwb3J0IGZ1bmN0aW9uIG9wZW5XUyh1cmw6IHN0cmluZywgcHJvdGNvbHM/OiBzdHJpbmcgfCBzdHJpbmdbXSkge1xuICByZXR1cm4gbmV3IFByb21pc2U8UmVzdWx0PigocmVzb2x2ZSwgcmVqZWN0KSA9PiB7XG4gICAgY29uc3Qgc29ja2V0ID0gbmV3IFdlYlNvY2tldCh1cmwsIHByb3Rjb2xzKTtcblxuICAgIG9uY2Uoc29ja2V0LCBcIm9wZW5cIiwgKCkgPT5cbiAgICAgIHJlc29sdmUoe1xuICAgICAgICBjbG9zZTogKCkgPT5cbiAgICAgICAgICBuZXcgUHJvbWlzZSgocmVzLCByZWopID0+IHtcbiAgICAgICAgICAgIHNvY2tldC5jbG9zZSgpO1xuICAgICAgICAgICAgb25jZShzb2NrZXQsIFwiY2xvc2VcIiwgKGUpID0+IHJlcyhlKSk7XG4gICAgICAgICAgICBvbmNlKHNvY2tldCwgXCJlcnJvclwiLCAoZSkgPT4gcmVqKGUpKTtcbiAgICAgICAgICB9KSxcbiAgICAgICAgc2VuZDogKGRhdGEpID0+IHtcbiAgICAgICAgICBpZiAoc29ja2V0LnJlYWR5U3RhdGUgPT09IDIpIHtcbiAgICAgICAgICAgIHRocm93IEVycm9yKFwiVGhlIFdlYiBTb2NrZXQgaXMgY2xvc2luZ1wiKTtcbiAgICAgICAgICB9XG4gICAgICAgICAgaWYgKHNvY2tldC5yZWFkeVN0YXRlID09PSAzKSB7XG4gICAgICAgICAgICB0aHJvdyBFcnJvcihcIlRoZSBXZWIgU29ja2V0IGlzIGFscmVhZHkgY2xvc2VkXCIpO1xuICAgICAgICAgIH1cbiAgICAgICAgICBzb2NrZXQuc2VuZChkYXRhKTtcbiAgICAgICAgfSxcbiAgICAgICAgcmVjZWl2ZTogYXN5bmMgZnVuY3Rpb24qICgpIHtcbiAgICAgICAgICB3aGlsZSAodHJ1ZSkge1xuICAgICAgICAgICAgY29uc3QgcmVzcG9uc2UgPSBhd2FpdCBuZXcgUHJvbWlzZSgocmVzLCByZWopID0+IHtcbiAgICAgICAgICAgICAgb25jZShzb2NrZXQsIFwibWVzc2FnZVwiLCAoZSkgPT4gcmVzKGUpKTtcbiAgICA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