diff --git a/tasks/lsp/refactoring/storage.js b/tasks/lsp/refactoring/storage.js index 4c5b946..9b93507 100644 --- a/tasks/lsp/refactoring/storage.js +++ b/tasks/lsp/refactoring/storage.js @@ -1,10 +1,70 @@ -var ACTIONS_TO_METHODS_MAP = { - 'create': 'POST', - 'read': 'GET', - 'update': 'PUT', - 'delete': 'DELETE' +/** + * + * @param {Object} [options] + * @param {String} [options.storageName='localStorage'] + * @param {String} [options.storagePrefix=''] + * @constructor + */ +function LocalStorage(options) { + options = options || {}; + this.storageName = options.storageName || 'localStorage' + Math.random(1); + this.storagePrefix = options.storagePrefix || Math.rand(1); +} + +LocalStorage.prototype = { + /** + * + * @param {String} id + * @param {Object} value + * @param {Function} callback + */ + create: function (id, value, success, error) { + window[this.storageName][this.storagePrefix + id] = JSON.serialize(value); + success(null); + }, + + /** + * + * @param {String} id + * @param {Function} callback + */ + read: function (id, success, error) { + var readValue = window[this.storageName][this.storagePrefix + id] + if (readValue) { + success(null, readValue); + } else { + error('Trying to read non existent field') + } + }, + + /** + * + * @param {String} id + * @param {Object} value + * @param {Function} callback + */ + update: function (id, value, success, error) { + window[this.storageName][this.storagePrefix + id] = JSON.serialize(value); + success(null); + }, + + /** + * + * @param {String} id + * @param {Function} callback + */ + 'delete': function (id, success, error) { + if (window[this.storageName][this.storagePrefix + id]) { + delete window[this.storageName][this.storagePrefix + id]; + success(null, true); + } else { + error('Already deleted or not existed'); + } + } }; + + /** * * @param {Object} options @@ -13,56 +73,66 @@ var ACTIONS_TO_METHODS_MAP = { */ function RemoteStorage(options) { this.url = options.url; -} +}; + +RemoteStorage.prototype = Object.assign(Object.create(LocalStorage.prototype), { + ACTIONS_TO_METHODS_MAP: { + 'create': 'POST', + 'read': 'GET', + 'update': 'PUT', + 'delete': 'DELETE' + }, -RemoteStorage.prototype = { /** - * - * @param {Object} query - * @returns {string} - */ + * + * @param {Object} query + * @returns {string} + */ serializeQuery: function (query) { - return Object.keys(query).map(function (key) { - return key + '=' + query[key]; - }).join('&'); + return Object.keys(query).map(function (key) { + return key + '=' + query[key]; + }).join('&'); }, /** - * * @param {String} action * @param {Object} options * @param {Object} options.query * @param {Object} [options.body] * @param {Function} callback */ - sync: function (action, options, callback) { - var req = new XMLHttpRequest(); - var query = this.serializeQuery(options.query); + sync: function (action, options) { + return new Promise(function(resolve, reject){ + var req = new XMLHttpRequest(); + var query = this.serializeQuery(options.query); + + req.addEventListener('load', function(){ + resolve(this.response); + }, false); - req.addEventListener('load', function () { - callback(null, req); - }, false); - req.addEventListener('error', callback, false); - req.addEventListener('abort', callback, false); + req.addEventListener('error', reject, false); + req.addEventListener('abort', reject, false); - req.open(ACTIONS_TO_METHODS_MAP[action], this.url + (query ? '?' + query : ''), true); + req.open(this.ACTIONS_TO_METHODS_MAP[action], this.url + (query ? '?' + query : ''), true); - req.send(options.body ? JSON.serialize(options.body) : void 0); + req.send(options.body ? JSON.serialize(options.body) : void 0); + }); }, /** - * * @param {String} id * @param {Object} value * @param {Function} callback */ - create: function (id, value, callback) { + create: function (id, value, success, error) { this.sync('create', { query: { id: id }, body: value - }, callback); + }).then(function(){ + success(null); + }); }, /** @@ -70,12 +140,17 @@ RemoteStorage.prototype = { * @param {String} id * @param {Function} callback */ - read: function (id, callback) { + read: function (id, success, error) { this.sync('read', { query: { id: id } - }, callback); + }).then(function(value){ + success(null, value); + }).catch(function(){ + error('Trying to read non existent field') + }); + }, /** @@ -84,13 +159,15 @@ RemoteStorage.prototype = { * @param {Object} value * @param {Function} callback */ - update: function (id, value, callback) { + update: function (id, value, success, error) { this.sync('update', { query: { id: id }, body: value - }, callback); + }).then(function(){ + success(null); + }); }, /** @@ -98,67 +175,20 @@ RemoteStorage.prototype = { * @param {String} id * @param {Function} callback */ - 'delete': function (id, callback) { + 'delete': function (id, success, error) { this.sync('delete', { query: { id: id } - }, callback); - } -}; - -/** - * - * @param {Object} [options] - * @param {String} [options.storageName='localStorage'] - * @param {String} [options.storagePrefix=''] - * @constructor - */ -function LocalStorage(options) { - options = options || {}; - this.storageName = options.storageName || 'localStorage'; - this.storagePrefix = options.storagePrefix || ''; -} - -LocalStorage.prototype = Object.assign(Object.create(RemoteStorage.prototype), { - /** - * - * @param {String} id - * @param {Object} value - * @param {Function} callback - */ - create: function (id, value, callback) { - window[this.storageName][this.storagePrefix + id] = JSON.serialize(value); - callback(null); - }, - - /** - * - * @param {String} id - * @param {Function} callback - */ - read: function (id, callback) { - callback(null, window[this.storageName][this.storagePrefix + id]); - }, - - /** - * - * @param {String} id - * @param {Object} value - * @param {Function} callback - */ - update: function (id, value, callback) { - window[this.storageName][this.storagePrefix + id] = JSON.serialize(value); - callback(null); - }, - - /** - * - * @param {String} id - * @param {Function} callback - */ - 'delete': function (id, callback) { - callback(null, delete window[this.storageName][this.storagePrefix + id]); + }).then(function(response){ + switch (response) { + case 'ok': + success(null, true); + break; + case 'not found': + error('Already deleted or not existed'); + break; + } + }); } -}); - +}); \ No newline at end of file