Stacksmith icon indicating copy to clipboard operation
Stacksmith copied to clipboard

Add async/await style syntax

Open uliwitness opened this issue 6 years ago • 4 comments

pause caller [until end of handler] -- until is like defer, call pause again to dequeue the defer resume caller

uliwitness avatar Sep 05 '17 07:09 uliwitness

We already allow pausing handlers internally (see implementation of "go" command for stacks on web servers).

uliwitness avatar Sep 05 '17 07:09 uliwitness

Idea of async/await is that you could e.g. implement a download command as put contentsAtURL( "foo:///bar/baz") into var where contentsAtURL() would pause the current handler and resume it when the asynchronous download command calls our when done code.

We need some syntax to allow this. E.g.:

on contentsAtURL theURL
  global gMyDownload
  download "theURL" to gMyDownload
  when done
    resume caller
  end download
  pause caller
  return gMyDownload
end contentsAtURL

Which kicks off the asynchronous operation, then pauses the caller (i.e. saves the script's state and stops it), and when the download finishes, it calls resume caller which causes the script to be started again, resuming right after "pause caller".

But the syntax seems too nerdy. Is there a more natural way to express this?

uliwitness avatar Feb 17 '19 12:02 uliwitness

Also: What if the asynchronous operation is not as simple as this download command? Do we need a "token" that we can hand to someone else so they can call "resume" with it? What do we call this? the pauseID of this handler? Technically it's a future, but that's too nerdy a term. Find something that humans understand.

Are queue tickets a good metaphor?

on contentsAtURL theURL
  global gMyDownload
  put the queueTicket into myTicket
  download "theURL" to gMyDownload
  when done
    resume ticket myTicket
  end download
  pause until ticket myTicket
  return gMyDownload
end contentsAtURL

uliwitness avatar Mar 30 '19 11:03 uliwitness

Maybe we could use the wait command for this? The old-style wait 5 seconds would block, so needs to be implemented in a non-blocking way anyway, and that would use the same mechanism go already uses to allow to access stacks over the network without blocking Stacksmith itself.

So wait for <duration> or wait for ticket <ticketID>?

But then resume makes no sense anymore. complete ticket <ticketID>? punch ticket <ticketID> ? mark ticket <ticketID> as done?

uliwitness avatar May 19 '20 09:05 uliwitness