001/*
002 * Configurate
003 * Copyright (C) zml and Configurate contributors
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *    http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package ninja.leaping.configurate.reactive;
018
019/**
020 * A listener to events that may be called by an {@link Publisher}.
021 * <p>
022 * For every publisher this subscriber is subscribed to, the subscriber will only process one event at a time --
023 * effectively, within a single publisher this subscriber does not have to be aware of concurrent effects
024 *
025 * @param <V> The value that will be received
026 */
027@FunctionalInterface
028public interface Subscriber<V> {
029    /**
030     * Called to submit a new item
031     *
032     * @param item The item available
033     */
034    void submit(V item);
035
036    /**
037     * When an error occurs while subscribing to an {@link Publisher}, or is thrown during the execution of {@link
038     * #submit(Object)} that is not otherwise handled, this method will be called with the error. The associated {@link
039     * Publisher} will not send further update signals after an error is thrown.
040     *
041     * @param e The exception thrown
042     */
043    default void onError(Throwable e) {
044        Thread t = Thread.currentThread();
045        t.getUncaughtExceptionHandler().uncaughtException(t, e);
046    }
047
048    /**
049     * When the {@link Publisher} this is subscribed to closes without error, this method will be called.
050     */
051    default void onClose() {
052        // no-op
053    }
054}