Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Commit

Permalink
Merge pull request #109 from t9md/remove-element-cache
Browse files Browse the repository at this point in the history
Remove element-cache
  • Loading branch information
daviwil authored Feb 26, 2018
2 parents 07610fc + 4b1c75c commit 496ac41
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 81 deletions.
17 changes: 2 additions & 15 deletions lib/command-palette-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import fuzzaldrinPlus from 'fuzzaldrin-plus'
export default class CommandPaletteView {
constructor (initiallyVisibleItemCount = 10) {
this.keyBindingsForActiveElement = []
this.elementCache = new WeakMap()
this.selectListView = new SelectListView({
initiallyVisibleItemCount: initiallyVisibleItemCount, // just for being able to disable visible-on-render in spec
items: [],
Expand All @@ -18,15 +17,6 @@ export default class CommandPaletteView {
if (!visible) {
return document.createElement("li")
}
const query = this.selectListView.getQuery()
const queryKey = `${query}:${selected}`
if(this.elementCache.has(item)) {
if(!this.preserveLastSearch && this.elementCache.get(item).has(queryKey)) {
return this.elementCache.get(item).get(queryKey)
}
} else {
this.elementCache.set(item, new Map())
}

const li = document.createElement('li')
li.classList.add('event', 'two-lines')
Expand All @@ -40,10 +30,7 @@ export default class CommandPaletteView {
.forEach(keyBinding => {
const kbd = document.createElement('kbd')
kbd.classList.add('key-binding')
if(!this.elementCache.get(item).has(keyBinding.keystrokes)) {
this.elementCache.get(item).set(keyBinding.keystrokes, humanizeKeystroke(keyBinding.keystrokes))
}
kbd.textContent = this.elementCache.get(item).get(keyBinding.keystrokes)
kbd.textContent = humanizeKeystroke(keyBinding.keystrokes)
rightBlock.appendChild(kbd)
})
li.appendChild(rightBlock)
Expand All @@ -54,6 +41,7 @@ export default class CommandPaletteView {
titleEl.title = item.name
leftBlock.appendChild(titleEl)

const query = this.selectListView.getQuery()
this.highlightMatchesInElement(item.displayName, query, titleEl)

if (selected) {
Expand Down Expand Up @@ -81,7 +69,6 @@ export default class CommandPaletteView {
}

li.appendChild(leftBlock)
this.elementCache.get(item).set(queryKey, li)
return li
},
didConfirmSelection: (keyBinding) => {
Expand Down
66 changes: 0 additions & 66 deletions test/command-palette-view.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,72 +138,6 @@ describe('CommandPaletteView', () => {
})
})

describe('element caching', () => {
it('caches all items when the query is changed', async () => {
const commandPalette = new CommandPaletteView(Infinity)
const spy = sinon.spy(commandPalette.selectListView.props, 'elementForItem')
await commandPalette.toggle()
commandPalette.selectListView.items.forEach(item => {
const selected = commandPalette.selectListView.getSelectedItem() === item
assert(spy.calledWithMatch(item))
assert(commandPalette.elementCache.has(item))
assert(commandPalette.elementCache.get(item).has(`:${selected}`))
assert(spy.returned(commandPalette.elementCache.get(item).get(`:${selected}`)))
})

spy.reset()
await commandPalette.selectListView.update({query: 'Z'})
commandPalette.selectListView.items.forEach(item => {
const selected = commandPalette.selectListView.getSelectedItem() === item
assert(spy.calledWithMatch(item))
assert(commandPalette.elementCache.has(item))
assert(commandPalette.elementCache.get(item).has(`Z:${selected}`))
assert(spy.returned(commandPalette.elementCache.get(item).get(`Z:${selected}`)))
})
})

it('caches items incrementally when state is changed', async () => {
const commandPalette = new CommandPaletteView(Infinity)
const spy = sinon.spy(commandPalette.selectListView.props, 'elementForItem')
await commandPalette.toggle()
commandPalette.selectListView.selectIndex((commandPalette.selectListView.selectionIndex + 1), false)
const selectedItem = commandPalette.selectListView.getSelectedItem()
assert(!commandPalette.elementCache.get(selectedItem).has(':true'))
await commandPalette.selectListView.update()
assert(commandPalette.elementCache.get(selectedItem).has(':true'))
})

it('uses cached elements when reactivating', async () => {
const commandPalette = new CommandPaletteView(Infinity)
const spy = sinon.spy(commandPalette.selectListView.props, 'elementForItem')
await commandPalette.toggle()
const originalItemObjects = commandPalette.selectListView.items
originalItemObjects.forEach(item => {
const selected = commandPalette.selectListView.getSelectedItem() === item
assert(spy.calledWithMatch(item))
assert(commandPalette.elementCache.has(item))
assert(commandPalette.elementCache.get(item).has(`:${selected}`))
assert(spy.returned(commandPalette.elementCache.get(item).get(`:${selected}`)))
})
spy.reset()
await commandPalette.toggle()
originalItemObjects.forEach(item => {
const selected = commandPalette.selectListView.getSelectedItem() === item
assert(spy.neverCalledWithMatch(item))
assert(commandPalette.elementCache.has(item))
assert(commandPalette.elementCache.get(item).has(`:${selected}`))
})
await commandPalette.toggle()
originalItemObjects.forEach(item => {
const selected = commandPalette.selectListView.getSelectedItem() === item
assert(spy.calledWithMatch(item))
assert(commandPalette.elementCache.has(item))
assert(commandPalette.elementCache.get(item).has(`:${selected}`))
assert(spy.returned(commandPalette.elementCache.get(item).get(`:${selected}`)))
})
})
})

describe('hidden commands', () => {
it('does not show commands that are marked as `hiddenInCommandPalette` by default, then *only* shows those commands when showHiddenCommands is invoked', async () => {
const commandsDisposable = atom.commands.add('*', 'foo:hidden-in-command-palette', {
Expand Down

0 comments on commit 496ac41

Please sign in to comment.