Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.

Commit 9adff71

Browse files
committed
Refactor cache keys into its own module
1 parent 29e175c commit 9adff71

2 files changed

Lines changed: 165 additions & 165 deletions

File tree

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
class CacheKey {
2+
constructor(primary, groups = []) {
3+
this.primary = primary;
4+
this.groups = groups;
5+
}
6+
7+
getPrimary() {
8+
return this.primary;
9+
}
10+
11+
getGroups() {
12+
return this.groups;
13+
}
14+
15+
removeFromCache(cache, withoutGroup = null) {
16+
cache.removePrimary(this.getPrimary());
17+
18+
const groups = this.getGroups();
19+
for (let i = 0; i < groups.length; i++) {
20+
const group = groups[i];
21+
if (group === withoutGroup) {
22+
continue;
23+
}
24+
25+
cache.removeFromGroup(group, this);
26+
}
27+
}
28+
29+
/* istanbul ignore next */
30+
toString() {
31+
return `CacheKey(${this.primary})`;
32+
}
33+
}
34+
35+
class GroupKey {
36+
constructor(group) {
37+
this.group = group;
38+
}
39+
40+
removeFromCache(cache) {
41+
for (const matchingKey of cache.keysInGroup(this.group)) {
42+
matchingKey.removeFromCache(cache, this.group);
43+
}
44+
}
45+
46+
/* istanbul ignore next */
47+
toString() {
48+
return `GroupKey(${this.group})`;
49+
}
50+
}
51+
52+
export const Keys = {
53+
statusBundle: new CacheKey('status-bundle'),
54+
55+
stagedChanges: new CacheKey('staged-changes'),
56+
57+
filePatch: {
58+
_optKey: ({staged}) => (staged ? 's' : 'u'),
59+
60+
oneWith: (fileName, options) => { // <-- Keys.filePatch
61+
const optKey = Keys.filePatch._optKey(options);
62+
const baseCommit = options.baseCommit || 'head';
63+
64+
const extraGroups = [];
65+
if (options.baseCommit) {
66+
extraGroups.push(`file-patch:base-nonhead:path-${fileName}`);
67+
extraGroups.push('file-patch:base-nonhead');
68+
} else {
69+
extraGroups.push('file-patch:base-head');
70+
}
71+
72+
return new CacheKey(`file-patch:${optKey}:${baseCommit}:${fileName}`, [
73+
'file-patch',
74+
`file-patch:opt-${optKey}`,
75+
`file-patch:opt-${optKey}:path-${fileName}`,
76+
...extraGroups,
77+
]);
78+
},
79+
80+
eachWithFileOpts: (fileNames, opts) => {
81+
const keys = [];
82+
for (let i = 0; i < fileNames.length; i++) {
83+
for (let j = 0; j < opts.length; j++) {
84+
keys.push(new GroupKey(`file-patch:opt-${Keys.filePatch._optKey(opts[j])}:path-${fileNames[i]}`));
85+
}
86+
}
87+
return keys;
88+
},
89+
90+
eachNonHeadWithFiles: fileNames => {
91+
return fileNames.map(fileName => new GroupKey(`file-patch:base-nonhead:path-${fileName}`));
92+
},
93+
94+
allAgainstNonHead: new GroupKey('file-patch:base-nonhead'),
95+
96+
eachWithOpts: (...opts) => opts.map(opt => new GroupKey(`file-patch:opt-${Keys.filePatch._optKey(opt)}`)),
97+
98+
all: new GroupKey('file-patch'),
99+
},
100+
101+
index: {
102+
oneWith: fileName => new CacheKey(`index:${fileName}`, ['index']),
103+
104+
all: new GroupKey('index'),
105+
},
106+
107+
lastCommit: new CacheKey('last-commit'),
108+
109+
recentCommits: new CacheKey('recent-commits'),
110+
111+
authors: new CacheKey('authors'),
112+
113+
branches: new CacheKey('branches'),
114+
115+
headDescription: new CacheKey('head-description'),
116+
117+
remotes: new CacheKey('remotes'),
118+
119+
config: {
120+
_optKey: options => (options.local ? 'l' : ''),
121+
122+
oneWith: (setting, options) => {
123+
const optKey = Keys.config._optKey(options);
124+
return new CacheKey(`config:${optKey}:${setting}`, ['config', `config:${optKey}`]);
125+
},
126+
127+
eachWithSetting: setting => [
128+
Keys.config.oneWith(setting, {local: true}),
129+
Keys.config.oneWith(setting, {local: false}),
130+
],
131+
132+
all: new GroupKey('config'),
133+
},
134+
135+
blob: {
136+
oneWith: sha => new CacheKey(`blob:${sha}`, ['blob']),
137+
},
138+
139+
// Common collections of keys and patterns for use with invalidate().
140+
141+
workdirOperationKeys: fileNames => [
142+
Keys.statusBundle,
143+
...Keys.filePatch.eachWithFileOpts(fileNames, [{staged: false}]),
144+
],
145+
146+
cacheOperationKeys: fileNames => [
147+
...Keys.workdirOperationKeys(fileNames),
148+
...Keys.filePatch.eachWithFileOpts(fileNames, [{staged: true}]),
149+
...fileNames.map(Keys.index.oneWith),
150+
Keys.stagedChanges,
151+
],
152+
153+
headOperationKeys: () => [
154+
Keys.headDescription,
155+
Keys.branches,
156+
...Keys.filePatch.eachWithOpts({staged: true}),
157+
Keys.filePatch.allAgainstNonHead,
158+
Keys.stagedChanges,
159+
Keys.lastCommit,
160+
Keys.recentCommits,
161+
Keys.authors,
162+
Keys.statusBundle,
163+
],
164+
};

