io.activej.ot.PollSanitizer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of activej-ot Show documentation
Show all versions of activej-ot Show documentation
Implementation of operational transformation technology. Allows building collaborative software systems.
/*
* Copyright (C) 2020 ActiveJ LLC.
*
* 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.activej.ot;
import io.activej.async.function.AsyncSupplier;
import io.activej.common.initializer.WithInitializer;
import io.activej.promise.Promise;
import io.activej.promise.Promises;
import org.jetbrains.annotations.Nullable;
import java.time.Duration;
import java.util.Objects;
import static io.activej.promise.RetryPolicy.exponentialBackoff;
public final class PollSanitizer implements AsyncSupplier, WithInitializer> {
public static final Duration DEFAULT_YIELD_INTERVAL = Duration.ofSeconds(1);
private Duration yieldInterval = DEFAULT_YIELD_INTERVAL;
private final AsyncSupplier poll;
private @Nullable T lastValue;
private PollSanitizer(AsyncSupplier poll) {
this.poll = poll;
}
public static PollSanitizer create(AsyncSupplier poll) {
return new PollSanitizer<>(poll);
}
public PollSanitizer withYieldInterval(Duration yieldInterval) {
this.yieldInterval = yieldInterval;
return this;
}
@Override
public Promise get() {
return Promises.retry(poll,
(value, e) -> {
if (e != null) return true;
if (Objects.equals(value, lastValue)) {
return false;
} else {
this.lastValue = value;
return true;
}
},
exponentialBackoff(Duration.ofMillis(1), yieldInterval));
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy