Skip to content

Commit 3eaca88

Browse files
committed
chore(shared-ini-file-loder): use Promises in slurpFile hash
1 parent 99235b4 commit 3eaca88

File tree

2 files changed

+15
-40
lines changed

2 files changed

+15
-40
lines changed

packages/shared-ini-file-loader/src/slurpFile.spec.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,17 @@ describe("slurpFile", () => {
88
const getMockFileContents = (path: string, options = UTF8) => JSON.stringify({ path, options });
99

1010
beforeEach(() => {
11-
(promises.readFile as jest.Mock).mockImplementation((path, options) =>
12-
Promise.resolve(getMockFileContents(path, options))
13-
);
11+
(promises.readFile as jest.Mock).mockImplementation(async (path, options) => {
12+
await new Promise((resolve) => setTimeout(resolve, 100));
13+
return getMockFileContents(path, options);
14+
});
1415
});
1516

1617
afterEach(() => {
1718
jest.clearAllMocks();
1819
});
1920

20-
it("makes one readFile call for a filepath irrepsective of slurpFile calls", (done) => {
21+
it("makes one readFile call for a filepath irrespective of slurpFile calls", (done) => {
2122
jest.isolateModules(async () => {
2223
const { slurpFile } = require("./slurpFile");
2324
const mockPath = "/mock/path";

packages/shared-ini-file-loader/src/slurpFile.ts

Lines changed: 10 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,45 +3,19 @@ import { promises as fsPromises } from "fs";
33

44
const { readFile } = fsPromises;
55

6-
type callbacks = { resolve: Function; reject: Function };
7-
type FileStatus = {
8-
contents: string;
9-
isReading: boolean;
10-
requestQueue: callbacks[];
11-
};
12-
13-
const fileStatusHash: { [key: string]: FileStatus } = {};
6+
const filePromisesHash: { [key: string]: Promise<string> } = {};
147

15-
export const slurpFile = (path: string) =>
16-
new Promise<string>((resolve, reject) => {
17-
if (!fileStatusHash[path]) {
18-
// File not read yet, set file isReading to true and read file.
19-
fileStatusHash[path] = { isReading: true, contents: "", requestQueue: [] };
20-
fileStatusHash[path].requestQueue.push({ resolve, reject });
8+
export const slurpFile = (path: string) => {
9+
if (!filePromisesHash[path]) {
10+
filePromisesHash[path] = new Promise((resolve, reject) => {
2111
readFile(path, "utf8")
2212
.then((data) => {
23-
// File read successful
24-
fileStatusHash[path].isReading = false;
25-
fileStatusHash[path].contents = data;
26-
const { requestQueue } = fileStatusHash[path];
27-
while (requestQueue.length) {
28-
const { resolve } = requestQueue.pop()!;
29-
resolve(data);
30-
}
13+
resolve(data);
3114
})
3215
.catch((err) => {
33-
// File read failed;
34-
fileStatusHash[path].isReading = false;
35-
const { requestQueue } = fileStatusHash[path];
36-
while (requestQueue.length) {
37-
const { reject } = requestQueue.pop()!;
38-
reject(err);
39-
}
16+
reject(err);
4017
});
41-
} else if (fileStatusHash[path].isReading) {
42-
// File currently being read. Add callbacks to the request queue.
43-
fileStatusHash[path].requestQueue.push({ resolve, reject });
44-
} else {
45-
resolve(fileStatusHash[path].contents);
46-
}
47-
});
18+
});
19+
}
20+
return filePromisesHash[path];
21+
};

0 commit comments

Comments
 (0)