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