blob: c49d53d984c08a884253f8ef7271f63ae7cc7955 [file] [log] [blame]
AviZi280f8012017-06-09 02:39:56 +03001/*!
Michael Landoefa037d2017-02-19 12:57:33 +02002 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
AviZi280f8012017-06-09 02:39:56 +03003 *
Michael Landoefa037d2017-02-19 12:57:33 +02004 * 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
AviZi280f8012017-06-09 02:39:56 +03007 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
Michael Landoefa037d2017-02-19 12:57:33 +020010 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
AviZi280f8012017-06-09 02:39:56 +030012 * 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 Landoefa037d2017-02-19 12:57:33 +020015 */
Michael Landoefa037d2017-02-19 12:57:33 +020016const queue = {
17 fetch: [],
AviZi280f8012017-06-09 02:39:56 +030018 put: [],
19 post: [],
Michael Landoefa037d2017-02-19 12:57:33 +020020 destroy: []
21};
22
23const initQueue = () => {
24 queue['fetch'] = [];
AviZi280f8012017-06-09 02:39:56 +030025 queue['put'] = [];
26 queue['post'] = [];
Michael Landoefa037d2017-02-19 12:57:33 +020027 queue['destroy'] = [];
28};
29
30const 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
39export 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
AviZi280f8012017-06-09 02:39:56 +030049 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 Landoefa037d2017-02-19 12:57:33 +020053 }
AviZi280f8012017-06-09 02:39:56 +030054 return handleOperation(put.shift(), {data, options, baseUrl});
Michael Landoefa037d2017-02-19 12:57:33 +020055 },
56
AviZi280f8012017-06-09 02:39:56 +030057 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 Landoefa037d2017-02-19 12:57:33 +020061 }
AviZi280f8012017-06-09 02:39:56 +030062 return handleOperation(post.shift(), {data, options, baseUrl});
Michael Landoefa037d2017-02-19 12:57:33 +020063 },
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};