Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Blinkz0rz
May 27, 2001

MY CONTEMPT FOR MY OWN EMPLOYEES IS ONLY MATCHED BY MY LOVE FOR TOM BRADY'S SWEATY MAGA BALLS

Raskolnikov2089 posted:

this guy lints with airbnb

:shrug:

Adbot
ADBOT LOVES YOU

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
Why does eslint "recommend" 2 space tabbing? Eslint's rules are insane based on that.

Raskolnikov2089
Nov 3, 2006

Schizzy to the matic

Nolgthorn posted:

Why does eslint "recommend" 2 space tabbing? Eslint's rules are insane based on that.

I've recently learned that bitbucket is a giant pile of poo poo and has trouble with spaces and tabs.

smackfu
Jun 7, 2004

Nolgthorn posted:

Why does eslint "recommend" 2 space tabbing? Eslint's rules are insane based on that.

Of course you can change the default rules but what’s the problem with it?

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
4 space tabbing makes the depth and thus scope generally clearer. 2 spaces is one tiny char different from an actual space. 4 spaces helps disincentivise callback hell and thus promotes generally cleaner coding practices. It's easier to read. 4 spaces instead of two isn't best for all languages but I'd say it's dominant in Javascript for good reason.

Why isn't it the default?

lunar detritus
May 6, 2009


Nolgthorn posted:

4 spaces instead of two isn't best for all languages but I'd say it's dominant in Javascript for good reason.

Why isn't it the default?

I really need a citation for that. I have always seen two spaces as the dominant one and a lot of the most popular styleguides (airbnb, standardjs, google) have it as the default. This guy even analyzed a lot of npm packages and the majority uses 2 spaces.

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
Well I'll be damned.

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
Here is what my screen looks like with a new file open that I'm working on for about 15 minutes. I have placed my first indented line, it's 4 spaces and it already looks practically cramped. I'm not sure how people can stand it being two spaces.

https://imgur.com/a/gN1D7Ld

Happy Thread
Jul 10, 2005

by Fluffdaddy
Plaster Town Cop
Check this out: newline, one space, your open brace, newline, two more spaces, start typing the definition

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
Thank god for insignificant whitespace so things like that are possible.

TheFluff
Dec 13, 2006

FRIENDS, LISTEN TO ME
I AM A SEAGULL
OF WEALTH AND TASTE
At work we use 4 spaces for Python and 2 for JS, and it's fine. Prior to that I used to use 4 for everything for ages. These days if I'm free to choose on my own, I'd probably be more inclined to use 2 for everything; it's easy enough to read once you get used to it and it wastes less space. If you use 4 you definitely need to allow longer lines than you otherwise would, though - our Python linting uses the default PEP8 80 column limit because nobody has gotten around to changing that, and a lot of poo poo that really shouldn't have to be multiple lines ends up having to be wrapped, which hurts readability quite a bit.

Also if you're using any kind of frontend JS that mixes HTML into your JS components in some way (be it Vue components or React JSX or whatever) you really want to be using 2 spaces.

LifeLynx
Feb 27, 2001

Dang so this is like looking over his shoulder in real-time
Grimey Drawer
This is driving me nuts. It's so simple in jQuery, but I can't find a solution in vanilla JS. I have a menu like this:

code:

<ul id="navigation">
<li><a href="#linkone">Link One</a></li>
<li><a href="#linktwo">Link Two</a></li>
<li><a href="#linkthree">Link Three</a></li>
</ul>

I want something to happen when someone clicks on the A tag. I've tried this:

code:
const navLink = document.querySelectorAll('.astmenu li a');
navLink.addEventListener( 'click', () => {
console.log('Button clicked');
});
Vanilla JS has all these different methods of finding a specific element, whereas jQuery just has:

code:
$('#navigation').children('a').click(function(){
    // code here
});
...so I don't know how to do exactly what I want in vanilla JS.

spiritual bypass
Feb 19, 2008

Grimey Drawer
That selector doesn't appear to be in your markup

huhu
Feb 24, 2006

LifeLynx posted:

This is driving me nuts. It's so simple in jQuery, but I can't find a solution in vanilla JS. I have a menu like this:

code:

<ul id="navigation">
<li><a href="#linkone">Link One</a></li>
<li><a href="#linktwo">Link Two</a></li>
<li><a href="#linkthree">Link Three</a></li>
</ul>

I want something to happen when someone clicks on the A tag. I've tried this:

code:
const navLink = document.querySelectorAll('.astmenu li a');
navLink.addEventListener( 'click', () => {
console.log('Button clicked');
});
Vanilla JS has all these different methods of finding a specific element, whereas jQuery just has:

