package com.atlassian.bitbucket.mesh.grpc;

import com.atlassian.bitbucket.mesh.execution.ExecutionContext;
import io.grpc.ForwardingServerCallListener;
import io.grpc.ServerCall;

class ExecutionContextServerCallListener<ReqT>
        extends ForwardingServerCallListener.SimpleForwardingServerCallListener<ReqT> {

    private final ExecutionContext executionContext;

    ExecutionContextServerCallListener(ServerCall.Listener<ReqT> delegate,
                                       ExecutionContext executionContext) {
        super(delegate);

        this.executionContext = executionContext;
    }

    @Override
    public void onCancel() {
        executionContext.run(super::onCancel);
    }

    @Override
    public void onComplete() {
        executionContext.run(super::onComplete);
    }

    @Override
    public void onHalfClose() {
        executionContext.run(super::onHalfClose);
    }

    @Override
    public void onMessage(ReqT message) {
        executionContext.run(() -> super.onMessage(message));
    }
}
