Skip to content

Commit b3b7f8c

Browse files
committed
fix borked global memory
1 parent 4dba353 commit b3b7f8c

File tree

7 files changed

+117
-8
lines changed

7 files changed

+117
-8
lines changed

packages/core/src/metadata/InternalMetadataSources.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export class InternalMetadataSource extends FilePathMetadataSource<FilePathMetad
2424
return {
2525
data: {},
2626
storagePath: storagePath,
27-
...this.manager.getDefaultCacheItem(),
27+
...this.manager.constructDefaultCacheItem(),
2828
};
2929
}
3030

@@ -51,7 +51,7 @@ export class TestMetadataSource extends FilePathMetadataSource<FilePathMetadataC
5151
return {
5252
data: this.readExternal(storagePath),
5353
storagePath: storagePath,
54-
...this.manager.getDefaultCacheItem(),
54+
...this.manager.constructDefaultCacheItem(),
5555
};
5656
}
5757

@@ -72,7 +72,7 @@ export class GlobalMetadataSource implements IMetadataSource<GlobalMetadataCache
7272

7373
this.cache = {
7474
data: {},
75-
...this.manager.getDefaultCacheItem(),
75+
...this.manager.constructDefaultCacheItem(),
7676
};
7777
}
7878

@@ -103,7 +103,7 @@ export class GlobalMetadataSource implements IMetadataSource<GlobalMetadataCache
103103
}
104104

105105
public getCacheItemForStoragePath(_storagePath: string): GlobalMetadataCacheItem | undefined {
106-
return undefined;
106+
return this.cache;
107107
}
108108

109109
public getCacheItems(): GlobalMetadataCacheItem[] {
@@ -155,6 +155,10 @@ export class GlobalMetadataSource implements IMetadataSource<GlobalMetadataCache
155155
public readEntireCacheItem(cacheItem: GlobalMetadataCacheItem): Metadata {
156156
return cacheItem.data;
157157
}
158+
159+
public usesStoragePath(): boolean {
160+
return false;
161+
}
158162
}
159163

160164
export class ScopeMetadataSource implements IMetadataSource<IMetadataCacheItem> {
@@ -259,4 +263,8 @@ export class ScopeMetadataSource implements IMetadataSource<IMetadataCacheItem>
259263
cause: `source 'scope' should have no cache items or subscriptions`,
260264
});
261265
}
266+
267+
public usesStoragePath(): boolean {
268+
return false;
269+
}
262270
}

