« get me outta code hell

record-store.js - mtui - Music Text User Interface - user-friendly command line music player
about summary refs log tree commit diff
path: root/record-store.js
blob: 2686457dfddd465c67c22197d74409cb07dcd828 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
const recordSymbolKey = Symbol('Record symbol')

export default class RecordStore {
  constructor() {
    // Each track (or whatever) gets a symbol which is used as a key here to
    // store more information.
    this.data = {}
  }

  getRecord(obj) {
    if (typeof obj !== 'object') {
      throw new TypeError('Cannot get the record of a non-object')
    }

    if (!obj[recordSymbolKey]) {
      obj[recordSymbolKey] = Symbol('Reference to a record')
    }

    if (!this.data[obj[recordSymbolKey]]) {
      this.data[obj[recordSymbolKey]] = {}
    }

    return this.data[obj[recordSymbolKey]]
  }

  deleteRecord(obj) {
    if (typeof obj !== 'object') {
      throw new TypeError('Non-objects cannot have a record in the first place')
    }

    if (obj[recordSymbolKey]) {
      delete this.data[obj[recordSymbolKey]]
    }
  }
}