Can JS Proxies do "method missing"?

Поделиться
HTML-код
  • Опубликовано: 8 сен 2024

Комментарии • 25

  • @andrew-burgess
    @andrew-burgess  Месяц назад +13

    I use TypeScript in this example just because it's easier to be explicit about return types, expectations, etc ... and because I enjoy TS! However, proxies are widely available in JS, and you don't need to be using TypeScript to give this a try. I guess you won't get the editor hints for the `find` functions, but everything else will work.

  • @rak3shpai
    @rak3shpai Месяц назад +22

    This is how tRPC works. You define functions on the server, and tRPC uses proxies to make it seem like it's available on the client, with type-safety and all, and internally handles serialising the call over HTTP. It's pretty neat.

  • @TheProcessor
    @TheProcessor Месяц назад +4

    immer is a fantastic example of Proxy usage

  • @JarheadCrayonEater
    @JarheadCrayonEater Месяц назад

    Nice!
    Years ago I worked for an employer that was creating metrology lab automation software, and I was responsible for the project since I'm a former calibration tech, metrologist, and dev for 35+ years.
    The project was using node-red to allow end-users to create automated calibration procedures that would either ask the user for input during the calibration or directly acquire the data from hardware, perform some checks on or calculations on the data, make a pass/fail decision, and store the result(s).
    However, node-red was not 100% up to the task, since there are fundamental parts of the internals that they don't expose. So, I started by forking node-red and customizing it to our liking, but that turned into a maintenance nightmare, and we quickly realized that altering node-reds behavior at runtime was the best approach.
    So, I started using Proxies to trap internal node-red calls, altering either the data going in, out, or sometimes both, depending on the needs.
    Anyway, thanks for bringing up this somewhat lost part of JS!

  • @ryandsouza2962
    @ryandsouza2962 29 дней назад

    Great vid! I love proxies and I use them in Vanilla JS/TS for two-way data binding

  • @paoloricciuti
    @paoloricciuti Месяц назад +2

    Trpc is using proxies... specifically recursice proxies to keep track of all the nested property access you do and convert that to an id that is passed to the trpc router to call the proper method on it. Pretty interesting

  • @0xtz_
    @0xtz_ Месяц назад +1

    wow 😯, that's coool man
    we need more videos like this with Ts ofc 😂

  • @Douwab
    @Douwab Месяц назад +1

    Like always, very interesting video and really nice and fun usage of Proxies imo! :)

  • @forinda
    @forinda Месяц назад +3

    Vuejs objects are almost all proxies

  • @Michaeljamieson10
    @Michaeljamieson10 Месяц назад

    legend state for react and react native uses proxies

  • @danielsharp2402
    @danielsharp2402 Месяц назад

    For anything that isn't a getter/setter (maybe getter and setter as well, not sure how that interacts with Object.keys) you can just map over the keys and create the object yourself or am I missing something?

  • @azizsafudin
    @azizsafudin Месяц назад

    Valtio uses proxies extensively to manage state!

  • @AppleGameification
    @AppleGameification Месяц назад

    Huh, I've never looked into proxies before but I think I recognise this as what Vue uses to implement reactivity when you change a value in an object?

    • @wobsoriano
      @wobsoriano Месяц назад +1

      Yeah, Vue 3 uses it.

  • @phamiersc2179
    @phamiersc2179 Месяц назад +1

    VueJS uses proxies!

    • @phamiersc2179
      @phamiersc2179 Месяц назад +1

      Everything state related in Vue is a proxy, that's why you do not need to use callbacks for mutation in Vue, you just mutate state directly, render micro task is created automatically

  • @parlor3115
    @parlor3115 Месяц назад

    Unless you're developing a library with very tight user experience constraints, I don't think proxies should be touched. It's the worse thing for readability, I've ever interacted with in JS

  • @Guergeiro
    @Guergeiro Месяц назад +2

    Proxies are actually a design pattern. It always felt weird to me that the language has a construct that represents a pattern, and feels badly implemented for consuming it honestly. Why JS had to do this? Don't know, but I think implementing the pattern yourself is way cleaner and easy to follow than using the native construct.

    • @andrew-burgess
      @andrew-burgess  Месяц назад

      Hmm, interesting take! Do you have an example of implementing this manually instead of using the native Proxy?
      Thanks for watching & commenting!

    • @Guergeiro
      @Guergeiro Месяц назад

      @@andrew-burgess I posted a comment that had a TS playground that for some reason got deleted...

    • @AngelHdzMultimedia
      @AngelHdzMultimedia Месяц назад

      @@Guergeiro RUclips marks comments with URLs as spam... :(

    • @andrew-burgess
      @andrew-burgess  29 дней назад

      @@Guergeiro ah, sorry, I can't control that youtube behaviour. If you still have the link, wanna email it to me? you can find my email on my website, linked in the video description.

  • @edgeeffect
    @edgeeffect Месяц назад

    I've come across Active Record and Method Missing multiple times in Ruby and Laravel and I've got to say, they're the work of the devil himself.

  • @meyou118
    @meyou118 Месяц назад

    nice. very clever. another example why AI can ever replace human ingenuity.