(core) Add PEEK() function to bypass circular dependencies

Summary:
Adds a Python function `PEEK()` for use in formulas which temporarily sets a new attribute `Engine._peeking` which disables the `_use_node` method, preventing dependency tracking and allowing the given expression to use outdated values. This allows circumventing circular reference errors. It's particularly meant for trigger formulas although it works in normal formulas as well. The expression is wrapped in a `lambda` by `codebuilder` for lazy evaluation.

Discussion: https://grist.slack.com/archives/C0234CPPXPA/p1653571024031359

Test Plan: Added a Python unit test for circular trigger formulas using PEEK.

Reviewers: dsagal

Reviewed By: dsagal

Subscribers: paulfitz

Differential Revision: https://phab.getgrist.com/D3453
This commit is contained in:
Alex Hall
2022-06-02 16:24:41 +02:00
parent f837f43e55
commit c5ebd7db3d
4 changed files with 95 additions and 0 deletions

View File

@@ -177,6 +177,9 @@ class Engine(object):
# on any of these cells implies a circular dependency.
self._locked_cells = set()
# Set to True by the PEEK() function to temporarily disable dependency tracking
self._peeking = False
# The lists of actions of different kinds, built up while applying an action.
self.out_actions = action_obj.ActionGroup()
@@ -474,6 +477,9 @@ class Engine(object):
# This is used whenever a formula accesses any part of any record. It's hot code, and
# it's worth optimizing.
if self._peeking:
return
if self._current_node:
# Add an edge to indicate that the node being computed depends on the node passed in.
# Note that during evaluation, we only *add* dependencies. We *remove* them by clearing them