From 48b6026d5ca3bf144e5075058e5e3777bfa6f718 Mon Sep 17 00:00:00 2001 From: James Kebinger Date: Mon, 14 Apr 2025 16:16:14 -0500 Subject: [PATCH 1/2] implements prefab.current-time lookup for all operators --- src/__tests__/contextLookup.test.ts | 9 +++++++++ src/contextLookup.ts | 5 +++++ src/evaluate.ts | 7 +------ 3 files changed, 15 insertions(+), 6 deletions(-) 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 ); From 91fb2e2b70aaa33be97772c545ca2b6dc6cce8ad Mon Sep 17 00:00:00 2001 From: James Kebinger Date: Mon, 14 Apr 2025 16:19:25 -0500 Subject: [PATCH 2/2] update integration test data --- prefab-cloud-integration-test-data | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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