Skip to content

Commit

Permalink
fixed decaf issues, cleaned up code
Browse files Browse the repository at this point in the history
  • Loading branch information
Ch4s3 committed Oct 24, 2017
1 parent 6ac53b2 commit f1f3690
Showing 1 changed file with 34 additions and 49 deletions.
83 changes: 34 additions & 49 deletions app/javascript/ink-stream/turboPatch.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,23 @@
/*
* decaffeinate suggestions:
* DS001: Remove Babel/TypeScript constructor workaround
* DS102: Remove unnecessary code created because of implicit returns
* DS103: Rewrite code to no longer use __guard__
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const OldHttpRequest = Turbolinks.HttpRequest;

Turbolinks.CachedHttpRequest = class CachedHttpRequest extends Turbolinks.HttpRequest {
constructor(_, location, referrer) {
{
// Hack: trick Babel/TypeScript into allowing this before super.
if (false) { super(); }
let thisFn = (() => { this; }).toString();
let thisName = thisFn.slice(thisFn.indexOf('{') + 1, thisFn.indexOf(';')).trim();
eval(`${thisName} = this;`);
}
super();
super(this, location, referrer);
}

requestCompletedWithResponse(response, redirectedToLocation) {
this.response = response;
return this.redirect = redirectedToLocation;
return(this.redirect = redirectedToLocation);
}

requestFailedWithStatusCode(code) {
return this.failCode = code;
return(this.failCode = code);
}

oldSend() {
Expand All @@ -35,17 +26,17 @@ Turbolinks.CachedHttpRequest = class CachedHttpRequest extends Turbolinks.HttpRe
this.setProgress(0);
this.xhr.send();
this.sent = true;
return __guardMethod__(this.delegate, 'requestStarted', o => o.requestStarted());
return(this.delegate, 'requestStarted', o => o.requestStarted());
}
}

send() {
if (this.failCode) {
return this.delegate.requestFailedWithStatusCode(this.failCode, this.failText);
return(this.delegate.requestFailedWithStatusCode(this.failCode, this.failText));
} else if (this.response) {
return this.delegate.requestCompletedWithResponse(this.response, this.redirect);
return(this.delegate.requestCompletedWithResponse(this.response, this.redirect));
} else {
return this.oldSend();
return(this.oldSend());
}
}
};
Expand All @@ -61,72 +52,66 @@ Turbolinks.HttpRequest = class HttpRequest {
cache.delegate = delegate;
return cache;
} else {
return new OldHttpRequest(delegate, location, referrer);
return(new OldHttpRequest(delegate, location, referrer));
}
}
};

Turbolinks.SnapshotCache.prototype.delete = function(location) {
const key = Turbolinks.Location.wrap(location).toCacheKey();
return delete this.snapshots[key];
return(delete this.snapshots[key]);
};

const preloadAttribute = function(link) {
const attr = link.attributes['data-turbolinks-preload']
if (attr == null || attr.value === 'false') {
const linkAttr = link.attributes['data-turbolinks-preload']
if (!linkAttr || linkAttr.value === 'false') {
return false;
} else {
return true;
}
}

const isNotGetMethod = function(link) {
(link.attributes['data-method'] != null) && (method.value !== 'get')
link.attributes['data-method'] && (link.attributes['data-method'].value !== 'get')
}

//This function returns true if the link or location shouldn't be
const notPreloadable = function(link, location){
if (preloadAttribute(link) === false) {
return true;
} else if (isNotGetMethod(link)) {
return true;
} else if (location.anchor || location.absoluteURL.endsWith("#")) {
return true;
} else if (location.absoluteURL === window.location.href) {
return true;
} else {
return false;
}
}

const preload = function(event) {
let link;
if (link = Turbolinks.controller.getVisitableLinkForNode(event.target)) {
let location;
if (location = Turbolinks.controller.getVisitableLocationForLink(link)) {
let link = Turbolinks.controller.getVisitableLinkForNode(event.target);
if (link) {
let location = Turbolinks.controller.getVisitableLocationForLink(link);
if (location) {
if (Turbolinks.controller.applicationAllowsFollowingLinkToLocation(link, location)) {
let method;
if (preloadAttribute(link) === false) {
return;
}
if (isNotGetMethod(link)) {
return;
}
if (location.anchor || location.absoluteURL.endsWith("#")) {
if (notPreloadable(link, location)) {
return;
}
if (location.absoluteURL === window.location.href) {
return;
}

// If Turbolinks has already cached this location internally, use that default behavior
// otherwise we can try and prefetch it here
let cache = Turbolinks.controller.cache.get(location);
if (!cache) {
cache = Turbolinks.controller.cache.get(`prefetch${location}`);
}

if (!cache) {
const request = new Turbolinks.CachedHttpRequest(null, location, window.location);
Turbolinks.controller.cache.put(`prefetch${location}`, request);
return request.send();
return(request.send());
}
}
}
}
};

document.addEventListener("touchstart", preload);
document.addEventListener("mouseover", preload);
function __guardMethod__(obj, methodName, transform) {
if (typeof obj !== 'undefined' && obj !== null && typeof obj[methodName] === 'function') {
return transform(obj, methodName);
} else {
return undefined;
}
}
document.addEventListener("mouseover", preload);

0 comments on commit f1f3690

Please sign in to comment.