packages/core/src/metadata/MetadataManager.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,10 @@ export class MetadataManager {
558558
*/
559559
public deleteCachesForStoragePath(storagePath: string): void {
560560
for (const source of this.sources.values()) {
561+
if (!source.usesStoragePath()) {
562+
continue;
563+
}
564+
561565
const cacheItem = source.getCacheItemForStoragePath(storagePath);
562566
if (cacheItem === undefined) {
563567
continue;
@@ -567,7 +571,7 @@ export class MetadataManager {
567571
}
568572
}
569573

570-
public getDefaultCacheItem(): IMetadataCacheItem {
574+
public constructDefaultCacheItem(): IMetadataCacheItem {
571575
return {
572576
subscriptions: [],
573577
externalWriteLock: 0,

packages/core/src/metadata/MetadataSource.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ export interface IMetadataSource<T extends IMetadataCacheItem> {
140140
* @returns The entire data of the cache item.
141141
*/
142142
readEntireCacheItem(cacheItem: T): Metadata;
143+
144+
usesStoragePath(): boolean;
143145
}
144146

145147
export abstract class FilePathMetadataSource<T extends FilePathMetadataCacheItem> implements IMetadataSource<T> {
@@ -283,4 +285,8 @@ export abstract class FilePathMetadataSource<T extends FilePathMetadataCacheItem
283285
readEntireCacheItem(cacheItem: T): Metadata {
284286
return cacheItem.data;
285287
}
288+
289+
usesStoragePath(): boolean {
290+
return true;
291+
}
286292
}

packages/obsidian/src/ObsMetadataSource.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export class ObsMetadataSource extends FilePathMetadataSource<ObsMetadataCacheIt
6060
data: structuredClone(frontmatter) ?? {},
6161
storagePath: storagePath,
6262
file: file,
63-
...this.manager.getDefaultCacheItem(),
63+
...this.manager.constructDefaultCacheItem(),
6464
};
6565
}
6666

packages/publish/src/PublishMetadataSource.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export class PublishMetadataSource extends FilePathMetadataSource<FilePathMetada
2727
return {
2828
data: structuredClone(frontmatter) ?? {},
2929
storagePath: storagePath,
30-
...this.manager.getDefaultCacheItem(),
30+
...this.manager.constructDefaultCacheItem(),
3131
};
3232
}
3333

tests/__mocks__/TestFileAPI.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ export class TestFileAPI extends FileAPI<TestComponents> {
3232
}
3333

3434
public create(folderPath: string, fileName: string, extension: string, open?: boolean): Promise<string> {
35-
const filePath = `${folderPath}/${fileName}.${extension}`;
35+
let filePath = `${folderPath}/${fileName}.${extension}`;
36+
if (filePath.startsWith('/')) {
37+
filePath = filePath.slice(1);
38+
}
3639
this.fileSystem.writeFile(filePath, '');
3740
return Promise.resolve(filePath);
3841
}

tests/api.test.ts

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,4 +262,92 @@ action:
262262
expect(callback).toHaveBeenLastCalledWith(1);
263263
});
264264
});
265+
266+
describe('update metadata interactions', () => {
267+
beforeEach(() => {
268+
plugin.file.create('', 'test', 'md');
269+
});
270+
271+
test.each(['foo', 'memory^foo', 'globalMemory^foo'])('view field created after', async value => {
272+
const bindTarget = plugin.api.parseBindTarget(value, 'test.md');
273+
274+
plugin.api.setMetadata(bindTarget, 123);
275+
276+
expect(plugin.api.getMetadata(bindTarget)).toBe(123);
277+
278+
const viewField = plugin.api.createViewFieldMountable('test.md', {
279+
declaration: `VIEW[{${value}}]`,
280+
renderChildType: RenderChildType.INLINE,
281+
});
282+
283+
viewField.mount(document.body);
284+
285+
await new Promise(resolve => setTimeout(resolve, 0));
286+
287+
expect(viewField.viewField?.getTargetEl()?.textContent).toBe('123');
288+
});
289+
290+
test.each(['foo', 'memory^foo', 'globalMemory^foo'])('input field created after', async value => {
291+
const bindTarget = plugin.api.parseBindTarget(value, 'test.md');
292+
293+
plugin.api.setMetadata(bindTarget, 123);
294+
295+
expect(plugin.api.getMetadata(bindTarget)).toBe(123);
296+
297+
const inputField = plugin.api.createInputFieldMountable('test.md', {
298+
declaration: `INPUT[number:${value}]`,
299+
renderChildType: RenderChildType.INLINE,
300+
});
301+
302+
inputField.mount(document.body);
303+
304+
await new Promise(resolve => setTimeout(resolve, 0));
305+
306+
expect(inputField.inputField?.getValue()).toBe(123);
307+
});
308+
309+
test.each(['foo', 'memory^foo', 'globalMemory^foo'])('view field created before', async value => {
310+
const bindTarget = plugin.api.parseBindTarget(value, 'test.md');
311+
312+
const viewField = plugin.api.createViewFieldMountable('test.md', {
313+
declaration: `VIEW[{${value}}]`,
314+
renderChildType: RenderChildType.INLINE,
315+
});
316+
317+
viewField.mount(document.body);
318+
319+
await new Promise(resolve => setTimeout(resolve, 0));
320+
321+
plugin.api.setMetadata(bindTarget, 123);
322+
323+
expect(plugin.api.getMetadata(bindTarget)).toBe(123);
324+
325+
await new Promise(resolve => setTimeout(resolve, 0));
326+
327+
expect(viewField.viewField?.getTargetEl()?.textContent).toBe('123');
328+
});
329+
330+
test.each(['foo', 'memory^foo', 'globalMemory^foo'])('input field created before', async value => {
331+
const bindTarget = plugin.api.parseBindTarget(value, 'test.md');
332+
333+
const inputField = plugin.api.createInputFieldMountable('test.md', {
334+
declaration: `INPUT[number:${value}]`,
335+
renderChildType: RenderChildType.INLINE,
336+
});
337+
338+
inputField.mount(document.body);
339+
340+
await new Promise(resolve => setTimeout(resolve, 0));
341+
342+
plugin.api.setMetadata(bindTarget, 123);
343+
344+
expect(plugin.api.getMetadata(bindTarget)).toBe(123);
345+
346+
await new Promise(resolve => setTimeout(resolve, 0));
347+
348+
console.log(plugin.metadataManager.getSource('globalMemory'));
349+
350+
expect(inputField.inputField?.getValue()).toBe(123);
351+
});
352+
});
265353
});

0 commit comments

Comments
 (0)