martinandert/babel-plugin-css-in-js

View on GitHub
src/utils/MemoryCache.js

Summary

Maintainability
A
0 mins
Test Coverage
const hasOwnProperty = Object.prototype.hasOwnProperty;
const cache = {};

export default class MemoryCache {
  constructor(name) {
    this.name = name;

    this.fetch.bind(this);
    this.clear.bind(this);

    cache[name] = cache[name] || {};
  }

  fetch(key, miss) {
    if (hasOwnProperty.call(cache[this.name], key)) {
      return cache[this.name][key];
    }

    cache[this.name][key] = miss(Object.keys(cache[this.name]));
    return cache[this.name][key];
  }

  clear() {
    cache[this.name] = {};
  }
}