uliwitness / Stacksmith

An intuitive software erector kit targeted at people new to programming, but with enough potential to stay useful once they've learned, inspired by HyperCard.
http://stacksmith.org
131 stars 13 forks source link

Add async/await style syntax #92

Open uliwitness opened 7 years ago

uliwitness commented 7 years ago

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

uliwitness commented 7 years ago

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

uliwitness commented 5 years ago

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 commented 5 years ago

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 commented 4 years ago

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?