Tags: 24

787

sparkline

Sunday, July 21st, 2019

Picture 1 Picture 2

Ribfest!

Picture 1 Picture 2 Picture 3

Thank you, @mezzoblue and @curio_research for a lovely day of cycling in the Canadian countryside.

Saturday, July 20th, 2019

Goodnight, Vancouver.

Goodnight, Vancouver.

Wednesday, July 17th, 2019

Got a postcard from @ArielWaldman of the cutest tardigrade in all Antarctica. Antarctigrade! 🇦🇶

Got a postcard from @ArielWaldman of the cutest tardigrade in all Antarctica.

Antarctigrade! 🇦🇶

Tuesday, July 16th, 2019

T + 50 years - 30 minutes:

https://apolloinrealtime.org/11/

Friday, July 12th, 2019

Farewell to Miltown Malbay.

Farewell to Miltown Malbay.

Thursday, July 11th, 2019

Checked in at Miltown Malbay. 💃🏻 — with Jessica map

Checked in at Miltown Malbay. 💃🏻 — with Jessica

Wednesday, July 10th, 2019

Playing tunes with fellow members of https://thesession.org

Playing tunes with fellow members of https://thesession.org

Tuesday, July 9th, 2019

Checked in at Clery's. 🎶 — with Jessica map

Checked in at Clery’s. 🎶 — with Jessica

The road to Miltown.

The road to Miltown.

Scoil Samhraidh Willie Clancy.

Scoil Samhraidh Willie Clancy.

Monday, July 8th, 2019

Picture 1 Picture 2

It’s a nice day in county Clare.

Sunday, July 7th, 2019

Going to Miltown Malbay. brb

Thursday, July 4th, 2019

Brightonians with websites: remember that Homebrew Website Club is happening in the @Clearleft studio from 6pm to 7:30pm this evening.

Tuesday, July 2nd, 2019

The trimCache function in Going Offline …again

It seems that some code that I wrote in Going Offline is haunted. It’s the trimCache function.

First, there was the issue of a typo. Or maybe it’s more of a brainfart than a typo, but either way, there’s a mistake in the syntax that was published in the book.

Now it turns out that there’s also a problem with my logic.

To recap, this is a function that takes two arguments: the name of a cache, and the maximum number of items that cache should hold.

function trimCache(cacheName, maxItems) {

First, we open up the cache:

caches.open(cacheName)
.then( cache => {

Then, we get the items (keys) in that cache:

cache.keys()
.then(keys => {

Now we compare the number of items (keys.length) to the maximum number of items allowed:

if (keys.length > maxItems) {

If there are too many items, delete the first item in the cache—that should be the oldest item:

cache.delete(keys[0])

And then run the function again:

.then(
    trimCache(cacheName, maxItems)
);

A-ha! See the problem?

Neither did I.

It turns out that, even though I’m using then, the function will be invoked immediately, instead of waiting until the first item has been deleted.

Trys helped me understand what was going on by making a useful analogy. You know when you use setTimeout, you can’t put a function—complete with parentheses—as the first argument?

window.setTimeout(doSomething(someValue), 1000);

In that example, doSomething(someValue) will be invoked immediately—not after 1000 milliseconds. Instead, you need to create an anonymous function like this:

window.setTimeout( function() {
    doSomething(someValue)
}, 1000);

Well, it’s the same in my trimCache function. Instead of this:

cache.delete(keys[0])
.then(
    trimCache(cacheName, maxItems)
);

I need to do this:

cache.delete(keys[0])
.then( function() {
    trimCache(cacheName, maxItems)
});

Or, if you prefer the more modern arrow function syntax:

cache.delete(keys[0])
.then( () => {
    trimCache(cacheName, maxItems)
});

Either way, I have to wrap the recursive function call in an anonymous function.

Here’s a gist with the updated trimCache function.

What’s annoying is that this mistake wasn’t throwing an error. Instead, it was causing a performance problem. I’m using this pattern right here on my own site, and whenever my cache of pages or images gets too big, the trimCaches function would get called …and then wouldn’t stop running.

I’m very glad that—witht the help of Trys at last week’s Homebrew Website Club Brighton—I was finally able to get to the bottom of this. If you’re using the trimCache function in your service worker, please update the code accordingly.

Management regrets the error.

Monday, July 1st, 2019

Replying to

Yes, mostly links (https://adactio.com/links) and notes (https://adactio.com/notes) with some blog posts (https://adactio.com/journal).

Friday, June 28th, 2019

Replying to

Thank you so much for coming—it was lovely to meet you!

Replying to

You’re a bad person and you should feel bad.

Tech checking for #PatternsDay. Here we go…

Tech checking for #PatternsDay. Here we go…

Wednesday, June 26th, 2019

Replying to

That’s a superb line-up!

Good to have you back in the conference-organising game, Patrick.