forked from pixie-lang/pixie
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasync.pxi
More file actions
31 lines (27 loc) · 765 Bytes
/
async.pxi
File metadata and controls
31 lines (27 loc) · 765 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
(ns pixie.async
(:require [pixie.stacklets :as st]))
(deftype Promise [val pending-callbacks delivered?]
IDeref
(-deref [self]
(if delivered?
val
(do
(st/call-cc (fn [k]
(swap! pending-callbacks conj
(fn [v]
(st/-run-later (partial st/run-and-process k v)))))))))
IFn
(-invoke [self v]
(assert (not delivered?) "Can only deliver a promise once")
(set-field! self :val v)
(set-field! self :delivered? true)
(doseq [f @pending-callbacks]
(f v))
(reset! pending-callbacks nil)
nil))
(defn promise []
(->Promise nil (atom []) false))
(defmacro future [& body]
`(let [p# (promise)]
(st/spawn (p# (do ~@body)))
p#))