77import type { Disposable } from './disposable.js' ;
88import type { URI } from './uri-utils.js' ;
99import type { LangiumSharedCoreServices } from '../services.js' ;
10+ import type { DocumentState } from '../workspace/documents.js' ;
1011
1112export abstract class DisposableCache implements Disposable {
1213
@@ -141,14 +142,35 @@ export class ContextCache<Context, Key, Value, ContextKey = Context> extends Dis
141142 * If this document is changed or deleted, all associated key/value pairs are deleted.
142143 */
143144export class DocumentCache < K , V > extends ContextCache < URI | string , K , V , string > {
144- constructor ( sharedServices : LangiumSharedCoreServices ) {
145+
146+ /**
147+ * Creates a new document cache.
148+ *
149+ * @param sharedServices Service container instance to hook into document lifecycle events.
150+ * @param state Optional document state on which the cache should evict.
151+ * If not provided, the cache will evict on `DocumentBuilder#onUpdate`.
152+ * Note that only *changed* documents are considered in this case.
153+ *
154+ * Providing a state here will use `DocumentBuilder#onDocumentPhase` instead,
155+ * which triggers on all documents that have been affected by this change, assuming that the
156+ * state is `DocumentState.Linked` or a later state.
157+ */
158+ constructor ( sharedServices : LangiumSharedCoreServices , state ?: DocumentState ) {
145159 super ( uri => uri . toString ( ) ) ;
146- this . onDispose ( sharedServices . workspace . DocumentBuilder . onUpdate ( ( changed , deleted ) => {
147- const allUris = changed . concat ( deleted ) ;
148- for ( const uri of allUris ) {
149- this . clear ( uri ) ;
150- }
151- } ) ) ;
160+ let disposable : Disposable ;
161+ if ( state ) {
162+ disposable = sharedServices . workspace . DocumentBuilder . onDocumentPhase ( state , document => {
163+ this . clear ( document . uri . toString ( ) ) ;
164+ } ) ;
165+ } else {
166+ disposable = sharedServices . workspace . DocumentBuilder . onUpdate ( ( changed , deleted ) => {
167+ const allUris = changed . concat ( deleted ) ;
168+ for ( const uri of allUris ) {
169+ this . clear ( uri ) ;
170+ }
171+ } ) ;
172+ }
173+ this . toDispose . push ( disposable ) ;
152174 }
153175}
154176
@@ -157,10 +179,26 @@ export class DocumentCache<K, V> extends ContextCache<URI | string, K, V, string
157179 * If any document in the workspace changes, the whole cache is evicted.
158180 */
159181export class WorkspaceCache < K , V > extends SimpleCache < K , V > {
160- constructor ( sharedServices : LangiumSharedCoreServices ) {
182+
183+ /**
184+ * Creates a new workspace cache.
185+ *
186+ * @param sharedServices Service container instance to hook into document lifecycle events.
187+ * @param state Optional document state on which the cache should evict.
188+ * If not provided, the cache will evict on `DocumentBuilder#onUpdate`.
189+ */
190+ constructor ( sharedServices : LangiumSharedCoreServices , state ?: DocumentState ) {
161191 super ( ) ;
162- this . onDispose ( sharedServices . workspace . DocumentBuilder . onUpdate ( ( ) => {
163- this . clear ( ) ;
164- } ) ) ;
192+ let disposable : Disposable ;
193+ if ( state ) {
194+ disposable = sharedServices . workspace . DocumentBuilder . onBuildPhase ( state , ( ) => {
195+ this . clear ( ) ;
196+ } ) ;
197+ } else {
198+ disposable = sharedServices . workspace . DocumentBuilder . onUpdate ( ( ) => {
199+ this . clear ( ) ;
200+ } ) ;
201+ }
202+ this . toDispose . push ( disposable ) ;
165203 }
166204}
0 commit comments