|
1513 | 1513 | (prefer-method print-method IDeferred IDeref)
|
1514 | 1514 |
|
1515 | 1515 |
|
1516 |
| -(defn- async-for |
1517 |
| - "Retuns a CompletionStage async version of the given java function" |
1518 |
| - [original] |
1519 |
| - (fn result |
1520 |
| - ([d f] |
1521 |
| - (result d f (or (ex/executor) (ex/execute-pool)))) |
1522 |
| - ([d f executor] |
1523 |
| - (original (onto d executor) f)))) |
1524 |
| - |
1525 |
| -(defn- async-for-dual |
1526 |
| - "Retuns a CompletionStage async version for the given two 2-deferred |
1527 |
| - java function" |
1528 |
| - [original] |
1529 |
| - (fn result |
1530 |
| - ([d other f] |
1531 |
| - (result d other f (or (ex/executor) (ex/execute-pool)))) |
1532 |
| - ([d other f executor] |
1533 |
| - (original (onto d executor) other f)))) |
| 1516 | +(defmacro ^:no-doc def-async-for |
| 1517 | + "Defines a CompletionStage async version of the function associated with |
| 1518 | + the given symbol." |
| 1519 | + [fn-name] |
| 1520 | + (let [async-name (symbol (str (name fn-name) "-async"))] |
| 1521 | + `(defn ~async-name |
| 1522 | + ([d# f#] |
| 1523 | + (~async-name d# f# (or (ex/executor) (ex/execute-pool)))) |
| 1524 | + ([d# f# executor#] |
| 1525 | + (~fn-name (onto d# executor#) f#))))) |
| 1526 | + |
| 1527 | +(defmacro ^:no-doc def-async-for-dual |
| 1528 | + "Defines a CompletionStage async version of the two-deferred |
| 1529 | + function associated with the given symbol." |
| 1530 | + [fn-name] |
| 1531 | + (let [async-name (symbol (str (name fn-name) "-async"))] |
| 1532 | + `(defn ~async-name |
| 1533 | + ([d# d2# f#] |
| 1534 | + (~async-name d# d2# f# (or (ex/executor) (ex/execute-pool)))) |
| 1535 | + ([d# d2# f# executor#] |
| 1536 | + (~fn-name (onto d# executor#) d2# f#))))) |
1534 | 1537 |
|
1535 | 1538 | (defmacro ^:no-doc assert-some
|
1536 | 1539 | "Throws NullPointerException if any of the arguments is null."
|
|
1567 | 1570 | (assert-some f)
|
1568 | 1571 | (map-deferred d #(.apply f %)))
|
1569 | 1572 |
|
1570 |
| -(def ^:private then-apply-async (async-for then-apply)) |
| 1573 | +(def-async-for then-apply) |
1571 | 1574 |
|
1572 | 1575 | (defn- then-accept [d ^Consumer c]
|
1573 | 1576 | (assert-some c)
|
1574 | 1577 | (map-deferred d #(.accept c %)))
|
1575 | 1578 |
|
1576 |
| -(def ^:private then-accept-async (async-for then-accept)) |
| 1579 | +(def-async-for then-accept) |
1577 | 1580 |
|
1578 | 1581 | (defn- then-run [d ^Runnable f]
|
1579 | 1582 | (assert-some f)
|
1580 | 1583 | (map-deferred d (fn [_] (.run f))))
|
1581 | 1584 |
|
1582 |
| -(def ^:private then-run-async (async-for then-run)) |
| 1585 | +(def-async-for then-run) |
1583 | 1586 |
|
1584 | 1587 |
|
1585 | 1588 | (defn- then-combine [d other ^BiFunction f]
|
1586 | 1589 | (assert-some other f)
|
1587 | 1590 | (map-deferred (zip d other)
|
1588 | 1591 | (fn [[x y]] (.apply f x y))))
|
1589 | 1592 |
|
1590 |
| -(def ^:private then-combine-async (async-for-dual then-combine)) |
| 1593 | +(def-async-for-dual then-combine) |
1591 | 1594 |
|
1592 | 1595 |
|
1593 | 1596 | (defn- then-accept-both [d other ^BiConsumer f]
|
1594 | 1597 | (assert-some other f)
|
1595 | 1598 | (map-deferred (zip d other)
|
1596 | 1599 | (fn [[x y]] (.accept f x y))))
|
1597 | 1600 |
|
1598 |
| -(def ^:private then-accept-both-async (async-for-dual then-accept-both)) |
| 1601 | +(def-async-for-dual then-accept-both) |
1599 | 1602 |
|
1600 | 1603 |
|
1601 | 1604 | (defn- run-after-both [d other ^Runnable f]
|
|
1604 | 1607 | (fn [[_ _]] (.run f))))
|
1605 | 1608 |
|
1606 | 1609 |
|
1607 |
| -(def ^:private run-after-both-async (async-for-dual run-after-both)) |
| 1610 | +(def-async-for-dual run-after-both) |
1608 | 1611 |
|
1609 | 1612 |
|
1610 | 1613 | (defn- apply-to-either [d other ^java.util.function.Function f]
|
1611 | 1614 | (assert-some other f)
|
1612 | 1615 | (then-apply (alt d other) f))
|
1613 | 1616 |
|
1614 |
| -(def ^:private apply-to-either-async (async-for-dual apply-to-either)) |
| 1617 | +(def-async-for-dual apply-to-either) |
1615 | 1618 |
|
1616 | 1619 |
|
1617 | 1620 | (defn- accept-either [d other ^java.util.function.Function f]
|
1618 | 1621 | (assert-some other f)
|
1619 | 1622 | (then-accept (alt d other) f))
|
1620 | 1623 |
|
1621 |
| -(def ^:private accept-either-async (async-for-dual accept-either)) |
| 1624 | +(def-async-for-dual accept-either) |
1622 | 1625 |
|
1623 | 1626 |
|
1624 | 1627 | (defn- run-after-either [d other ^java.util.function.Function f]
|
1625 | 1628 | (assert-some other f)
|
1626 | 1629 | (then-run (alt d other) f))
|
1627 | 1630 |
|
1628 |
| -(def ^:private run-after-either-async (async-for-dual run-after-either)) |
| 1631 | +(def-async-for-dual run-after-either) |
1629 | 1632 |
|
1630 | 1633 |
|
1631 | 1634 | (defn- then-compose [d ^java.util.function.Function f]
|
1632 | 1635 | (assert-some f)
|
1633 | 1636 | (mapcat-deferred d (fn [value] (.apply f value))))
|
1634 | 1637 |
|
1635 |
| -(def ^:private then-compose-async (async-for then-compose)) |
| 1638 | +(def-async-for then-compose) |
1636 | 1639 |
|
1637 | 1640 |
|
1638 | 1641 | (defn- then-handle [d ^java.util.function.BiFunction f]
|
|
1644 | 1647 | (fn [error] (success! d' (.apply f nil error))))
|
1645 | 1648 | d'))
|
1646 | 1649 |
|
1647 |
| -(def ^:private then-handle-async (async-for then-handle)) |
| 1650 | + |
| 1651 | +(def-async-for then-handle) |
1648 | 1652 |
|
1649 | 1653 |
|
1650 | 1654 | (defn- then-exceptionally [d ^java.util.function.Function f]
|
|
1682 | 1686 | (error! d' err)))))
|
1683 | 1687 | d'))
|
1684 | 1688 |
|
1685 |
| -(def ^:private when-complete-async (async-for when-complete)) |
| 1689 | +(def-async-for when-complete) |
1686 | 1690 |
|
1687 | 1691 | ;;;
|
1688 | 1692 |
|
|
0 commit comments