code:
$('#navigation').children('a').click(function(){
    // code here
});
...so I don't know how to do exactly what I want in vanilla JS.

https://jsfiddle.net/0wrnov5d/

LifeLynx
Feb 27, 2001

Dang so this is like looking over his shoulder in real-time
Grimey Drawer

Happy New Year! :toot:

Happy Thread
Jul 10, 2005

by Fluffdaddy
Plaster Town Cop
You're gonna be glad in the long run, you can get a much more complete understanding the vanilla way, and be sure there's nothing lurking under the hood to demystify later if there's a problem

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
I (unpopularly) believe 4 spaces are better than 2 but I also think jQuery is stupid and much more complicated than vanilla js. It comes from a time when something was needed to enforce compatibility between browsers and has been obsolete for nearly a decade.

Cugel the Clever
Apr 5, 2009
I LOVE AMERICA AND CAPITALISM DESPITE BEING POOR AS FUCK. I WILL NEVER RETIRE BUT HERE'S ANOTHER 200$ FOR UKRAINE, SLAVA

Nolgthorn posted:

I (unpopularly) believe 4 spaces are better than 2 but I also think jQuery is stupid and much more complicated than vanilla js. It comes from a time when something was needed to enforce compatibility between browsers and has been obsolete for nearly a decade.
jQuery being unnecessary has been the popular consensus for a few years now. It does have some legitimate use case if you need to support older browsers, but the remaining proponents of it are mainly curmudgeonly devs who have deliberately and vociferously avoided learning anything new in the last six years (like a former coworker of mine!).

porksmash
Sep 30, 2008
Typescript question - if I have a type defined as a list of specific strings, is there a way to create an interface that has optional keys for each possible value in that type? Example

code:
type possibleThings = 'all' | 'some' | 'none';

interface shapeIWant {
  all?: any;
  some?: any;
  none?: any;
}
but I don't want to manually keep these two in sync.

LifeLynx
Feb 27, 2001

Dang so this is like looking over his shoulder in real-time
Grimey Drawer

Cugel the Clever posted:

jQuery being unnecessary has been the popular consensus for a few years now. It does have some legitimate use case if you need to support older browsers, but the remaining proponents of it are mainly curmudgeonly devs who have deliberately and vociferously avoided learning anything new in the last six years (like a former coworker of mine!).

I haven't written anything in jQuery for a while now, but I still used it in the form of scripts I was already comfortable with. For a while it was hard to find answers to how to do things that weren't written in jQuery, but it's getting easier. Finding the proper way to reference an element is one thing jQuery does simpler - just a dollar sign and then write the #id or .class in parentheses and then call a function or whatever - but there's probably reasons for why there's different ways to find different things in vanilla JS.

Happy Thread
Jul 10, 2005

by Fluffdaddy
Plaster Town Cop
It's not hard to find how to do things in vanilla JS, there's the docs and language specification for that. JQuery-specific answers on the web are a plague and it only makes it worse when more people choose to adopt it because of that

Bruegels Fuckbooks
Sep 14, 2004

Now, listen - I know the two of you are very different from each other in a lot of ways, but you have to understand that as far as Grandpa's concerned, you're both pieces of shit! Yeah. I can prove it mathematically.

porksmash posted:

Typescript question - if I have a type defined as a list of specific strings, is there a way to create an interface that has optional keys for each possible value in that type? Example

code:
type possibleThings = 'all' | 'some' | 'none';

interface shapeIWant {
  all?: any;
  some?: any;
  none?: any;
}
but I don't want to manually keep these two in sync.

how about :
code:
type possibleThings = 'all' | 'some' | 'none';

interface shapeIWant {
  all?: possibleThings;
  some?: possibleThings;
  none?: possibleThings;
}

Emergency Mittens
Mar 20, 2010

porksmash posted:

Typescript question - if I have a type defined as a list of specific strings, is there a way to create an interface that has optional keys for each possible value in that type?

https://github.com/Microsoft/TypeScript-Handbook/blob/master/pages/Advanced%20Types.md#mapped-types

Slimy Hog
Apr 22, 2008


Yeah, mapped types is what you want. I ran into this at work recently and found a few good resources, I could dig them up if this doesn't cut it for ya.

LifeLynx
Feb 27, 2001

Dang so this is like looking over his shoulder in real-time
Grimey Drawer
I was happy with this script I wrote (maybe not entirely from an efficiency standpoint) until I realized it doesn't work in any iOS browser. It doesn't work in IE11 either, but I don't mind that too much.

code:
<script>

