Skip to content

Commit

Permalink
[markers] don't store by default
Browse files Browse the repository at this point in the history
Signed-off-by: Anton Kosyakov <anton.kosyakov@typefox.io>
  • Loading branch information
akosyakov committed Jul 10, 2018
1 parent 32339fe commit ec169c1
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 159 deletions.
72 changes: 0 additions & 72 deletions packages/markers/src/browser/marker-manager.spec.ts

This file was deleted.

83 changes: 16 additions & 67 deletions packages/markers/src/browser/marker-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,12 @@
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import { injectable, inject } from 'inversify';
import { injectable, inject, postConstruct } from 'inversify';
import { Event, Emitter } from "@theia/core/lib/common";
import URI from "@theia/core/lib/common/uri";
import { StorageService } from '@theia/core/lib/browser/storage-service';
import { FileSystemWatcher, FileChangeType } from '@theia/filesystem/lib/browser/filesystem-watcher';
import { Marker } from '../common/marker';

const debounce = require("lodash.debounce");

/*
* argument to the `findMarkers` method.
*/
Expand Down Expand Up @@ -113,66 +110,23 @@ export abstract class MarkerManager<D extends object> {

protected readonly uri2MarkerCollection = new Map<string, MarkerCollection<D>>();
protected readonly onDidChangeMarkersEmitter = new Emitter<URI>();
readonly initialized: Promise<void>;

constructor(
@inject(StorageService) protected storageService: StorageService,
@inject(FileSystemWatcher) protected fileWatcher?: FileSystemWatcher) {
this.initialized = this.loadMarkersFromStorage();
if (fileWatcher) {
fileWatcher.onFilesChanged(changes => {
for (const change of changes) {
if (change.type === FileChangeType.DELETED) {
const uriString = change.uri.toString();
const collection = this.uri2MarkerCollection.get(uriString);
if (collection !== undefined) {
this.uri2MarkerCollection.delete(uriString);
this.fireOnDidChangeMarkers(change.uri);
}
@inject(FileSystemWatcher) protected fileWatcher: FileSystemWatcher;

@postConstruct()
protected init(): void {
this.fileWatcher.onFilesChanged(changes => {
for (const change of changes) {
if (change.type === FileChangeType.DELETED) {
const uriString = change.uri.toString();
const collection = this.uri2MarkerCollection.get(uriString);
if (collection !== undefined) {
this.uri2MarkerCollection.delete(uriString);
this.fireOnDidChangeMarkers(change.uri);
}
}
});
}
}

protected getStorageKey(): string | undefined {
return 'marker-' + this.getKind();
}

protected async loadMarkersFromStorage(): Promise<void> {
const key = this.getStorageKey();
if (key) {
const entries = await this.storageService.getData<Uri2MarkerEntry[]>(key, []);
for (const entry of entries) {
for (const ownerEntry of entry.markers) {
this.internalSetMarkers(new URI(entry.uri), ownerEntry.owner, ownerEntry.markerData as D[]);
}
}
this.onDidChangeMarkers(() => this.saveMarkersToStorage());
}
}

protected readonly saveMarkersToStorage = debounce(() => this.doSaveMarkersToStorage(), 500);
protected doSaveMarkersToStorage(): void {
const key = this.getStorageKey();
if (key) {
const result: Uri2MarkerEntry[] = [];
for (const [uri, collection] of this.uri2MarkerCollection.entries()) {
const ownerEntries: Owner2MarkerEntry[] = [];
for (const owner of collection.getOwners()) {
const markers = collection.getMarkers(owner);
ownerEntries.push({
owner,
markerData: Array.from(markers.map(m => m.data))
});
}
result.push({
uri,
markers: ownerEntries
});
}
this.storageService.setData<Uri2MarkerEntry[]>(key, result);
}
});
}

get onDidChangeMarkers(): Event<URI> {
Expand All @@ -186,14 +140,9 @@ export abstract class MarkerManager<D extends object> {
/*
* replaces the current markers for the given uri and owner with the given data.
*/
async setMarkers(uri: URI, owner: string, data: D[]): Promise<Marker<D>[]> {
await this.initialized;
return this.internalSetMarkers(uri, owner, data);
}

protected internalSetMarkers(uri: URI, owner: string, data: D[]): Marker<D>[] {
setMarkers(uri: URI, owner: string, data: D[]): Marker<D>[] {
const uriString = uri.toString();
const collection = this.uri2MarkerCollection.get(uriString) || new MarkerCollection<D>(uri, this.getKind());
const collection = this.uri2MarkerCollection.get(uriString) ||  new MarkerCollection<D>(uri, this.getKind());
const oldMarkers = collection.setMarkers(owner, data);
if (data.length > 0) {
this.uri2MarkerCollection.set(uriString, collection);
Expand Down
18 changes: 7 additions & 11 deletions packages/markers/src/browser/problem/problem-manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import * as chai from 'chai';
import { ProblemManager } from './problem-manager';
import URI from "@theia/core/lib/common/uri";
import { LocalStorageService, StorageService } from '@theia/core/lib/browser/storage-service';
import { Event } from '@theia/core/lib/common/event';
import { ILogger } from '@theia/core/lib/common/logger';
import { MockLogger } from '@theia/core/lib/common/test/mock-logger';
import { FileSystemWatcher } from '@theia/filesystem/lib/browser/filesystem-watcher';
Expand All @@ -27,17 +28,18 @@ const expect = chai.expect;
let manager: ProblemManager;
let testContainer: Container;

before(async () => {
before(() => {
testContainer = new Container();
testContainer.bind(ILogger).to(MockLogger);
testContainer.bind(StorageService).to(LocalStorageService).inSingletonScope();
testContainer.bind(LocalStorageService).toSelf().inSingletonScope();
// tslint:disable-next-line:no-any
testContainer.bind(FileSystemWatcher).toConstantValue(<any>undefined);
testContainer.bind(FileSystemWatcher).toConstantValue({
onFilesChanged: Event.None
} as FileSystemWatcher);
testContainer.bind(ProblemManager).toSelf();

manager = testContainer.get(ProblemManager);
await manager.initialized;
manager.setMarkers(new URI('file:/foo/bar.txt'), 'me', [
{
range: {
Expand Down Expand Up @@ -98,13 +100,13 @@ before(async () => {
});

describe('problem-manager', () => {
it('replaces markers', async () => {
it('replaces markers', () => {
let events = 0;
manager.onDidChangeMarkers(() => {
events++;
});
expect(events).equal(0);
const previous = await manager.setMarkers(new URI('file:/foo/bar.txt'), 'me', [
const previous = manager.setMarkers(new URI('file:/foo/bar.txt'), 'me', [
{
range: {
start: {
Expand Down Expand Up @@ -155,10 +157,4 @@ describe('problem-manager', () => {
dataFilter: data => data.range.end.character > 1
}).length).equal(1);
});

it('should persist markers', async () => {
const newManager = testContainer.get(ProblemManager);
await newManager.initialized;
expect(newManager.findMarkers().length).eq(4);
});
});
10 changes: 1 addition & 9 deletions packages/markers/src/browser/problem/problem-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import { injectable, inject } from 'inversify';
import { injectable } from 'inversify';
import { MarkerManager } from '../marker-manager';
import { PROBLEM_KIND } from '../../common/problem-marker';
import { Marker } from '../../common/marker';
import { StorageService } from '@theia/core/lib/browser/storage-service';
import { FileSystemWatcher } from '@theia/filesystem/lib/browser/filesystem-watcher';
import URI from '@theia/core/lib/common/uri';
import { Diagnostic } from "vscode-languageserver-types";

Expand All @@ -35,12 +33,6 @@ export class ProblemManager extends MarkerManager<Diagnostic> {
return PROBLEM_KIND;
}

constructor(
@inject(StorageService) storageService: StorageService,
@inject(FileSystemWatcher) protected fileWatcher?: FileSystemWatcher) {
super(storageService, fileWatcher);
}

getProblemStat(): ProblemStat {
const allMarkers: Marker<Diagnostic>[] = [];
for (const uri of this.getUris()) {
Expand Down

0 comments on commit ec169c1

Please sign in to comment.