Commit b70b5714 authored by Tim Zallmann's avatar Tim Zallmann

Improvements to Emoji Loading Mechanism

Fixed Emojis FrontendFixture Creature
parent 7db81dd6
...@@ -13,8 +13,7 @@ class GlEmoji extends HTMLElement { ...@@ -13,8 +13,7 @@ class GlEmoji extends HTMLElement {
const { fallbackSpriteClass, fallbackSrc } = this.dataset; const { fallbackSpriteClass, fallbackSrc } = this.dataset;
let { name, unicodeVersion } = this.dataset; let { name, unicodeVersion } = this.dataset;
return initEmojiMap() return initEmojiMap().then(() => {
.then(() => {
if (!unicodeVersion) { if (!unicodeVersion) {
const emojiInfo = getEmojiInfo(name); const emojiInfo = getEmojiInfo(name);
...@@ -24,7 +23,7 @@ class GlEmoji extends HTMLElement { ...@@ -24,7 +23,7 @@ class GlEmoji extends HTMLElement {
this.dataset.name = emojiInfo.name; this.dataset.name = emojiInfo.name;
} }
unicodeVersion = emojiInfo.u; unicodeVersion = emojiInfo.u;
this.dataset.uni = unicodeVersion; this.dataset.unicodeVersion = unicodeVersion;
emojiUnicode = emojiInfo.e; emojiUnicode = emojiInfo.e;
this.innerHTML = emojiInfo.e; this.innerHTML = emojiInfo.e;
...@@ -64,10 +63,6 @@ class GlEmoji extends HTMLElement { ...@@ -64,10 +63,6 @@ class GlEmoji extends HTMLElement {
this.innerHTML = emojiImageTag(name, src); this.innerHTML = emojiImageTag(name, src);
} }
} }
})
.catch(error => {
// Only reject is already handled in initEmojiMap
throw error;
}); });
} }
} }
......
...@@ -5,19 +5,25 @@ import axios from '../lib/utils/axios_utils'; ...@@ -5,19 +5,25 @@ import axios from '../lib/utils/axios_utils';
import AccessorUtilities from '../lib/utils/accessor'; import AccessorUtilities from '../lib/utils/accessor';
let emojiMap = null; let emojiMap = null;
let emojiPromise = null;
let validEmojiNames = null; let validEmojiNames = null;
export const EMOJI_VERSION = '1'; export const EMOJI_VERSION = '1';
const EMOJI_VERSION_LOCALSTORAGE = `EMOJIS_${EMOJI_VERSION}`;
const isLocalStorageAvailable = AccessorUtilities.isLocalStorageAccessSafe(); const isLocalStorageAvailable = AccessorUtilities.isLocalStorageAccessSafe();
export function initEmojiMap() { export function initEmojiMap() {
return new Promise((resolve, reject) => { emojiPromise =
emojiPromise ||
new Promise((resolve, reject) => {
if (emojiMap) { if (emojiMap) {
resolve(emojiMap); resolve(emojiMap);
} else if (isLocalStorageAvailable && window.localStorage.getItem(EMOJI_VERSION_LOCALSTORAGE)) { } else if (
emojiMap = JSON.parse(window.localStorage.getItem(EMOJI_VERSION_LOCALSTORAGE)); isLocalStorageAvailable &&
window.localStorage.getItem('gl-emoji-map-version') === EMOJI_VERSION &&
window.localStorage.getItem('gl-emoji-map')
) {
emojiMap = JSON.parse(window.localStorage.getItem('gl-emoji-map'));
validEmojiNames = [...Object.keys(emojiMap), ...Object.keys(emojiAliases)]; validEmojiNames = [...Object.keys(emojiMap), ...Object.keys(emojiAliases)];
resolve(emojiMap); resolve(emojiMap);
} else { } else {
...@@ -31,7 +37,8 @@ export function initEmojiMap() { ...@@ -31,7 +37,8 @@ export function initEmojiMap() {
validEmojiNames = [...Object.keys(emojiMap), ...Object.keys(emojiAliases)]; validEmojiNames = [...Object.keys(emojiMap), ...Object.keys(emojiAliases)];
resolve(emojiMap); resolve(emojiMap);
if (isLocalStorageAvailable) { if (isLocalStorageAvailable) {
window.localStorage.setItem(EMOJI_VERSION_LOCALSTORAGE, JSON.stringify(emojiMap)); window.localStorage.setItem('gl-emoji-map-version', EMOJI_VERSION);
window.localStorage.setItem('gl-emoji-map', JSON.stringify(emojiMap));
} }
}) })
.catch(err => { .catch(err => {
...@@ -39,6 +46,8 @@ export function initEmojiMap() { ...@@ -39,6 +46,8 @@ export function initEmojiMap() {
}); });
} }
}); });
return emojiPromise;
} }
export function normalizeEmojiName(name) { export function normalizeEmojiName(name) {
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
require 'spec_helper' require 'spec_helper'
describe 'Emojis (JavaScript fixtures)', type: :request do RSpec.describe 'Emojis (JavaScript fixtures)', type: :request do
include JavaScriptFixturesHelpers include JavaScriptFixturesHelpers
before(:all) do before(:all) do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment