All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.dragome.forms.bindings.client.command.AsyncEventsImpl Maven / Gradle / Ivy

The newest version!
package com.dragome.forms.bindings.client.command;

import com.dragome.forms.bindings.client.binding.Disposable;
import com.dragome.forms.bindings.client.channel.Channel;
import com.dragome.forms.bindings.client.channel.DefaultChannel;
import com.dragome.forms.bindings.client.channel.Destination;
import com.dragome.forms.bindings.client.channel.Publisher;
import com.dragome.forms.bindings.client.util.SubscriptionList;
import com.dragome.forms.bindings.client.value.ValueTarget;
import com.dragome.forms.bindings.extra.user.client.Command;

/**
 * AbstractAsyncEvents provides a the default implementation of {@link com.pietschy.gwt.pectin.client.command.AsyncEvents}.
 */
public class AsyncEventsImpl extends AbstractEvents implements AsyncEvents
{
	private DefaultChannel resultChannel= new DefaultChannel();
	private DefaultChannel errorChannel= new DefaultChannel();

	protected AsyncEventsImpl()
	{
	}

	protected Channel getResultChannel()
	{
		return resultChannel;
	}

	protected Channel getErrorChannel()
	{
		return errorChannel;
	}

	protected void fireSuccess(final R result)
	{
		resultChannel.publish(result);
		visitAsyncSubscribers(new SubscriptionList.Visitor>()
		{
			public void visit(AsyncLifeCycleCallback subscriber)
			{
				subscriber.onSuccess(result);
			}
		});
	}

	protected void fireError(final E error)
	{
		errorChannel.publish(error);
		visitAsyncSubscribers(new SubscriptionList.Visitor>()
		{
			public void visit(AsyncLifeCycleCallback subscriber)
			{
				subscriber.onError(error);
			}
		});

	}

	public  SendToBuilder onSuccessSend(final T value)
	{
		return new SendToBuilderImpl(resultChannel, value);
	}

	public  SendToBuilder onErrorSend(final T value)
	{
		return new SendToBuilderImpl(errorChannel, value);
	}

	public Disposable sendResultTo(Destination destination)
	{
		return resultChannel.sendTo(destination);
	}

	public Disposable sendResultTo(ValueTarget destination)
	{
		return resultChannel.sendTo(destination);
	}

	public Disposable sendResultTo(Publisher publisher)
	{
		return resultChannel.sendTo(publisher);
	}

	public Disposable onSuccessInvoke(final Command command)
	{
		if (command == null)
		{
			throw new NullPointerException("command is null");
		}
		return resultChannel.sendTo(new Destination()
		{
			public void receive(R value)
			{
				command.execute();
			}
		});
	}

	public Disposable onSuccessInvoke(final ParameterisedCommand command)
	{
		return resultChannel.sendTo(command);
	}

	public Disposable sendErrorTo(Destination destination)
	{
		return errorChannel.sendTo(destination);
	}

	public Disposable sendErrorTo(ValueTarget destination)
	{
		return errorChannel.sendTo(destination);
	}

	public Disposable sendErrorTo(Publisher publisher)
	{
		return errorChannel.sendTo(publisher);
	}

	public Disposable onErrorInvoke(final ParameterisedCommand command)
	{
		return errorChannel.sendTo(command);
	}

	public Disposable onErrorInvoke(final Command command)
	{
		if (command == null)
		{
			throw new NullPointerException("command is null");
		}
		return errorChannel.sendTo(new Destination()
		{
			public void receive(E value)
			{
				command.execute();
			}
		});
	}

	public Disposable sendAllEventsTo(AsyncLifeCycleCallback callback)
	{
		// we all all our async subscribers as regulars subscribers so they
		// automatically pickup the start and finished events.
		return super.sendAllEventsTo(callback);
	}

	protected void visitAsyncSubscribers(final SubscriptionList.Visitor> visitor)
	{
		// now we fire async events to those subscribers that implement AsyncLifeCycleCallback
		super.visitSubscribers(new SubscriptionList.Visitor()
		{
			public void visit(LifeCycleCallback subscriber)
			{
				if (visitor instanceof AsyncLifeCycleCallback)
				{
					visitor.visit((AsyncLifeCycleCallback) subscriber);
				}
			}
		});
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy