Make image zoom fetch requests cancellable. Issue #1178
This commit is contained in:
parent
9de38b00b9
commit
fe7f6bb8df
@ -85,6 +85,9 @@ class ArticleViewController: UIViewController {
|
|||||||
|
|
||||||
deinit {
|
deinit {
|
||||||
if webView != nil {
|
if webView != nil {
|
||||||
|
webView?.evaluateJavaScript("cancelImageLoad();")
|
||||||
|
webView.configuration.userContentController.removeScriptMessageHandler(forName: MessageName.imageWasClicked)
|
||||||
|
webView.configuration.userContentController.removeScriptMessageHandler(forName: MessageName.imageWasShown)
|
||||||
webView.removeFromSuperview()
|
webView.removeFromSuperview()
|
||||||
ArticleViewControllerWebViewProvider.shared.enqueueWebView(webView)
|
ArticleViewControllerWebViewProvider.shared.enqueueWebView(webView)
|
||||||
webView = nil
|
webView = nil
|
||||||
@ -109,8 +112,6 @@ class ArticleViewController: UIViewController {
|
|||||||
webView.navigationDelegate = self
|
webView.navigationDelegate = self
|
||||||
webView.uiDelegate = self
|
webView.uiDelegate = self
|
||||||
|
|
||||||
webView.configuration.userContentController.removeScriptMessageHandler(forName: MessageName.imageWasClicked)
|
|
||||||
webView.configuration.userContentController.removeScriptMessageHandler(forName: MessageName.imageWasShown)
|
|
||||||
webView.configuration.userContentController.add(WrapperScriptMessageHandler(self), name: MessageName.imageWasClicked)
|
webView.configuration.userContentController.add(WrapperScriptMessageHandler(self), name: MessageName.imageWasClicked)
|
||||||
webView.configuration.userContentController.add(WrapperScriptMessageHandler(self), name: MessageName.imageWasShown)
|
webView.configuration.userContentController.add(WrapperScriptMessageHandler(self), name: MessageName.imageWasShown)
|
||||||
|
|
||||||
|
@ -1,23 +1,37 @@
|
|||||||
var imageIsLoading = false;
|
var controller = new AbortController()
|
||||||
|
|
||||||
|
// Cancel any pending image loads (there might be none) and reset the controller
|
||||||
|
function cancelImageLoad() {
|
||||||
|
controller.abort();
|
||||||
|
controller = new AbortController();
|
||||||
|
}
|
||||||
|
|
||||||
// Used to pop a resizable image view
|
// Used to pop a resizable image view
|
||||||
async function imageWasClicked(img) {
|
async function imageWasClicked(img) {
|
||||||
img.classList.add("nnwClicked");
|
cancelImageLoad();
|
||||||
|
showNetworkLoading(img);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
showNetworkLoading(img);
|
|
||||||
const response = await fetch(img.src);
|
const signal = controller.signal;
|
||||||
|
const response = await fetch(img.src, { signal: signal });
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new Error('Network response was not ok.');
|
throw new Error('Network response was not ok.');
|
||||||
}
|
}
|
||||||
|
|
||||||
const imgBlob = await response.blob();
|
const imgBlob = await response.blob();
|
||||||
|
if (signal.aborted) {
|
||||||
|
throw new Error('Network response was aborted.');
|
||||||
|
}
|
||||||
|
|
||||||
hideNetworkLoading(img);
|
hideNetworkLoading(img);
|
||||||
|
|
||||||
var reader = new FileReader();
|
var reader = new FileReader();
|
||||||
reader.readAsDataURL(imgBlob);
|
reader.readAsDataURL(imgBlob);
|
||||||
reader.onloadend = function() {
|
reader.onloadend = function() {
|
||||||
|
|
||||||
|
img.classList.add("nnwClicked");
|
||||||
|
|
||||||
const rect = img.getBoundingClientRect();
|
const rect = img.getBoundingClientRect();
|
||||||
var message = {
|
var message = {
|
||||||
x: rect.x,
|
x: rect.x,
|
||||||
@ -31,6 +45,7 @@ async function imageWasClicked(img) {
|
|||||||
window.webkit.messageHandlers.imageWasClicked.postMessage(jsonMessage);
|
window.webkit.messageHandlers.imageWasClicked.postMessage(jsonMessage);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
hideNetworkLoading(img);
|
hideNetworkLoading(img);
|
||||||
console.log('There has been a problem with your fetch operation: ', error.message);
|
console.log('There has been a problem with your fetch operation: ', error.message);
|
||||||
@ -39,7 +54,6 @@ async function imageWasClicked(img) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function showNetworkLoading(img) {
|
function showNetworkLoading(img) {
|
||||||
imageIsLoading = true;
|
|
||||||
|
|
||||||
var wrapper = document.createElement("div");
|
var wrapper = document.createElement("div");
|
||||||
wrapper.classList.add("activityIndicatorWrap");
|
wrapper.classList.add("activityIndicatorWrap");
|
||||||
@ -64,8 +78,6 @@ function hideNetworkLoading(img) {
|
|||||||
var wrapperParent = wrapper.parentNode;
|
var wrapperParent = wrapper.parentNode;
|
||||||
wrapperParent.insertBefore(img, wrapper);
|
wrapperParent.insertBefore(img, wrapper);
|
||||||
wrapperParent.removeChild(wrapper);
|
wrapperParent.removeChild(wrapper);
|
||||||
|
|
||||||
imageIsLoading = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Used to animate the transition to a fullscreen image
|
// Used to animate the transition to a fullscreen image
|
||||||
@ -85,7 +97,7 @@ function showClickedImage() {
|
|||||||
// Add the click listener for images
|
// Add the click listener for images
|
||||||
function imageClicks() {
|
function imageClicks() {
|
||||||
window.onclick = function(event) {
|
window.onclick = function(event) {
|
||||||
if (event.target.matches('img') && !imageIsLoading) {
|
if (event.target.matches('img')) {
|
||||||
imageWasClicked(event.target);
|
imageWasClicked(event.target);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -101,8 +113,9 @@ function inlineVideos() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function postRenderProcessing() {
|
function postRenderProcessing() {
|
||||||
imageClicks()
|
cancelImageLoad();
|
||||||
inlineVideos()
|
imageClicks();
|
||||||
|
inlineVideos();
|
||||||
}
|
}
|
||||||
|
|
||||||
const activityIndicator = "data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9Im5vIj8+PHN2ZyB4bWxuczpzdmc9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHhtbG5zOnhsaW5rPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5L3hsaW5rIiB2ZXJzaW9uPSIxLjAiIHdpZHRoPSI2NHB4IiBoZWl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