Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@
<version>4.13.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.18.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.flowingcode.vaadin.addons.demo</groupId>
<artifactId>commons-demo</artifactId>
Expand Down
84 changes: 73 additions & 11 deletions src/main/java/com/flowingcode/vaadin/addons/rssitems/RssItems.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@
import com.vaadin.flow.component.HasSize;
import com.vaadin.flow.component.HasStyle;
import com.vaadin.flow.component.Tag;
import com.vaadin.flow.component.UI;
import com.vaadin.flow.component.dependency.JsModule;
import com.vaadin.flow.component.dependency.NpmPackage;
import com.vaadin.flow.internal.StateTree.ExecutionRegistration;

/**
* Simple RSS Reader component based on https://github.com/TherapyChat/rss-items
Expand All @@ -54,7 +56,9 @@ public class RssItems extends Component implements HasSize, HasStyle {

private boolean extractImageFromDescription;

private static final String ERROR_RSS = "<rss>\r\n" +
private ExecutionRegistration pendingRefreshRegistration;

public static final String ERROR_RSS = "<rss>\r\n" +
" <channel>\r\n" +
" <item>\r\n" +
" <title>Error Retrieving RSS</title>\r\n" +
Expand All @@ -63,7 +67,7 @@ public class RssItems extends Component implements HasSize, HasStyle {
" <thumbnail url=\"https://\"></thumbnail>\r\n" +
" </item>";

private static final String IMAGE_METHOD = " $0._getItemImageScr = function (item) {\r\n" +
public static final String IMAGE_METHOD = " $0._getItemImageScr = function (item) {\r\n" +
" var element = document.createElement('div');\r\n" +
" element.innerHTML = item.%%ATTRIBUTE_NAME%%;\r\n" +
" var image = element.querySelector('img') || {};\r\n" +
Expand All @@ -72,11 +76,11 @@ public class RssItems extends Component implements HasSize, HasStyle {
"";


private static final int DEFAULT_MAX = Integer.MAX_VALUE;
public static final int DEFAULT_MAX = Integer.MAX_VALUE;

private static final int DEFAULT_MAX_TITLE_LENGTH = 50;
public static final int DEFAULT_MAX_TITLE_LENGTH = 50;

private static final int DEFAULT_MAX_EXCERPT_LENGTH = 100;
public static final int DEFAULT_MAX_EXCERPT_LENGTH = 100;

/**
* @param url rss feed url
Expand All @@ -102,7 +106,6 @@ public RssItems(String url, int max, int maxTitleLength, int maxExcerptLength, b
this.setMaxTitleLength(maxTitleLength);
addClassName("x-scope");
addClassName("rss-items-0");
refreshUrl();
}

/**
Expand All @@ -112,7 +115,49 @@ public RssItems(String url) {
this(url,DEFAULT_MAX, DEFAULT_MAX_TITLE_LENGTH, DEFAULT_MAX_EXCERPT_LENGTH, false);
}

/**
* Constructor for testing purposes.
*/
protected RssItems() {
}

private void scheduleRefresh() {
UI ui = UI.getCurrent();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • The check is not where there is a current UI, but whether the component is attached (refreshUrl calls invokeXmlToItems which calls this.getElement().executeJs which requires the element to be attached).
  • If the component is not attached, scheduleRefresh will do nothing.
  • Call scheduleRefresh on attach.

if (ui == null) {
// If no UI is available (e.g., background thread or testing without proper UI setup),
// consider an immediate refresh or log a warning.
// For now, let's do an immediate refresh as a fallback,
// though this might not be ideal in all detached scenarios.
// This matches the original behavior more closely if UI isn't available.
refreshUrl();
return;
}

// If there's a pending registration, remove it.
if (pendingRefreshRegistration != null) {
try {
pendingRefreshRegistration.remove();
} catch (Exception e) {
// Log or handle potential exceptions if .remove() fails, though typically it shouldn't.
// For example, if the registration is already inactive.
System.err.println("Error removing pending refresh registration: " + e.getMessage());
}
pendingRefreshRegistration = null;
}

// Schedule refreshUrl to be called before the client response.
// The lambda uiParam -> refreshUrl() is used because the consumer takes the UI as a parameter.
pendingRefreshRegistration = ui.beforeClientResponse(this, uiParam -> refreshUrl());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Schedule in the UI to which the component is attached.

}

private void refreshUrl() {
if (pendingRefreshRegistration != null) {
// If refreshUrl is called directly while a refresh was scheduled,
// the scheduled one is now effectively preempted or redundant.
// We nullify the registration to reflect this.
// Note: We don't call .remove() here as this method IS the execution (or a direct call).
pendingRefreshRegistration = null;
}
try {
String rss = obtainRss(url);
invokeXmlToItems(rss);
Expand All @@ -126,7 +171,7 @@ private void invokeXmlToItems(String rss) {
this.getElement().executeJs("this.xmlToItems($0)", rss);
}

private String obtainRss(String url) throws ClientProtocolException, IOException {
protected String obtainRss(String url) throws ClientProtocolException, IOException {
HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(URI.create(url));
request.addHeader("Content-Type", "application/xml");
Expand Down Expand Up @@ -155,7 +200,7 @@ public void setAuto(boolean auto) {
*/
public void setMaxTitleLength(int length) {
this.getElement().setProperty("maxTitleLength", length);
refreshUrl();
scheduleRefresh();
}

/**
Expand All @@ -164,7 +209,7 @@ public void setMaxTitleLength(int length) {
*/
public void setMaxExcerptLength(int length) {
this.getElement().setProperty("maxExcerptLength", length);
refreshUrl();
scheduleRefresh();
}

/**
Expand All @@ -173,12 +218,12 @@ public void setMaxExcerptLength(int length) {
*/
public void setMax(int max) {
this.getElement().setProperty("max", max);
refreshUrl();
scheduleRefresh();
}

public void setExtractImageFromDescription(boolean extractImageFromDescription) {
this.extractImageFromDescription = extractImageFromDescription;
refreshUrl();
scheduleRefresh();
}

/**
Expand All @@ -188,6 +233,23 @@ public void setExtractImageFromDescription(boolean extractImageFromDescription)
public void setUrl(String url) {
this.getElement().setProperty("url", url);
this.url = url;
scheduleRefresh();
}

/**
* Refreshes the RSS feed.
*/
public void refresh() {
if (pendingRefreshRegistration != null) {
try {
pendingRefreshRegistration.remove();
} catch (Exception e) {
// Log or handle potential exceptions.
System.err.println("Error removing pending refresh registration during manual refresh: " + e.getMessage());
}
pendingRefreshRegistration = null;
}
refreshUrl();
}

}
Loading
Loading