Skip to content

Commit

Permalink
#64 Implemented timeout for requesting embed information
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamminf committed May 9, 2018
1 parent 4fe7222 commit 7149107
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 24 deletions.
76 changes: 54 additions & 22 deletions client/src/classes/Form.js
Expand Up @@ -60,17 +60,6 @@ export default class Form extends Emitter
this.request(url)
})

this._callbackName = uniqueId('embeddedAssets_')
window[this._callbackName] = () =>
{
const isPreviewUrl = Boolean(this._getCurrentPreviewUrl())

if (isPreviewUrl)
{
this.setState('requested')
}
}

this._monitorPreviewHeight()
this.setState('idle')
}
Expand All @@ -92,14 +81,30 @@ export default class Form extends Emitter

request(url = this.$input.val())
{
if (this._url !== url)
if (this._state !== 'saving' && this._url !== url)
{
this._url = url

if (isUrl(url))
{
this._setPreview(url)
const isRequesting = () => this._url === url && this._state === 'requesting'
this.setState('requesting')
this._setPreview(url)
.then(() =>
{
if (isRequesting())
{
this.setState('requested')
}
})
.catch(() =>
{
if (isRequesting())
{
Craft.cp.displayError(Craft.t('embeddedassets', `Could not retrieve embed information.`))
this.setState('idle')
}
})
}
else
{
Expand All @@ -126,7 +131,7 @@ export default class Form extends Emitter
{
Craft.queueActionRequest('embeddedassets/actions/save', { url, folderId }, (response, status) =>
{
if (status === 'success' && response.success)
if (this._state === 'saving' && status === 'success' && response.success)
{
this.clear()
this.trigger('save', response.payload)
Expand Down Expand Up @@ -176,16 +181,43 @@ export default class Form extends Emitter
}
}

_setPreview(url)
_setPreview(url, timeout = 15000)
{
const callback = this._callbackName
const previewUrl = url ? Craft.getActionUrl('embeddedassets/actions/preview', { url, callback }) : 'about:blank'
const previewWindow = this.$preview[0].contentWindow

if (previewWindow)
return new Promise((resolve, reject) =>
{
previewWindow.location.replace(previewUrl)
}
const previewWindow = this.$preview[0].contentWindow

if (previewWindow)
{
const isPreviewUrl = Boolean(url)
let previewUrl = 'about:blank'

if (isPreviewUrl)
{
const callback = uniqueId('embeddedAssets_')
const cleanup = () => delete window[callback]

previewUrl = Craft.getActionUrl('embeddedassets/actions/preview', { url, callback })

// On load
window[callback] = () => { resolve(); cleanup() }

// On timeout
setTimeout(() => { reject(); cleanup() }, timeout)
}

previewWindow.location.replace(previewUrl)

if (!isPreviewUrl)
{
resolve()
}
}
else
{
reject()
}
})
}

_getCurrentPreviewUrl()
Expand Down
3 changes: 2 additions & 1 deletion client/src/utilities.js
Expand Up @@ -9,9 +9,10 @@ export function monkeypatch(Class, method, callback)
}
}

let uidCounter = 0
export function uniqueId(prefix = 'uid')
{
return prefix + Math.random().toString(36).substr(2)
return prefix + Math.random().toString(36).substr(2) + (uidCounter++)
}

export function isUrl(url)
Expand Down

0 comments on commit 7149107

Please sign in to comment.