Skip to content

Commit

Permalink
Remove unused functions from utils package (#5197)
Browse files Browse the repository at this point in the history
  • Loading branch information
timleslie authored Mar 23, 2021
1 parent ca1be41 commit 97609a6
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 165 deletions.
5 changes: 5 additions & 0 deletions .changeset/few-poets-think.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystone-next/utils-legacy': major
---

Removed unused functions `captureSuspensePromises` and `countArrays`.
1 change: 0 additions & 1 deletion packages/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
},
"dependencies": {
"@babel/runtime": "^7.13.10",
"p-is-promise": "^3.0.0",
"p-lazy": "^3.1.0",
"p-reflect": "^2.1.0",
"semver": "^7.3.4"
Expand Down
40 changes: 0 additions & 40 deletions packages/utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import pLazy from 'p-lazy';
import pReflect from 'p-reflect';
import isPromise from 'p-is-promise';
import semver from 'semver';

export const noop = <T>(x: T): T => x;
Expand Down Expand Up @@ -212,45 +211,6 @@ export const createLazyDeferred = <T, S>() => {
};
};

/**
* Given an array of functions which may throw a Promise when executed, we want
* to ensure all functions are executed, reducing any thrown Promises to a
* single Promise, which is itself rethrown.
* If no Promises are thrown, this is the equivalent of a .map
* @param {Array} executors
*/
export const captureSuspensePromises = <T>(executors: (() => T)[]) => {
const values: T[] = [];
const promises = executors
.map(executor => {
try {
values.push(executor());
} catch (loadingPromiseOrError) {
// An actual error was thrown, so we want to bubble that up
if (!isPromise(loadingPromiseOrError)) {
throw loadingPromiseOrError;
}
// Return a Suspense promise
return loadingPromiseOrError;
}
})
.filter(Boolean);

if (promises.length) {
// All the suspense promises are reduced to a single promise then rethrown
throw Promise.all(promises);
}

return values;
};

/**
* Returns the length of all arrays in obj
* @param {*} obj An objects whose property values are arrays.
*/
export const countArrays = (obj: Record<string, any[]>) =>
Object.values(obj).reduce((total, items) => total + (items ? items.length : 0), 0);

/**
* Compares two version strings or number arrays in the major.minor.patch format.
* @param {Array<Number>|String} comp The version to compare.
Expand Down
124 changes: 0 additions & 124 deletions packages/utils/tests/utils.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const isPromise = require('p-is-promise');
const {
getType,
escapeRegExp,
Expand All @@ -15,7 +14,6 @@ const {
arrayToObject,
flatten,
zipObj,
captureSuspensePromises,
upcase,
...utils
} = require('../src');
Expand Down Expand Up @@ -276,126 +274,4 @@ describe('utils', () => {
},
});
});

describe('captureSuspensePromises', () => {
test('executes all fns sync when no Promises thrown', () => {
const funcs = [jest.fn(), jest.fn()];

try {
captureSuspensePromises(funcs);
} catch (e) {}

expect(funcs[0]).toHaveBeenCalledTimes(1);
expect(funcs[1]).toHaveBeenCalledTimes(1);
});

test('does not throw when no executors throw', () => {
const funcs = [jest.fn(), jest.fn()];

try {
captureSuspensePromises(funcs);
} catch (e) {
expect(true).toBeFalsey();
}

expect(funcs[0]).toHaveBeenCalledTimes(1);
expect(funcs[1]).toHaveBeenCalledTimes(1);
});

test('executes all fns sync when a Promise is thrown', () => {
const funcs = [
jest.fn(),
jest.fn(() => {
throw Promise.resolve();
}),
jest.fn(),
];

try {
captureSuspensePromises(funcs);
} catch (e) {}

expect(funcs[0]).toHaveBeenCalledTimes(1);
expect(funcs[1]).toHaveBeenCalledTimes(1);
expect(funcs[2]).toHaveBeenCalledTimes(1);
});

test('throws a Promise when executor throws a Promise', () => {
const funcs = [
jest.fn(),
jest.fn(() => {
throw Promise.resolve();
}),
jest.fn(),
];

try {
captureSuspensePromises(funcs);
expect(true).toBeFalsey();
} catch (maybePromise) {
expect(isPromise(maybePromise)).toBeTruthy();
}
});

test('thrown Promise resolves all thrown Promises', () => {
const resolvers = [jest.fn(), jest.fn(), jest.fn()];

const funcs = [
jest.fn(() => {
throw new Promise(resolve => {
// Artificially delay this promise's resolution to the next tick
// which will ensure the microtask (aka: Promise) queue is fully
// flushed
process.nextTick(() => {
resolvers[0]();
resolve();
});
});
}),
jest.fn(() => {
throw Promise.resolve().then(resolvers[1]);
}),
jest.fn(() => {
throw Promise.resolve().then(resolvers[2]);
}),
];

try {
captureSuspensePromises(funcs);
} catch (thrownPromise) {
return thrownPromise.then(() => {
expect(resolvers[0]).toHaveBeenCalledTimes(1);
expect(resolvers[1]).toHaveBeenCalledTimes(1);
expect(resolvers[2]).toHaveBeenCalledTimes(1);
});
}

expect(true).toBeFalsey();
});

test('throws a Error when executor throws a Error', () => {
const funcs = [
jest.fn(),
jest.fn(() => {
throw new Error();
}),
jest.fn(),
];

try {
captureSuspensePromises(funcs);
expect(true).toBeFalsey();
} catch (maybePromise) {
expect(!isPromise(maybePromise)).toBeTruthy();
}
});

test('acts like .map when no promises thrown', () => {
const funcs = [jest.fn(() => 'foo'), jest.fn(() => 'bar')];

const result = captureSuspensePromises(funcs);

expect(result).toMatchObject(['foo', 'bar']);
});
});
});

1 comment on commit 97609a6

@vercel
Copy link

@vercel vercel bot commented on 97609a6 Mar 23, 2021

Choose a reason for hiding this comment

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

Please sign in to comment.