3 * Copyright 2017 RIFT.IO Inc
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
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'
24 const storeCache
= new StoreCache('schema');
25 storeCache
.init(); // get the ball rolling
27 function getCachedSchema(request
) {
28 const cachedSchema
= {};
29 const requestSchema
= [];
30 request
.forEach((path
) => {
31 let schema
= storeCache
.get(path
);
33 cachedSchema
[path
] = schema
35 requestSchema
.push(path
);
44 const schemaSource
= {
45 loadSchema: function () {
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
));
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
) {
62 schema
: results
.cachedSchema
66 url
: '/schema?request=' + results
.requestSchema
.join(','),
71 for (let path
in schema
) {
72 storeCache
.set(path
, schema
[path
]);
74 resolve(getCachedSchema(request
).cachedSchema
);
76 error: function (error
) {
77 console
.log("There was an error getting the schema: ", error
);
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.")
89 success
: schemaActions
.loadSchemaSuccess
,
90 loading
: schemaActions
.loadSchemaLoading
,
91 error
: schemaActions
.loadSchemaFail
95 export default schemaSource
;