Skip to content

Commit

Permalink
perf: various synchronizer optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed Jul 20, 2020
1 parent 547d345 commit 87a7602
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 20 deletions.
8 changes: 2 additions & 6 deletions lib/createInlinePluginCreator.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@ const hasChangedDeep = require("./hasChangedDeep");
function createInlinePluginCreator(packages, multiContext, synchronizer) {
// Vars.
const { cwd } = multiContext;
const { todo, waitForAll, announceForAll, emit, getLucky } = synchronizer;

// Announcement of readiness for release.
announceForAll("_readyToGenerateNotes");
announceForAll("_readyForRelease");
const { todo, waitFor, waitForAll, emit, getLucky } = synchronizer;

/**
* Update pkg deps.
Expand Down Expand Up @@ -172,7 +168,7 @@ function createInlinePluginCreator(packages, multiContext, synchronizer) {

// Wait until the current pkg is ready to generate notes
getLucky("_readyToGenerateNotes", pkg);
await pkg._readyToGenerateNotes;
await waitFor("_readyToGenerateNotes", pkg);

// Update pkg deps.
updateManifestDeps(pkg, path);
Expand Down
24 changes: 13 additions & 11 deletions lib/getSynchronizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ const { identity } = require("lodash");
/**
* Cross-packages synchronization context.
* @typedef Synchronizer
* @param {EventEmitter} ee Shared signal bus
* @param {EventEmitter} ee Shared event emitter class.
* @param {Function} todo Gets the list of packages which are still todo
* @params {Function} waitForAll return a promise that waits until all the packages have the same target probe value.
* @params {Function} announce Attach expected state promise to target package.
* @params {Function} announceForAll Attach expected state promise to all packages.
* @param {Function} once Memoized event subscriber.
* @param {Function} emit Memoized event emitter.
* @params {Function} waitFor Function returns a promise that waits until the package has target probe value.
* @params {Function} waitForAll Function returns a promise that waits until all the packages have the same target probe value.
*/

/**
Expand All @@ -28,17 +29,22 @@ const getSynchronizer = (packages) => {
evt: {},
subscr: {},
};

const emit = (probe, pkg) => {
const name = getEventName(probe, pkg);

return store.evt[name] || (store.evt[name] = ee.emit(name));
};

const once = (probe, pkg) => {
const name = getEventName(probe, pkg);

return store.evt[name] || store.subscr[name] || (store.subscr[name] = ee.once(name));
};

const waitFor = (probe, pkg) => {
const name = getEventName(probe, pkg);
return pkg[name] || (pkg[name] = once(probe, pkg));
};

// Status sync point.
const waitForAll = (probe, filter = identity) => {
const promise = once(probe);
Expand All @@ -54,9 +60,6 @@ const getSynchronizer = (packages) => {
return promise;
};

const announce = (probe, pkg) => (pkg[probe] = once(probe, pkg));
const announceForAll = (probe) => todo().forEach((p) => announce(probe, p));

// Only the first lucky package passes the probe.
const getLucky = (probe, pkg) => {
if (getLucky[probe]) {
Expand All @@ -71,8 +74,7 @@ const getSynchronizer = (packages) => {
emit,
once,
todo,
announce,
announceForAll,
waitFor,
waitForAll,
getLucky,
};
Expand Down
6 changes: 3 additions & 3 deletions lib/multiSemanticRelease.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,17 @@ async function multiSemanticRelease(

// Shared signal bus.
const synchronizer = getSynchronizer(packages);
const { getLucky } = synchronizer;
const { getLucky, waitFor } = synchronizer;

// Release all packages.
const createInlinePlugin = createInlinePluginCreator(packages, multiContext, synchronizer);
await Promise.all(
packages.map(async (pkg) => {
// Avoid hypothetical concurrent initialization collisions.
// Avoid hypothetical concurrent initialization collisions / throttling issues.
// https://github.com/dhoulb/multi-semantic-release/issues/24
if (flags.sequentialInit) {
getLucky("_readyForRelease", pkg);
await pkg._readyForRelease;
await waitFor("_readyForRelease", pkg);
}

return releasePackage(pkg, createInlinePlugin, multiContext);
Expand Down

0 comments on commit 87a7602

Please sign in to comment.