document.addEventListener("DOMContentLoaded", function(event) { 
const scrollPastTargetNo = document.querySelectorAll('.anim-target');
observer = new IntersectionObserver(entries => {
  entries.forEach(entry => {
    if (entry.isIntersecting) {
			if (entry.target.classList.contains('anim-target-fadeIn')) {
      	entry.target.classList.add('animated', 'fadeIn');
			}
			if (entry.target.classList.contains('anim-target-fadeInUp')) {
      	entry.target.classList.add('animated', 'fadeInUp');
			}
			if (entry.target.classList.contains('anim-target-fadeInDown')) {
      	entry.target.classList.add('animated', 'fadeInDown');
			}
			if (entry.target.classList.contains('anim-target-bounceIn')) {
      	entry.target.classList.add('animated', 'bounceIn');
			}
			if (entry.target.classList.contains('anim-target-zoomIn')) {
      	entry.target.classList.add('animated', 'zoomIn');
			}
			if (entry.target.classList.contains('anim-target-slideInLeft')) {
      	entry.target.classList.add('animated', 'slideInLeft');
			}
			console.log('Animate target activated');
		}
		else {
			entry.target.classList.remove('animated', 'zoomIn', 'fadeIn', 'fadeInUp', 'bounceIn', 'slideInLeft');
			entry.target.style.opacity = 0;
      //observer.unobserve(entry.target);
      console.log('Animate target deactivated');
		}
  });
});

scrollPastTargetNo.forEach(image => {
  observer.observe(image);
});

});
</script>
Is it possible to tweak this slightly so it works without IntersectionObserver, which is the reason it's not behaving nicely with Safari on iOS?

Cugel the Clever
Apr 5, 2009
I LOVE AMERICA AND CAPITALISM DESPITE BEING POOR AS FUCK. I WILL NEVER RETIRE BUT HERE'S ANOTHER 200$ FOR UKRAINE, SLAVA

LifeLynx posted:

I was happy with this script I wrote (maybe not entirely from an efficiency standpoint) until I realized it doesn't work in any iOS browser. It doesn't work in IE11 either, but I don't mind that too much.

Is it possible to tweak this slightly so it works without IntersectionObserver, which is the reason it's not behaving nicely with Safari on iOS?
Probably best to just go with the polyfill. The IntersectionObserver API is a massive improvement performance-wise of the same functionality in plain JS.

LifeLynx
Feb 27, 2001

Dang so this is like looking over his shoulder in real-time
Grimey Drawer

Cugel the Clever posted:

Probably best to just go with the polyfill. The IntersectionObserver API is a massive improvement performance-wise of the same functionality in plain JS.

I don't know what polyfill is but apparently it's coded in some kind of magic runes that make old browsers behave like modern ones? It worked like a charm, thank you!

Cugel the Clever
Apr 5, 2009
I LOVE AMERICA AND CAPITALISM DESPITE BEING POOR AS FUCK. I WILL NEVER RETIRE BUT HERE'S ANOTHER 200$ FOR UKRAINE, SLAVA

LifeLynx posted:

I don't know what polyfill is but apparently it's coded in some kind of magic runes that make old browsers behave like modern ones? It worked like a charm, thank you!
Basically, it's an equivalent implementation in plain JS that shouldn't override the actual implementation if the browser supports it. A good way to make use of fancy new things without sacrificing support for archaic/laggardly JavaScript engines.

That said, it comes with the tradeoffs of having none of the performance benefits of the fancy new thing and being ___kb of extra code shipped to users that already support the original implementation.

porksmash
Sep 30, 2008

That was exactly what I couldn't find myself - thank you!

roomforthetuna
Mar 22, 2005

I don't need to know anything about virii! My CUSTOM PROGRAM keeps me protected! It's not like they'll try to come in through the Internet or something!
Argh I have spent two days trying to make this poo poo work. How do you set up two typescript modules, one of which depends on the other, without going through a full publish and import rigmarole every time you change the dependency one?

The internet is all "use yarn link or npm link but it won't work lol" which is true, it doesn't work.

Today I found yarn workspaces that sounded good but the instructions for that are like "here's how you create a workspace, ha ha that is all, good luck actually using it". So I've got a yarn workspace that as expected includes symlinks to my local modules and installs of all the other dependencies, but now what, how do I actually build a thing here, the tsconfig.json file is now inside a symlink inside node_modules which presumably is not how you're supposed to use that, and I have no loving idea how this is going to eventually tie in with webpack. Is my main project's package.json supposed to *be* the workspace-defining one?

Also something about typescript project references and "tsc --build" maybe? Maybe it can perform the build inside the node_modules folder?

Slimy Hog
Apr 22, 2008

roomforthetuna posted:

