Skip to content

Commit

Permalink
Merge pull request #125 from doughsay/fix-text-search
Browse files Browse the repository at this point in the history
Fix exceptions when unknown search results come from server.
  • Loading branch information
doughsay authored Jul 1, 2017
2 parents 8f8355d + 80de89c commit 1f7cb5f
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 66 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

## [2.2.1] - 2017-06-30
### Fixed
- Don't blow up when unknown search result types come from the server ([#123](https://github.com/doughsay/room.js-client/issues/123))

## [2.2.0] - 2017-06-30
### Added
- Right prompt (requires server version >= 4.0.0)
Expand All @@ -15,6 +19,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
### Added
- Release tracking via an initial [github release](https://github.com/doughsay/room.js-client/releases/tag/2.1.0) and a tag.

[Unreleased]: https://github.com/doughsay/room.js-client/compare/2.2.0...HEAD
[Unreleased]: https://github.com/doughsay/room.js-client/compare/2.2.1...HEAD
[2.2.1]: https://github.com/doughsay/room.js-client/compare/2.2.0...2.2.1
[2.2.0]: https://github.com/doughsay/room.js-client/compare/2.1.0...2.2.0
[2.1.0]: https://github.com/doughsay/room.js-client/releases/tag/2.1.0
2 changes: 1 addition & 1 deletion dist/js/bundle.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/js/bundle.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "room.js-client",
"version": "2.2.0",
"version": "2.2.1",
"description": "Room.js client.",
"scripts": {
"build": "./bin/build",
Expand Down
2 changes: 1 addition & 1 deletion src/lib/ko-codemirror.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function register () {
function init (element, valueAccessor, allBindings) {
const options = allBindings.get('codemirrorOptions') || {}
options.value = ko.unwrap(valueAccessor())
const editor = CodeMirror(element, options) // eslint-disable-line new-cap
const editor = CodeMirror(element, options)

editor.on('change', cm => {
const value = valueAccessor()
Expand Down
54 changes: 54 additions & 0 deletions src/lib/search-result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import ko from 'knockout'

export default class SearchResult {
constructor (data) {
this.data = data
this.objectId = data.objectId
this.active = ko.observable(false)
this.name = ko.computed(this.computeName.bind(this))
}

static newFromResult (result) {
if (result.function) {
return new FunctionSearchResult(result)
} else if (result.verb) {
return new VerbSearchResult(result)
}

return null
}

computeName () {
throw new Error('Must be subclassed.')
}

openEditor () {
throw new Error('Must be subclassed.')
}
}

class FunctionSearchResult extends SearchResult {
computeName () {
return `${this.objectId}.${this.data.function}`
}

openEditor (socket, tabsViewModel) {
const params = { objectId: this.objectId, name: this.data.function }
socket.emit('get-function', params, data => {
tabsViewModel.newEditFunctionTab(socket, data)
})
}
}

class VerbSearchResult extends SearchResult {
computeName () {
return `${this.objectId}.${this.data.verb}`
}

openEditor (socket, tabsViewModel) {
const params = { objectId: this.objectId, name: this.data.verb }
socket.emit('get-verb', params, data => {
tabsViewModel.newEditVerbTab(socket, data)
})
}
}
3 changes: 0 additions & 3 deletions src/view-models/function-editor-view-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ export default class FunctionEditorViewModel {
}

if (!this.socket.connected) {
// eslint-disable-next-line no-alert
this.window.alert([
'The client tab that this editor was opened from has been',
'closed. You must keep that open for saving to work.'
Expand All @@ -56,7 +55,6 @@ export default class FunctionEditorViewModel {
if (response === 'saved') {
this._code(this.code())
} else {
// eslint-disable-next-line no-alert
this.window.alert(response)
}
})
Expand All @@ -69,7 +67,6 @@ export default class FunctionEditorViewModel {
].join(' ')

return this.dirty()
// eslint-disable-next-line no-alert
? this.window.confirm(msg)
: true
}
Expand Down
57 changes: 2 additions & 55 deletions src/view-models/search-view-model.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import ko from 'knockout'
import SearchResult from '../lib/search-result'

function isElementVisibleIn (el, container) {
const elRect = el.getBoundingClientRect()
Expand All @@ -12,60 +13,6 @@ function isElementVisibleIn (el, container) {
)
}

class SearchResult {
constructor (data) {
this.data = data
this.objectId = data.objectId
this.active = ko.observable(false)
this.name = ko.computed(this.computeName.bind(this))
}

static newFromResult (result) {
if (result.function) {
// eslint-disable-next-line no-use-before-define
return new FunctionSearchResult(result)
} else if (result.verb) {
// eslint-disable-next-line no-use-before-define
return new VerbSearchResult(result)
}
throw new Error('Invalid result type.')
}

computeName () {
throw new Error('Must be subclassed.')
}

openEditor () {
throw new Error('Must be subclassed.')
}
}

class FunctionSearchResult extends SearchResult {
computeName () {
return `${this.objectId}.${this.data.function}`
}

openEditor (socket, tabsViewModel) {
const params = { objectId: this.objectId, name: this.data.function }
socket.emit('get-function', params, data => {
tabsViewModel.newEditFunctionTab(socket, data)
})
}
}

class VerbSearchResult extends SearchResult {
computeName () {
return `${this.objectId}.${this.data.verb}`
}

openEditor (socket, tabsViewModel) {
const params = { objectId: this.objectId, name: this.data.verb }
socket.emit('get-verb', params, data => {
tabsViewModel.newEditVerbTab(socket, data)
})
}
}

// TODO: socket permissions
export default class SearchViewModel {
constructor ({ doc }, parentViewModel, socket) {
Expand Down Expand Up @@ -118,7 +65,7 @@ export default class SearchViewModel {
onSearch (str) {
this.socket.emit('search', str, results => {
this.selectedIndex(0)
this.results(results.map(result => SearchResult.newFromResult(result)))
this.results(results.map(result => SearchResult.newFromResult(result)).filter(r => r))
})
}

Expand Down
3 changes: 0 additions & 3 deletions src/view-models/verb-editor-view-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ export default class VerbEditorViewModel {
}

if (!this.socket.connected) {
// eslint-disable-next-line no-alert
this.window.alert([
'The client tab that this editor was opened from has been',
'closed. You must keep that open for saving to work.'
Expand All @@ -84,7 +83,6 @@ export default class VerbEditorViewModel {
this._iobjarg(this.iobjarg())
this._code(this.code())
} else {
// eslint-disable-next-line no-alert
this.window.alert(response)
}
})
Expand All @@ -97,7 +95,6 @@ export default class VerbEditorViewModel {
].join(' ')

return this.dirty()
// eslint-disable-next-line no-alert
? this.window.confirm(msg)
: true
}
Expand Down
23 changes: 23 additions & 0 deletions test/lib/search-result-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import test from 'ava'
import SearchResult from '../../src/lib/search-result'

test('SearchResult: newFromResult works for function result', t => {
const result = { objectId: 'foo', function: 'bar' }
const searchResult = SearchResult.newFromResult(result)

t.truthy(searchResult)
})

test('SearchResult: newFromResult works for verb result', t => {
const result = { objectId: 'foo', verb: 'bar' }
const searchResult = SearchResult.newFromResult(result)

t.truthy(searchResult)
})

test('SearchResult: newFromResult doesn\'t work for text result', t => {
const result = { objectId: 'foo', text: 'bar' }
const searchResult = SearchResult.newFromResult(result)

t.falsy(searchResult)
})

0 comments on commit 1f7cb5f

Please sign in to comment.