Skip to content

Commit 34cc0d5

Browse files
committed
refactor(project): Cleanup build cache utils
Remove unused firstTruthy method, drop the stale local ResourceMetadata typedef (use existing type from HashTree.js), and strip the UI5_CACHE_PERF instrumentation.
1 parent 4b46467 commit 34cc0d5

2 files changed

Lines changed: 3 additions & 105 deletions

File tree

Lines changed: 2 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,3 @@
1-
/**
2-
* @typedef {object} ResourceMetadata
3-
* @property {string} integrity Content integrity of the resource
4-
* @property {number} lastModified Last modified timestamp (mtimeMs)
5-
* @property {number} inode Inode number of the resource
6-
* @property {number} size Size of the resource in bytes
7-
*/
8-
9-
const PERF_TRACKING = !!process.env.UI5_CACHE_PERF;
10-
const perfCounters = {
11-
calls: 0,
12-
shortCircuitTrue: 0,
13-
sizeMismatch: 0,
14-
inodeMismatch: 0,
15-
integrityFallback: 0,
16-
};
17-
export {perfCounters as isResourceUnchangedCounters};
18-
191
/**
202
* Determines if a resource has changed compared to cached metadata
213
*
@@ -25,7 +7,8 @@ export {perfCounters as isResourceUnchangedCounters};
257
*
268
* @public
279
* @param {@ui5/fs/Resource} resource Resource instance to compare
28-
* @param {ResourceMetadata} cachedMetadata Cached metadata from the tree
10+
* @param {@ui5/project/build/cache/index/HashTree~ResourceMetadata} cachedMetadata
11+
* Cached metadata from the tree
2912
* @param {number} [indexTimestamp] Timestamp when the tree state was created
3013
* @returns {Promise<boolean>} True if resource content is unchanged
3114
* @throws {Error} If resource or metadata is undefined
@@ -34,14 +17,12 @@ export async function isResourceUnchanged(resource, cachedMetadata, indexTimesta
3417
if (!resource || !cachedMetadata) {
3518
throw new Error("Cannot compare undefined resources or metadata");
3619
}
37-
if (PERF_TRACKING) perfCounters.calls++;
3820

3921
// Size mismatch indicates a definite content change. Required before any
4022
// "unchanged" decision because mtime preservation (cp -p, tar -x, rsync -t,
4123
// atomic rename) does not imply content unchanged.
4224
const currentSize = await resource.getSize();
4325
if (currentSize !== cachedMetadata.size) {
44-
if (PERF_TRACKING) perfCounters.sizeMismatch++;
4526
return false;
4627
}
4728

@@ -54,27 +35,23 @@ export async function isResourceUnchanged(resource, cachedMetadata, indexTimesta
5435
const inodeMismatch =
5536
cachedMetadata.inode !== undefined && currentInode !== undefined &&
5637
currentInode !== cachedMetadata.inode;
57-
if (inodeMismatch && PERF_TRACKING) perfCounters.inodeMismatch++;
5838

5939
// mtime + size both match and the file has not been replaced → unchanged,
6040
// unless we are in the racy-git window (mtime === indexTimestamp), where
6141
// content may have changed during indexing without mtime moving.
6242
const currentLastModified = resource.getLastModified();
6343
if (!inodeMismatch && currentLastModified === cachedMetadata.lastModified) {
6444
if (indexTimestamp && currentLastModified !== indexTimestamp) {
65-
if (PERF_TRACKING) perfCounters.shortCircuitTrue++;
6645
return true;
6746
}
6847
// Race condition possible — fall through to integrity check
6948
}
7049

7150
// mtime differs, file was replaced, or racy window — verify content.
72-
if (PERF_TRACKING) perfCounters.integrityFallback++;
7351
const currentIntegrity = await resource.getIntegrity();
7452
return currentIntegrity === cachedMetadata.integrity;
7553
}
7654

77-
7855
/**
7956
* Creates an index of resource metadata from an array of resources
8057
*
@@ -98,40 +75,3 @@ export async function createResourceIndex(resources) {
9875
};
9976
}));
10077
}
101-
102-
/**
103-
* Returns the first truthy value from an array of promises
104-
*
105-
* This function evaluates all promises in parallel and returns immediately
106-
* when the first truthy value is found. If all promises resolve to falsy
107-
* values, null is returned.
108-
*
109-
* @param {Promise[]} promises Array of promises to evaluate
110-
* @returns {Promise<*>} The first truthy resolved value or null if all are falsy
111-
*/
112-
export async function firstTruthy(promises) {
113-
return new Promise((resolve, reject) => {
114-
let completed = 0;
115-
const total = promises.length;
116-
117-
if (total === 0) {
118-
resolve(null);
119-
return;
120-
}
121-
122-
promises.forEach((promise) => {
123-
Promise.resolve(promise)
124-
.then((value) => {
125-
if (value) {
126-
resolve(value);
127-
} else {
128-
completed++;
129-
if (completed === total) {
130-
resolve(null);
131-
}
132-
}
133-
})
134-
.catch(reject);
135-
});
136-
});
137-
}

packages/project/test/lib/build/cache/utils.js

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import test from "ava";
22
import {
33
isResourceUnchanged,
4-
createResourceIndex,
5-
firstTruthy
4+
createResourceIndex
65
} from "../../../../lib/build/cache/utils.js";
76

87
function createMockResource(opts = {}) {
@@ -163,44 +162,3 @@ test("createResourceIndex: empty array returns empty result", async (t) => {
163162
const result = await createResourceIndex([]);
164163
t.deepEqual(result, []);
165164
});
166-
167-
// === firstTruthy ===
168-
169-
test("firstTruthy: returns null for empty array", async (t) => {
170-
const result = await firstTruthy([]);
171-
t.is(result, null);
172-
});
173-
174-
test("firstTruthy: returns first truthy value", async (t) => {
175-
const result = await firstTruthy([
176-
Promise.resolve(null),
177-
Promise.resolve("found"),
178-
Promise.resolve("also found"),
179-
]);
180-
t.is(result, "found");
181-
});
182-
183-
test("firstTruthy: returns null when all resolve to falsy", async (t) => {
184-
const result = await firstTruthy([
185-
Promise.resolve(null),
186-
Promise.resolve(false),
187-
Promise.resolve(0),
188-
]);
189-
t.is(result, null);
190-
});
191-
192-
test("firstTruthy: rejects when a promise rejects", async (t) => {
193-
const error = new Error("test error");
194-
await t.throwsAsync(firstTruthy([
195-
Promise.resolve(null),
196-
Promise.reject(error),
197-
]), {message: "test error"});
198-
});
199-
200-
test("firstTruthy: resolves with first truthy regardless of order", async (t) => {
201-
const result = await firstTruthy([
202-
new Promise((resolve) => setTimeout(() => resolve(null), 50)),
203-
Promise.resolve("immediate"),
204-
]);
205-
t.is(result, "immediate");
206-
});

0 commit comments

Comments
 (0)