fix: expose currentTime and utcOffset to template logic in trigger() and init()#116
fix: expose currentTime and utcOffset to template logic in trigger() and init()#116Sup-ri-tha wants to merge 1 commit intoaccordproject:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR aims to make currentTime and utcOffset available to template logic executed by TemplateArchiveProcessor.trigger() and init(), and corrects a JSDoc typo.
Changes:
- Add
currentTime/utcOffsetto theargumentNamesarrays passed toevalDangerously()intrigger()andinit(). - Fix JSDoc typo: “UTC offer” → “UTC offset”.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| code: compiledCode['logic/logic.ts'].code, // TODO DCS - how to find the code to run? | ||
| argumentNames: ['data', 'request', 'state'], | ||
| argumentNames: ['data', 'request', 'state', 'currentTime', 'utcOffset'], | ||
| arguments: [data, request, state, currentTime, utcOffset] | ||
| }); |
There was a problem hiding this comment.
argumentNames is not used by JavaScriptEvaluator.evalDangerously when templateLogic: true (it dynamic-imports the module and calls instance[functionName](...arguments)). As a result, adding currentTime/utcOffset here will not make them available as in-scope variables inside template logic unless the template logic method signature explicitly declares them. If the intent is to inject these into scope, the evaluator/path needs to change accordingly (or the logic API/types should be updated to include these optional parameters).
There was a problem hiding this comment.
That's right. argumentNames is not used in the templateLogic path. Reverted those changes. Instead applied defaults for currentTime and utcOffset before passing them in arguments.
src/TemplateArchiveProcessor.ts
Outdated
| argumentNames: ['data', 'request', 'state', 'currentTime', 'utcOffset'], | ||
| arguments: [data, request, state, currentTime, utcOffset] | ||
| }); |
There was a problem hiding this comment.
The JSDoc says currentTime/utcOffset “default to now/zero”, but this method currently forwards the optional values as-is. If callers omit them, template logic will receive undefined; consider defaulting currentTime to new Date().toISOString() and utcOffset to 0 before constructing the arguments array to match the documented behavior.
There was a problem hiding this comment.
Done. currentTime now defaults to new Date().toISOString() and utcOffset to 0 when not provided.
| functionName: 'init', | ||
| code: compiledCode['logic/logic.ts'].code, // TODO DCS - how to find the code to run? | ||
| argumentNames: ['data'], | ||
| argumentNames: ['data', 'currentTime', 'utcOffset'], | ||
| arguments: [data, currentTime, utcOffset] | ||
| }); |
There was a problem hiding this comment.
argumentNames is not used by JavaScriptEvaluator.evalDangerously when templateLogic: true (module import + method call uses only arguments). This change likely won’t affect whether currentTime/utcOffset are accessible inside init() logic unless they are declared as parameters on the template logic method (or the evaluator/path is changed).
There was a problem hiding this comment.
Reverted those changes and instead applied defaults for currentTime and utcOffset before passing them in arguments.
src/TemplateArchiveProcessor.ts
Outdated
| argumentNames: ['data', 'currentTime', 'utcOffset'], | ||
| arguments: [data, currentTime, utcOffset] | ||
| }); |
There was a problem hiding this comment.
The JSDoc says currentTime/utcOffset “default to now/zero”, but init() currently forwards the optional values as-is. Consider applying defaults before building the arguments array so template logic gets deterministic values when callers omit them.
There was a problem hiding this comment.
Done. currentTime now defaults to new Date().toISOString() and utcOffset to 0 when not provided.
src/TemplateArchiveProcessor.ts
Outdated
| * @param {[string]} currentTime - the current time, defaults to now | ||
| * @param {[number]} utcOffset - the UTC offer, defaults to zero | ||
| * @param {[number]} utcOffset - the UTC offset, defaults to zero | ||
| * @returns {Promise} the response and any events |
There was a problem hiding this comment.
The init() JSDoc @returns says “the response and any events”, but InitResponse in this file only contains state (no result/events). Please update the @returns description to match the actual return shape.
| * @returns {Promise} the response and any events | |
| * @returns {Promise<InitResponse>} the new state |
…and init() Signed-off-by: Supritha Rajashekar <supritharajashekar10@gmail.com>
4f5211f to
984bb79
Compare
Fixes #115
Problem
trigger()andinit()inTemplateArchiveProcessorpasscurrentTimeandutcOffsetin theargumentsarray but omit them fromargumentNames.evalDangerously()buildsnew Function(...argumentNames, code)- only named arguments are accessible inside the function. As a result,currentTimeandutcOffsetsilently becomeundefinedwith no error or warning. [Confirmed via console.log in logic.ts , currentTime is undefined despite being passed by the caller.]Proof
Fix
Added missing names to
argumentNamesin bothtrigger()andinit():Also fixed JSDoc typo: "UTC offer" → "UTC offset" in both methods.
Testing
All existing tests pass. The two failing tests (stress test, optional-nested) are pre-existing and unrelated to this change.
Checklist