|
74 | 74 |
|
75 | 75 | to-completable-future |
76 | 76 |
|
77 | | - when-complete when-complete-async) |
| 77 | + when-complete when-complete-async |
| 78 | + |
| 79 | + deferred cs-default-executor) |
78 | 80 |
|
79 | 81 | ;; The potemkin abstract type for |
80 | 82 | ;; implementations such as CompletionStage |
|
84 | 86 | ADeferred |
85 | 87 | CompletionStage |
86 | 88 | (thenApply [d f] |
87 | | - (then-apply d f)) |
| 89 | + (then-apply d f nil)) |
88 | 90 | (thenApplyAsync [d f] |
89 | | - (then-apply-async d f)) |
| 91 | + (then-apply d f (deferred (cs-default-executor)))) |
90 | 92 | (thenApplyAsync [d f executor] |
91 | | - (then-apply-async d f executor)) |
| 93 | + (then-apply d f (deferred executor))) |
92 | 94 |
|
93 | 95 | (thenAccept [d f] |
94 | | - (then-accept d f)) |
| 96 | + (then-accept d f nil)) |
95 | 97 | (thenAcceptAsync [d f] |
96 | | - (then-accept-async d f)) |
| 98 | + (then-accept d f (deferred (cs-default-executor)))) |
97 | 99 | (thenAcceptAsync [d f executor] |
98 | | - (then-accept-async d f executor)) |
| 100 | + (then-accept d f (deferred executor))) |
99 | 101 |
|
100 | 102 | (thenRun [d f] |
101 | | - (then-run d f)) |
| 103 | + (then-run d f nil)) |
102 | 104 | (thenRunAsync [d f] |
103 | | - (then-run-async d f)) |
| 105 | + (then-run d f (deferred (cs-default-executor)))) |
104 | 106 | (thenRunAsync [d f executor] |
105 | | - (then-run-async d f executor)) |
| 107 | + (then-run d f (deferred executor))) |
106 | 108 |
|
107 | 109 | (thenCombine [d other f] |
108 | | - (then-combine d other f)) |
| 110 | + (then-combine d other f nil)) |
109 | 111 | (thenCombineAsync [d other f] |
110 | | - (then-combine-async d other f)) |
| 112 | + (then-combine d other f (deferred (cs-default-executor)))) |
111 | 113 | (thenCombineAsync [d other f executor] |
112 | | - (then-combine-async d other f executor)) |
| 114 | + (then-combine d other f (deferred executor))) |
113 | 115 |
|
114 | 116 | (thenAcceptBoth [d other f] |
115 | | - (then-accept-both d other f)) |
| 117 | + (then-accept-both d other f nil)) |
116 | 118 | (thenAcceptBothAsync [d other f] |
117 | | - (then-accept-both-async d other f)) |
| 119 | + (then-accept-both d other f (deferred (cs-default-executor)))) |
118 | 120 | (thenAcceptBothAsync [d other f executor] |
119 | | - (then-accept-both-async d other f executor)) |
| 121 | + (then-accept-both d other f (deferred executor))) |
120 | 122 |
|
121 | 123 | (runAfterBoth [d other f] |
122 | | - (run-after-both d other f)) |
| 124 | + (run-after-both d other f nil)) |
123 | 125 | (runAfterBothAsync [d other f] |
124 | | - (run-after-both-async d other f)) |
| 126 | + (run-after-both d other f (deferred (cs-default-executor)))) |
125 | 127 | (runAfterBothAsync [d other f executor] |
126 | | - (run-after-both-async d other f executor)) |
| 128 | + (run-after-both d other f (deferred executor))) |
127 | 129 |
|
128 | 130 | (applyToEither [d other f] |
129 | | - (apply-to-either d other f)) |
| 131 | + (apply-to-either d other f nil)) |
130 | 132 | (applyToEitherAsync [d other f] |
131 | | - (apply-to-either-async d other f)) |
| 133 | + (apply-to-either d other f (deferred (cs-default-executor)))) |
132 | 134 | (applyToEitherAsync [d other f executor] |
133 | | - (apply-to-either-async d other f executor)) |
| 135 | + (apply-to-either d other f (deferred executor))) |
134 | 136 |
|
135 | 137 | (acceptEither [d other f] |
136 | | - (accept-either d other f)) |
| 138 | + (accept-either d other f nil)) |
137 | 139 | (acceptEitherAsync [d other f] |
138 | | - (accept-either-async d other f)) |
| 140 | + (accept-either d other f (deferred (cs-default-executor)))) |
139 | 141 | (acceptEitherAsync [d other f executor] |
140 | | - (accept-either-async d other f executor)) |
| 142 | + (accept-either d other f (deferred executor))) |
141 | 143 |
|
142 | 144 | (runAfterEither [d other f] |
143 | | - (run-after-either d other f)) |
| 145 | + (run-after-either d other f nil)) |
144 | 146 | (runAfterEitherAsync [d other f] |
145 | | - (run-after-either-async d other f)) |
| 147 | + (run-after-either d other f (deferred (cs-default-executor)))) |
146 | 148 | (runAfterEitherAsync [d other f executor] |
147 | | - (run-after-either-async d other f executor)) |
| 149 | + (run-after-either d other f (deferred executor))) |
148 | 150 |
|
149 | 151 | (thenCompose [d f] |
150 | | - (then-compose d f)) |
| 152 | + (then-compose d f nil)) |
151 | 153 | (thenComposeAsync [d f] |
152 | | - (then-compose-async d f)) |
| 154 | + (then-compose d f (deferred (cs-default-executor)))) |
153 | 155 | (thenComposeAsync [d f executor] |
154 | | - (then-compose-async d f executor)) |
| 156 | + (then-compose d f (deferred executor))) |
155 | 157 |
|
156 | 158 | (handle [d f] |
157 | | - (then-handle d f)) |
| 159 | + (then-handle d f nil)) |
158 | 160 | (handleAsync [d f] |
159 | | - (then-handle-async d f)) |
| 161 | + (then-handle d f (deferred (cs-default-executor)))) |
160 | 162 | (handleAsync [d f executor] |
161 | | - (then-handle-async d f executor)) |
| 163 | + (then-handle d f (deferred executor))) |
162 | 164 |
|
163 | 165 | (exceptionally [d f] |
164 | | - (then-exceptionally d f)) |
165 | | - |
166 | | - (toCompletableFuture [d] |
167 | | - (to-completable-future d)) |
| 166 | + (then-exceptionally d f nil)) |
| 167 | + ;; Only available since Java 12 |
| 168 | + ;; (exceptionallyAsync [d f] |
| 169 | + ;; (then-exceptionally d f (deferred (cs-default-executor)))) |
| 170 | + ;; (exceptionallyAsync [d f executor] |
| 171 | + ;; (then-exceptionally d (deferred f))) |
168 | 172 |
|
169 | 173 | (whenComplete [d f] |
170 | | - (when-complete d f)) |
| 174 | + (when-complete d f nil)) |
171 | 175 | (whenCompleteAsync [d f] |
172 | | - (when-complete-async d f)) |
| 176 | + (when-complete d f (deferred (cs-default-executor)))) |
173 | 177 | (whenCompleteAsync [d f executor] |
174 | | - (when-complete-async d f executor))) |
| 178 | + (when-complete d f (deferred executor))) |
| 179 | + |
| 180 | + (toCompletableFuture [d] |
| 181 | + (to-completable-future d))) |
175 | 182 |
|
176 | 183 | (definline realized? |
177 | 184 | "Returns true if the manifold deferred is realized." |
@@ -1532,141 +1539,109 @@ |
1532 | 1539 | ;; CompletionStage helper fns |
1533 | 1540 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
1534 | 1541 |
|
1535 | | -(defmacro ^:no-doc def-async-for |
1536 | | - "Defines a CompletionStage async version of the function associated with |
1537 | | - the given symbol, with '-async' appended." |
1538 | | - [fn-name] |
1539 | | - (let [async-name (symbol (str (name fn-name) "-async"))] |
1540 | | - `(defn- ~async-name |
1541 | | - ([d# f#] |
1542 | | - (~async-name d# f# (or (ex/executor) (ex/execute-pool)))) |
1543 | | - ([d# f# executor#] |
1544 | | - (~fn-name (onto d# executor#) f#))))) |
1545 | | - |
1546 | | -(defmacro ^:no-doc def-async-for-dual |
1547 | | - "Defines a CompletionStage async version of the two-deferred |
1548 | | - function associated with the given symbol, with '-async' appended." |
1549 | | - [fn-name] |
1550 | | - (let [async-name (symbol (str (name fn-name) "-async"))] |
1551 | | - `(defn- ~async-name |
1552 | | - ([d# d2# f#] |
1553 | | - (~async-name d# d2# f# (or (ex/executor) (ex/execute-pool)))) |
1554 | | - ([d# d2# f# executor#] |
1555 | | - (~fn-name (onto d# executor#) d2# f#))))) |
1556 | | - |
1557 | | -(defn- fmap-deferred |
1558 | | - "Returns a new deferred with function `f` applies to realized value of `d`. |
1559 | | - (Like fmap but for deferreds.) |
1560 | | -
|
1561 | | - This function does not unwrap the result of f; it will only be applied to |
1562 | | - the immediate value of `d`. This is for mimicking CompletionStage's |
1563 | | - behavior." |
| 1542 | +(defn- cs-default-executor [] |
| 1543 | + (or (ex/executor) (ex/execute-pool))) |
| 1544 | + |
| 1545 | +(defn- shallow-connect |
| 1546 | + "Like `connect` but without implicit unwrapping of conveyed value." |
| 1547 | + [from to] |
| 1548 | + (on-realized from |
| 1549 | + (fn [val] (success! to val)) |
| 1550 | + (fn [error] (error! to error)))) |
| 1551 | + |
| 1552 | +(defn- shallow-chain |
| 1553 | + "Returns a new deferred with function `f` applied to realized value of `d`. |
| 1554 | +
|
| 1555 | + Unlike `chain`, this function does not unwrap the result of `f`; it will only be applied to the |
| 1556 | + immediate value of `d`. This is for mimicking `CompletionStage`'s behavior." |
1564 | 1557 | [d f] |
1565 | 1558 | (let [d' (deferred)] |
1566 | 1559 | (on-realized d |
1567 | 1560 | (fn [val] (success! d' (f val))) |
1568 | 1561 | (fn [error] (error! d' error))) |
1569 | 1562 | d')) |
1570 | 1563 |
|
1571 | | -(defn- then-apply [d ^Function f] |
1572 | | - (assert-some f) |
1573 | | - (fmap-deferred d #(.apply f %))) |
| 1564 | +(defn- completion-stage-result [d f result] |
| 1565 | + (when result |
| 1566 | + (shallow-connect d result)) |
| 1567 | + (shallow-chain (or result d) f)) |
1574 | 1568 |
|
1575 | | -(def-async-for then-apply) |
| 1569 | +(defn- then-apply [d ^Function f result] |
| 1570 | + (assert-some f) |
| 1571 | + (completion-stage-result d #(.apply f %) result)) |
1576 | 1572 |
|
1577 | | -(defn- then-accept [d ^Consumer c] |
| 1573 | +(defn- then-accept [d ^Consumer c result] |
1578 | 1574 | (assert-some c) |
1579 | | - (fmap-deferred d #(.accept c %))) |
1580 | | - |
1581 | | -(def-async-for then-accept) |
| 1575 | + (completion-stage-result d #(.accept c %) result)) |
1582 | 1576 |
|
1583 | | -(defn- then-run [d ^Runnable f] |
| 1577 | +(defn- then-run [d ^Runnable f result] |
1584 | 1578 | (assert-some f) |
1585 | | - (fmap-deferred d (fn [_] (.run f)))) |
1586 | | - |
1587 | | -(def-async-for then-run) |
| 1579 | + (completion-stage-result d (fn [_] (.run f)) result)) |
1588 | 1580 |
|
1589 | | - |
1590 | | -(defn- then-combine [d other ^BiFunction f] |
| 1581 | +(defn- then-combine [d other ^BiFunction f result] |
1591 | 1582 | (assert-some other f) |
1592 | | - (fmap-deferred (zip d other) |
1593 | | - (fn [[x y]] (.apply f x y)))) |
1594 | | - |
1595 | | -(def-async-for-dual then-combine) |
| 1583 | + (completion-stage-result (zip d other) |
| 1584 | + (fn [[x y]] (.apply f x y)) |
| 1585 | + result)) |
1596 | 1586 |
|
1597 | | - |
1598 | | -(defn- then-accept-both [d other ^BiConsumer f] |
| 1587 | +(defn- then-accept-both [d other ^BiConsumer f result] |
1599 | 1588 | (assert-some other f) |
1600 | | - (fmap-deferred (zip d other) |
1601 | | - (fn [[x y]] (.accept f x y)))) |
1602 | | - |
1603 | | -(def-async-for-dual then-accept-both) |
| 1589 | + (completion-stage-result (zip d other) |
| 1590 | + (fn [[x y]] (.accept f x y)) |
| 1591 | + result)) |
1604 | 1592 |
|
1605 | | - |
1606 | | -(defn- run-after-both [d other ^Runnable f] |
| 1593 | +(defn- run-after-both [d other ^Runnable f result] |
1607 | 1594 | (assert-some other f) |
1608 | | - (fmap-deferred (zip d other) |
1609 | | - (fn [[_ _]] (.run f)))) |
1610 | | - |
| 1595 | + (completion-stage-result (zip d other) |
| 1596 | + (fn [[_ _]] (.run f)) |
| 1597 | + result)) |
1611 | 1598 |
|
1612 | | -(def-async-for-dual run-after-both) |
1613 | | - |
1614 | | - |
1615 | | -(defn- apply-to-either [d other ^Function f] |
| 1599 | +(defn- apply-to-either [d other ^Function f result] |
1616 | 1600 | (assert-some other f) |
1617 | | - (then-apply (alt d other) f)) |
1618 | | - |
1619 | | -(def-async-for-dual apply-to-either) |
| 1601 | + (then-apply (alt d other) f result)) |
1620 | 1602 |
|
1621 | | - |
1622 | | -(defn- accept-either [d other ^Function f] |
| 1603 | +(defn- accept-either [d other ^Function f result] |
1623 | 1604 | (assert-some other f) |
1624 | | - (then-accept (alt d other) f)) |
1625 | | - |
1626 | | -(def-async-for-dual accept-either) |
| 1605 | + (then-accept (alt d other) f result)) |
1627 | 1606 |
|
1628 | | - |
1629 | | -(defn- run-after-either [d other ^Function f] |
| 1607 | +(defn- run-after-either [d other ^Function f result] |
1630 | 1608 | (assert-some other f) |
1631 | | - (then-run (alt d other) f)) |
1632 | | - |
1633 | | -(def-async-for-dual run-after-either) |
| 1609 | + (then-run (alt d other) f result)) |
1634 | 1610 |
|
1635 | | - |
1636 | | -(defn- then-compose [d ^Function f] |
| 1611 | +(defn- then-compose [d ^Function f result] |
1637 | 1612 | (assert-some f) |
1638 | 1613 | (let [d' (deferred)] |
1639 | | - (on-realized d |
1640 | | - (fn [val] |
1641 | | - (on-realized (->deferred (.apply f val)) |
1642 | | - #(success! d' %) |
1643 | | - #(error! d' %))) |
1644 | | - (fn [error] (error! d' error))) |
| 1614 | + (-> (completion-stage-result d #(->deferred (.apply f %)) result) |
| 1615 | + (on-realized (fn [fd] |
| 1616 | + (shallow-connect fd d')) |
| 1617 | + (fn [error] |
| 1618 | + (error! d' error)))) |
1645 | 1619 | d')) |
1646 | 1620 |
|
1647 | | -(def-async-for then-compose) |
1648 | | - |
1649 | | - |
1650 | | -(defn- then-handle [d ^BiFunction f] |
| 1621 | +(defn- then-handle [d ^BiFunction f result] |
1651 | 1622 | (assert-some f) |
| 1623 | + ;; Can't use `completion-stage-result` here because it only covers |
| 1624 | + ;; the success case. |
| 1625 | + (when result |
| 1626 | + (shallow-connect d result)) |
1652 | 1627 | (let [d' (deferred)] |
1653 | 1628 | (on-realized |
1654 | | - d |
| 1629 | + (or result d) |
1655 | 1630 | (fn [val] (success! d' (.apply f val nil))) |
1656 | 1631 | (fn [error] (success! d' (.apply f nil error)))) |
1657 | 1632 | d')) |
1658 | 1633 |
|
1659 | | - |
1660 | | -(def-async-for then-handle) |
1661 | | - |
1662 | | - |
1663 | | -(defn- then-exceptionally [d ^Function f] |
| 1634 | +(defn- then-exceptionally [d ^Function f result] |
1664 | 1635 | (assert-some f) |
| 1636 | + ;; Can't use `completion-stage-result` here because it only covers |
| 1637 | + ;; the success case. |
| 1638 | + (when result |
| 1639 | + (shallow-connect d result)) |
1665 | 1640 | (let [d' (deferred)] |
1666 | 1641 | (on-realized |
1667 | | - d |
1668 | | - (fn [val] (success! d' val)) |
1669 | | - (fn [error] (success! d' (.apply f error)))) |
| 1642 | + (or result d) |
| 1643 | + (fn [val] (success! d' val)) |
| 1644 | + (fn [error] (success! d' (.apply f error)))) |
1670 | 1645 | d')) |
1671 | 1646 |
|
1672 | 1647 | (defn- to-completable-future [d] |
|
1679 | 1654 |
|
1680 | 1655 | result)) |
1681 | 1656 |
|
1682 | | -(defn- when-complete [d ^BiConsumer f] |
| 1657 | +(defn- when-complete [d ^BiConsumer f result] |
1683 | 1658 | (assert-some f) |
| 1659 | + ;; Can't use `completion-stage-result` here because it only covers |
| 1660 | + ;; the success case. |
| 1661 | + (when result |
| 1662 | + (shallow-connect d result)) |
1684 | 1663 | (let [d' (deferred)] |
1685 | | - (on-realized d |
| 1664 | + (on-realized (or result d) |
1686 | 1665 | (fn [val] |
1687 | 1666 | (try (.accept f val nil) |
1688 | 1667 | (success! d' val) |
|
1695 | 1674 | (error! d' err))))) |
1696 | 1675 | d')) |
1697 | 1676 |
|
1698 | | -(def-async-for when-complete) |
1699 | | - |
1700 | 1677 | ;;; |
1701 | 1678 |
|
1702 | 1679 | (alter-meta! #'->Deferred assoc :private true) |
|
0 commit comments