Argh I have spent two days trying to make this poo poo work. How do you set up two typescript modules, one of which depends on the other, without going through a full publish and import rigmarole every time you change the dependency one?

The internet is all "use yarn link or npm link but it won't work lol" which is true, it doesn't work.

Today I found yarn workspaces that sounded good but the instructions for that are like "here's how you create a workspace, ha ha that is all, good luck actually using it". So I've got a yarn workspace that as expected includes symlinks to my local modules and installs of all the other dependencies, but now what, how do I actually build a thing here, the tsconfig.json file is now inside a symlink inside node_modules which presumably is not how you're supposed to use that, and I have no loving idea how this is going to eventually tie in with webpack. Is my main project's package.json supposed to *be* the workspace-defining one?

Also something about typescript project references and "tsc --build" maybe? Maybe it can perform the build inside the node_modules folder?

I think I've used something like npm link + "tsc --build --watch" or maybe "webpack --watch"

Setting up a project is the worst thing about TypeScript

roomforthetuna
Mar 22, 2005

I don't need to know anything about virii! My CUSTOM PROGRAM keeps me protected! It's not like they'll try to come in through the Internet or something!
Aha, I found a sort-of answer to my question, which is that evidently webpack is doing something a bit different from the way package.json people think you'll be building stuff. As such, having
code:
"baseUrl": "path/to/your/dependencies"
in your tsconfig.json allows a webpack set up with ts-loader to do its thing without any package.json in the dependencies nor any intermediate js files being generated nor any references to the dependencies in the node_modules of the topmost package.

