Create multiple image blocks with file.create.after hook

I would like to create image blocks for each file upload to an image section. I tried to follow this recipe. The following hook works for single images. When I upload multiple images at once, it only adds one block to the blocks field.

    'hooks' => [
        'file.create:after' => function ($file) {
            $kirby    = $this;
            $page = $file->parent();
            $blocks = $page->projectContent()->toBlocks();

            $imageBlock = new Kirby\Cms\Block([
                'content' => [
                    'location' => 'kirby',
                    'image' => [$file->filename()],
                ],
                'type' => 'image',
            ]);

            $blocks = $blocks->add(new Kirby\Cms\Blocks([$imageBlock]));
            $kirby->impersonate('kirby');
            $page->update([
              'projectContent' => json_encode($blocks->toArray()),
            ]);
        }
    ]

As i understand the hook file.create.after is called for each file?
Or does this get handled differently when uploading multiple files at once?
At the moment I don’t see another approach doing something like this, other than a hook. But maybe there is another way to do this?

I’m pretty sure this is due to some race condition and multiple references to the same value.

As far as i was able to find out, each call of file.create:after executes the callback on a single file. That would mean you are generating one $blocks variable per call and therfore overwriting your changes on every execution, with only the last one sticking. but i’m just guessing here.

gonna have a closer look tonight when i get to it tonight