All files / services/document preset-document-service.ts

100% Statements 7/7
100% Branches 2/2
100% Functions 4/4
100% Lines 7/7

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46                                              3x     5x 4x 4x       5x               4x 40x        
import { PRESET_GRAPHS } from '@src/services/core/preset-graphs';
import { MentorVfsUri } from '@src/providers/mentor-vfs-uri';
import { DocumentContextService } from './document-context-service';
import { IPresetDocumentService } from './preset-document-service.interface';
 
/**
 * Loads the read-only documents that show the sources of the preset graphs, such as the
 * framework vocabularies and the preset shape graphs, into document contexts. The contexts
 * provide the source ranges of the resources defined in the preset graphs, so that language
 * features such as go to definition can navigate to them.
 *
 * The documents are loaded on demand rather than at startup, because they are only needed
 * when a resource is not defined in the workspace.
 */
export class PresetDocumentService implements IPresetDocumentService {
	/**
	 * The load in progress, shared by concurrent callers.
	 */
	private _pendingLoad: Promise<void> | undefined;
 
	/**
	 * @param _contextService The service that holds the loaded document contexts.
	 */
	constructor(private readonly _contextService: Pick<DocumentContextService, 'loadDocumentContent'>) { }
 
	ensureLoaded(): Promise<void> {
		if (!this._pendingLoad) {
			this._pendingLoad = this._loadDocuments().finally(() => {
				this._pendingLoad = undefined;
			});
		}
 
		return this._pendingLoad;
	}
 
	/**
	 * Loads every preset document. Documents that are already loaded are returned by the
	 * context service as they are, so contexts that were removed are loaded again.
	 */
	private async _loadDocuments(): Promise<void> {
		for (const graph of PRESET_GRAPHS) {
			await this._contextService.loadDocumentContent(MentorVfsUri.toUri(graph), graph.source);
		}
	}
}