mirror of
https://github.com/discourse/discourse.git
synced 2024-12-16 14:23:43 +08:00
f9608c0af5
There were two constants here, `INLINE_ONEBOX_LOADING_CSS_CLASS` and `INLINE_ONEBOX_CSS_CLASS` that were both longer than the strings they were DRYing up: `inline-onebox-loading` and `inline-onebox` I normally appreciate constants, but in this case it meant that we had a lot of JS imports resulting in many more lines of code (and CPU cycles spent figuring them out.) It also meant we had an `.erb` file and had to invoke Ruby to create the JS file, which meant the app was harder to port to Ember CLI. I removed the constants. It's less DRY but faster and simpler, and arguably the loss of DRYness is not significant as you can still search for the `inline-onebox-loading` and `inline-onebox` strings easily if you are refactoring.
44 lines
1004 B
JavaScript
44 lines
1004 B
JavaScript
const _cache = {};
|
|
|
|
export function applyInlineOneboxes(inline, ajax, opts) {
|
|
opts = opts || {};
|
|
|
|
Object.keys(inline).forEach(url => {
|
|
// cache a blank locally, so we never trigger a lookup
|
|
_cache[url] = {};
|
|
});
|
|
|
|
return ajax("/inline-onebox", {
|
|
data: {
|
|
urls: Object.keys(inline),
|
|
category_id: opts.categoryId,
|
|
topic_id: opts.topicId
|
|
}
|
|
}).then(result => {
|
|
result["inline-oneboxes"].forEach(onebox => {
|
|
if (onebox.title) {
|
|
_cache[onebox.url] = onebox;
|
|
let links = inline[onebox.url] || [];
|
|
links.forEach(link => {
|
|
$(link)
|
|
.text(onebox.title)
|
|
.addClass("inline-onebox")
|
|
.removeClass("inline-onebox-loading");
|
|
});
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
export function cachedInlineOnebox(url) {
|
|
return _cache[url];
|
|
}
|
|
|
|
export function applyCachedInlineOnebox(url, onebox) {
|
|
return (_cache[url] = onebox);
|
|
}
|
|
|
|
export function deleteCachedInlineOnebox(url) {
|
|
return delete _cache[url];
|
|
}
|