From 881cbbd17f833ad3945783c1cdbcf3c02a5c2848 Mon Sep 17 00:00:00 2001 From: Matthew Dapena-Tretter Date: Tue, 4 Apr 2017 13:45:14 -0700 Subject: [PATCH 1/2] Add `delete()` method to StateStore This allows us to delete single items. --- spec/state-store-spec.js | 11 ++++++++++- src/state-store.js | 15 +++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/spec/state-store-spec.js b/spec/state-store-spec.js index 95fdcb71b..0b62066c8 100644 --- a/spec/state-store-spec.js +++ b/spec/state-store-spec.js @@ -7,13 +7,22 @@ describe("StateStore", () => { let databaseName = `test-database-${Date.now()}` let version = 1 - it("can save and load states", () => { + it("can save, load, and delete states", () => { const store = new StateStore(databaseName, version) return store.save('key', {foo:'bar'}) .then(() => store.load('key')) .then((state) => { expect(state).toEqual({foo:'bar'}) }) + .then(() => store.delete('key')) + .then(() => store.load('key')) + .then((value) => { + expect(value).toBeNull() + }) + .then(() => store.count()) + .then((count) => { + expect(count).toBe(0) + }) }) it("resolves with null when a non-existent key is loaded", () => { diff --git a/src/state-store.js b/src/state-store.js index e16857580..278696cb4 100644 --- a/src/state-store.js +++ b/src/state-store.js @@ -77,6 +77,21 @@ class StateStore { }) } + delete (key) { + return new Promise((resolve, reject) => { + this.dbPromise.then((db) => { + if (db == null) return resolve() + + var request = db.transaction(['states'], 'readwrite') + .objectStore('states') + .delete(key) + + request.onsuccess = resolve + request.onerror = reject + }) + }) + } + clear () { return this.dbPromise.then((db) => { if (!db) return From fed2372c30f90d7c4a4832ab86457a6cae5ec906 Mon Sep 17 00:00:00 2001 From: Matthew Dapena-Tretter Date: Tue, 4 Apr 2017 13:48:09 -0700 Subject: [PATCH 2/2] Don't store default locations This will probably be less confusing for developers experimenting with changing the default location. Also, we avoid storing extra info we don't really need. --- spec/workspace-spec.js | 27 +++++++++++++++++++++------ src/workspace.js | 12 +++++++++++- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/spec/workspace-spec.js b/spec/workspace-spec.js index ba4d4496b..822b0ab5a 100644 --- a/spec/workspace-spec.js +++ b/spec/workspace-spec.js @@ -266,14 +266,13 @@ describe('Workspace', () => { const ITEM_URI = 'atom://test' const item = { getURI: () => ITEM_URI, - getDefaultLocation: jasmine.createSpy().andReturn('left'), + getDefaultLocation: () => 'left', getElement: () => document.createElement('div') } dock.getActivePane().addItem(item) expect(dock.getPaneItems()).toHaveLength(1) waitsForPromise(() => atom.workspace.open(ITEM_URI, {searchAllPanes: true})) runs(() => { - expect(item.getDefaultLocation).not.toHaveBeenCalled() expect(atom.workspace.getPaneItems()).toHaveLength(1) expect(dock.getPaneItems()).toHaveLength(1) expect(dock.getPaneItems()[0]).toBe(item) @@ -330,7 +329,7 @@ describe('Workspace', () => { const ITEM_URI = 'atom://test' const item = { getURI: () => ITEM_URI, - getDefaultLocation: jasmine.createSpy().andReturn('left'), + getDefaultLocation: () => 'left', getElement: () => document.createElement('div') } const opener = uri => uri === ITEM_URI ? item : null @@ -344,7 +343,6 @@ describe('Workspace', () => { runs(() => { expect(dock.getPaneItems()).toHaveLength(1) expect(dock.getPaneItems()[0]).toBe(item) - expect(item.getDefaultLocation).not.toHaveBeenCalled() }) }) }) @@ -2328,11 +2326,11 @@ i = /test/; #FIXME\ }) describe('when an item is moved', () => { - it('stores the new location', () => { + it("stores the new location if it's not the default", () => { const ITEM_URI = 'atom://test' const item = { getURI: () => ITEM_URI, - getDefaultLocation: jasmine.createSpy().andReturn('left'), + getDefaultLocation: () => 'left', getElement: () => document.createElement('div') } const centerPane = workspace.getActivePane() @@ -2342,6 +2340,23 @@ i = /test/; #FIXME\ centerPane.moveItemToPane(item, dockPane) expect(workspace.itemLocationStore.save).toHaveBeenCalledWith(ITEM_URI, 'right') }) + + it("clears the location if it's the default", () => { + const ITEM_URI = 'atom://test' + const item = { + getURI: () => ITEM_URI, + getDefaultLocation: () => 'right', + getElement: () => document.createElement('div') + } + const centerPane = workspace.getActivePane() + centerPane.addItem(item) + const dockPane = atom.workspace.getRightDock().getActivePane() + spyOn(workspace.itemLocationStore, 'save') + spyOn(workspace.itemLocationStore, 'delete') + centerPane.moveItemToPane(item, dockPane) + expect(workspace.itemLocationStore.delete).toHaveBeenCalledWith(ITEM_URI) + expect(workspace.itemLocationStore.save).not.toHaveBeenCalled() + }) }) }) diff --git a/src/workspace.js b/src/workspace.js index 3dd732d04..b5ee4616c 100644 --- a/src/workspace.js +++ b/src/workspace.js @@ -301,7 +301,17 @@ module.exports = class Workspace extends Model { if (typeof item.getURI === 'function') { const uri = item.getURI() if (uri != null) { - this.itemLocationStore.save(item.getURI(), paneContainer.getLocation()) + const location = paneContainer.getLocation() + let defaultLocation + if (typeof item.getDefaultLocation === 'function') { + defaultLocation = item.getDefaultLocation() + } + defaultLocation = defaultLocation || 'center' + if (location === defaultLocation) { + this.itemLocationStore.delete(item.getURI()) + } else { + this.itemLocationStore.save(item.getURI(), location) + } } } })