diff --git a/prefab-cloud-integration-test-data b/prefab-cloud-integration-test-data index 4ccd647..98ac355 160000 --- a/prefab-cloud-integration-test-data +++ b/prefab-cloud-integration-test-data @@ -1 +1 @@ -Subproject commit 4ccd6472078b1ef0a8088cc2fb242c39939f8bf1 +Subproject commit 98ac3550a2e2d1d3f445d189c73ac1d16dfbc5a7 diff --git a/src/__tests__/contextLookup.test.ts b/src/__tests__/contextLookup.test.ts index fd4ec06..3c44515 100644 --- a/src/__tests__/contextLookup.test.ts +++ b/src/__tests__/contextLookup.test.ts @@ -79,4 +79,13 @@ describe("contextLookup", () => { const result = contextLookup(contexts, "custom.isMobile"); expect(result).toBe(true); }); + + it("should return current timestamp for prefab.current-time", () => { + const result = contextLookup(contexts, "prefab.current-time"); + const now = +new Date(); + + // Allow for a small time difference (within 100ms) since the test and the function call + // might not execute at exactly the same millisecond + expect(Math.abs(result as number - now)).toBeLessThan(100); + }); }); diff --git a/src/contextLookup.ts b/src/contextLookup.ts index dd4da95..c040355 100644 --- a/src/contextLookup.ts +++ b/src/contextLookup.ts @@ -8,6 +8,11 @@ export const contextLookup = ( return undefined; } + // Special case for "prefab.current-time" to always return the current timestamp + if (propertyName === "prefab.current-time") { + return +new Date(); + } + let [name, key] = propertyName.split("."); if (key === undefined) { diff --git a/src/evaluate.ts b/src/evaluate.ts index 3766863..5c50665 100644 --- a/src/evaluate.ts +++ b/src/evaluate.ts @@ -123,16 +123,11 @@ const inSegment = ( }; const inIntRange = (criterion: Criterion, contexts: Contexts): boolean => { - const contextsWithCurrentTime = new Map(contexts); - const prefabContext = contextsWithCurrentTime.get("prefab") ?? new Map(); - prefabContext.set("current-time", +new Date()); - contextsWithCurrentTime.set("prefab", prefabContext); - const start = criterion.valueToMatch?.intRange?.start; const end = criterion.valueToMatch?.intRange?.end; const comparable = contextLookup( - contextsWithCurrentTime, + contexts, criterion.propertyName );