Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/__tests__/contextLookup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
5 changes: 5 additions & 0 deletions src/contextLookup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
7 changes: 1 addition & 6 deletions src/evaluate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
);

Expand Down