Skip to content

Commit cb905d0

Browse files
committed
cleanup commitState, introduce state caching by committish
Signed-off-by: Kipras Melnikovas <kipras@kipras.org>
1 parent 1b66643 commit cb905d0

File tree

1 file changed

+45
-22
lines changed

1 file changed

+45
-22
lines changed

nvim-git-rebase-todo/nvim-git-rebase-todo.ts

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/* eslint-disable indent */
2+
/* eslint-disable no-cond-assign */
23

34
import cp from "child_process";
45
import util from "util";
@@ -27,6 +28,7 @@ export default function nvimGitRebaseTodo(plugin: NvimPlugin): void {
2728
maxWidth: 60,
2829
fixedWidth: 60,
2930
showStatParsingCount: false,
31+
showCacheHitOrMiss: false,
3032

3133
// relativeToCursor: true,
3234
// closeToLeft: 5,
@@ -277,45 +279,64 @@ export default function nvimGitRebaseTodo(plugin: NvimPlugin): void {
277279
};
278280

279281
type Committish = string | null;
280-
let previousCommittish: Committish;
282+
type State = {
283+
committish: Committish;
284+
lines: string[];
285+
width: number;
286+
height: number;
287+
};
281288

282-
const drawLinesOfCommittishStat = async (): Promise<Committish> => {
289+
let prevState: State | null = null;
290+
const committishToStatCache: Map<NonNullable<Committish>, State> = new Map();
291+
const commitState = (state: State): Promise<State> =>
292+
Promise.resolve()
293+
.then(() => updateBuffer(state.lines))
294+
.then((buffer) =>
295+
updateWindow({
296+
buffer, //
297+
width: state.width,
298+
height: state.height,
299+
})
300+
)
301+
.then(() => state.committish && committishToStatCache.set(state.committish, state))
302+
.then(() => (prevState = state));
303+
304+
const drawLinesOfCommittishStat = async (): Promise<State> => {
283305
const committish: Committish = await getCommittishOfCurrentLine();
284306

285-
if (committish === previousCommittish) {
286-
return previousCommittish;
307+
if (committish === prevState?.committish) {
308+
return prevState;
287309
}
288310

289-
/**
290-
* TODO OPTIMIZE
291-
* could cache already computed Committish'es
292-
* see also the above idea for pre-computing all lines in the file
293-
*/
294-
const commitState = async (opts: Omit<InitWindowOpts, "buffer"> & { lines: string[] }): Promise<Committish> => {
295-
const { lines, ...rest } = opts;
296-
297-
const buffer: Buffer = await updateBuffer(lines); //
298-
await updateWindow({
299-
buffer,
300-
...rest,
301-
});
302-
303-
previousCommittish = committish;
304-
return committish;
305-
};
306-
307311
if (!committish) {
308312
return commitState({
313+
committish,
309314
lines: [],
310315
width: 0,
311316
height: 0,
312317
});
313318
}
314319

320+
let tmp: State | undefined;
321+
if ((tmp = committishToStatCache.get(committish))) {
322+
if (!config.showCacheHitOrMiss) {
323+
return commitState(tmp);
324+
}
325+
326+
const cacheHit = "cache hit";
327+
if (!tmp.lines[0].includes(cacheHit)) {
328+
tmp.lines.unshift("");
329+
}
330+
tmp.lines[0] = cacheHit;
331+
332+
return commitState(tmp);
333+
}
334+
315335
const stat: string[] = await getStatLines(committish);
316336

317337
if (!stat.length) {
318338
return commitState({
339+
committish,
319340
lines: [],
320341
width: 0,
321342
height: 0,
@@ -326,6 +347,7 @@ export default function nvimGitRebaseTodo(plugin: NvimPlugin): void {
326347

327348
if (longestLineLength === 0) {
328349
return commitState({
350+
committish,
329351
lines: [],
330352
width: 0,
331353
height: 0,
@@ -348,6 +370,7 @@ export default function nvimGitRebaseTodo(plugin: NvimPlugin): void {
348370
const height: number = Math.max(stat.length, config.minHeight + Number(!!config.showStatParsingCount));
349371

350372
return commitState({
373+
committish,
351374
lines: stat,
352375
width,
353376
height,

0 commit comments

Comments
 (0)