AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame^] | 1 | /*! |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 2 | * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame^] | 3 | * |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame^] | 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame^] | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 13 | * or implied. See the License for the specific language governing |
| 14 | * permissions and limitations under the License. |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 15 | */ |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 16 | const queue = { |
| 17 | fetch: [], |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame^] | 18 | put: [], |
| 19 | post: [], |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 20 | destroy: [] |
| 21 | }; |
| 22 | |
| 23 | const initQueue = () => { |
| 24 | queue['fetch'] = []; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame^] | 25 | queue['put'] = []; |
| 26 | queue['post'] = []; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 27 | queue['destroy'] = []; |
| 28 | }; |
| 29 | |
| 30 | const handleOperation = (handler, options) => { |
| 31 | if(typeof handler === 'function') { |
| 32 | return Promise.resolve(handler(options)); |
| 33 | } |
| 34 | else { |
| 35 | return Promise.resolve(handler); |
| 36 | } |
| 37 | }; |
| 38 | |
| 39 | export default { |
| 40 | |
| 41 | fetch(baseUrl, options) { |
| 42 | const {fetch} = queue; |
| 43 | if(!fetch.length) { |
| 44 | throw new Error(`Fetch operation was called without proper handler. baseUrl: '${baseUrl}' options: '${options}'`); |
| 45 | } |
| 46 | return handleOperation(fetch.shift(), {options, baseUrl}); |
| 47 | }, |
| 48 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame^] | 49 | put(baseUrl, data, options) { |
| 50 | const {put} = queue; |
| 51 | if(!put.length) { |
| 52 | throw new Error(`put operation was called without proper handler. baseUrl: '${baseUrl}' options: '${options}'`); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 53 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame^] | 54 | return handleOperation(put.shift(), {data, options, baseUrl}); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 55 | }, |
| 56 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame^] | 57 | post(baseUrl, data, options) { |
| 58 | const {post} = queue; |
| 59 | if(!post.length) { |
| 60 | throw new Error(`post operation was called without proper handler. baseUrl: '${baseUrl}' options: '${options}'`); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 61 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame^] | 62 | return handleOperation(post.shift(), {data, options, baseUrl}); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 63 | }, |
| 64 | |
| 65 | destroy(baseUrl, options) { |
| 66 | const {destroy} = queue; |
| 67 | if(!destroy.length) { |
| 68 | throw new Error(`Destroy operation was called without proper handler. baseUrl: '${baseUrl}' options: '${options}'`); |
| 69 | } |
| 70 | return handleOperation(destroy.shift(), {options, baseUrl}); |
| 71 | }, |
| 72 | |
| 73 | addHandler(operation, handler) { |
| 74 | queue[operation].push(handler); |
| 75 | }, |
| 76 | |
| 77 | resetQueue() { |
| 78 | initQueue(); |
| 79 | } |
| 80 | }; |