The one remaining difficulty I was having was that for some reason a 'package' in that path that was only exporting one function (a debug-log function that's basically just a wrapper around console.log whose purpose is to be stripped out in a production build) was simultaneously "not found" and capable of whining about syntax errors or the function from it not being found if I changed its name. I just moved that file arbitrarily into one of the other packages and from there it worked fine. Mysterious. So obviously I'm not super comfortable with what I have going on, but it does build now, and doesn't require any extra command-line operations to pick up on dependencies being updated, so the setup finally meets pretty much all the criteria I had.

Relatedly, has anyone here done anything with WebRTC? That's nearly as much of a clusterfuck to get going as webpack+typescript+npm. I got that working too, finally, and all it requires is like 14 messages through two server connections in order to establish one peer-to-peer connection.

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
Are you sure you're designing something that needs webpack+typescript+npm? I imagine you are hitting yourself in the foot with a hammer because of all that tooling. WebRTC is just one of those things, the spec is designed by committee and so even though it's P2P you need like 2-5 intermediary or ancillary servers that do whatever.

I'm not convinced any of it is necessary, if I needed to do something with it I'd probably just pay for some service that provides it.

TIP
Mar 21, 2006

Your move, creep.



roomforthetuna posted:

Relatedly, has anyone here done anything with WebRTC? That's nearly as much of a clusterfuck to get going as webpack+typescript+npm. I got that working too, finally, and all it requires is like 14 messages through two server connections in order to establish one peer-to-peer connection.

That's disheartening, I'm about to start work on some new features that I planned to use WebRTC for... Maybe I'll just do it the old fashioned way instead.

I just spent forever fighting with the Web Audio API, which is a giant piece of poo poo with all kinds of weird limitations placed on it for no reason. Like, yeah, you can encode mp3s, but only in real time. And yeah, the api has most of the standard audio effects and filters, but it doesn't specify their implementation, so you get different results in every browser.

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense

Tip posted:

That's disheartening, I'm about to start work on some new features that I planned to use WebRTC for... Maybe I'll just do it the old fashioned way instead.

I just spent forever fighting with the Web Audio API, which is a giant piece of poo poo with all kinds of weird limitations placed on it for no reason. Like, yeah, you can encode mp3s, but only in real time. And yeah, the api has most of the standard audio effects and filters, but it doesn't specify their implementation, so you get different results in every browser.

You sound like you're doing something I tried to do a while ago. I was streaming audio from the user to a server where the recording was saved. At that time the only thing you could reliably capture was WAV-like gigantic amounts of data. So then I needed to include a several mb js-compiled version of an Opus encoder on the page.

Then with the audio streaming into a compressed format I eventually gave up on WebRTC and decided on using websockets, breaking the audio up into chunks encoded as base64. None of it was ideal at all. But in the end it felt reasonably as good as I was going to get it.

roomforthetuna
Mar 22, 2005

I don't need to know anything about virii! My CUSTOM PROGRAM keeps me protected! It's not like they'll try to come in through the Internet or something!

Tip posted:

That's disheartening, I'm about to start work on some new features that I planned to use WebRTC for... Maybe I'll just do it the old fashioned way instead.

I just spent forever fighting with the Web Audio API, which is a giant piece of poo poo with all kinds of weird limitations placed on it for no reason. Like, yeah, you can encode mp3s, but only in real time. And yeah, the api has most of the standard audio effects and filters, but it doesn't specify their implementation, so you get different results in every browser.
If it's not important to you to go peer-to-peer, I strongly recommend not even thinking about WebRTC. Websockets are pretty good and not at all a giant pain in the rear end, comparatively. You basically have to implement your own peer-to-peer connection through your server to communicate how to form a peer-to-peer connection not through your server, so for probably 95% of use-cases you might as well just carry on communicating through your server since you already set that up.

Nolgthorn posted:

Are you sure you're designing something that needs webpack+typescript+npm? I imagine you are hitting yourself in the foot with a hammer because of all that tooling. WebRTC is just one of those things, the spec is designed by committee and so even though it's P2P you need like 2-5 intermediary or ancillary servers that do whatever.
Well no, it doesn't need all the things, but webpack doesn't work without some amount of npm, and typescript for a large project would suck without some sort of package wrapper, and webpack seems to be the only one that supports dynamic module loading which I want because my project is intended to be extensible, and I don't want it to have to load everything before anything works.

I have reduced it to not using npm to control my own libraries - I was going to open-source my WebRTC wrapping library and its server sibling, but npm being a pain in the rear end has persuaded me not to (which is funny because I got the feeling npm is to some extent designed as "you open-source or you pay!" but somehow it became so annoying that I'm not even going to open-source the thing I was intending to.)

TIP
Mar 21, 2006

Your move, creep.



Nolgthorn posted:

You sound like you're doing something I tried to do a while ago. I was streaming audio from the user to a server where the recording was saved. At that time the only thing you could reliably capture was WAV-like gigantic amounts of data. So then I needed to include a several mb js-compiled version of an Opus encoder on the page.

Then with the audio streaming into a compressed format I eventually gave up on WebRTC and decided on using websockets, breaking the audio up into chunks encoded as base64. None of it was ideal at all. But in the end it felt reasonably as good as I was going to get it.

My use case was basically, simultaneously playing a track and recording a microphone input, and then being able to change the mix on the fly client side and save as an mp3.

I tried a ton of js libraries that claim to handle this kinda thing, and they were all hosed in one way or another. I did finally find a js lame mp3 encoder that's only like 100kb minified and gzipped, and I turned that into a web worker so I can encode on a separate thread.

The things that still really suck are that you have to load the song from the server twice, once as a normal stream and then again as an audioBuffer. Also, the mp3 encoding with the js webworker is slower than real time on some devices. :suicide:

TIP
Mar 21, 2006

Your move, creep.



roomforthetuna posted:

If it's not important to you to go peer-to-peer, I strongly recommend not even thinking about WebRTC. Websockets are pretty good and not at all a giant pain in the rear end, comparatively. You basically have to implement your own peer-to-peer connection through your server to communicate how to form a peer-to-peer connection not through your server, so for probably 95% of use-cases you might as well just carry on communicating through your server since you already set that up.

I'm going to be using web sockets anyways for other features, but I wanted to go peer-to-peer for this because it involves a lot of users exchanging large amounts of audio data, and I don't want to pay for that data.

I'll probably still tool around with WebRTC a bit before I decide whether it's worth fighting with for the saved bandwidth.

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense

roomforthetuna posted:

Well no, it doesn't need all the things, but webpack doesn't work without some amount of npm, and typescript for a large project would suck without some sort of package wrapper, and webpack seems to be the only one that supports dynamic module loading which I want because my project is intended to be extensible, and I don't want it to have to load everything before anything works.

Not to tell you how to do it but I'd just compile separate js files as needed, then use the `async` html tag. Bingo bongo and it's done. The only thing typescript's compiler is bizarrely still missing is iife, so I use something lightweight like buble when I need it if I'm using typescript.

Webpack is like trying to land a rocket on mars, when all you really want is a little fin that's on the rocket. It has like 8 billion dependencies, it's a bloody mess.

Adbot
ADBOT LOVES YOU

Dominoes
Sep 20, 2007

What method do you recommend for causing a long calculation to not block the main thread? I'm attempting to use async, but am likely doing it wrong. Case: Have some long validations that once complete, trigger a Redux update. What currently happens is the app hangs until they're done. Is there a way I can make the GUI run smoothly while the calc is being performed, and update the Redux state once complete? From what I've read, it appears that prepending the long funcs with 'async' should accomplish this. Don't need to return anything, just let Redux update the state once complete.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply