From 9e134e5caf9f96e2d19f2b1793d04f6194a7cfdc Mon Sep 17 00:00:00 2001 From: Ryuichi Inagaki Date: Fri, 16 Feb 2018 22:27:50 +1100 Subject: [PATCH] ref #5 Extracted collection management logic --- lib/decoration-registry.js | 36 ++++++++++++++++-------------- lib/text-decoration-collection.js | 37 +++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 17 deletions(-) create mode 100644 lib/text-decoration-collection.js diff --git a/lib/decoration-registry.js b/lib/decoration-registry.js index 568b604..6890bf9 100644 --- a/lib/decoration-registry.js +++ b/lib/decoration-registry.js @@ -1,6 +1,7 @@ const _ = require('lodash'); const OverviewRulerLane = require('vscode').OverviewRulerLane; +const TextDecorationCollection = require('./text-decoration-collection'); const OVERVIEW_RULER_COLOUR = 'violet'; @@ -11,7 +12,7 @@ class DecorationRegistry { this._window = params.window; this._generateUuid = params.generateUuid; - this._textDecorationMap = {}; + this._textDecorationMap = new TextDecorationCollection(); } inquireById(decorationId) { @@ -19,18 +20,20 @@ class DecorationRegistry { } inquireByPattern(pattern) { - const decorationId = _.findKey( - this._textDecorationMap, - decoration => decoration && decoration.pattern.equalTo(pattern) - ); - return decorationId ? this._buildDecorationFromId(decorationId) : null; + const isSamePattern = decoration => decoration.pattern.equalTo(pattern); + const decoration = this._textDecorationMap.find(isSamePattern); + return decoration && this._buildDecorationInfo(decoration); } - _buildDecorationFromId(decorationId) { - const decoration = Object.assign({id: decorationId}, this._textDecorationMap[decorationId]); + _buildDecorationInfo(decoration) { return _.pick(decoration, ['id', 'decorationType', 'pattern']); } + _buildDecorationFromId(decorationId) { + const decoration = this._textDecorationMap.get(decorationId); + return decoration && this._buildDecorationInfo(decoration); + } + issue(pattern) { const decoration = this.inquireByPattern(pattern); if (decoration) return null; @@ -38,26 +41,25 @@ class DecorationRegistry { const colour = this._colourRegistry.issue(); const id = this._generateUuid(); const decorationType = this._generateDecorationType(colour); - this._textDecorationMap[id] = {pattern, colour, decorationType}; + this._textDecorationMap.add({id, pattern, colour, decorationType}); return this._buildDecorationFromId(id); } updatePattern(decorationId, newPattern) { - this._textDecorationMap[decorationId].pattern = newPattern; - return this._buildDecorationFromId(decorationId); + const decoration = this._textDecorationMap.get(decorationId); + decoration.pattern = newPattern; + return this._buildDecorationInfo(decoration); } revoke(decorationId) { - const decoration = this._textDecorationMap[decorationId]; + const decoration = this._textDecorationMap.get(decorationId); this._colourRegistry.revoke(decoration.colour); - this._textDecorationMap[decorationId] = null; + this._textDecorationMap.remove(decorationId); } retrieveAll() { - return _.reduce(this._textDecorationMap, - (result, value, key) => value ? result.concat(this._buildDecorationFromId(key)) : result, - [] - ); + return this._textDecorationMap.toList() + .map(this._buildDecorationInfo); } _generateDecorationType(colour) { diff --git a/lib/text-decoration-collection.js b/lib/text-decoration-collection.js new file mode 100644 index 0000000..5d26224 --- /dev/null +++ b/lib/text-decoration-collection.js @@ -0,0 +1,37 @@ + +const _ = require('lodash'); + +class TextDecorationCollection { + + constructor() { + this._map = Object.create(null); + } + + add({id, pattern, colour, decorationType}) { + this._map[id] = {pattern, colour, decorationType}; + } + + get(id) { + const value = this._map[id]; + return value ? Object.assign({id}, value) : null; + } + + remove(id) { + this._map[id] = null; + } + + find(predicate) { + const id = _.findKey(this._map, decoration => decoration && predicate(decoration)); + return id ? Object.assign({id}, this._map[id]) : null; + } + + toList() { + return _.reduce(this._map, + (result, value, key) => value ? [...result, Object.assign({id: key}, value)] : result, + [] + ); + } + +} + +module.exports = TextDecorationCollection;