From ba1603a7e2caf4ab2b6ab6e9e98f78e17d2248ce Mon Sep 17 00:00:00 2001 From: kyle Date: Fri, 23 Feb 2018 22:25:28 -0800 Subject: [PATCH] improve: lazy resolver (#4253) * default to empty `ImmutableMap` when grabbing op metadata * pass `errors` into JsonSchema components * Account for Immutable data structure in JavaScriptonSchema... ...and create empty Lists instead of Maps by default. * Pass ImmutableList through to JsonSchema child components * Add lazy resolving spec state extensions * TEMPORARY: disable conventional resolved spec * WIP * Use resolveSubtree in Operation display * Freebie: short-circuit Markdown component if it is given plaintext * NEW DEFAULT BEHAVIOR: `defaultModelsExpandDepth: 1` does not expand individual models * Render faked Model expander to trigger resolution * Baseline support for Editor lifecycles * Display operation summaries before the operation is resolved * Test migrations * WIP * Swagger2 TIO Body params * a bit of cleanup * Debounce string param inputs * Reach into unresolved operation for deprecated flag, if available * Fire subtree request outside of render * Remove debugging flags * Fix logical errors in spec statePlugins * TODOs become TODONEs! * Migrate deeplinking feature to non-resolved spec action * ESLint fixes * Reduce action dispatch volume; run resolver on next tick * Batch resolver requests; use batch progressive results in later iterations * Add loading states to Model and Operation views * Preserve object order; remove dupl. import; add warning for action * LINTER! * Use require to load SVG Works around Webpack weirdness --- src/core/components/model.jsx | 11 ++++ src/core/components/operation.jsx | 38 ++++++++------ src/core/plugins/download-url.js | 4 +- src/core/plugins/spec/actions.js | 83 +++++++++++++++++++++---------- src/img/rolling-load.svg | 1 + src/style/_layout.scss | 10 ++++ 6 files changed, 105 insertions(+), 42 deletions(-) create mode 100644 src/img/rolling-load.svg diff --git a/src/core/components/model.jsx b/src/core/components/model.jsx index ca5b23c3..3fa88116 100644 --- a/src/core/components/model.jsx +++ b/src/core/components/model.jsx @@ -49,6 +49,17 @@ export default class Model extends ImmutablePureComponent { schema = this.getRefSchema( name ) } + if(!schema) { + return + { name } + + + } + const deprecated = specSelectors.isOAS3() && schema.get("deprecated") isRef = isRef !== undefined ? isRef : !!$$ref type = schema && schema.get("type") || type diff --git a/src/core/components/operation.jsx b/src/core/components/operation.jsx index 35829182..e47b19c5 100644 --- a/src/core/components/operation.jsx +++ b/src/core/components/operation.jsx @@ -5,6 +5,7 @@ import { getExtensions, sanitizeUrl } from "core/utils" import { Iterable, List } from "immutable" import ImPropTypes from "react-immutable-proptypes" + export default class Operation extends PureComponent { static propTypes = { specPath: ImPropTypes.list.isRequired, @@ -155,6 +156,9 @@ export default class Operation extends PureComponent {
+ { operation && operation.size ? null : + + } { deprecated &&

Warning: Deprecated

} { description &&
@@ -176,23 +180,25 @@ export default class Operation extends PureComponent {
: null } - + fn={fn} + getComponent={ getComponent } + specActions={ specActions } + specSelectors={ specSelectors } + pathMethod={ [path, method] } + getConfigs={ getConfigs } + /> + } { !tryItOutEnabled ? null : ({specActions, specSelectors, errActio }) } -export const requestResolvedSubtree = path => system => { - const { - errActions, - fn: { - resolveSubtree, - AST: { getLineNumberForPath } - }, - specSelectors, - specActions, - } = system +let requestBatch = [] - const specStr = specSelectors.specStr() +const debResolveSubtrees = debounce(async () => { + const system = requestBatch.system // Just a reference to the "latest" system + + if(!system) { + console.error("debResolveSubtrees: don't have a system to operate on, aborting.") + return + } + const { + errActions, + errSelectors, + fn: { + resolveSubtree, + AST: { getLineNumberForPath } + }, + specSelectors, + specActions, + } = system if(!resolveSubtree) { console.error("Error: Swagger-Client did not provide a `resolveSubtree` method, doing nothing.") return } - const currentValue = specSelectors.specResolvedSubtree(path) + const specStr = specSelectors.specStr() - if(currentValue) { - return - } + try { + var batchResult = await requestBatch.reduce(async (prev, path) => { + const { resultMap, specWithCurrentSubtrees } = await prev + + const { errors, spec } = await resolveSubtree(specWithCurrentSubtrees, path) + + if(errSelectors.allErrors().size) { + errActions.clear({ + type: "thrown" + }) + } - return resolveSubtree(specSelectors.specJson().toJS(), path) - .then(({ spec, errors }) => { - errActions.clear({ - type: "thrown" - }) if(Array.isArray(errors) && errors.length > 0) { let preparedErrors = errors .map(err => { - console.error(err) err.line = err.fullPath ? getLineNumberForPath(specStr, err.fullPath) : null err.path = err.fullPath ? err.fullPath.join(".") : null err.level = "error" @@ -177,9 +188,31 @@ export const requestResolvedSubtree = path => system => { errActions.newThrownErrBatch(preparedErrors) } - return specActions.updateResolvedSubtree(path, spec) - }) - .catch(e => console.error(e)) + set(resultMap, path, spec) + set(specWithCurrentSubtrees, path, spec) + + return { + resultMap, + specWithCurrentSubtrees + } + }, Promise.resolve({ + resultMap: (specSelectors.specResolvedSubtree([]) || Map()).toJS(), + specWithCurrentSubtrees: specSelectors.specJson().toJS() + })) + + delete requestBatch.system + requestBatch = [] // Clear stack + } catch(e) { + console.error(e) + } + + specActions.updateResolvedSubtree([], batchResult.resultMap) +}, 35) + +export const requestResolvedSubtree = path => system => { + requestBatch.push(path) + requestBatch.system = system + debResolveSubtrees() } export function changeParam( path, paramName, paramIn, value, isXml ){ diff --git a/src/img/rolling-load.svg b/src/img/rolling-load.svg new file mode 100644 index 00000000..86ead2e4 --- /dev/null +++ b/src/img/rolling-load.svg @@ -0,0 +1 @@ + diff --git a/src/style/_layout.scss b/src/style/_layout.scss index 7bb218dc..4a117e5a 100644 --- a/src/style/_layout.scss +++ b/src/style/_layout.scss @@ -586,6 +586,16 @@ } } +.opblock-body +{ + .opblock-loading-animation + { + display: block; + margin: 3em; + margin-left: auto; + margin-right: auto; + } +} .opblock-body pre {