Skip to content

Commit

Permalink
Handle cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ascorbic committed Aug 5, 2024
1 parent 921a303 commit a6c4864
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
14 changes: 9 additions & 5 deletions packages/astro/src/content/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export class ContentLayer {
#settings: AstroSettings;
#watcher?: FSWatcher;
#lastConfigDigest?: string;
#unsubscribe?: () => void;

#generateDigest?: (data: Record<string, unknown> | string) => string;

Expand All @@ -48,7 +49,8 @@ export class ContentLayer {
* Watch for changes to the content config and trigger a sync when it changes.
*/
watchContentConfig() {
globalContentConfigObserver.subscribe(async (ctx) => {
this.#unsubscribe?.();
this.#unsubscribe = globalContentConfigObserver.subscribe(async (ctx) => {
if (
!this.#loading &&
ctx.status === 'loaded' &&
Expand All @@ -60,6 +62,10 @@ export class ContentLayer {
});
}

unwatchContentConfig() {
this.#unsubscribe?.();
}

/**
* Run the `load()` method of each collection's loader, which will load the data and save it in the data store.
* The loader itself is responsible for deciding whether this will clear and reload the full collection, or
Expand Down Expand Up @@ -223,9 +229,7 @@ function contentLayerSingleton() {
return {
initialized: () => Boolean(instance),
init: (options: ContentLayerOptions) => {
if (instance) {
throw new Error('Content layer already initialized');
}
instance?.unwatchContentConfig();
instance = new ContentLayer(options);
return instance;
},
Expand All @@ -235,8 +239,8 @@ function contentLayerSingleton() {
}
return instance;
},
/** @internal */
dispose: () => {
instance?.unwatchContentConfig();
instance = null;
},
};
Expand Down
1 change: 1 addition & 0 deletions packages/astro/test/test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ export async function loadFixture(inlineConfig) {
return await check(opts);
},
startDevServer: async (extraInlineConfig = {}) => {
globalContentLayer.dispose();
process.env.NODE_ENV = 'development';
devServer = await dev(mergeConfig(inlineConfig, extraInlineConfig));
config.server.host = parseAddressToHost(devServer.address.address); // update host
Expand Down

0 comments on commit a6c4864

Please sign in to comment.