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
Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

Knifegrab posted:

Yeah that's my understand too, so how do I expose a function that utilizes the class to return a value?

I don't actually know how javascript classes work exactly, but if it's similar to Java classes, there is a slight difference whether you want to call a static method or an instance method.

Adbot
ADBOT LOVES YOU

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

Knifegrab posted:

code:
Weaver's a tree. Once he has a "young weaver" attached to himself, he can't jump
Obviously for display purposes this is super ugly. I need to prevent quotes/apostrophes/square brackets etc from ebing interpolated as html but I also want them display nicely and not as the weird item they are being displayed as. Any ideas?

You're double-escaping your text somewhere.

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

CodfishCartographer posted:

I actually wound up running into another problem - it doesn't seem to update as often as it should. Even with it updating 1000 times a second, it would only fill up about a quarter of the way per second. I lowered the progress bar max to just be 250 and then it appears to be working, but I know it's not perfect and there's something wrong under the hood but again, I can't quite figure it out. I looked at the java console and it's getting to around 250ish, but moving to quickly for me to pinpoint its exact stopping spot. It feels like only one out of every 4 is actually filling up the progress bar?

You can never trust the requested delay being accurate in any language or framework. (Actually, If you're doing embedded work and working on assemly and have counted your cycles, you can)

Anyway: Save the current time from before you make your delay request, then request your delay. When your code runs, compare the current time to the time you saved from before you made the call, then animate your thing according to the difference between the two values.

Some frameworks make this easier by just telling you the actual delay when your code is run.

Here is an example:
JavaScript code:
var requestedDelay = 17;

var startTime = window.performance.now();

setTimeout(function() {
    var endTime = window.performance.now();
    console.log('actual delay:' + (endTime - startTime) + 'ms');
}, requestedDelay);
In my case, the actual delay is fractions of a millisecond longer than the requested delay, but those fractions add up

Wheany fucked around with this message at 09:10 on May 6, 2017

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

Maluco Marinero posted:

basic facts about JavaScript equality and how to rigorously test it properly.

Please elaborate what you mean by this.

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

Ghost of Reagan Past posted:

I propose we adopt the four Boolean values of LP: true, false, both, and neither.

Don't forget FILE_NOT_FOUND

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

Honest Thief posted:

Maybe this is just me bitching and being annoying but it seems every other article about some new js tool barely covers the basic use cases. Case in point, I'm trying to figure out the syntax it accepts for custom routing, only the given example on the repo doesn't work, and none of the article I can find seem to consider the possibility of wanting to have custom routes.
edit:Ok, fine, it uses path-to-regexp, but some headsup would be nice

Yeah, that's javascript.

Even seemingly well documented frameworks either document a function superficially*, or there have been breaking changes in the api in minor versions and nobody bothered to update the documentation/tutorials.

*)something like "the function takes a configuration object and a callback" without telling the structure of the object or the parameters the callback will be called with

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

The Merkinman posted:

But with HTTP2 you don't need to concatenate so Webpack is useless now.

Looking forward to using HTTP2 in 2027

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope
My greasemonkey boilerplate:
JavaScript code:
[...document.querySelectorAll('.your-posts')]
	.forEach(post => post.style.display = 'none')
...or chain other native Array methods in place of forEach

It's not quite
JavaScript code:
$('.your-posts').hide()
but it's close enough

Wheany fucked around with this message at 12:40 on Sep 12, 2017

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope
This is definitely a case of "what? why?"

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope
Were discussing possible technologies to use with a future project and I suggested React for the UI part. Another person suggested Angular.

So my question is: Is Angular good now?

edit: also if it isn't, why? (I'm sure they'll ask me the same thing)

Wheany fucked around with this message at 12:55 on Oct 30, 2017

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope
Why do service workers define self?

I tried to google it, but google is useless for precise techical questions.

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope
I have looked at two (or three) new APIs in the last week. Service workers + caches seems like a really good and simple API and Indexed DB looks like complicated garbage.

It's really easy to cache a few resources and then just serve them from the cache forever while everything else uses normal http requests.

Indexed DB looks like a ton of yak shaving before you get to the part where you can dump data in there.

I was looking at Indexed DB because Safari on ios doesn't share localstorage with PWAs and that's where our app's auth keys are stored, but then I read that it doesn't share Indexed DB either. So I guess our iphone users get to log in again with the app.

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope
I have an actual javascript question: does cache.addAll guarantee that those files will always stay cached?

Currently my service worker just caches all the js, css, images and fonts that the app uses using addAll (8 files, a little over 1 megabyte total) and the fetch listener will just return the cached version or fetch the resource if not found in cache. The cache is never updated.

Should I be checking if the requested resources match the files listed in addAll and re-cache them if they have been purged?

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope
I've read that before but I guess what I wanted to hear was that everything in the cache is treated equally and can disappear from under you. I guess that makes the default boilerplate javascript that keeps static resources cached a bit more complex.

Or can the browser just reinstall the service worker after a cache purge?

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope
My react knowledge is old an rusty, so I look up a 2019 react article and see this:

