Last active
February 9, 2022 17:26
-
-
Save Ch4s3/49a018d32e93bbf7d52064ad9b314d78 to your computer and use it in GitHub Desktop.
Revisions
-
Ch4s3 revised this gist
Oct 24, 2017 . 1 changed file with 3 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -75,7 +75,7 @@ const isNotGetMethod = function(link) { 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; @@ -104,6 +104,8 @@ const preload = function(event) { 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()); -
Ch4s3 revised this gist
Oct 24, 2017 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -75,7 +75,7 @@ const isNotGetMethod = function(link) { link.attributes['data-method'] && (link.attributes['data-method'].value !== 'get') } //This function returns true if the link or location shouldn't be preloaded const notPreloadable = function(link, location){ if (preloadAttribute(link) === false) { return true; -
Ch4s3 revised this gist
Oct 24, 2017 . 1 changed file with 34 additions and 49 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,32 +1,23 @@ /* * decaffeinate suggestions: * DS102: Remove unnecessary code created because of implicit returns * 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) { super(); super(this, location, referrer); } requestCompletedWithResponse(response, redirectedToLocation) { this.response = response; return(this.redirect = redirectedToLocation); } requestFailedWithStatusCode(code) { return(this.failCode = code); } oldSend() { @@ -35,17 +26,17 @@ Turbolinks.CachedHttpRequest = class CachedHttpRequest extends Turbolinks.HttpRe this.setProgress(0); this.xhr.send(); this.sent = true; return(this.delegate, 'requestStarted', o => o.requestStarted()); } } send() { if (this.failCode) { return(this.delegate.requestFailedWithStatusCode(this.failCode, this.failText)); } else if (this.response) { return(this.delegate.requestCompletedWithResponse(this.response, this.redirect)); } else { return(this.oldSend()); } } }; @@ -61,72 +52,66 @@ Turbolinks.HttpRequest = class HttpRequest { cache.delegate = delegate; return cache; } else { return(new OldHttpRequest(delegate, location, referrer)); } } }; Turbolinks.SnapshotCache.prototype.delete = function(location) { const key = Turbolinks.Location.wrap(location).toCacheKey(); return(delete this.snapshots[key]); }; const preloadAttribute = function(link) { 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'] && (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 = Turbolinks.controller.getVisitableLinkForNode(event.target); if (link) { let location = Turbolinks.controller.getVisitableLocationForLink(link); if (location) { if (Turbolinks.controller.applicationAllowsFollowingLinkToLocation(link, location)) { if (notPreloadable(link, location)) { 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}`); const request = new Turbolinks.CachedHttpRequest(null, location, window.location); Turbolinks.controller.cache.put(`prefetch${location}`, request); return(request.send()); } } } } }; document.addEventListener("touchstart", preload); document.addEventListener("mouseover", preload); -
Ch4s3 created this gist
Oct 24, 2017 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,132 @@ /* * 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(this, location, referrer); } requestCompletedWithResponse(response, redirectedToLocation) { this.response = response; return this.redirect = redirectedToLocation; } requestFailedWithStatusCode(code) { return this.failCode = code; } oldSend() { if (this.xhr && !this.sent) { this.notifyApplicationBeforeRequestStart(); this.setProgress(0); this.xhr.send(); this.sent = true; return __guardMethod__(this.delegate, 'requestStarted', o => o.requestStarted()); } } send() { if (this.failCode) { return this.delegate.requestFailedWithStatusCode(this.failCode, this.failText); } else if (this.response) { return this.delegate.requestCompletedWithResponse(this.response, this.redirect); } else { return this.oldSend(); } } }; Turbolinks.HttpRequest = class HttpRequest { constructor(delegate, location, referrer) { const cache = Turbolinks.controller.cache.get(`prefetch${location}`); if (cache) { //Turbolinks.controller.cache = new Turbolinks.SnapshotCache 10 Turbolinks.controller.cache.delete(`prefetch${location}`); console.log(JSON.stringify(Turbolinks.controller.cache.keys)); cache.delegate = delegate; return cache; } else { return new OldHttpRequest(delegate, location, referrer); } } }; Turbolinks.SnapshotCache.prototype.delete = function(location) { const key = Turbolinks.Location.wrap(location).toCacheKey(); return delete this.snapshots[key]; }; const preloadAttribute = function(link) { const attr = link.attributes['data-turbolinks-preload'] if (attr == null || attr.value === 'false') { return false; } else { return true; } } const isNotGetMethod = function(link) { (link.attributes['data-method'] != null) && (method.value !== 'get') } const preload = function(event) { let link; if (link = Turbolinks.controller.getVisitableLinkForNode(event.target)) { let location; if (location = Turbolinks.controller.getVisitableLocationForLink(link)) { if (Turbolinks.controller.applicationAllowsFollowingLinkToLocation(link, location)) { let method; if (preloadAttribute(link) === false) { return; } if (isNotGetMethod(link)) { return; } if (location.anchor || location.absoluteURL.endsWith("#")) { 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(); } } } } }; 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; } }