com.dragome.forms.bindings.client.command.AsyncEventsImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dragome-form-bindings Show documentation
Show all versions of dragome-form-bindings Show documentation
Dragome SDK module: form bindings
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 super R> destination)
{
return resultChannel.sendTo(destination);
}
public Disposable sendResultTo(ValueTarget super R> destination)
{
return resultChannel.sendTo(destination);
}
public Disposable sendResultTo(Publisher super R> 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 super R> command)
{
return resultChannel.sendTo(command);
}
public Disposable sendErrorTo(Destination super E> destination)
{
return errorChannel.sendTo(destination);
}
public Disposable sendErrorTo(ValueTarget super E> destination)
{
return errorChannel.sendTo(destination);
}
public Disposable sendErrorTo(Publisher super E> publisher)
{
return errorChannel.sendTo(publisher);
}
public Disposable onErrorInvoke(final ParameterisedCommand super E> 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);
}
}
});
}
}