| Jeremy Mordkoff | 03156e3 | 2017-09-30 21:42:44 -0400 | [diff] [blame^] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2017 RIFT.IO Inc |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | * |
| 17 | */ |
| 18 | import rw from 'utils/rw' |
| 19 | import schemaActions from './schemaActions' |
| 20 | import Utils from 'utils/utils' |
| 21 | import $ from 'jquery'; |
| 22 | import StoreCache from '../SourceCache' |
| 23 | |
| 24 | const storeCache = new StoreCache('schema'); |
| 25 | storeCache.init(); // get the ball rolling |
| 26 | |
| 27 | function getCachedSchema(request) { |
| 28 | const cachedSchema = {}; |
| 29 | const requestSchema = []; |
| 30 | request.forEach((path) => { |
| 31 | let schema = storeCache.get(path); |
| 32 | if (schema) { |
| 33 | cachedSchema[path] = schema |
| 34 | } else { |
| 35 | requestSchema.push(path); |
| 36 | } |
| 37 | }); |
| 38 | return { |
| 39 | cachedSchema, |
| 40 | requestSchema |
| 41 | }; |
| 42 | } |
| 43 | |
| 44 | const schemaSource = { |
| 45 | loadSchema: function () { |
| 46 | return { |
| 47 | local: function (state, request) { |
| 48 | request = Array.isArray(request) ? request : [request]; |
| 49 | const results = getCachedSchema(request); |
| 50 | if (!results.requestSchema.length) { |
| 51 | return(Promise.resolve(results.cachedSchema)); |
| 52 | } |
| 53 | }, |
| 54 | |
| 55 | remote: function (state, request) { |
| 56 | return new Promise(function (resolve, reject) { |
| 57 | storeCache.init().then(() => { |
| 58 | request = Array.isArray(request) ? request : [request]; |
| 59 | const results = getCachedSchema(request); |
| 60 | if (!results.requestSchema.length) { |
| 61 | resolve({ |
| 62 | schema: results.cachedSchema |
| 63 | }); |
| 64 | } else { |
| 65 | $.ajax({ |
| 66 | url: '/schema?request=' + results.requestSchema.join(','), |
| 67 | type: 'GET', |
| 68 | success: function ({ |
| 69 | schema |
| 70 | }) { |
| 71 | for (let path in schema) { |
| 72 | storeCache.set(path, schema[path]); |
| 73 | } |
| 74 | resolve(getCachedSchema(request).cachedSchema); |
| 75 | }, |
| 76 | error: function (error) { |
| 77 | console.log("There was an error getting the schema: ", error); |
| 78 | reject(error); |
| 79 | } |
| 80 | }).fail(function (xhr) { |
| 81 | console.log("There was an error getting the schema: ", xhr); |
| 82 | Utils.checkAuthentication(xhr.status); |
| 83 | reject("There was an error getting the schema.") |
| 84 | }); |
| 85 | } |
| 86 | }) |
| 87 | }) |
| 88 | }, |
| 89 | success: schemaActions.loadSchemaSuccess, |
| 90 | loading: schemaActions.loadSchemaLoading, |
| 91 | error: schemaActions.loadSchemaFail |
| 92 | } |
| 93 | }, |
| 94 | } |
| 95 | export default schemaSource; |