Polyfills - HTTP203

  Рет қаралды 21,644

Chrome for Developers

Chrome for Developers

Күн бұрын

Пікірлер: 49
@Cuddlehead
@Cuddlehead 3 жыл бұрын
nothing quite as humbling as watching these guys have a simple conversation
@HughGuiney
@HughGuiney 6 жыл бұрын
What on earth is a TransformStream?? Google devs live in the year 3000…
@evandroLG2
@evandroLG2 2 жыл бұрын
Still today it is a super interesting discussion guys! Thanks a million!
@googleplexer
@googleplexer 5 жыл бұрын
This is the best podcast I've ever seen. 🤘
@zyst6
@zyst6 6 жыл бұрын
Cool! I dig the code examples, they really helped this episode.
@ViniciusRocha-bl1lk
@ViniciusRocha-bl1lk 6 жыл бұрын
Thank you guys! I was thinking about conditional polyfills these days. Keep using code snippets please.
@JorgeRafaelNogueras
@JorgeRafaelNogueras 6 жыл бұрын
Thanks for the great video! I have a question about the solution you present at 7:35: doesn't that implementation mean that when you do a "new (await TransportStreamPromise)" in your code, you'll have to retrieve the polyfill module asynchronously every single time (even if you had already done so previously)?
@dassurma
@dassurma 6 жыл бұрын
No, once the promise is settled, it keeps its value. The promise body is only executed once.
@JorgeRafaelNogueras
@JorgeRafaelNogueras 6 жыл бұрын
Oh, I see: so you get memoization for free. Neat! Thanks for the info and for making this series. By the way, I really like the longer videos with code samples, like this one: keep up the good work! :-)
@petecapecod
@petecapecod 6 жыл бұрын
You guys are wicked smart!! Great video love the code shots
@MaxArt2501
@MaxArt2501 6 жыл бұрын
I find it weird to create a promise with `Promise.resolve().then` rather than `new Promise`. It looks like an abuse to avoid using `new`. Is there a specific reason to this?
@dassurma
@dassurma 6 жыл бұрын
I mostly used this because I wanted to use an early return. With the constructor pattern I’d have to use `return resolve(...)` which would have been arguably more confusing. But it’s not a big difference, so feel free to use `new Promise()` instead :D
@freund17
@freund17 6 жыл бұрын
Additionally, you couldn't make the constuctor-function async - which is missing in the example by the way - which would prevent you from using "await". More so: thrown exceptions in the "then"-function automatically get converted to rejected promises; while I think this isn't the case for the constructor-function...
@malipetek
@malipetek 6 жыл бұрын
what about every library loads polyfills on demand and dispatch a event on global containing loaded module with its version and stuff. Other libraries then first attach event listeners when loading a polyfill on demand first look for loaded one and repeat same.
@snapstromegon
@snapstromegon 6 жыл бұрын
What about the following approach? self.TransformStream = TransformStream || (await import('/streams-module.js')); You can use this in an async iife (like some examples in the video) and it's short and only imports the polyfill, if it's needed. I know, that it's not the most beautiful code, but for most cases it should be absolutely fine and easy to read.
@DenisTRUFFAUT
@DenisTRUFFAUT 6 жыл бұрын
I like it !
@pavelagarkov2634
@pavelagarkov2634 6 жыл бұрын
It would throw an error 'Uncaught ReferenceError: TransformStream is not defined' in browsers that don't know about TransformStream
@DenisTRUFFAUT
@DenisTRUFFAUT 6 жыл бұрын
self.TransformStream = 'TransformStream' in self ? TransformStream : await import('/streams-module.js');
@neovov
@neovov 6 жыл бұрын
You're still overriding the global which is bad. Also, it won't work because the import statement will return an object you usually would deconstruct (either const {default: TransformStream} or {TransformStream}))
@jakearchibald
@jakearchibald 6 жыл бұрын
Having a property that's a promise or something else is unusual. It'd be better to wrap TransformStream in Promise.resolve. *Edit:* I misread the code above, see my later comment.
@MarkJKellett
@MarkJKellett 6 жыл бұрын
Can you give more examples of using Streams to solve some real world problems? For me at least, some use cases don't immediately jump to mind.
@dandan7884
@dandan7884 6 жыл бұрын
i thought you would talk about a server side + client side solution that may serve the polifyll version of an API in the first request and also catch errors on the client side, in case the browser actually doesnt support that API. this is hard stuff :( SyntaxError at 07:00, unexpected token await
@dassurma
@dassurma 6 жыл бұрын
Eh, you are right. The `then` callback should have been async! The problem with server-side loading is that you would have to do UA sniffing, which always feels brittle and costs more in case it goes wrong. But it’s definitely a valid alternative solution!
@travelmoustache
@travelmoustache 6 жыл бұрын
Can you make a blog post for this please? Fantastic idea, we should do some library for easier implementation maybe?
@neovov
@neovov 6 жыл бұрын
I have mixed feelings about this video. On one hand you explain an issue and propose a fairly elegant solution ; on the other hand I feel that it lack some depth. I think some people may be lost with ‘self’ (if you haven’t had the chance to build a Worker you probably don’t have a clue of what it is), TransformStream (honestly, who have used it yet ? BTW, I’m interested in what you did with a TransformStream) and even async/await and Promise to an extend. I think there is room for another show where you too explain in depth one concept, with examples and slowly (you guys talks quite quickly ; for foreigners it may be hard to understand). Anyway, I would nitpick about one point: polyfill-ing isn’t always black and white. If you take the example of ReadableStream it is available in almost all browsers (*coughing* Firefox ?) but yet, their implementation are sparse, none support BYOB for example (Cache.matchAll is another example). Still, we can use your solution with multiple checks depending on the need when we want to use our API. Also, I don’t quite agree about TransformStream being used asynchronously ; most of the time the pipe is constructed in a synchronous function, converting to an async function might be challenging sometimes.
@SalmanAbbas007
@SalmanAbbas007 6 жыл бұрын
I get what you're saying but my solution would support all browsers: If (!('TransformSteam' in window)) { document.write('
@dassurma
@dassurma 6 жыл бұрын
Except that browsers are blocking document.write() on 2G connections. If you wanna stay in script-tag land (which is totally fine!), then create the script tag dynamically and defer your code until `onload` has fired on the polyfill. This episode was specifically about ES6 modules, tho.
@lucrativelepton
@lucrativelepton 6 жыл бұрын
Dynamic imports cannot be used in normal script tags, in Firefox. The entire script refuses to run 🙄 such a PITA...
@raulcalvomartin2979
@raulcalvomartin2979 6 жыл бұрын
What about loading the promise polyfill? Just kidding ;-) that’s a great idea thanks for sharing
@kosamari
@kosamari 6 жыл бұрын
WHAT IS THE JOKE ABOUT SOY MILK TELL ME
@DenisTRUFFAUT
@DenisTRUFFAUT 6 жыл бұрын
I don't understand. Milk seems polyfilled, but not conditionally.
@Richienb
@Richienb 4 жыл бұрын
I think soy milk polyfills actual milk.
@devvx-fe-gu1d3d0g6
@devvx-fe-gu1d3d0g6 5 жыл бұрын
Why am I not surprised
@mathewmcleod9958
@mathewmcleod9958 5 жыл бұрын
Super useful!
@RafaelCouto
@RafaelCouto 6 жыл бұрын
This is how I would do it in the old days: var TransformStreamCallback = function (callback) { if ('TransformStream' in self) { return callback(); } var s = document.createElement('script'); s.type = 'text/javascript'; s.src = '/stream-module.js'; s.addEventListener('load', callback); document.getElementsByTagName('head')[0].appendChild(s); };
@atomize
@atomize 6 жыл бұрын
Imagine the code involved in a solution to the issue they discussed, dynamically loading polyfills, without promises. Yikes. Thx promises.
@425ryank
@425ryank 6 жыл бұрын
Polyfills? Pollyfills?
@business_max
@business_max 6 жыл бұрын
Noice Noice. Clean and Easy. Clean and Easy.
@loic.bertrand
@loic.bertrand 4 жыл бұрын
Soy milk 😍🥛
@griffadev
@griffadev 6 жыл бұрын
Spendin that Google money on allsaints jumpers 💲💲
Old vs New JavaScript - HTTP203
15:33
Chrome for Developers
Рет қаралды 41 М.
Cross-origin fetches - HTTP 203
23:42
Chrome for Developers
Рет қаралды 39 М.
10 years of coding in 13 minutes
13:28
Joma Tech
Рет қаралды 5 МЛН
JavaScript for-loops are… complicated - HTTP203
14:17
Chrome for Developers
Рет қаралды 106 М.
Push Notifications - HTTP 203
14:45
Chrome for Developers
Рет қаралды 22 М.
Four silly browser hacks - HTTP 203
21:01
Chrome for Developers
Рет қаралды 39 М.
Dear Game Developers, Stop Messing This Up!
22:19
Jonas Tyroller
Рет қаралды 775 М.
JavaScript proposals - HTTP203
12:47
Chrome for Developers
Рет қаралды 18 М.
Scope in JavaScript - HTTP 203
17:21
Chrome for Developers
Рет қаралды 27 М.
Writing Code That Runs FAST on a GPU
15:32
Low Level
Рет қаралды 573 М.