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