From f5e93c6e9129d78a5ac79f6c17e4df85206e9aa5 Mon Sep 17 00:00:00 2001 From: Miguel Jimenez Esun Date: Sat, 14 Apr 2018 05:17:52 -0700 Subject: [PATCH] Remove PersistedMapStore Reviewed By: cpojer Differential Revision: D7628723 fbshipit-source-id: 0efb8d42da621870e46ab57a2c23775d5f0e373c --- packages/metro-cache/src/index.js | 2 - .../src/stores/PersistedMapStore.js | 75 ----------- .../__tests__/PersistedMapStore-test.js | 121 ------------------ 3 files changed, 198 deletions(-) delete mode 100644 packages/metro-cache/src/stores/PersistedMapStore.js delete mode 100644 packages/metro-cache/src/stores/__tests__/PersistedMapStore-test.js diff --git a/packages/metro-cache/src/index.js b/packages/metro-cache/src/index.js index c91b84b8..ddc33dc7 100644 --- a/packages/metro-cache/src/index.js +++ b/packages/metro-cache/src/index.js @@ -13,7 +13,6 @@ const Cache = require('./Cache'); const FileStore = require('./stores/FileStore'); const HttpStore = require('./stores/HttpStore'); -const PersistedMapStore = require('./stores/PersistedMapStore'); const stableHash = require('./stableHash'); @@ -22,6 +21,5 @@ export type {CacheStore} from './types.flow'; module.exports.Cache = Cache; module.exports.FileStore = FileStore; module.exports.HttpStore = HttpStore; -module.exports.PersistedMapStore = PersistedMapStore; module.exports.stableHash = stableHash; diff --git a/packages/metro-cache/src/stores/PersistedMapStore.js b/packages/metro-cache/src/stores/PersistedMapStore.js deleted file mode 100644 index 4c44dcd2..00000000 --- a/packages/metro-cache/src/stores/PersistedMapStore.js +++ /dev/null @@ -1,75 +0,0 @@ -/** - * Copyright (c) 2018-present, Facebook, Inc. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - * - * @format - * @flow - */ - -'use strict'; - -const fs = require('fs'); -const serializer = require('jest-serializer'); - -export type Options = {| - path: string, - writeDelay: ?number, -|}; - -class PersistedMapStore { - _map: ?Map; - _path: string; - _store: () => void; - _timeout: ?TimeoutID; - _writeDelay: number; - - constructor(options: Options) { - this._path = options.path; - this._writeDelay = options.writeDelay || 5000; - - this._store = this._store.bind(this); - this._timeout = null; - this._map = null; - } - - get(key: Buffer): mixed { - this._getMap(); - - if (this._map) { - return this._map.get(key.toString('hex')); - } - - return null; - } - - set(key: Buffer, value: mixed) { - this._getMap(); - - if (this._map) { - this._map.set(key.toString('hex'), value); - } - - if (!this._timeout) { - this._timeout = setTimeout(this._store, this._writeDelay); - } - } - - _getMap() { - if (!this._map) { - if (fs.existsSync(this._path)) { - this._map = serializer.readFileSync(this._path); - } else { - this._map = new Map(); - } - } - } - - _store() { - serializer.writeFileSync(this._path, this._map); - this._timeout = null; - } -} - -module.exports = PersistedMapStore; diff --git a/packages/metro-cache/src/stores/__tests__/PersistedMapStore-test.js b/packages/metro-cache/src/stores/__tests__/PersistedMapStore-test.js deleted file mode 100644 index bf9e5597..00000000 --- a/packages/metro-cache/src/stores/__tests__/PersistedMapStore-test.js +++ /dev/null @@ -1,121 +0,0 @@ -/** - * Copyright (c) 2018-present, Facebook, Inc. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - * - * @emails oncall+javascript_foundation - * @format - */ - -'use strict'; - -describe('PersistedMapStore', () => { - const key1 = Buffer.from('foo'); - const key2 = Buffer.from('bar'); - let now; - let serializer; - let fs; - let PersistedMapStore; - - function advance(time) { - now += time; - jest.advanceTimersByTime(time); - } - - Date.now = () => now; - - beforeEach(() => { - jest - .resetModules() - .resetAllMocks() - .useFakeTimers(); - - jest.mock('fs', () => ({ - existsSync: jest.fn(), - })); - - jest.mock('jest-serializer', () => ({ - readFileSync: jest.fn(), - writeFileSync: jest.fn(), - })); - - fs = require('fs'); - serializer = require('jest-serializer'); - PersistedMapStore = require('../PersistedMapStore'); - - now = 0; - }); - - it('ensures that the persisted map file is checked first', () => { - const store = new PersistedMapStore({path: '/foo'}); - - fs.existsSync.mockReturnValue(false); - store.get(key1); - - expect(fs.existsSync).toHaveBeenCalledTimes(1); - expect(serializer.readFileSync).not.toBeCalled(); - }); - - it('loads the file when it exists', () => { - const store = new PersistedMapStore({path: '/foo'}); - - fs.existsSync.mockReturnValue(true); - serializer.readFileSync.mockReturnValue(new Map()); - store.get(key1); - - expect(fs.existsSync).toHaveBeenCalledTimes(1); - expect(serializer.readFileSync).toHaveBeenCalledTimes(1); - expect(serializer.readFileSync.mock.calls[0]).toEqual(['/foo']); - }); - - it('throws if the file is invalid', () => { - const store = new PersistedMapStore({path: '/foo'}); - - fs.existsSync.mockReturnValue(true); - serializer.readFileSync.mockImplementation(() => { - throw new Error(); - }); - expect(() => store.get(key1)).toThrow(); - }); - - it('deserializes and serializes correctly from/to disk', () => { - let file; - - fs.existsSync.mockReturnValue(false); - serializer.readFileSync.mockImplementation(() => file); - serializer.writeFileSync.mockImplementation((_, data) => (file = data)); - - const store1 = new PersistedMapStore({path: '/foo'}); - - store1.set(key1, 'value1'); - store1.set(key2, 123456); - - // Force throttle to kick in and perform the file storage. - advance(7500); - fs.existsSync.mockReturnValue(true); - - const store2 = new PersistedMapStore({path: '/foo'}); - - expect(store2.get(key1)).toBe('value1'); - expect(store2.get(key2)).toBe(123456); - }); - - it('ensures that the throttling is working correctly', () => { - const store1 = new PersistedMapStore({ - path: '/foo', - writeDelay: 1234, - }); - - // Triggers the write, multiple times (only one write should happen). - store1.set(key1, 'foo'); - store1.set(key1, 'bar'); - store1.set(key1, 'baz'); - - advance(1233); - expect(serializer.writeFileSync).toHaveBeenCalledTimes(0); - - advance(1); - expect(serializer.writeFileSync).toHaveBeenCalledTimes(1); - }); -});