WICKET-6523 use behaviorId for timerId
Project: http://git-wip-us.apache.org/repos/asf/wicket/repo Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/23b7a0f5 Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/23b7a0f5 Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/23b7a0f5 Branch: refs/heads/WICKET-6523-ajax-timers Commit: 23b7a0f5cc27031625f01da8a65ad21b1eae449c Parents: 15aa06f Author: Sven Meier <svenme...@apache.org> Authored: Wed Jan 24 22:15:09 2018 +0100 Committer: Sven Meier <svenme...@apache.org> Committed: Sat Jan 27 00:02:58 2018 +0100 ---------------------------------------------------------------------- .../wicket/ajax/AbstractAjaxTimerBehavior.java | 28 +++++++++++++++----- .../wicket/ajax/AjaxTimerBehaviorTest.java | 2 +- .../SimpleTestPageExpectedResult-1.html | 2 +- .../SimpleTestPageExpectedResult.html | 2 +- 4 files changed, 25 insertions(+), 9 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/wicket/blob/23b7a0f5/wicket-core/src/main/java/org/apache/wicket/ajax/AbstractAjaxTimerBehavior.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/main/java/org/apache/wicket/ajax/AbstractAjaxTimerBehavior.java b/wicket-core/src/main/java/org/apache/wicket/ajax/AbstractAjaxTimerBehavior.java index d193691..94407ad 100644 --- a/wicket-core/src/main/java/org/apache/wicket/ajax/AbstractAjaxTimerBehavior.java +++ b/wicket-core/src/main/java/org/apache/wicket/ajax/AbstractAjaxTimerBehavior.java @@ -44,6 +44,11 @@ public abstract class AbstractAjaxTimerBehavior extends AbstractDefaultAjaxBehav private Duration updateInterval; private boolean stopped = false; + + /** + * Id of timer in JavaScript. + */ + private String timerId; /** * Construct. @@ -100,9 +105,13 @@ public abstract class AbstractAjaxTimerBehavior extends AbstractDefaultAjaxBehav protected final String getJsTimeoutCall(final Duration updateInterval) { CharSequence js = getCallbackScript(); + + Component component = getComponent(); + // remember id for timer + timerId = component.getMarkupId() + "." + component.getBehaviorId(this); return String.format("Wicket.Timer.set('%s', function(){%s}, %d);", - getComponent().getMarkupId(), js, updateInterval.getMilliseconds()); + timerId, js, updateInterval.getMilliseconds()); } /** @@ -112,6 +121,9 @@ public abstract class AbstractAjaxTimerBehavior extends AbstractDefaultAjaxBehav @Override protected final void respond(final AjaxRequestTarget target) { + // timerId is no longer valid after Ajax request + timerId = null; + if (shouldTrigger()) { onTimer(target); @@ -174,9 +186,13 @@ public abstract class AbstractAjaxTimerBehavior extends AbstractDefaultAjaxBehav headerResponse.render(OnLoadHeaderItem.forScript(getJsTimeoutCall(updateInterval))); } - private void clearTimeout(Component component, IHeaderResponse headerResponse) + private void clearTimeout(IHeaderResponse headerResponse) { - headerResponse.render(OnLoadHeaderItem.forScript("Wicket.Timer.clear('" + component.getMarkupId() + "');")); + if (timerId != null) { + headerResponse.render(OnLoadHeaderItem.forScript("Wicket.Timer.clear('" + timerId + "');")); + + timerId = null; + } } /** @@ -193,7 +209,7 @@ public abstract class AbstractAjaxTimerBehavior extends AbstractDefaultAjaxBehav if (target != null) { - clearTimeout(getComponent(), target.getHeaderResponse()); + clearTimeout(target.getHeaderResponse()); } } } @@ -201,7 +217,7 @@ public abstract class AbstractAjaxTimerBehavior extends AbstractDefaultAjaxBehav @Override public void onRemove(Component component) { - component.getRequestCycle().find(IPartialPageRequestHandler.class).ifPresent(target -> clearTimeout(component, target.getHeaderResponse())); + component.getRequestCycle().find(IPartialPageRequestHandler.class).ifPresent(target -> clearTimeout(target.getHeaderResponse())); } @Override @@ -209,7 +225,7 @@ public abstract class AbstractAjaxTimerBehavior extends AbstractDefaultAjaxBehav { Component component = getComponent(); - component.getRequestCycle().find(IPartialPageRequestHandler.class).ifPresent(target -> clearTimeout(component, target.getHeaderResponse())); + component.getRequestCycle().find(IPartialPageRequestHandler.class).ifPresent(target -> clearTimeout(target.getHeaderResponse())); } /** http://git-wip-us.apache.org/repos/asf/wicket/blob/23b7a0f5/wicket-core/src/test/java/org/apache/wicket/ajax/AjaxTimerBehaviorTest.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/test/java/org/apache/wicket/ajax/AjaxTimerBehaviorTest.java b/wicket-core/src/test/java/org/apache/wicket/ajax/AjaxTimerBehaviorTest.java index be26eb6..d8c2392 100644 --- a/wicket-core/src/test/java/org/apache/wicket/ajax/AjaxTimerBehaviorTest.java +++ b/wicket-core/src/test/java/org/apache/wicket/ajax/AjaxTimerBehaviorTest.java @@ -239,7 +239,7 @@ public class AjaxTimerBehaviorTest extends WicketTestCase tester.executeBehavior(timer); - assertMatches("Wicket.Timer.clear", 1); + assertMatches("Wicket.Timer.clear", 0); assertMatches("Wicket.Timer.set", 0); } http://git-wip-us.apache.org/repos/asf/wicket/blob/23b7a0f5/wicket-core/src/test/java/org/apache/wicket/ajax/markup/html/componentMap/SimpleTestPageExpectedResult-1.html ---------------------------------------------------------------------- diff --git a/wicket-core/src/test/java/org/apache/wicket/ajax/markup/html/componentMap/SimpleTestPageExpectedResult-1.html b/wicket-core/src/test/java/org/apache/wicket/ajax/markup/html/componentMap/SimpleTestPageExpectedResult-1.html index 9a8f5f0..d837064 100644 --- a/wicket-core/src/test/java/org/apache/wicket/ajax/markup/html/componentMap/SimpleTestPageExpectedResult-1.html +++ b/wicket-core/src/test/java/org/apache/wicket/ajax/markup/html/componentMap/SimpleTestPageExpectedResult-1.html @@ -11,4 +11,4 @@ Wicket.Ajax.DebugWindow.enabled=true; Wicket.Ajax.baseUrl="wicket/bookmarkable/org.apache.wicket.ajax.markup.html.componentMap.SimpleTestPage?0-1.0-testPanel-baseSpan-linja1"; /*]]]]><![CDATA[>*/ </script> -</head>]]></header-contribution><evaluate><![CDATA[(function(){Wicket.Timer.set('linja11', function(){Wicket.Ajax.ajax({"u":"./org.apache.wicket.ajax.markup.html.componentMap.SimpleTestPage?0-1.0-testPanel-baseSpan-linja1","c":"linja11"});}, 2000);})();]]></evaluate></ajax-response> \ No newline at end of file +</head>]]></header-contribution><evaluate><![CDATA[(function(){Wicket.Timer.set('linja11.0', function(){Wicket.Ajax.ajax({"u":"./org.apache.wicket.ajax.markup.html.componentMap.SimpleTestPage?0-1.0-testPanel-baseSpan-linja1","c":"linja11"});}, 2000);})();]]></evaluate></ajax-response> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/wicket/blob/23b7a0f5/wicket-core/src/test/java/org/apache/wicket/ajax/markup/html/componentMap/SimpleTestPageExpectedResult.html ---------------------------------------------------------------------- diff --git a/wicket-core/src/test/java/org/apache/wicket/ajax/markup/html/componentMap/SimpleTestPageExpectedResult.html b/wicket-core/src/test/java/org/apache/wicket/ajax/markup/html/componentMap/SimpleTestPageExpectedResult.html index 0eae403..617d53f 100644 --- a/wicket-core/src/test/java/org/apache/wicket/ajax/markup/html/componentMap/SimpleTestPageExpectedResult.html +++ b/wicket-core/src/test/java/org/apache/wicket/ajax/markup/html/componentMap/SimpleTestPageExpectedResult.html @@ -17,7 +17,7 @@ Wicket.Ajax.baseUrl="wicket/bookmarkable/org.apache.wicket.ajax.markup.html.comp <script type="text/javascript" > /*<![CDATA[*/ Wicket.Event.add(window, "load", function(event) { -Wicket.Timer.set('linja11', function(){Wicket.Ajax.ajax({"u":"./org.apache.wicket.ajax.markup.html.componentMap.SimpleTestPage?0-1.0-testPanel-baseSpan-linja1","c":"linja11"});}, 2000);; +Wicket.Timer.set('linja11.0', function(){Wicket.Ajax.ajax({"u":"./org.apache.wicket.ajax.markup.html.componentMap.SimpleTestPage?0-1.0-testPanel-baseSpan-linja1","c":"linja11"});}, 2000);; ;}); /*]]>*/ </script>