https://discord.cloudflare.com logo
Join Discord
Powered by
# durable-objects
  • j

    jed

    02/08/2021, 11:54 PM
    hmm, i'm not a TS user. 🤷‍♂️
  • g

    Greg Brimble | Cloudflare Pages

    02/08/2021, 11:54 PM
    Testing it now...
  • j

    jed

    02/09/2021, 12:00 AM
    (another thing that's nice is responses can be returned using either
    return
    or
    yield
    , so you can use existing callbacks as-is, just add an asterisk!)
  • k

    kenton

    02/09/2021, 12:02 AM
    huh that is interesting. I guess it means we'd have to check whether the handler returns a promise vs. an iterator and handle differently?
  • j

    jed

    02/09/2021, 12:05 AM
    yeah, would definitely need some more logic on your end, probably looking for
    Symbol.asyncIterator
    on the returned value.
  • k

    kenton

    02/09/2021, 12:06 AM
    it's very pleasingly elegant. Though in practice I frequently see cases where
    waitUntil()
    is being called by some deeply-nested function, requiring passing the
    event
    object down to it... I guess refactoring that sort of code to use this instead would be weird.
  • k

    kenton

    02/09/2021, 12:07 AM
    but maybe we could support both
  • k

    kenton

    02/09/2021, 12:07 AM
    we're diverging from the service worker spec now so we can do what we like... 🙂
  • j

    jed

    02/09/2021, 12:08 AM
    exactly. i think that API served you very well when workers was basically a proxy.
  • j

    jed

    02/09/2021, 12:08 AM
    but clearly it's getting a bit limiting.
  • g

    Greg Brimble | Cloudflare Pages

    02/09/2021, 12:09 AM
    Not immediately. Might need a bit of wangling. Seemingly despite what it says in the docs (https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-6.html), TS isn't immediately inferring the two types separately. It thinks that
    value
    could be either a Response or void.
  • j

    jed

    02/09/2021, 12:10 AM
    i also think async iterators could help usability on other parts of the platform, like KV/DO list operations, etc.
  • k

    kenton

    02/09/2021, 12:10 AM
    yeah the thing that really bothered me about Service Workers was the non-composability. You can't take two service workers and just "link" them together into one, unless they carefully handle disjoint URL patterns.
  • j

    jed

    02/09/2021, 12:10 AM
    i mean, classic DOM API story, really.
  • k

    kenton

    02/09/2021, 12:11 AM
    heh
  • j

    jed

    02/09/2021, 12:11 AM
    taken further, deno for example uses top-level async iterators.
  • j

    jed

    02/09/2021, 12:11 AM
    for await (let request of requests) { ... }
  • j

    jed

    02/09/2021, 12:12 AM
    (i'm not saying this makes sense for cloudflare workers, but definitely makes it easier to not have to define bespoke APIs)
  • k

    kenton

    02/09/2021, 12:12 AM
    that might be getting a little bit to cute for me. 🙂
  • j

    jed

    02/09/2021, 12:13 AM
    it makes more sense when you own the logic for starting the server.
  • j

    jed

    02/09/2021, 12:13 AM
    i agree it's not good for your workers.
  • k

    kenton

    02/09/2021, 12:13 AM
    but how do you refactor it to handle multiple kinds of requests?
  • j

    jed

    02/09/2021, 12:14 AM
    multiple async iterators, one per type i guess?
  • k

    kenton

    02/09/2021, 12:14 AM
    I guess you just have an async function for each request type, and then call them all...
  • g

    Greg Brimble | Cloudflare Pages

    02/09/2021, 12:14 AM
    Used them quite a lot for listing all the keys in KV: https://github.com/cloudflare/workers-types/issues/32#issuecomment-633119231
  • j

    jed

    02/09/2021, 12:14 AM
    but then your API would be ugly, because you'd have to wrap each in an IIFE to prevent blocking, i guess.
  • k

    kenton

    02/09/2021, 12:14 AM
    yeah
  • j

    jed

    02/09/2021, 12:15 AM
    one thing that's kinda weird about the CF module implementation is that you use both default and named exports.
  • j

    jed

    02/09/2021, 12:15 AM
    ie,
    export {MyDurableObject}
    but also
    export default {fetch}
    .
  • j

    jed

    02/09/2021, 12:17 AM
    kinda makes the actual output API harder to grok, since i'm not sure too many folks actually know the difference.
1...121314...567Latest