    001    // Copyright 2006, 2008, 2010 The Apache Software Foundation
    002    //
    003    // Licensed under the Apache License, Version 2.0 (the "License");
    004    // you may not use this file except in compliance with the License.
    005    // You may obtain a copy of the License at
    006    //
    007    // http://www.apache.org/licenses/LICENSE-2.0
    008    //
    009    // Unless required by applicable law or agreed to in writing, software
    010    // distributed under the License is distributed on an "AS IS" BASIS,
    011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    012    // See the License for the specific language governing permissions and
    013    // limitations under the License.
    014
    015    package org.apache.tapestry5.services;
    016
    017    import org.apache.tapestry5.annotations.HeartbeatDeferred;
    018
    019    /**
    020     * Allows for deferred execution of logic, useful when trying to get multiple components to coordinate behavior. A
    021     * component may add a command to be executed "{@linkplain #end() at the end of the heartbeat}". The classic example of
    022     * this is a Label and the field it labels; since we don't know which order the two will render, we can't tell if the
    023     * field's id is correct until after both have rendered.
    024     * <p/>
    025     * The Heartbeat is injected into components via the {@link org.apache.tapestry5.annotations.Environmental} annotation.
    026     */
    027    public interface Heartbeat
    028    {
    029        /**
    030         * Begins a new Heartbeat. Heartbeats nest. Every call to begin() should be matched by a call to {@link #end()}.
    031         */
    032        void begin();
    033
    034        /**
    035         * Executes all commands since the most recent {@link #begin()}.
    036         */
    037        void end();
    038
    039        /**
    040         * Adds a new command to the current Heartbeat. The command will be executed by {@link #end()}.
    041         *
    042         * @param command
    043         *            command to be executed at the end of the heartbeat
    044         * @see HeartbeatDeferred
    045         */
    046        void defer(Runnable command);
    047    }