|
| 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 | +}; |
0 commit comments