Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fire JSON schema changed event if an underlying in-memory resource is changed #6035

Merged
merged 1 commit into from
Sep 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 24 additions & 12 deletions packages/core/src/browser/json-schema-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import debounce = require('lodash.debounce');

import { injectable, inject } from 'inversify';
import { InMemoryResources } from '../common/resource';
import { Disposable } from '../common/disposable';
import { Disposable, DisposableCollection } from '../common/disposable';
import { Emitter } from '../common/event';

import debounce = require('lodash.debounce');
import URI from '../common/uri';

export interface JsonSchemaConfiguration {
url: string
Expand All @@ -28,9 +29,11 @@ export interface JsonSchemaConfiguration {

@injectable()
export class JsonSchemaStore {
@inject(InMemoryResources) resources: InMemoryResources;

private _schemas: JsonSchemaConfiguration[] = [];
@inject(InMemoryResources)
protected readonly inMemoryResources: InMemoryResources;

private readonly schemas: JsonSchemaConfiguration[] = [];

protected readonly onSchemasChangedEmitter = new Emitter<void>();
readonly onSchemasChanged = this.onSchemasChangedEmitter.event;
Expand All @@ -40,19 +43,28 @@ export class JsonSchemaStore {
}, 500);

registerSchema(config: JsonSchemaConfiguration): Disposable {
this._schemas.push(config);
this.notifyChanged();
return Disposable.create(() => {
const idx = this._schemas.indexOf(config);
const toDispose = new DisposableCollection();
const uri = new URI(config.url);
if (uri.scheme === 'vscode') {
const resource = this.inMemoryResources.resolve(new URI(config.url));
if (resource && resource.onDidChangeContents) {
toDispose.push(resource.onDidChangeContents(() => this.notifyChanged()));
}
}
this.schemas.push(config);
toDispose.push(Disposable.create(() => {
const idx = this.schemas.indexOf(config);
if (idx > -1) {
this._schemas.splice(idx, 1);
this.schemas.splice(idx, 1);
this.notifyChanged();
}
});
}));
this.notifyChanged();
return toDispose;
}

getJsonSchemaConfigurations(): JsonSchemaConfiguration[] {
return [ ...this._schemas];
return [...this.schemas];
}

}
9 changes: 5 additions & 4 deletions packages/core/src/common/resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,11 @@ export class InMemoryResources implements ResourceResolver {
return resource;
}

resolve(uri: URI): MaybePromise<Resource> {
if (!this.resources.has(uri.toString())) {
throw new Error('Resource does not exist.');
resolve(uri: URI): Resource {
const uriString = uri.toString();
if (!this.resources.has(uriString)) {
throw new Error(`In memory '${uriString}' resource does not exist.`);
}
return this.resources.get(uri.toString())!;
return this.resources.get(uriString)!;
}
}