superyuuki / yuukomponent

component framework for block game
2 stars 0 forks source link

Make sure EventDispatcher's child linking queue is updated when components are m... #14

Closed github-actions[bot] closed 2 years ago

github-actions[bot] commented 2 years ago

Make sure EventDispatcher's child linking queue is updated when components are moved between slots

https://github.com/superyuuki/yuukomponent/blob/03d5824af7767838a391689ba46fbe4f5611e7dc/yuukomponent-core/src/main/java/com/superyuuki/yuukomponent/core/event/BasicDispatcher.java#L19


package com.superyuuki.yuukomponent.core.event;

import com.google.common.cache.Cache;
import com.superyuuki.yuukomponent.api.behavior.Event;
import com.superyuuki.yuukomponent.api.component.Component;
import com.superyuuki.yuukomponent.api.component.ComponentExecutionFailure;
import com.superyuuki.yuukomponent.api.component.ComponentStorage;
import com.superyuuki.yuukomponent.api.component.error.CacheExecutionFailure;
import com.superyuuki.yuukomponent.api.event.EventDispatcher;

import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.ExecutionException;

public class BasicDispatcher implements EventDispatcher {

    private final ComponentStorage storage;

    //TODO Make sure EventDispatcher's child linking queue is updated when components are moved between slots
    private final Cache<UUID, Component> subcomponentCache;

    public BasicDispatcher(ComponentStorage storage, Cache<UUID, Component> subcomponentCache) {
        this.storage = storage;
        this.subcomponentCache = subcomponentCache;
    }

    @Override
    public boolean dispatchChildInclusive(Event event, UUID uuid) throws ComponentExecutionFailure {

        Optional<Component> surfaceLevel = storage.retrieveOpt(uuid);

        if (surfaceLevel.isPresent()) {
            surfaceLevel.get().handle(event);
            return true;
        }

        try {
            Component nullableChild = subcomponentCache.get(uuid, () -> {
                for (Component component : storage.components()) {
                    if (component.present(uuid)) {
                        return component;
                    }
                }

                throw new NoKeyFailure(uuid.toString());
            });

            nullableChild.handle(event);

            return true;
        } catch (ExecutionException e) {
            if (e.getCause() instanceof NoKeyFailure) {
                return false;
            }

            throw new CacheExecutionFailure(uuid, e.getCause());
        }

    }
}

fb5552e02e4357e7fb8d5b1728114e7cb43868fe

zoeself commented 2 years ago

@github-actions[bot] thank you for reporting this. I'll assign someone to take care of it soon.

zoeself commented 2 years ago

@superyuuki I couldn't find any assignee for this task. This is either because there are no contributors with role DEV available or because the project does not have enough funds.

Please, make sure there is at least one available contributor with the required role and the project can afford to pay them.

superyuuki commented 2 years ago

NO LONGER RELEVANT