Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: clone record before emitting event #833

Conversation

TimoGlastra
Copy link
Contributor

Clone records before emitting them in events to avoid mutation later on.

Fixes #832

Signed-off-by: Timo Glastra <timo@animo.id>
@TimoGlastra TimoGlastra requested a review from a team as a code owner June 2, 2022 19:02
@codecov-commenter
Copy link

codecov-commenter commented Jun 2, 2022

Codecov Report

Merging #833 (213ea0f) into main (28e0ffa) will increase coverage by 0.05%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##             main     #833      +/-   ##
==========================================
+ Coverage   87.71%   87.76%   +0.05%     
==========================================
  Files         439      439              
  Lines       10830    10854      +24     
  Branches     1837     1837              
==========================================
+ Hits         9499     9526      +27     
+ Misses       1269     1267       -2     
+ Partials       62       61       -1     
Impacted Files Coverage Δ
...les/basic-messages/services/BasicMessageService.ts 92.59% <100.00%> (+1.28%) ⬆️
.../modules/connections/services/ConnectionService.ts 86.95% <100.00%> (+0.15%) ⬆️
...les/credentials/protocol/v1/V1CredentialService.ts 87.19% <100.00%> (-0.04%) ⬇️
...les/credentials/protocol/v2/V2CredentialService.ts 94.15% <100.00%> (-0.05%) ⬇️
.../modules/credentials/services/CredentialService.ts 100.00% <100.00%> (ø)
packages/core/src/modules/oob/OutOfBandModule.ts 87.16% <100.00%> (ø)
packages/core/src/modules/oob/OutOfBandService.ts 96.05% <100.00%> (+0.21%) ⬆️
...s/core/src/modules/proofs/services/ProofService.ts 85.79% <100.00%> (+0.12%) ⬆️
...ules/routing/services/MediationRecipientService.ts 83.52% <100.00%> (+0.38%) ⬆️
...re/src/modules/routing/services/MediatorService.ts 88.04% <100.00%> (+0.54%) ⬆️
... and 3 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 28e0ffa...213ea0f. Read the comment docs.

Copy link
Contributor

@berendsliedrecht berendsliedrecht left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the addition of adding the emitStateChangedEvent and cloning/copying, but would it not make more sense to implement clone on the BaseRecord class? This way, we would avoid calling const clonedRecord = record.clone().

I also think that this will be forgotten in the future and cause problems again. I am sure of a solution but it might be nice to revisit this later on.

this.emitStateChangedEvent(connectionRecord, previousState)
}

private emitStateChangedEvent(connectionRecord: ConnectionRecord, previousState: DidExchangeState | null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not

Suggested change
private emitStateChangedEvent(connectionRecord: ConnectionRecord, previousState: DidExchangeState | null) {
private emitStateChangedEvent(connectionRecord: ConnectionRecord, previousState?: DidExchangeState) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Events are emitted with previousState value null (which has a different meaning than undefined). If we change this now it would be a breaking change to events (because previousState will now be undefined)

@TimoGlastra
Copy link
Contributor Author

This way, we would avoid calling const clonedRecord = record.clone().

How could we avoid calling record.clone? I like the idea of adding a clone to the base record, but instead of calling JsonTransformer.clone we'd now call record.clone() right? Or am I missing something?

@berendsliedrecht
Copy link
Contributor

This way, we would avoid calling const clonedRecord = record.clone().

How could we avoid calling record.clone? I like the idea of adding a clone to the base record, but instead of calling JsonTransformer.clone we'd now call record.clone() right? Or am I missing something?

Yeah record.clone().

Maybe clone adds a property clone: true and where we want to passed a cloned object we require that property? This does not avoid calling it but we would not forget to call it at least.

@TimoGlastra
Copy link
Contributor Author

I'm not 100% following you. Where would we add the clone property to? Could you give an example of how this would work?

I'm going to merge this now to unblock me in the AATH work, but happy to make a follow up PR

@TimoGlastra TimoGlastra merged commit 8192861 into openwallet-foundation:main Jun 3, 2022
@TimoGlastra TimoGlastra deleted the fix/clone-events-before-emit branch June 3, 2022 08:23
@berendsliedrecht
Copy link
Contributor

I'm not 100% following you. Where would we add the clone property to? Could you give an example of how this would work?

I'm going to merge this now to unblock me in the AATH work, but happy to make a follow up PR

Yeah my apologies, not the best explanantion.

I want to enforce calling clone() on the record so we dont encounter more issues like this. My idea was that cloning would add a property to the record called shouldBeCloned and set it to true. Then every function that would modify the record would basically check if a property on the record called shouldBeCloned would be true.

This is for sure not the best solution and just an idea. Ignore this if its still too vague :).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

record emitted in events are modified afterwards, changing the payload of an event
3 participants