From 5e81041d44645f424a94c0e70f9df5735f74870b Mon Sep 17 00:00:00 2001 From: Elad Ben-Israel Date: Mon, 19 Feb 2024 12:53:22 +0200 Subject: [PATCH] feat(ngrok): `onConnect` Use the shiny new array lifting to support `tunnel.onConnect()`. --- ngrok/README.md | 5 +++++ ngrok/ngrok.test.w | 17 +++++++++++++++-- ngrok/ngrok.w | 17 +++++++++++++++-- 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/ngrok/README.md b/ngrok/README.md index 5dbd0a72..6f47db9e 100644 --- a/ngrok/README.md +++ b/ngrok/README.md @@ -42,6 +42,11 @@ let t = new ngrok.Tunnel(api.url, }, ); +// you can add listeners +t.onConnect(inflight (url) => { + log("url: {url}"); +}); + new cloud.Function(inflight () => { log("tunnel connected to {t.url}"); }); diff --git a/ngrok/ngrok.test.w b/ngrok/ngrok.test.w index 616e6949..9452a2c0 100644 --- a/ngrok/ngrok.test.w +++ b/ngrok/ngrok.test.w @@ -26,8 +26,21 @@ api.get("/uri", inflight () => { let w = new cloud.Website(path: "./public"); -let external = new ngrok.Tunnel(api.url, domain: "eladbgithub.ngrok.dev"); +let external = new ngrok.Tunnel(api.url, + domain: "eladbgithub.ngrok.dev", + onConnect: inflight (url) => { + log("onConnect called: {url}"); + } +); + +external.onConnect(inflight (url) => { + log("another onConnect callback: {url}"); +}); new cloud.Function(inflight () => { log("ready {external.url}"); -}); \ No newline at end of file +}); + +test "url" { + log(external.url); +} \ No newline at end of file diff --git a/ngrok/ngrok.w b/ngrok/ngrok.w index f8db12d0..f53e9bac 100644 --- a/ngrok/ngrok.w +++ b/ngrok/ngrok.w @@ -14,16 +14,19 @@ pub interface OnConnectHandler { pub struct NgrokProps { domain: str?; - onConnect: OnConnectHandler?; + onConnect: (inflight (str): void)?; } pub class Tunnel { pub url: str; state: sim.State; + var onConnectHandlers: MutArray; + new(url: str, props: NgrokProps?) { this.state = new sim.State(); this.url = this.state.token("url"); + this.onConnectHandlers = MutArray[]; if !nodeof(this).app.isTestEnvironment { if !util.tryEnv("NGROK_AUTHTOKEN")? { @@ -37,7 +40,9 @@ pub class Tunnel { log("ngrok: {child.url()} => {url}"); let url = child.url(); this.state.set("url", url); - props?.onConnect?.handle(url); + for handler in this.onConnectHandlers { + handler(url); + } return () => { child.kill(); }; @@ -60,6 +65,14 @@ pub class Tunnel { this.state.set("url", ""); }); } + + if let h = props?.onConnect { + this.onConnect(h); + } + } + + pub onConnect(handler: inflight (str): void) { + this.onConnectHandlers.push(handler); } extern "./util.js"