lib/models/repository-states/present.js

Lines changed: 1 addition & 165 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {Emitter} from 'event-kit';
33
import fs from 'fs-extra';
44

55
import State from './state';
6+
import {Keys} from './cache/keys';
67

78
import {LargeRepoError} from '../../git-shell-out-strategy';
89
import {FOCUS} from '../workspace-change-observer';
@@ -1048,168 +1049,3 @@ class Cache {
10481049
this.emitter.dispose();
10491050
}
10501051
}
1051-
1052-
class CacheKey {
1053-
constructor(primary, groups = []) {
1054-
this.primary = primary;
1055-
this.groups = groups;
1056-
}
1057-
1058-
getPrimary() {
1059-
return this.primary;
1060-
}
1061-
1062-
getGroups() {
1063-
return this.groups;
1064-
}
1065-
1066-
removeFromCache(cache, withoutGroup = null) {
1067-
cache.removePrimary(this.getPrimary());
1068-
1069-
const groups = this.getGroups();
1070-
for (let i = 0; i < groups.length; i++) {
1071-
const group = groups[i];
1072-
if (group === withoutGroup) {
1073-
continue;
1074-
}
1075-
1076-
cache.removeFromGroup(group, this);
1077-
}
1078-
}
1079-
1080-
/* istanbul ignore next */
1081-
toString() {
1082-
return `CacheKey(${this.primary})`;
1083-
}
1084-
}
1085-
1086-
class GroupKey {
1087-
constructor(group) {
1088-
this.group = group;
1089-
}
1090-
1091-
removeFromCache(cache) {
1092-
for (const matchingKey of cache.keysInGroup(this.group)) {
1093-
matchingKey.removeFromCache(cache, this.group);
1094-
}
1095-
}
1096-
1097-
/* istanbul ignore next */
1098-
toString() {
1099-
return `GroupKey(${this.group})`;
1100-
}
1101-
}
1102-
1103-
const Keys = {
1104-
statusBundle: new CacheKey('status-bundle'),
1105-
1106-
stagedChanges: new CacheKey('staged-changes'),
1107-
1108-
filePatch: {
1109-
_optKey: ({staged}) => (staged ? 's' : 'u'),
1110-
1111-
oneWith: (fileName, options) => { // <-- Keys.filePatch
1112-
const optKey = Keys.filePatch._optKey(options);
1113-
const baseCommit = options.baseCommit || 'head';
1114-
1115-
const extraGroups = [];
1116-
if (options.baseCommit) {
1117-
extraGroups.push(`file-patch:base-nonhead:path-${fileName}`);
1118-
extraGroups.push('file-patch:base-nonhead');
1119-
} else {
1120-
extraGroups.push('file-patch:base-head');
1121-
}
1122-
1123-
return new CacheKey(`file-patch:${optKey}:${baseCommit}:${fileName}`, [
1124-
'file-patch',
1125-
`file-patch:opt-${optKey}`,
1126-
`file-patch:opt-${optKey}:path-${fileName}`,
1127-
...extraGroups,
1128-
]);
1129-
},
1130-
1131-
eachWithFileOpts: (fileNames, opts) => {
1132-
const keys = [];
1133-
for (let i = 0; i < fileNames.length; i++) {
1134-
for (let j = 0; j < opts.length; j++) {
1135-
keys.push(new GroupKey(`file-patch:opt-${Keys.filePatch._optKey(opts[j])}:path-${fileNames[i]}`));
1136-
}
1137-
}
1138-
return keys;
1139-
},
1140-
1141-
eachNonHeadWithFiles: fileNames => {
1142-
return fileNames.map(fileName => new GroupKey(`file-patch:base-nonhead:path-${fileName}`));
1143-
},
1144-
1145-
allAgainstNonHead: new GroupKey('file-patch:base-nonhead'),
1146-
1147-
eachWithOpts: (...opts) => opts.map(opt => new GroupKey(`file-patch:opt-${Keys.filePatch._optKey(opt)}`)),
1148-
1149-
all: new GroupKey('file-patch'),
1150-
},
1151-
1152-
index: {
1153-
oneWith: fileName => new CacheKey(`index:${fileName}`, ['index']),
1154-
1155-
all: new GroupKey('index'),
1156-
},
1157-
1158-
lastCommit: new CacheKey('last-commit'),
1159-
1160-
recentCommits: new CacheKey('recent-commits'),
1161-
1162-
authors: new CacheKey('authors'),
1163-
1164-
branches: new CacheKey('branches'),
1165-
1166-
headDescription: new CacheKey('head-description'),
1167-
1168-
remotes: new CacheKey('remotes'),
1169-
1170-
config: {
1171-
_optKey: options => (options.local ? 'l' : ''),
1172-
1173-
oneWith: (setting, options) => {
1174-
const optKey = Keys.config._optKey(options);
1175-
return new CacheKey(`config:${optKey}:${setting}`, ['config', `config:${optKey}`]);
1176-
},
1177-
1178-
eachWithSetting: setting => [
1179-
Keys.config.oneWith(setting, {local: true}),
1180-
Keys.config.oneWith(setting, {local: false}),
1181-
],
1182-
1183-
all: new GroupKey('config'),
1184-
},
1185-
1186-
blob: {
1187-
oneWith: sha => new CacheKey(`blob:${sha}`, ['blob']),
1188-
},
1189-
1190-
// Common collections of keys and patterns for use with invalidate().
1191-
1192-
workdirOperationKeys: fileNames => [
1193-
Keys.statusBundle,
1194-
...Keys.filePatch.eachWithFileOpts(fileNames, [{staged: false}]),
1195-
],
1196-
1197-
cacheOperationKeys: fileNames => [
1198-
...Keys.workdirOperationKeys(fileNames),
1199-
...Keys.filePatch.eachWithFileOpts(fileNames, [{staged: true}]),
1200-
...fileNames.map(Keys.index.oneWith),
1201-
Keys.stagedChanges,
1202-
],
1203-
1204-
headOperationKeys: () => [
1205-
Keys.headDescription,
1206-
Keys.branches,
1207-
...Keys.filePatch.eachWithOpts({staged: true}),
1208-
Keys.filePatch.allAgainstNonHead,
1209-
Keys.stagedChanges,
1210-
Keys.lastCommit,
1211-
Keys.recentCommits,
1212-
Keys.authors,
1213-
Keys.statusBundle,
1214-
],
1215-
};

0 commit comments

Comments
 (0)