example = SharedContext()
example['sheet_id'] = '1B34VeLhAnujcnDO1Q8mcTsgDW42Q2nZBEA8mWOLFiPE'
print(example['sheet_id'])1B34VeLhAnujcnDO1Q8mcTsgDW42Q2nZBEA8mWOLFiPE
TriggerKit is in active development and not ready for public use. Should be ready in a week or two!
We should facilitate the sharing of information between actions. This is a common need in many applications, and we can achieve this by using a shared context.
To achieve this, we can create a SharedContext class that will hold the shared information. This class will be responsible for storing and retrieving the information as needed.
SharedContext (base:Dict[str,Any]=None)
A dictionary-like object that allows sharing of context between different parts of a program. It is designed to be used as a singleton, ensuring that all parts of the program share the same context.
For example, a TriggerKit user might want their create a google sheet and then refrence sheet’s id in a later action. This can be done by storing the sheet id in the shared context after creating the sheet, and then retrieving it in the next action.
example = SharedContext()
example['sheet_id'] = '1B34VeLhAnujcnDO1Q8mcTsgDW42Q2nZBEA8mWOLFiPE'
print(example['sheet_id'])1B34VeLhAnujcnDO1Q8mcTsgDW42Q2nZBEA8mWOLFiPE
Let’s also setup how SharedContext is represented and allow users to check if a key exists in the context. This will be useful for checking if a certain piece of information has been set before trying to use it.
SharedContext.__repr__ ()
Return repr(self).
SharedContext.__contains__ (key)
Maybe the user also chains in a function that looks for what emails to share the sheet with and wants to store the result in the shared context as well
['cO8dD@example.com', 'hYh0q@example.com']
We might want a way to see what value pairs are added by actions, so let’s add a way to check what values a action added during it’s run. This could be a simple dictionary that get’s cleared after each action run. This will allow us to see what values were added by each action, and we can use this information to debug any issues that may arise.
SharedContext.clear_updates ()
Clears the updates made during this run.
SharedContext.updates ()
Returns the subset of keys written during this run.
Now, let’s see if the updates method works. This method could be called by the action runner to update the shared context with new information. The method should take a dictionary as an argument and update the shared context with the new values.
{'sheet_id': '1B34VeLhAnujcnDO1Q8mcTsgDW42Q2nZBEA8mWOLFiPE',
'emails': ['cO8dD@example.com', 'hYh0q@example.com']}
That looks good! What about the clear method? This method should be called by the action runner to clear the log of what is added between runs.
Perfect! Looks like things are working as expected.