reduce one extra guessMimeTypes() call when resolving a folder in the explorer

This commit is contained in:
Benjamin Pasero 2016-08-16 11:28:05 +02:00
parent d699c077a6
commit 40ca4cba4a
3 changed files with 16 additions and 15 deletions

View file

@ -10,8 +10,8 @@ import types = require('vs/base/common/types');
import URI from 'vs/base/common/uri';
import {isLinux} from 'vs/base/common/platform';
import paths = require('vs/base/common/paths');
import {guessMimeTypes} from 'vs/base/common/mime';
import {IFileStat} from 'vs/platform/files/common/files';
import {guessMimeTypes} from 'vs/base/common/mime';
export enum StatType {
FILE,
@ -32,12 +32,12 @@ export class FileStat implements IFileStat {
public isDirectoryResolved: boolean;
constructor(resource: URI, isDirectory?: boolean, hasChildren?: boolean, name: string = paths.basename(resource.fsPath), mtime?: number, etag?: string) {
constructor(resource: URI, isDirectory?: boolean, hasChildren?: boolean, name: string = paths.basename(resource.fsPath), mime = !isDirectory ? guessMimeTypes(resource.fsPath).join(', ') : void (0), mtime?: number, etag?: string) {
this.resource = resource;
this.name = name;
this.isDirectory = !!isDirectory;
this.hasChildren = isDirectory && hasChildren;
this.mime = !isDirectory ? guessMimeTypes(this.resource.fsPath).join(', ') : void (0);
this.mime = mime;
this.etag = etag;
this.mtime = mtime;
@ -54,7 +54,7 @@ export class FileStat implements IFileStat {
}
public static create(raw: IFileStat, resolveTo?: URI[]): FileStat {
let stat = new FileStat(raw.resource, raw.isDirectory, raw.hasChildren, raw.name, raw.mtime, raw.etag);
let stat = new FileStat(raw.resource, raw.isDirectory, raw.hasChildren, raw.name, raw.mime, raw.mtime, raw.etag);
// Recursively add children if present
if (stat.isDirectory) {
@ -139,7 +139,7 @@ export class FileStat implements IFileStat {
* Returns a deep copy of this model object.
*/
public clone(): FileStat {
let stat = new FileStat(URI.parse(this.resource.toString()), this.isDirectory, this.hasChildren, this.name, this.mtime, this.etag);
let stat = new FileStat(URI.parse(this.resource.toString()), this.isDirectory, this.hasChildren, this.name, this.mime, this.mtime, this.etag);
stat.isDirectoryResolved = this.isDirectoryResolved;
if (this.parent) {

View file

@ -10,12 +10,13 @@ import {isUndefinedOrNull, isArray} from 'vs/base/common/types';
import {isLinux, isWindows} from 'vs/base/common/platform';
import URI from 'vs/base/common/uri';
import {join} from 'vs/base/common/paths';
import {guessMimeTypes} from 'vs/base/common/mime';
import {validateFileName} from 'vs/workbench/parts/files/browser/fileActions';
import {LocalFileChangeEvent} from 'vs/workbench/parts/files/common/files';
import {FileStat} from 'vs/workbench/parts/files/common/explorerViewModel';
function createStat(path, name, isFolder, hasChildren, size, mtime) {
return new FileStat(toResource(path), isFolder, hasChildren, name, mtime);
return new FileStat(toResource(path), isFolder, hasChildren, name, !isFolder ? guessMimeTypes(path).join(', ') : null, mtime);
}
function toResource(path) {
@ -284,9 +285,9 @@ suite('Files - View Model', () => {
test('File Change Event (with stats)', function () {
let d = new Date().toUTCString();
let s1 = new FileStat(toResource('/path/to/sName'), false, false, 'sName', 8096 /* Size */, d);
let s2 = new FileStat(toResource('/path/to/sName'), false, false, 'sName', 16000 /* Size */, d);
let s3 = new FileStat(toResource('/path/to/sNameMoved'), false, false, 'sNameMoved', 8096 /* Size */, d);
let s1 = new FileStat(toResource('/path/to/sName'), false, false, 'sName', void 0, 8096 /* Size */, d);
let s2 = new FileStat(toResource('/path/to/sName'), false, false, 'sName', void 0, 16000 /* Size */, d);
let s3 = new FileStat(toResource('/path/to/sNameMoved'), false, false, 'sNameMoved', void 0, 8096 /* Size */, d);
// Got Added
let event = new LocalFileChangeEvent(null, s1);
@ -327,20 +328,20 @@ suite('Files - View Model', () => {
test('Merge Local with Disk', function () {
let d = new Date().toUTCString();
let merge1 = new FileStat(URI.file(join('C:\\', '/path/to')), true, false, 'to', 8096, d);
let merge2 = new FileStat(URI.file(join('C:\\', '/path/to')), true, false, 'to', 16000, new Date(0).toUTCString());
let merge1 = new FileStat(URI.file(join('C:\\', '/path/to')), true, false, 'to', void 0, 8096, d);
let merge2 = new FileStat(URI.file(join('C:\\', '/path/to')), true, false, 'to', void 0, 16000, new Date(0).toUTCString());
// Merge Properties
FileStat.mergeLocalWithDisk(merge2, merge1);
assert.strictEqual(merge1.mtime, merge2.mtime);
// Merge Child when isDirectoryResolved=false is a no-op
merge2.addChild(new FileStat(URI.file(join('C:\\', '/path/to/foo.html')), true, false, 'foo.html', 8096, d));
merge2.addChild(new FileStat(URI.file(join('C:\\', '/path/to/foo.html')), true, false, 'foo.html', void 0, 8096, d));
FileStat.mergeLocalWithDisk(merge2, merge1);
assert.strictEqual(merge1.children.length, 0);
// Merge Child with isDirectoryResolved=true
merge2.addChild(new FileStat(URI.file(join('C:\\', '/path/to/foo.html')), true, false, 'foo.html', 8096, d));
merge2.addChild(new FileStat(URI.file(join('C:\\', '/path/to/foo.html')), true, false, 'foo.html', void 0, 8096, d));
merge2.isDirectoryResolved = true;
FileStat.mergeLocalWithDisk(merge2, merge1);
assert.strictEqual(merge1.children.length, 1);

View file

@ -658,7 +658,7 @@ export class StatResolver {
this.isDirectory = isDirectory;
this.mtime = mtime;
this.name = paths.basename(resource.fsPath);
this.mime = !this.isDirectory ? baseMime.guessMimeTypes(resource.fsPath).join(', ') : null;
this.mime = !this.isDirectory ? baseMime.guessMimeTypes(resource.fsPath).join(', ') : void 0;
this.etag = etag(size, mtime);
this.size = size;
@ -760,7 +760,7 @@ export class StatResolver {
mtime: fileStat.mtime.getTime(),
etag: etag(fileStat),
size: fileStat.size,
mime: !fileStat.isDirectory() ? baseMime.guessMimeTypes(fileResource.fsPath).join(', ') : undefined
mime: !fileStat.isDirectory() ? baseMime.guessMimeTypes(fileResource.fsPath).join(', ') : void 0
};
// Return early for files