|
74 | 74 |
|
75 | 75 | to-completable-future |
76 | 76 |
|
77 | | - when-complete when-complete-async) |
| 77 | + when-complete when-complete-async |
| 78 | + |
| 79 | + 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 (cs-default-executor))) |
90 | 92 | (thenApplyAsync [d f executor] |
91 | | - (then-apply-async d f executor)) |
| 93 | + (then-apply d f 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 (cs-default-executor))) |
97 | 99 | (thenAcceptAsync [d f executor] |
98 | | - (then-accept-async d f executor)) |
| 100 | + (then-accept d f 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 (cs-default-executor))) |
104 | 106 | (thenRunAsync [d f executor] |
105 | | - (then-run-async d f executor)) |
| 107 | + (then-run d f 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 (cs-default-executor))) |
111 | 113 | (thenCombineAsync [d other f executor] |
112 | | - (then-combine-async d other f executor)) |
| 114 | + (then-combine d other f 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 (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 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 (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 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 (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 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 (cs-default-executor))) |
139 | 141 | (acceptEitherAsync [d other f executor] |
140 | | - (accept-either-async d other f executor)) |
| 142 | + (accept-either d other f 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 (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 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 (cs-default-executor))) |
153 | 155 | (thenComposeAsync [d f executor] |
154 | | - (then-compose-async d f executor)) |
| 156 | + (then-compose d f 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 (cs-default-executor))) |
160 | 162 | (handleAsync [d f executor] |
161 | | - (then-handle-async d f executor)) |
| 163 | + (then-handle d f 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 (cs-default-executor))) |
| 170 | + ;; (exceptionallyAsync [d f executor] |
| 171 | + ;; (then-exceptionally d executor)) |
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 (cs-default-executor))) |
173 | 177 | (whenCompleteAsync [d f executor] |
174 | | - (when-complete-async d f executor))) |
| 178 | + (when-complete d f 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." |
@@ -1538,141 +1545,111 @@ |
1538 | 1545 | ;; CompletionStage helper fns |
1539 | 1546 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
1540 | 1547 |
|
1541 | | -(defmacro ^:no-doc def-async-for |
1542 | | - "Defines a CompletionStage async version of the function associated with |
1543 | | - the given symbol, with '-async' appended." |
1544 | | - [fn-name] |
1545 | | - (let [async-name (symbol (str (name fn-name) "-async"))] |
1546 | | - `(defn- ~async-name |
1547 | | - ([d# f#] |
1548 | | - (~async-name d# f# (or (ex/executor) (ex/execute-pool)))) |
1549 | | - ([d# f# executor#] |
1550 | | - (~fn-name (onto d# executor#) f#))))) |
1551 | | - |
1552 | | -(defmacro ^:no-doc def-async-for-dual |
1553 | | - "Defines a CompletionStage async version of the two-deferred |
1554 | | - function associated with the given symbol, with '-async' appended." |
1555 | | - [fn-name] |
1556 | | - (let [async-name (symbol (str (name fn-name) "-async"))] |
1557 | | - `(defn- ~async-name |
1558 | | - ([d# d2# f#] |
1559 | | - (~async-name d# d2# f# (or (ex/executor) (ex/execute-pool)))) |
1560 | | - ([d# d2# f# executor#] |
1561 | | - (~fn-name (onto d# executor#) d2# f#))))) |
1562 | | - |
1563 | | -(defn- fmap-deferred |
1564 | | - "Returns a new deferred with function `f` applies to realized value of `d`. |
1565 | | - (Like fmap but for deferreds.) |
1566 | | -
|
1567 | | - This function does not unwrap the result of f; it will only be applied to |
1568 | | - the immediate value of `d`. This is for mimicking CompletionStage's |
1569 | | - behavior." |
| 1548 | +(defn- cs-default-executor [] |
| 1549 | + (or (ex/executor) (ex/execute-pool))) |
| 1550 | + |
| 1551 | +(defn- shallow-connect |
| 1552 | + "Like `connect` but without implicit unwrapping of conveyed value." |
| 1553 | + [from to] |
| 1554 | + (on-realized from |
| 1555 | + (fn [val] (success! to val)) |
| 1556 | + (fn [error] (error! to error)))) |
| 1557 | + |
| 1558 | +(defn- shallow-chain |
| 1559 | + "Returns a new deferred with function `f` applied to realized value of `d`. |
| 1560 | +
|
| 1561 | + Unlike `chain`, this function does not unwrap the result of `f`; it will only be applied to the |
| 1562 | + immediate value of `d`. This is for mimicking `CompletionStage`'s behavior." |
1570 | 1563 | [d f] |
1571 | 1564 | (let [d' (deferred)] |
1572 | 1565 | (on-realized d |
1573 | 1566 | (fn [val] (success! d' (f val))) |
1574 | 1567 | (fn [error] (error! d' error))) |
1575 | 1568 | d')) |
1576 | 1569 |
|
1577 | | -(defn- then-apply [d ^Function f] |
1578 | | - (assert-some f) |
1579 | | - (fmap-deferred d #(.apply f %))) |
1580 | | - |
1581 | | -(def-async-for then-apply) |
1582 | | - |
1583 | | -(defn- then-accept [d ^Consumer c] |
1584 | | - (assert-some c) |
1585 | | - (fmap-deferred d #(.accept c %))) |
| 1570 | +(defn- shallow-onto [^IDeferred d executor] |
| 1571 | + (if (identical? executor (.executor d)) |
| 1572 | + d |
| 1573 | + (let [d' (deferred executor)] |
| 1574 | + (shallow-connect d d') |
| 1575 | + d'))) |
1586 | 1576 |
|
1587 | | -(def-async-for then-accept) |
| 1577 | +(defn- shallow-chain-onto [d f executor] |
| 1578 | + (-> d |
| 1579 | + (shallow-onto executor) |
| 1580 | + (shallow-chain f))) |
1588 | 1581 |
|
1589 | | -(defn- then-run [d ^Runnable f] |
| 1582 | +(defn- then-apply [d ^Function f executor] |
1590 | 1583 | (assert-some f) |
1591 | | - (fmap-deferred d (fn [_] (.run f)))) |
| 1584 | + (shallow-chain-onto d #(.apply f %) executor)) |
1592 | 1585 |
|
1593 | | -(def-async-for then-run) |
| 1586 | +(defn- then-accept [d ^Consumer c executor] |
| 1587 | + (assert-some c) |
| 1588 | + (shallow-chain-onto d #(.accept c %) executor)) |
1594 | 1589 |
|
| 1590 | +(defn- then-run [d ^Runnable f executor] |
| 1591 | + (assert-some f) |
| 1592 | + (shallow-chain-onto d (fn [_] (.run f)) executor)) |
1595 | 1593 |
|
1596 | | -(defn- then-combine [d other ^BiFunction f] |
| 1594 | +(defn- then-combine [d other ^BiFunction f executor] |
1597 | 1595 | (assert-some other f) |
1598 | | - (fmap-deferred (zip d other) |
1599 | | - (fn [[x y]] (.apply f x y)))) |
1600 | | - |
1601 | | -(def-async-for-dual then-combine) |
| 1596 | + (shallow-chain-onto (zip d other) |
| 1597 | + (fn [[x y]] (.apply f x y)) |
| 1598 | + executor)) |
1602 | 1599 |
|
1603 | | - |
1604 | | -(defn- then-accept-both [d other ^BiConsumer f] |
| 1600 | +(defn- then-accept-both [d other ^BiConsumer f executor] |
1605 | 1601 | (assert-some other f) |
1606 | | - (fmap-deferred (zip d other) |
1607 | | - (fn [[x y]] (.accept f x y)))) |
1608 | | - |
1609 | | -(def-async-for-dual then-accept-both) |
| 1602 | + (shallow-chain-onto (zip d other) |
| 1603 | + (fn [[x y]] (.accept f x y)) |
| 1604 | + executor)) |
1610 | 1605 |
|
1611 | | - |
1612 | | -(defn- run-after-both [d other ^Runnable f] |
| 1606 | +(defn- run-after-both [d other ^Runnable f executor] |
1613 | 1607 | (assert-some other f) |
1614 | | - (fmap-deferred (zip d other) |
1615 | | - (fn [[_ _]] (.run f)))) |
1616 | | - |
1617 | | - |
1618 | | -(def-async-for-dual run-after-both) |
| 1608 | + (shallow-chain-onto (zip d other) |
| 1609 | + (fn [[_ _]] (.run f)) |
| 1610 | + executor)) |
1619 | 1611 |
|
1620 | | - |
1621 | | -(defn- apply-to-either [d other ^Function f] |
| 1612 | +(defn- apply-to-either [d other ^Function f executor] |
1622 | 1613 | (assert-some other f) |
1623 | | - (then-apply (alt d other) f)) |
1624 | | - |
1625 | | -(def-async-for-dual apply-to-either) |
| 1614 | + (then-apply (alt d other) f executor)) |
1626 | 1615 |
|
1627 | | - |
1628 | | -(defn- accept-either [d other ^Function f] |
| 1616 | +(defn- accept-either [d other ^Function f executor] |
1629 | 1617 | (assert-some other f) |
1630 | | - (then-accept (alt d other) f)) |
1631 | | - |
1632 | | -(def-async-for-dual accept-either) |
1633 | | - |
| 1618 | + (then-accept (alt d other) f executor)) |
1634 | 1619 |
|
1635 | | -(defn- run-after-either [d other ^Function f] |
| 1620 | +(defn- run-after-either [d other ^Function f executor] |
1636 | 1621 | (assert-some other f) |
1637 | | - (then-run (alt d other) f)) |
1638 | | - |
1639 | | -(def-async-for-dual run-after-either) |
1640 | | - |
| 1622 | + (then-run (alt d other) f executor)) |
1641 | 1623 |
|
1642 | | -(defn- then-compose [d ^Function f] |
| 1624 | +(defn- then-compose [d ^Function f executor] |
1643 | 1625 | (assert-some f) |
1644 | 1626 | (let [d' (deferred)] |
1645 | | - (on-realized d |
1646 | | - (fn [val] |
1647 | | - (on-realized (->deferred (.apply f val)) |
1648 | | - #(success! d' %) |
1649 | | - #(error! d' %))) |
1650 | | - (fn [error] (error! d' error))) |
| 1627 | + (-> (shallow-chain-onto d #(->deferred (.apply f %)) executor) |
| 1628 | + (on-realized (fn [fd] |
| 1629 | + (shallow-connect fd d')) |
| 1630 | + (fn [error] |
| 1631 | + (error! d' error)))) |
1651 | 1632 | d')) |
1652 | 1633 |
|
1653 | | -(def-async-for then-compose) |
1654 | | - |
1655 | | - |
1656 | | -(defn- then-handle [d ^BiFunction f] |
| 1634 | +(defn- then-handle [d ^BiFunction f executor] |
1657 | 1635 | (assert-some f) |
| 1636 | + ;; Can't use `shallow-chain-onto` here because it only covers the success case. |
1658 | 1637 | (let [d' (deferred)] |
1659 | 1638 | (on-realized |
1660 | | - d |
| 1639 | + (shallow-onto d executor) |
1661 | 1640 | (fn [val] (success! d' (.apply f val nil))) |
1662 | 1641 | (fn [error] (success! d' (.apply f nil error)))) |
1663 | 1642 | d')) |
1664 | 1643 |
|
1665 | | - |
1666 | | -(def-async-for then-handle) |
1667 | | - |
1668 | | - |
1669 | | -(defn- then-exceptionally [d ^Function f] |
| 1644 | +(defn- then-exceptionally [d ^Function f executor] |
1670 | 1645 | (assert-some f) |
| 1646 | + ;; Can't use `shallow-chain-onto` here because it only covers |
| 1647 | + ;; the success case. |
1671 | 1648 | (let [d' (deferred)] |
1672 | 1649 | (on-realized |
1673 | | - d |
1674 | | - (fn [val] (success! d' val)) |
1675 | | - (fn [error] (success! d' (.apply f error)))) |
| 1650 | + (shallow-onto d executor) |
| 1651 | + (fn [val] (success! d' val)) |
| 1652 | + (fn [error] (success! d' (.apply f error)))) |
1676 | 1653 | d')) |
1677 | 1654 |
|
1678 | 1655 | (defn- to-completable-future [d] |
|
1685 | 1662 |
|
1686 | 1663 | result)) |
1687 | 1664 |
|
1688 | | -(defn- when-complete [d ^BiConsumer f] |
| 1665 | +(defn- when-complete [d ^BiConsumer f executor] |
1689 | 1666 | (assert-some f) |
| 1667 | + ;; Can't use `shallow-chain-onto` here because it only covers |
| 1668 | + ;; the success case. |
1690 | 1669 | (let [d' (deferred)] |
1691 | | - (on-realized d |
| 1670 | + (on-realized (shallow-onto d executor) |
1692 | 1671 | (fn [val] |
1693 | 1672 | (try (.accept f val nil) |
1694 | 1673 | (success! d' val) |
|
1701 | 1680 | (error! d' err))))) |
1702 | 1681 | d')) |
1703 | 1682 |
|
1704 | | -(def-async-for when-complete) |
1705 | | - |
1706 | 1683 | ;;; |
1707 | 1684 |
|
1708 | 1685 | (alter-meta! #'->Deferred assoc :private true) |
|
0 commit comments