UNPKG

@nent/core

Version:

Functional elements to add routing, data-binding, dynamic HTML, declarative actions, audio, video, and so much more. Supercharge static HTML files into web apps without script or builds.

47 lines (43 loc) 1.35 kB
/*! * NENT 2022 */ 'use strict'; const mutex = require('./mutex-7ae5ebad.js'); const factory = require('./factory-0d7ddff9.js'); const memory = require('./memory-33fe6263.js'); const tracksKey = 'tracks'; const sessionFallback = new memory.InMemoryProvider(); const collectionMutex = new mutex.Mutex(); function parseTracks(tracks) { return JSON.parse(tracks || '[]'); } function stringifyTracks(tracks) { return JSON.stringify(tracks || '[]'); } async function getSessionTracks() { const unlock = await collectionMutex.lock(); var provider = (await factory.getDataProvider('session')) || sessionFallback; const tracks = await provider.get(tracksKey); unlock(); return tracks ? parseTracks(tracks) : []; } async function setSessionTracks(tracks) { return await collectionMutex.dispatch(async () => { const provider = ((await factory.getDataProvider('session')) || sessionFallback); await provider.set(tracksKey, stringifyTracks(tracks)); }); } async function playedTrack(trackId) { const tracks = await getSessionTracks(); return tracks.includes(trackId); } async function markTrack(trackId) { const tracks = await getSessionTracks(); if (tracks.includes(trackId)) return; tracks.push(trackId); await setSessionTracks(tracks); } exports.markTrack = markTrack; exports.playedTrack = playedTrack;