/*
* Copyright 2016 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package io.reactivex.netty.channel;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufHolder;
import io.netty.channel.Channel;
import rx.Observable;
import rx.Subscriber;
import rx.functions.Func1;
/**
* A source for any content/data read from a channel.
*
*
Managing {@link ByteBuf} lifecycle.
*
* If this source emits {@link ByteBuf} or a {@link ByteBufHolder}, using {@link #autoRelease()} will release the buffer
* after emitting it from this source.
*
* Replaying content
*
* Since, the content read from a channel is not re-readable, this also provides a {@link #replayable()} function that
* produces a source which can be subscribed multiple times to replay the same data. This is specially useful if the
* content read from one channel is written on to another with an option to retry.
*
* @param
*/
public final class ContentSource extends Observable {
private ContentSource(final Observable source) {
super(new OnSubscribe() {
@Override
public void call(Subscriber super T> subscriber) {
source.unsafeSubscribe(subscriber);
}
});
}
public ContentSource(final Channel channel, final Func1, Object> subscriptionEventFactory) {
super(new OnSubscribe() {
@Override
public void call(Subscriber super T> subscriber) {
channel.pipeline()
.fireUserEventTriggered(subscriptionEventFactory.call(subscriber));
}
});
}
public ContentSource(final Throwable error) {
super(new OnSubscribe() {
@Override
public void call(Subscriber super T> subscriber) {
subscriber.onError(error);
}
});
}
/**
* If this source emits {@link ByteBuf} or {@link ByteBufHolder} then using this operator will release the buffer
* after it is emitted from this source.
*
* @return A new instance of the stream with auto-release enabled.
*/
public Observable autoRelease() {
return this.lift(new AutoReleaseOperator());
}
/**
* This provides a replayable content source that only subscribes once to the actual content and then caches it,
* till {@link DisposableContentSource#dispose()} is called.
*
* @return A new replayable content source.
*/
public DisposableContentSource replayable() {
return DisposableContentSource.createNew(this);
}
public ContentSource transform(Transformer transformer) {
return new ContentSource<>(transformer.call(this));
}
}