DEV: Convert emoji-store into a native class (#23572)

This commit is contained in:
Jarek Radosz 2023-09-14 10:32:17 +02:00 committed by GitHub
parent a4238a3726
commit 91e456ba30
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 17 additions and 16 deletions

View File

@ -1,39 +1,41 @@
import KeyValueStore from "discourse/lib/key-value-store";
import Service from "@ember/service";
import KeyValueStore from "discourse/lib/key-value-store";
import { disableImplicitInjections } from "discourse/lib/implicit-injections";
const EMOJI_USAGE = "emojiUsage";
const EMOJI_SELECTED_DIVERSITY = "emojiSelectedDiversity";
const TRACKED_EMOJIS = 15;
const STORE_NAMESPACE = "discourse_emojis_";
export default Service.extend({
init() {
this._super(...arguments);
@disableImplicitInjections
export default class EmojiStore extends Service {
store = new KeyValueStore(STORE_NAMESPACE);
this.store = new KeyValueStore(STORE_NAMESPACE);
constructor() {
super(...arguments);
if (!this.store.getObject(EMOJI_USAGE)) {
this.favorites = [];
}
},
}
get diversity() {
return this.store.getObject(EMOJI_SELECTED_DIVERSITY) || 1;
},
}
set diversity(value) {
this.store.setObject({ key: EMOJI_SELECTED_DIVERSITY, value: value || 1 });
this.notifyPropertyChange("diversity");
},
}
get favorites() {
return this.store.getObject(EMOJI_USAGE) || [];
},
}
set favorites(value) {
this.store.setObject({ key: EMOJI_USAGE, value: value || [] });
this.notifyPropertyChange("favorites");
},
}
track(code) {
const normalizedCode = code.replace(/(^:)|(:$)/g, "");
@ -41,11 +43,10 @@ export default Service.extend({
recent.unshift(normalizedCode);
recent.length = Math.min(recent.length, TRACKED_EMOJIS);
this.favorites = recent;
},
}
reset() {
const store = new KeyValueStore(STORE_NAMESPACE);
store.setObject({ key: EMOJI_USAGE, value: [] });
store.setObject({ key: EMOJI_SELECTED_DIVERSITY, value: 1 });
},
});
this.store.setObject({ key: EMOJI_USAGE, value: [] });
this.store.setObject({ key: EMOJI_SELECTED_DIVERSITY, value: 1 });
}
}