« get me outta code hell

Initial commit - mtui - Music Text User Interface - user-friendly command line music player
about summary refs log tree commit diff
path: root/index.js
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2018-05-28 22:42:37 -0300
committerFlorrie <towerofnix@gmail.com>2018-05-28 22:42:37 -0300
commitb64a14b761a38da13b81370828a2d5e9ad68c330 (patch)
tree09449457e9b2dc4a6a1d1828e8c4371e0fb47eff /index.js
Initial commit
Diffstat (limited to 'index.js')
-rw-r--r--index.js47
1 files changed, 47 insertions, 0 deletions
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..6edbd01
--- /dev/null
+++ b/index.js
@@ -0,0 +1,47 @@
+// omg I am tired of code
+
+const { getPlayer } = require('./players')
+const { getDownloaderFor } = require('./downloaders')
+const EventEmitter = require('events')
+
+class InternalApp extends EventEmitter {
+  constructor() {
+    super()
+
+    // downloadCache [downloaderFunction] [downloaderArg]
+    this.downloadCache = new Map()
+  }
+
+  async download(arg) {
+    const downloader = getDownloaderFor(arg)
+    if (this.downloadCache.has(downloader)) {
+      const category = this.downloadCache.get(downloader)
+      if (category.hasOwnProperty(arg)) {
+        return category[arg]
+      }
+    }
+
+    const ret = await this.downloadIgnoringCache(arg)
+
+    if (!this.downloadCache.has(downloader)) {
+      this.downloadCache.set(downloader, {})
+    }
+
+    this.downloadCache.get(downloader)[arg] = ret
+
+    return ret
+  }
+
+  downloadIgnoringCache(arg) {
+    const downloader = getDownloaderFor(arg)
+    return downloader(arg)
+  }
+}
+
+async function main() {
+  const internalApp = new InternalApp()
+  const player = await getPlayer()
+  player.playFile(await internalApp.download('http://billwurtz.com/cable-television.mp3'))
+}
+
+main().catch(err => console.error(err))