Skip to content

Commit

Permalink
feat(ngrok): onConnect
Browse files Browse the repository at this point in the history
Use the shiny new array lifting to support `tunnel.onConnect()`.
  • Loading branch information
eladb committed Feb 19, 2024
1 parent cb7c8a8 commit 5e81041
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
5 changes: 5 additions & 0 deletions ngrok/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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}");
});
Expand Down
17 changes: 15 additions & 2 deletions ngrok/ngrok.test.w
Original file line number Diff line number Diff line change
Expand Up @@ -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}");
});
});

test "url" {
log(external.url);
}
17 changes: 15 additions & 2 deletions ngrok/ngrok.w
Original file line number Diff line number Diff line change
Expand Up @@ -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<inflight (str): void>;

new(url: str, props: NgrokProps?) {
this.state = new sim.State();
this.url = this.state.token("url");
this.onConnectHandlers = MutArray<inflight (str): void>[];

if !nodeof(this).app.isTestEnvironment {
if !util.tryEnv("NGROK_AUTHTOKEN")? {
Expand All @@ -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();
};
Expand All @@ -60,6 +65,14 @@ pub class Tunnel {
this.state.set("url", "<test>");
});
}

if let h = props?.onConnect {
this.onConnect(h);
}
}

pub onConnect(handler: inflight (str): void) {
this.onConnectHandlers.push(handler);
}

extern "./util.js"
Expand Down

0 comments on commit 5e81041

Please sign in to comment.