|
echinopsis posted:am I heading in the right direction? (The advantage of a module is webpack or whatever can make the whole thing into a single distribution file, rather than loading the game requiring multiple requests, and you can skip the json.decode step, you can just have a variable named JSONFile or a more appropriate name.) Edit: Also, if you make it a module with a variable then you can unquote the dictionary keys, and the elements become fully typed objects, which a good editor might allow to be used for autocomplete and error correction. roomforthetuna fucked around with this message at 00:01 on Jun 19, 2022 |
# ? Jun 18, 2022 23:43 |
|
|
# ? May 28, 2024 15:42 |
|
Looks good, just drop the two lines with } and { after crimes and put a comma there instead. Those are keys in the object Future tip: JSON doesn't allow extraneous commas like JS does, and you can't serialize a non-dictionary object (becomes "[object]") or a circular dictionary
|
# ? Jun 18, 2022 23:46 |
|
roomforthetuna posted:That's a fine layout, though why a json file rather than a js module? oo I never saw this although I am thinking about importing a csv library and parsing a csv file at runtime because it’s much easier for me to maintain the data in excel and export to csv when I’m done and then that’s it, not another step Analytic Engine posted:Looks good, just drop the two lines with } and { after crimes and put a comma there instead. Those are keys in the object about dictionaries I finally worked out how to use them for ordinary key:value pairs but what I want to know is if I can use a dictionary so that I can access a handful of variables via the dictionary key. eg code:
code:
code:
I realise I’ve coded some stuff very poorly, and if I could create data structures that used keys to access them it’d be incredible
|
# ? Jun 27, 2022 22:09 |
|
I used a basic dictionary for some functionality I recently added and man it just worked so easily lol
|
# ? Jun 27, 2022 22:14 |
|
echinopsis, you need someone to pair program with for an afternoon.
|
# ? Jun 27, 2022 23:40 |
|
Object.keys() will let you iterate over the JSON object and you can pull from that. Echi I can provide assistance for you tomorrow but I'm not on NZ time.
|
# ? Jun 28, 2022 00:03 |
|
Doom Mathematic posted:echinopsis, you need someone to pair program with for an afternoon. I had literally thought about something like this, even paying someone for a couple hours or something, help me crack open some of these awful implementations of mine. Video Nasty posted:Object.keys() will let you iterate over the JSON object and you can pull from that. Echi I can provide assistance for you tomorrow but I'm not on NZ time. I appreciate this, but it is a bit of a challenge to find a time given I've got kids and work and squeeze this in just when I can, but I might take you up on this if at some stage, if you're still keen and willing.
|
# ? Jun 28, 2022 09:35 |
I'm also happy to hop on and help, though I'm on CET. Plenty of time this weekend, or most evenings my time.
|
|
# ? Jul 1, 2022 10:56 |
|
Seems like I have a memory leak with images when using toDataURL. More specifically, I have a canvas element, and a button that calls the covertCanvasToImage() function. imageAsFile is just a div where an image element is added. imageToSave is global, and keeps being reused. No additional image elements are continuously added to the page, yet when I look at the sources in the inspector, new images keep being added, making the memory use go up and up. How do I keep it from leaking?code:
|
# ? Jul 2, 2022 18:41 |
|
Good Sphere posted:How do I keep it from leaking? code:
Alternatively you might just want to have the image object in the HTML to begin with, and then just set its src and not modify the tree at all.
|
# ? Jul 2, 2022 18:50 |
|
You're appending the Image element to the page every single time the function is called?
|
# ? Jul 2, 2022 18:51 |
|
I was just about to mention, even if I take care of removing it explicitly like that, it still has the same result leak. code:
Jabor posted:You're appending the Image element to the page every single time the function is called? In either case, if you look at the elements in the inspector, there is only ever one image element. roomforthetuna posted:Alternatively you might just want to have the image object in the HTML to begin with, and then just set its src and not modify the tree at all. This also leaks. Good Sphere fucked around with this message at 19:02 on Jul 2, 2022 |
# ? Jul 2, 2022 18:57 |
|
Oh, right, because you're making them via data src urls, they're probably ending up in the "file cache" sort of allocation. I don't think there's any way to tell the browser not to do that for data urls. If you could switch to blob urls that might help you.
|
# ? Jul 2, 2022 19:08 |
|
roomforthetuna posted:Oh, right, because you're making them via data src urls, they're probably ending up in the "file cache" sort of allocation. I don't think there's any way to tell the browser not to do that for data urls. Thank you so much for helping me, but I've also tried using blob urls, lol. Maybe in a weird way? But the same result. Leak! code:
|
# ? Jul 2, 2022 19:18 |
|
What if you use a new Image object each time, instead of just replacing the src attribute?
|
# ? Jul 2, 2022 19:25 |
|
necrotic posted:What if you use a new Image object each time, instead of just replacing the src attribute? Okay, I put var imageToSave = new Image(); right inside the convertCanvasToImage, and commented out the global one. Cleared cache. Refreshed. Same issue.
|
# ? Jul 2, 2022 19:30 |
|
Good Sphere posted:Thank you so much for helping me, but I've also tried using blob urls, lol. Maybe in a weird way? But the same result. Leak!
|
# ? Jul 2, 2022 21:15 |
|
Good Sphere posted:Okay, I put var imageToSave = new Image(); right inside the convertCanvasToImage, and commented out the global one. Cleared cache. Refreshed. Same issue. When you render a new image are you leaving the old one in the dom? You’d need to remove it to fully clean that up.
|
# ? Jul 2, 2022 21:35 |
|
roomforthetuna posted:There were a couple of other suggestions on that stackoverflow link too - putting the image inside an iframe, and changing the iframe, apparently worked for some people when nothing else would. (Which makes sense because that makes it a separate page, so the caching behavior becomes like closing and opening the page.) Good call. I've tried an iframe, and although it no longer continues to generate sources, it still fills up the memory footprint when looking at the network. necrotic posted:When you render a new image are you leaving the old one in the dom? You’d need to remove it to fully clean that up. Yes I think I've tried that. Here's something similar with removing the image, except I'm removing the iframe, which I'd think would release everything put inside the iframe: code:
|
# ? Jul 2, 2022 22:32 |
|
Images tend to build up waiting for garbage collection, this is why infinite scrolling with images is so broken everywhere. The CreateBitmap API has matching API to free resources, and is the only reliable method to do that. It is relatively new and not a lot of support, but I’m using it across browsers and is working.
|
# ? Jul 2, 2022 23:02 |
|
MrMoo posted:Images tend to build up waiting for garbage collection, this is why infinite scrolling with images is so broken everywhere. The CreateBitmap API has matching API to free resources, and is the only reliable method to do that. It is relatively new and not a lot of support, but I’m using it across browsers and is working. Ahh.. I hate to be needy, but I'd like to create an image element.
|
# ? Jul 2, 2022 23:08 |
|
Canvas-only. I’m surprised no real solution for Image elements has been implemented.
|
# ? Jul 3, 2022 02:24 |
|
I've got a file that came in gzipped via a HTTP request and is currently sitting in the browser cache. PHP's gzdecode uncompresses it fine. However in Node.js, when I'm trying this code:code:
I've tried using the Sync methods, reading the file, unzipping and writing it, but the same error prevents the script from actually saving, of course. Could it be that PHP's gzdecode has some error correction in it that the zlib library in Node.js does not? I've also tried pako.js, the errors are the same. e: I've also tried it with a 6 times bigger file and the missing ending is still around ~12k. Don't know what this means yet, but it's apparently not a memory issue or a file error, but rather something with the stream reading maybe? e2: setting chunkSize in createUnzip has an effect on the size of the missing file ending. Seems like it gives up on the last chunk. Jim DiGriz fucked around with this message at 12:13 on Jul 4, 2022 |
# ? Jul 4, 2022 11:42 |
|
I have a node story about fs that's a bit surprising. I have a few static files being served. I populate the set of valid files into a list first to make it trivial to avoid malicious attempts to read outside the target path, so I use fs.promise.readdir('client') to populate the set of files, and e.g. fs.promise.readFile('client/index.html') to read the file. The readdir works, but the readFile errors out on Linux with ENOENT (the readFile works on Windows). What does work for readFile is fs.promise.readFile(path.join(process.cwd(), 'client/index.html')) I'd just shrug it off and not even mention it, but it's bizarre that readdir works with the relative path but readFile doesn't. (Also, making it './client/index.html' doesn't help.)
|
# ? Jul 4, 2022 12:49 |
|
Jim DiGriz posted:I've got a file that came in gzipped via a HTTP request and is currently sitting in the browser cache. PHP's gzdecode uncompresses it fine. However in Node.js, when I'm trying this code: Zip and GZip are two different things.
|
# ? Jul 4, 2022 14:56 |
|
MrMoo posted:Zip and GZip are two different things. Yes, I'm aware (zlib's createUnzip uses inflate/gunzip, depending on what it detects, but I've tried the specific methods as well, to no avail). The file(s) in question came via an XHTTPRequest gzipped, and as I mentioned, gzdecode works fine on them. For now it seems that the last chunk the readstrem pipes into the decompressor makes it choke. I've even tried feeding it byte-by-byte, and it throws the same error. Setting the chunk size of the decompressor to 64 bytes proves this, as only the last 48 bytes are not decompressed from the input.
|
# ? Jul 4, 2022 15:51 |
|
Are you sure the browser hasn't already unzipped it before storing it in the cache?
|
# ? Jul 4, 2022 16:00 |
|
Jabor posted:Are you sure the browser hasn't already unzipped it before storing it in the cache? Yes, 100%.
|
# ? Jul 4, 2022 21:09 |
|
Well, gently caress, I should have checked the actual file sooner. Firefox attaches the request data in plain text to the end of the gzipped file. PHP just shrugged it off but Node didn't. Thanks for the input, fellas.
|
# ? Jul 5, 2022 09:12 |
|
I don't know why but the whole fetch api and .then stuff is absolutely destroying my brain today. I guess I'm used to more synchronous stuff where you grab the JSON from somewhere, turn it into an array and just iterate over it. The .then stuff and arrow functions are just not jiving with my brain. I found some example code for working with the weather.gov api. The api returns an array of 'periods', each of which contain temperature and a forecast. I'm trying to iterate over each 'period' and then log (and eventually display) the temperature and forecast, but I just can't get the syntax right. What should I brush up on, arrow functions? code:
Apologies if this is a really dumb question.
|
# ? Jul 21, 2022 02:26 |
|
-- Ignore welp
Zephirus fucked around with this message at 03:00 on Jul 21, 2022 |
# ? Jul 21, 2022 02:56 |
|
frogbs posted:I don't know why but the whole fetch api and .then stuff is absolutely destroying my brain today. I guess I'm used to more synchronous stuff where you grab the JSON from somewhere, turn it into an array and just iterate over it. The .then stuff and arrow functions are just not jiving with my brain. That looks right to me. I can successfully get to the final .then block only to bomb on document.getElementById('temperature').innerText You could definitely use less .then blocks and consolidate all your logic into one block or even use async/await, which might help you visualize what you're doing better: TypeScript code:
|
# ? Jul 21, 2022 03:48 |
|
teen phone cutie posted:That looks right to me. I can successfully get to the final .then block only to bomb on document.getElementById('temperature').innerText Thank you so much! This is so much cleaner and readable. Will mess with it more tonight. I really appreciate the help!
|
# ? Jul 22, 2022 03:22 |
|
frogbs posted:Thank you so much! This is so much cleaner and readable. Will mess with it more tonight. I really appreciate the help! no worries. also as an enhancement, you could make that function return a promise or promise rejection and then chain a .then or .catch off of it like so: TypeScript code:
|
# ? Jul 22, 2022 03:35 |
|
frogbs posted:I really just need to loop over forecast.properties.periods and output temperature, but no matter what syntax I try it doesn't want to do it. JavaScript code:
JavaScript code:
|
# ? Jul 24, 2022 10:06 |
|
How would I do this without creating the array and pushing to it in Typescript:JavaScript code:
JavaScript code:
EDIT: the magic of posting on the internet worked yet again: JavaScript code:
Lumpy fucked around with this message at 19:23 on Jul 26, 2022 |
# ? Jul 26, 2022 19:17 |
|
yeah normally you would just need to type the function argument like you were and it should work, but it looks like .keys has a bad TS definition out of the box. the TS def for keys should go from this: to this: if anyone here wants to fix that and have some open source bragging rights lol
|
# ? Jul 26, 2022 19:54 |
|
Here's one I am stumped on. I need to remove the multiple keys from an array of objects. If they are fixed, it is simple:JavaScript code:
JavaScript code:
JavaScript code:
|
# ? Jul 26, 2022 20:21 |
|
something like this? TypeScript code:
teen phone cutie fucked around with this message at 20:46 on Jul 26, 2022 |
# ? Jul 26, 2022 20:38 |
|
|
# ? May 28, 2024 15:42 |
|
teen phone cutie posted:something like this? I was trying to avoid loops, but that works. However, since someone else on the team already is using Ramda, I just wound up cheating: JavaScript code:
|
# ? Jul 26, 2022 20:51 |