|
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 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 (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." |
@@ -1538,157 +1545,129 @@ |
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 %))) |
| 1570 | +(defn- completion-stage-result [d f to] |
| 1571 | + (when to |
| 1572 | + (shallow-connect d to)) |
| 1573 | + (shallow-chain (or to d) f)) |
1580 | 1574 |
|
1581 | | -(def-async-for then-apply) |
| 1575 | +(defn- then-apply [d ^Function f to] |
| 1576 | + (assert-some f) |
| 1577 | + (completion-stage-result d #(.apply f %) to)) |
1582 | 1578 |
|
1583 | | -(defn- then-accept [d ^Consumer c] |
| 1579 | +(defn- then-accept [d ^Consumer c to] |
1584 | 1580 | (assert-some c) |
1585 | | - (fmap-deferred d #(.accept c %))) |
1586 | | - |
1587 | | -(def-async-for then-accept) |
| 1581 | + (completion-stage-result d #(.accept c %) to)) |
1588 | 1582 |
|
1589 | | -(defn- then-run [d ^Runnable f] |
| 1583 | +(defn- then-run [d ^Runnable f to] |
1590 | 1584 | (assert-some f) |
1591 | | - (fmap-deferred d (fn [_] (.run f)))) |
1592 | | - |
1593 | | -(def-async-for then-run) |
| 1585 | + (completion-stage-result d (fn [_] (.run f)) to)) |
1594 | 1586 |
|
1595 | | - |
1596 | | -(defn- then-combine [d other ^BiFunction f] |
| 1587 | +(defn- then-combine [d other ^BiFunction f to] |
1597 | 1588 | (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) |
| 1589 | + (completion-stage-result (zip d other) |
| 1590 | + (fn [[x y]] (.apply f x y)) |
| 1591 | + to)) |
1602 | 1592 |
|
1603 | | - |
1604 | | -(defn- then-accept-both [d other ^BiConsumer f] |
| 1593 | +(defn- then-accept-both [d other ^BiConsumer f to] |
1605 | 1594 | (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) |
| 1595 | + (completion-stage-result (zip d other) |
| 1596 | + (fn [[x y]] (.accept f x y)) |
| 1597 | + to)) |
1610 | 1598 |
|
1611 | | - |
1612 | | -(defn- run-after-both [d other ^Runnable f] |
| 1599 | +(defn- run-after-both [d other ^Runnable f to] |
1613 | 1600 | (assert-some other f) |
1614 | | - (fmap-deferred (zip d other) |
1615 | | - (fn [[_ _]] (.run f)))) |
1616 | | - |
| 1601 | + (completion-stage-result (zip d other) |
| 1602 | + (fn [[_ _]] (.run f)) |
| 1603 | + to)) |
1617 | 1604 |
|
1618 | | -(def-async-for-dual run-after-both) |
1619 | | - |
1620 | | - |
1621 | | -(defn- apply-to-either [d other ^Function f] |
| 1605 | +(defn- apply-to-either [d other ^Function f to] |
1622 | 1606 | (assert-some other f) |
1623 | | - (then-apply (alt d other) f)) |
1624 | | - |
1625 | | -(def-async-for-dual apply-to-either) |
| 1607 | + (then-apply (alt d other) f to)) |
1626 | 1608 |
|
1627 | | - |
1628 | | -(defn- accept-either [d other ^Function f] |
| 1609 | +(defn- accept-either [d other ^Function f to] |
1629 | 1610 | (assert-some other f) |
1630 | | - (then-accept (alt d other) f)) |
1631 | | - |
1632 | | -(def-async-for-dual accept-either) |
| 1611 | + (then-accept (alt d other) f to)) |
1633 | 1612 |
|
1634 | | - |
1635 | | -(defn- run-after-either [d other ^Function f] |
| 1613 | +(defn- run-after-either [d other ^Function f to] |
1636 | 1614 | (assert-some other f) |
1637 | | - (then-run (alt d other) f)) |
1638 | | - |
1639 | | -(def-async-for-dual run-after-either) |
| 1615 | + (then-run (alt d other) f to)) |
1640 | 1616 |
|
1641 | | - |
1642 | | -(defn- then-compose [d ^Function f] |
| 1617 | +(defn- then-compose [d ^Function f to] |
1643 | 1618 | (assert-some f) |
1644 | 1619 | (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))) |
| 1620 | + (-> (completion-stage-result d #(->deferred (.apply f %)) to) |
| 1621 | + (on-realized (fn [fd] |
| 1622 | + (shallow-connect fd d')) |
| 1623 | + (fn [error] |
| 1624 | + (error! d' error)))) |
1651 | 1625 | d')) |
1652 | 1626 |
|
1653 | | -(def-async-for then-compose) |
1654 | | - |
1655 | | - |
1656 | | -(defn- then-handle [d ^BiFunction f] |
| 1627 | +(defn- then-handle [d ^BiFunction f to] |
1657 | 1628 | (assert-some f) |
| 1629 | + ;; Can't use `completion-stage-result` here because it only covers |
| 1630 | + ;; the success case. |
| 1631 | + (when to |
| 1632 | + (shallow-connect d to)) |
1658 | 1633 | (let [d' (deferred)] |
1659 | 1634 | (on-realized |
1660 | | - d |
| 1635 | + (or to d) |
1661 | 1636 | (fn [val] (success! d' (.apply f val nil))) |
1662 | 1637 | (fn [error] (success! d' (.apply f nil error)))) |
1663 | 1638 | d')) |
1664 | 1639 |
|
1665 | | - |
1666 | | -(def-async-for then-handle) |
1667 | | - |
1668 | | - |
1669 | | -(defn- then-exceptionally [d ^Function f] |
| 1640 | +(defn- then-exceptionally [d ^Function f to] |
1670 | 1641 | (assert-some f) |
| 1642 | + ;; Can't use `completion-stage-result` here because it only covers |
| 1643 | + ;; the success case. |
| 1644 | + (when to |
| 1645 | + (shallow-connect d to)) |
1671 | 1646 | (let [d' (deferred)] |
1672 | 1647 | (on-realized |
1673 | | - d |
1674 | | - (fn [val] (success! d' val)) |
1675 | | - (fn [error] (success! d' (.apply f error)))) |
| 1648 | + (or to d) |
| 1649 | + (fn [val] (success! d' val)) |
| 1650 | + (fn [error] (success! d' (.apply f error)))) |
1676 | 1651 | d')) |
1677 | 1652 |
|
1678 | 1653 | (defn- to-completable-future [d] |
1679 | 1654 |
|
1680 | | - (let [result (CompletableFuture.)] |
| 1655 | + (let [to (CompletableFuture.)] |
1681 | 1656 |
|
1682 | 1657 | (on-realized d |
1683 | | - #(.complete result %) |
1684 | | - #(.completeExceptionally result %)) |
| 1658 | + #(.complete to %) |
| 1659 | + #(.completeExceptionally to %)) |
1685 | 1660 |
|
1686 | | - result)) |
| 1661 | + to)) |
1687 | 1662 |
|
1688 | | -(defn- when-complete [d ^BiConsumer f] |
| 1663 | +(defn- when-complete [d ^BiConsumer f to] |
1689 | 1664 | (assert-some f) |
| 1665 | + ;; Can't use `completion-stage-result` here because it only covers |
| 1666 | + ;; the success case. |
| 1667 | + (when to |
| 1668 | + (shallow-connect d to)) |
1690 | 1669 | (let [d' (deferred)] |
1691 | | - (on-realized d |
| 1670 | + (on-realized (or to d) |
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