UNPKG

paella-core

Version:
79 lines (71 loc) 2.31 kB
import PlayerResource from "./PlayerResource"; import { setCookieIfAllowed, getCookie } from "./utils"; const g_defaultPreferences = '{ "global": {}, "videos": {} }'; async function load() { switch (this.source.name) { case "cookie": try { return JSON.parse(getCookie("preferences")); } catch (err) { return JSON.parse(g_defaultPreferences); } case "dataPlugin": try { const data = await this.player.data.read(this.source.context, {}); return data || JSON.parse(g_defaultPreferences); } catch (err) { return JSON.parse(g_defaultPreferences); } } } async function save(data) { switch (this.source.name) { case "cookie": setCookieIfAllowed(this.player, this.source.consentType, "preferences", JSON.stringify(data)); break; case "dataPlugin": await this.player.data.write(this.source.context, {}, data); break; } } export default class Preferences extends PlayerResource { constructor(player) { super(player); const { currentSource, sources } = player.config.preferences || { currentSource: "cookie", sources: { cookie: { consentType: "necessary" } } }; this.source = sources[currentSource]; this.source.name = currentSource; this._loaded = false; if (!this.source) { throw Error("Invalid configuration in preferences. Check the configuration file."); } } async set(key, value, { global = false } = {}) { const data = await load.apply(this); if (global) { data.global[key] = value; } else { data.videos[this.player.videoId] = data.videos[this.player.videoId] || {}; data.videos[this.player.videoId][key] = value; } await save.apply(this, [data]); } async get(key, { global = false } = {}) { const data = await load.apply(this); if (global) { return data.global[key]; } else { return data.videos[this.player.videoId] && data.videos[this.player.videoId][key] || undefined; } } }