Skip to content

Commit

Permalink
refactor: improve namespace source update plugin lifecycle
Browse files Browse the repository at this point in the history
  • Loading branch information
DavieReid committed Nov 17, 2023
1 parent 014ae3c commit 187b780
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 18 deletions.
4 changes: 4 additions & 0 deletions packages/core/src/Source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ export default class Source {
}

async requestNamespaceSourceUpdate(
namespaceSourceDesc: string,
updatedSourceFilesystem: IVolumeImmutable,
sharedFilesystem: IVolumeImmutable,
globalConfig: MutableData<unknown>
Expand All @@ -158,6 +159,9 @@ export default class Source {
);
}
if (shouldInvokeAfterUpdate === true) {
console.log(
`[Mosaic][Source] namespace source ${namespaceSourceDesc} updated. Triggering 'afterNamespaceSourceUpdate for ${this.id.description}`
);
this.filesystem.unfreeze();
await this.invokeAfterUpdate(sharedFilesystem, globalConfig, 'afterNamespaceSourceUpdate');
this.filesystem.freeze();
Expand Down
27 changes: 14 additions & 13 deletions packages/core/src/SourceManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,20 +247,21 @@ export default class SourceManager {

#updateNamespaceSources(immutableSourceFilesystem: IVolumeImmutable, source: Source) {
// Notify other frozen sources that share this sources namespace that something has changed.
const sourcesToUpdate = Array.from(this.#sources.values()).filter(
existingSource =>
existingSource !== source &&
existingSource.filesystem.frozen &&
existingSource.namespace === source.namespace
);

return Promise.all(
Array.from(this.#sources.values()).map(existingSource => {
if (
existingSource !== source &&
existingSource.filesystem.frozen &&
existingSource.namespace === source.namespace
) {
return existingSource.requestNamespaceSourceUpdate(
immutableSourceFilesystem,
this.#sharedFilesystem,
this.#globalConfig
);
}
return existingSource;
sourcesToUpdate.map(async sourceToUpdate => {
await sourceToUpdate.requestNamespaceSourceUpdate(
source.id.description,
immutableSourceFilesystem,
this.#sharedFilesystem,
this.#globalConfig
);
})
);
}
Expand Down
8 changes: 5 additions & 3 deletions packages/core/src/WorkerSubscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,12 @@ export default class WorkerSubscription {
} else if (type === 'track') {
this.#emitter.emit(EVENT.TRACK, { data: JSON.parse(textDecoder.decode(data)) });
} else if (type === 'init') {
this.#emitter.emit(EVENT.START);
if (data) {
// We receive a file buffer for 'init', not an object
this.#emitter.emit(EVENT.UPDATE, { data: JSON.parse(textDecoder.decode(data)) });
// We receive a file buffer (cached fs) for 'init', not an object
this.#emitter.emit(EVENT.START, { data: JSON.parse(textDecoder.decode(data)) });
} else {
// started with no cached fs
this.#emitter.emit(EVENT.START);
}
} else {
this.#onError(
Expand Down
17 changes: 15 additions & 2 deletions packages/plugins/src/BreadcrumbsPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,21 @@ const BreadcrumbsPlugin: PluginType<BreadcrumbsPluginPage, BreadcrumbsPluginOpti
}
}
},
async shouldUpdateNamespaceSources() {
return true;
async shouldUpdateNamespaceSources(updatedSourceFilesystem, { ignorePages, namespace }) {
/**
* Only trigger an update when the updated source happens to be the "root" namespace source
* This is determined this by checking if the filesystem has files inside the "namespace" directory
*
* "child" sources will have the namespace directory but they wont have files until further down the directory hierarchy
*/
const namespaceDirFiles = (await updatedSourceFilesystem.promises.glob(`${namespace}/*`, {
onlyFiles: true,
ignore: ignorePages.map(ignore => `**/${ignore}`),
cwd: '/'
})) as string[];

// the root namespace has updated
return namespaceDirFiles.length > 0;
}
};

Expand Down

1 comment on commit 187b780

@vercel
Copy link

@vercel vercel bot commented on 187b780 Nov 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

mosaic – ./

mosaic-git-main-mosaic-dev-team.vercel.app
mosaic-mosaic-dev-team.vercel.app

Please sign in to comment.