record-store.js 839 B

123456789101112131415161718192021222324252627282930313233343536
  1. const recordSymbolKey = Symbol('Record symbol')
  2. export default class RecordStore {
  3. constructor() {
  4. // Each track (or whatever) gets a symbol which is used as a key here to
  5. // store more information.
  6. this.data = {}
  7. }
  8. getRecord(obj) {
  9. if (typeof obj !== 'object') {
  10. throw new TypeError('Cannot get the record of a non-object')
  11. }
  12. if (!obj[recordSymbolKey]) {
  13. obj[recordSymbolKey] = Symbol('Reference to a record')
  14. }
  15. if (!this.data[obj[recordSymbolKey]]) {
  16. this.data[obj[recordSymbolKey]] = {}
  17. }
  18. return this.data[obj[recordSymbolKey]]
  19. }
  20. deleteRecord(obj) {
  21. if (typeof obj !== 'object') {
  22. throw new TypeError('Non-objects cannot have a record in the first place')
  23. }
  24. if (obj[recordSymbolKey]) {
  25. delete this.data[obj[recordSymbolKey]]
  26. }
  27. }
  28. }