001 // Copyright 2006, 2008 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 /**
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 * <p/>
023 * The Heartbeat is injected into components via the {@link org.apache.tapestry5.annotations.Environmental} annotation.
024 */
025 public 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 command to be executed at the end of the heartbeat
041 */
042 void defer(Runnable command);
043 }