2020-10-24 23:57:45 +05:30
|
|
|
import { uniqueId } from 'lodash';
|
|
|
|
import {
|
|
|
|
SNIPPET_BLOB_ACTION_CREATE,
|
|
|
|
SNIPPET_BLOB_ACTION_UPDATE,
|
|
|
|
SNIPPET_BLOB_ACTION_MOVE,
|
|
|
|
SNIPPET_BLOB_ACTION_DELETE,
|
2020-11-24 15:15:51 +05:30
|
|
|
SNIPPET_LEVELS_MAP,
|
|
|
|
SNIPPET_VISIBILITY,
|
2020-10-24 23:57:45 +05:30
|
|
|
} from '../constants';
|
2021-01-29 00:20:46 +05:30
|
|
|
import { performanceMarkAndMeasure } from '~/performance/utils';
|
|
|
|
import { SNIPPET_MARK_BLOBS_CONTENT, SNIPPET_MEASURE_BLOBS_CONTENT } from '~/performance/constants';
|
2020-10-24 23:57:45 +05:30
|
|
|
|
|
|
|
const createLocalId = () => uniqueId('blob_local_');
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
export const decorateBlob = (blob) => ({
|
2020-10-24 23:57:45 +05:30
|
|
|
...blob,
|
|
|
|
id: createLocalId(),
|
|
|
|
isLoaded: false,
|
|
|
|
content: '',
|
|
|
|
});
|
|
|
|
|
|
|
|
export const createBlob = () => ({
|
|
|
|
id: createLocalId(),
|
|
|
|
content: '',
|
|
|
|
path: '',
|
|
|
|
isLoaded: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
const diff = ({ content, path }, origBlob) => {
|
|
|
|
if (!origBlob) {
|
|
|
|
return {
|
|
|
|
action: SNIPPET_BLOB_ACTION_CREATE,
|
|
|
|
previousPath: path,
|
|
|
|
content,
|
|
|
|
filePath: path,
|
|
|
|
};
|
|
|
|
} else if (origBlob.path !== path || origBlob.content !== content) {
|
|
|
|
return {
|
|
|
|
action: origBlob.path === path ? SNIPPET_BLOB_ACTION_UPDATE : SNIPPET_BLOB_ACTION_MOVE,
|
|
|
|
previousPath: origBlob.path,
|
|
|
|
content,
|
|
|
|
filePath: path,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function returns an array of diff actions (to be sent to the BE) based on the current vs. original blobs
|
|
|
|
*
|
|
|
|
* @param {Object} blobs
|
|
|
|
* @param {Object} origBlobs
|
|
|
|
*/
|
|
|
|
export const diffAll = (blobs, origBlobs) => {
|
|
|
|
const deletedEntries = Object.values(origBlobs)
|
2021-03-08 18:12:59 +05:30
|
|
|
.filter((x) => !blobs[x.id])
|
2020-10-24 23:57:45 +05:30
|
|
|
.map(({ path, content }) => ({
|
|
|
|
action: SNIPPET_BLOB_ACTION_DELETE,
|
|
|
|
previousPath: path,
|
|
|
|
filePath: path,
|
|
|
|
content,
|
|
|
|
}));
|
|
|
|
|
|
|
|
const newEntries = Object.values(blobs)
|
2021-03-08 18:12:59 +05:30
|
|
|
.map((blob) => diff(blob, origBlobs[blob.id]))
|
|
|
|
.filter((x) => x);
|
2020-10-24 23:57:45 +05:30
|
|
|
|
|
|
|
return [...deletedEntries, ...newEntries];
|
|
|
|
};
|
2020-11-24 15:15:51 +05:30
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
export const defaultSnippetVisibilityLevels = (arr) => {
|
2020-11-24 15:15:51 +05:30
|
|
|
if (Array.isArray(arr)) {
|
2021-03-08 18:12:59 +05:30
|
|
|
return arr.map((l) => {
|
2020-11-24 15:15:51 +05:30
|
|
|
const translatedLevel = SNIPPET_LEVELS_MAP[l];
|
|
|
|
return {
|
|
|
|
value: translatedLevel,
|
|
|
|
...SNIPPET_VISIBILITY[translatedLevel],
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return [];
|
|
|
|
};
|
2021-01-03 14:25:43 +05:30
|
|
|
|
|
|
|
export const markBlobPerformance = () => {
|
|
|
|
performanceMarkAndMeasure({
|
|
|
|
mark: SNIPPET_MARK_BLOBS_CONTENT,
|
|
|
|
measures: [
|
|
|
|
{
|
|
|
|
name: SNIPPET_MEASURE_BLOBS_CONTENT,
|
|
|
|
start: undefined,
|
|
|
|
end: SNIPPET_MARK_BLOBS_CONTENT,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
};
|