UNPKG

@git-temporal/git-temporal-react

Version:

<!-- START doctoc generated TOC please keep comment here to allow auto update --> <!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->

100 lines (99 loc) 4.22 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const logger_1 = require("app/utilities/logger"); const setDates_1 = require("app/actions/setDates"); const ActionTypes_1 = require("app/actions/ActionTypes"); const vscode_1 = require("app/actions/vscode"); const diff_1 = require("app/actions/diff"); const PAGE_SIZE = 1000; exports.fetchCommitsIfNeeded = path => (dispatch, getState) => { if (shouldFetchCommits(getState(), path)) { dispatch(exports.requestCommits(path)); dispatch(fetchCommits(path)); } }; const shouldFetchCommits = (state, path) => { return (!state.commits || state.commits.length <= 0 || state.selectedPath !== path); }; exports.requestCommits = path => ({ selectedPath: path, type: ActionTypes_1.ActionTypes.REQUEST_COMMITS, }); const fetchCommits = path => (dispatch) => __awaiter(this, void 0, void 0, function* () { dispatch(diff_1.fetchDiff(path, null, null)); const response = yield _fetch('commitRange', { path }); if (response) { const commitRange = yield response.json(); dispatch(exports.receiveCommitRange(path, commitRange)); } }); exports.receiveCommitRange = (path, commitRange) => (dispatch) => __awaiter(this, void 0, void 0, function* () { dispatch(_receiveCommitRange(path, commitRange)); dispatch(exports.fetchPageOfCommits(path, 0, PAGE_SIZE)); }); const _receiveCommitRange = (path, commitRange) => { return { path, commitRange, type: ActionTypes_1.ActionTypes.RECEIVE_COMMIT_RANGE, }; }; exports.fetchPageOfCommits = (path, skip, maxCount) => (dispatch) => __awaiter(this, void 0, void 0, function* () { const response = yield _fetch('history', { path, skip, maxCount }); if (response) { const commits = yield response.json(); dispatch(exports.receiveCommits(path, commits)); } }); exports.receiveCommits = (path, response) => (dispatch, getState) => { const state = getState(); if (!state.isFetching || path !== state.selectedPath) { return; } dispatch(exports._receiveCommits(path, response)); logger_1.debug('actions/commits receiveCommits', response.skip); if (state.isDiffDeferred) { const { commits } = getState(); dispatch(diff_1.fetchDiff(path, commits[1], commits[0])); } const nextSkip = response.skip + PAGE_SIZE; const { totalCommits } = getState(); if (nextSkip < totalCommits) { const pageSize = nextSkip + PAGE_SIZE > totalCommits ? totalCommits - nextSkip : PAGE_SIZE; dispatch(exports.fetchPageOfCommits(path, nextSkip, pageSize)); } else { dispatch(exports.receivedAllCommits(path)); } }; exports._receiveCommits = (path, response) => ({ selectedPath: path, commits: response.commits, isFileSelected: response.isFile, type: ActionTypes_1.ActionTypes.RECEIVE_COMMITS, }); exports.receivedAllCommits = path => ({ selectedPath: path, type: ActionTypes_1.ActionTypes.RECEIVED_All_COMMITS, }); const _fetch = (command, params) => { if (vscode_1.isVscode) { logger_1.debug('sending request to vscode', command, params); vscode_1.vscode.postMessage(Object.assign({}, params, { command })); return null; } const url = new URL(`http://localhost:11966/git-temporal/${command}`); url.search = new URLSearchParams(params).toString(); return fetch(url.toString()); }; exports.selectSingleCommit = commit => (dispatch, _getState) => { dispatch(setDates_1.setDates((commit.authorDate - 1) * 1000, (commit.authorDate + 1) * 1000)); };