-(define-syntax-rule (8sync-at args ...)
- "Alias for %sync-at"
- (%sync-at args ...))
-
-(define-syntax-rule (8sync-delay args ...)
- "Alias for %sync-delay"
- (8sync-delay args ...))
+;; Async port request and run-request meta-requests
+(define (make-async-request proc)
+ "Wrap PROC in an async-request
+
+The purpose of this is to make sure that users don't accidentally
+return the wrong thing via (8sync) and trip themselves up."
+ (cons '*async-request* proc))
+
+(define (setup-async-request resume-kont async-request)
+ "Complete an async request for agenda-run-once's continuation handling"
+ (match async-request
+ (('*async-request* . async-setup-proc)
+ (async-setup-proc resume-kont))
+ ;; TODO: deliver more helpful errors depending on what the user
+ ;; returned
+ (_ (throw 'invalid-async-request
+ "Invalid request passed back via an (%sync) procedure."
+ async-request))))
+
+(define-syntax-rule (%run body ...)
+ (%run-at body ... #f))
+
+(define-syntax-rule (%run-at body ... when)
+ (make-async-request
+ (lambda (kont)
+ (make-run-request
+ (wrap
+ (kont
+ (begin body ...)))
+ when))))
+
+(define-syntax-rule (%run-delay body ... delay-time)
+ (%run-at body ... (tdelta delay-time)))
+
+(define-syntax-rule (%port-request add-this-port port-request-args ...)
+ (make-async-request
+ (lambda (kont)
+ (list (make-port-request port-request-args ...)
+ (make-run-request kont)))))
+
+;; TODO
+(define-syntax-rule (%run-with-return return body ...)
+ (make-async-request
+ (lambda (kont)
+ (let ((return kont))
+ (lambda ()
+ body ...)))))
+
+;; Aliases
+(define-syntax-rule (8run args ...) (%run args ...))
+(define-syntax-rule (8run-at args ...) (%run-at args ...))
+(define-syntax-rule (8run-delay args ...) (%run-delay args ...))
+(define-syntax-rule (8port-request args ...) (%port-request args ...))