JavaScript code:
const [state, setState] = useState(initialState);
:eyepop: now that's what I'm here for. No writing setters for me.

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope
model.vehicle.get('device.type') returns a promise.
model.vehicle.get('device.type.id') returns the value of the id field directly.

Ember! :razz:

Never having any idea what type or shape the data is that I'm going to get without trial and error for the epic win!

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope
Is there a way to use some kind of a background worker script (similar to a service worker or a web worker) in TamperMonkey?

I have a script that does some heavy processing when I press a button and currently it blocks the UI long enough that Firefox starts complaining about a script slowing the page down. It's not a huge deal since this script is for my own use, but I'd like to get rid of it if possible anyway.

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope
I have tried making an offline PWA with a service worker that caches all the files needed for the app. I also added a manifest and an icon and I'm able to install the app on my phone's home screen and run it from there.

However, if I turn on the phone's airplane mode and try to reload the page it will say "no internet" "chrome will let you know when this page is ready"

Am I doing something wrong or does reloading just always bypass the service worker and the cache?

edit: okay, I'm definitely doing something wrong since airhorner.com works fine even in airplane mode and reloading the page

Wheany fucked around with this message at 16:15 on Jun 1, 2020

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope
Okay, I followed the "Offline fallback" recipe from https://serviceworke.rs/offline-fallback_service-worker_doc.html

That example tries to fetch first, then catch errors and respond with a cached version. Instead I got it working by reading the cache first and then trying to fetch data over the network if not found.

Which sounds reasonable in hindsight.

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

roomforthetuna posted:

Depends if you want to be able to update the data - you've got a permanent cache in what you describe, the server is never asked again once the client has any data.

Another option that can be good is read the offline version first, then request the live version, and update if a live one comes back - that way you get the fast response of a cache, but remain server-updatable (possibly at the cost of a glitchy-looking blink if an update happens.)

And if you want to get fancy, you can send a version number or a hash of the offline one with your request, and have the server just reply with a "you're up to date" signal instead of a new value if there's no update, to save bandwidth - e.g. http status code 204.

I think what I want to accomplish in the end is to have a fallback cache and a fresh cache and try to fetch the data into the fresh cache. If the fetch succeeds, the fresh cache becomes the new fallback and the old fallback is deleted.

This has all been for https://github.com/everestpipkin/image-scrubber where I think the idea is that it stays as offline and simple as possible. Either using service workers for "app like" behavior or easily zippable html js and css that you can unzip and use on your computer without ever sending anything over the network.

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

Guildenstern Mother posted:

Thanks for the explanations and sorry for wrong thread posting

If it's any consolation, javascript was named javascript in the 90s because there was this new cool programming language called java. You were meant to be confused by the name

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope
Seconding webpack. The tutorial/getting started on the official website is pretty good too. Once you get the project structure mostly there, you can start adding plugins to the mix

Wheany fucked around with this message at 14:01 on Jun 21, 2020

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

Doom Mathematic posted:

Should your code return the empty string? Then you can use ??. Should it return null instead? Then you should use ||.

What's your reasoning here?

My opinion is that if you have a default value, always use ?? and only use || if you're dealing with boolean logic.

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

Nolgthorn posted:

For ages I used || for defaults, going through old code I always change it to ??.

Yeah, same.

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope
Yeah actually I did create a function just today that returns Number(value) || 0, just in case Number(value) evaluates to NaN

Wheany fucked around with this message at 06:37 on Nov 11, 2023

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

some kinda jackal posted:

but I wanted to manipulate the name properties at assignment time, is the right approach to make the properties private with some sort of name distinction, then make get/set accessors for firstname lastname? Something like

To make a property private, prepend its name with a # character:

JavaScript code:
class Person {
	#name = '';
	get name() {
		return this.#name;
	}
	set name(n) {
		this.#name = n;
	}

}

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

some kinda jackal posted:

Is that a style thing or an actual typescript syntax thing because it’s not giving me any errors on private, but for all I know I could be doing something unintended..

Switching to the hash anyway, thanks!

quote:

» class Person {
#name = '';
get name() {
return this.#name;
}
set name(n) {
this.#name = n;
}

}
← undefined
» const p = new Person()
← undefined
» p.name='QQQ'
← "QQQ"
» p.#name
← Uncaught SyntaxError: reference to undeclared private field or method #name debugger eval code:1:3
it's actually private

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

Leng posted:

If anyone could point me in the right direction, I would be very grateful for the help!

From skimming the code:

You seem to be using a non-checkbox element like a checkbox, you could try using checkboxes and trigger your filtering with the "input" event.

You also seem to be doing both the active/not active toggling and filtering in the same pass. I would do the toggling in one function, then run a separate function that just gathers the current selection and filters on that.

Adbot
ADBOT LOVES YOU

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope
One case where I will make completely trivial functions is when I'm filtering a collection. So if you have multple things in a list then instead of
JavaScript code:
const smellyThings = things.filter(thing => thing.scentRatio>0.8 && thing.scentType==ScentType.Bad)
I will do
JavaScript code:
const hasHighScent = thing => thing.scentRatio > 0.8;
const hasBadSmell = thing => thing.scentType == ScentType.Bad;

const smellyThings = things.filter(hasHighScent)
                           .filter(hasBadSmell)

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