Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 | 1x 1x 1x 1x 1x 1x 1x 2x 2x 1x 1x 1x 1x 1x 1x 1x 66x 66x 66x | import { checkValidator, syntaxErrorOccurred } from './validation'; /** * Sample documents fetched action. */ export const SAMPLE_DOCUMENTS_FETCHED = 'validation/namespace/SAMPLE_DOCUMENTS_FETCHED'; /** * Loading sample documents aciton name. */ export const LOADING_SAMPLE_DOCUMENTS = 'validation/namespace/LOADING_SAMPLE_DOCUMENTS'; /** * The initial state. */ export const INITIAL_STATE = { isLoading: false }; /** * Collection max limit. */ const MAX_LIMIT = 100000; /** * Refresh sample document. * * @param {Object} state - The state * @param {Object} action - The action. * * @returns {Object} The new state. */ const refreshSampleDocuments = (state, action) => ({ ...state, matching: action.matching, notmatching: action.notmatching, isLoading: false }); /** * Action creator for sample documents changed events. * * @param {Object} sampleDocuments - Sample documents. * * @returns {Object} Validation saved action. */ export const sampleDocumentsFetched = (sampleDocuments) => ({ type: SAMPLE_DOCUMENTS_FETCHED, matching: sampleDocuments.matching, notmatching: sampleDocuments.notmatching }); /** * Action creator for load sample documents events. * * @param {Object} state - The state * * @returns {Object} Validation saved action. */ const loadSampleDocuments = (state) => ({ ...state, type: LOADING_SAMPLE_DOCUMENTS, isLoading: true }); /** * The loading sample documents. * * @returns {Object} The action. */ export const loadingSampleDocuments = () => ({ type: LOADING_SAMPLE_DOCUMENTS }); /** * To not have a huge switch statement in the reducer. */ const MAPPINGS = {}; MAPPINGS[SAMPLE_DOCUMENTS_FETCHED] = refreshSampleDocuments; MAPPINGS[LOADING_SAMPLE_DOCUMENTS] = loadSampleDocuments; /** * Sets zero documents. * * @param {Function} dispatch - Dispatch. * * @returns {Function} The function. */ const setZeroDocuments = (dispatch) => dispatch(sampleDocumentsFetched({ matching: undefined, notmatching: undefined })); /** * Sets syntax error. * * @param {Function} dispatch - Dispatch. * @param {Object} error - Error. * * @returns {Function} The function. */ const setSyntaxError = (dispatch, error) => dispatch(syntaxErrorOccurred(error)); /** * Fetch sample documents. * * @param {Object} docsOptions - Collection of auxiliary options. * @param {Function} callback - Callback function that returns * matching or not mathing document. */ const getSampleDocuments = (docsOptions, callback) => { const aggOptions = { allowDiskUse: true }; const pipeline = docsOptions.pipeline; if (docsOptions.count > MAX_LIMIT) { pipeline.unshift({ $limit: MAX_LIMIT }); } docsOptions.dataService.aggregate( docsOptions.namespace, docsOptions.pipeline, aggOptions, (aggError, cursor) => { if (aggError) { setZeroDocuments(docsOptions.dispatch); setSyntaxError(docsOptions.dispatch, aggError); return; } cursor.toArray((toArrayError, documents) => { if (toArrayError) { setZeroDocuments(docsOptions.dispatch); setSyntaxError(docsOptions.dispatch, toArrayError); return; } cursor.close(); return callback(documents); }); } ); }; /** * Fetch sample documents. * * @param {Object} validator - Validator. * @param {Object} error - Error. * * @returns {Function} The function. */ export const fetchSampleDocuments = (validator, error) => { return (dispatch, getState) => { dispatch(loadingSampleDocuments()); if (error) { return setZeroDocuments(dispatch); } const state = getState(); const dataService = state.dataService.dataService; const namespace = state.namespace.ns; const checkedValidator = checkValidator(validator); const query = checkValidator(checkedValidator.validator).validator; const pipeline = [{ $match: query }, { $limit: 1 }]; if (dataService) { dataService.count(namespace, query, {}, (countError, count) => { if (countError) { setZeroDocuments(dispatch); setSyntaxError(dispatch, countError); return; } const docsOptions = { pipeline, namespace, dispatch, dataService, count }; getSampleDocuments(docsOptions, (matching) => { docsOptions.pipeline = [ { $match: { '$nor': [ query ] } }, { $limit: 1 } ]; getSampleDocuments(docsOptions, (notmatching) => { return dispatch(sampleDocumentsFetched({ matching: matching[0], notmatching: notmatching[0] })); }); }); }); } }; }; /** * Reducer function for handle state changes to status. * * @param {String} state - The status state. * @param {Object} action - The action. * * @returns {String} The new state. */ export default function reducer(state = INITIAL_STATE, action) { const fn = MAPPINGS[action.type]; return fn ? fn(state, action) : state; } |