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

Events for simple coffee machine #1229

Merged
merged 6 commits into from
Feb 13, 2024
Merged

Events for simple coffee machine #1229

merged 6 commits into from
Feb 13, 2024

Conversation

egekorkan
Copy link
Member

This is adding events to the simple coffee machine. With @relu91 we will use this in the nodejs conf presentation. There are those return statements added after event emissions since without doing that I was getting an event emission with data "water""beans". Some race condition maybe? It is not an elegant solution but if that is indeed an issue with node-wot (and not my oversight), I would propose to go with this PR and open an issue about it.

Copy link

codecov bot commented Feb 8, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (82f7f7a) 77.77% compared to head (3b1136c) 77.77%.
Report is 2 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1229   +/-   ##
=======================================
  Coverage   77.77%   77.77%           
=======================================
  Files          84       84           
  Lines       17476    17476           
  Branches     1770     1770           
=======================================
  Hits        13592    13592           
  Misses       3848     3848           
  Partials       36       36           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@danielpeintner
Copy link
Member

without doing that I was getting an event emission with data "water""beans". Some race condition maybe?

That would mean that when emitPropertyChange is called both if statements are true:

  • waterAmount <= 10
  • beansAmount <= 10

How does the code prevent that this is not happening?

@egekorkan
Copy link
Member Author

That would mean that when emitPropertyChange is called both if statements are true:

Just to be precise, it is the emitEvent that is the issue.

I am only sending a GET request from the browser to localhost:8081/coffee-machine/events/resourceEmpty and if both conditions are met, I get "water""beans". Both payloads are somehow merged into one since they are emitted very quickly. I should have gotten only "water" since the browser will not send another GET before the other event could be emitted.

@egekorkan
Copy link
Member Author

I had to adapt the code a bit, a bit more than just adding an event. Now there are arrays for refilling and empty event so that there can be more resources empty etc. This avoid the race condition but also makes it more logical (both resources can finish at the same time and you want to fill both at the same time)

Comment on lines 126 to 131
if (resourceEvent.length > 0) {
thing.emitEvent("resourceEmpty", resourceEvent);
return undefined;
} else {
return undefined;
}
Copy link
Member

Choose a reason for hiding this comment

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

I think now we can have just once

return undefined;

Comment on lines 152 to 157
if (resourceEvent.length > 0) {
thing.emitEvent("resourceEmpty", resourceEvent);
return undefined;
} else {
return undefined;
}
Copy link
Member

Choose a reason for hiding this comment

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

Same here

Comment on lines 174 to 179
if (resourceEvent.length > 0) {
thing.emitEvent("resourceEmpty", resourceEvent);
return undefined;
} else {
return undefined;
}
Copy link
Member

Choose a reason for hiding this comment

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

And here also, just one return statement

@egekorkan
Copy link
Member Author

@danielpeintner adapted to your feedback

Copy link
Member

@relu91 relu91 left a comment

Choose a reason for hiding this comment

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

Since we are using the value function is better to cast than to ignore ts error. I fixed it and I think we can now merge.

@relu91
Copy link
Member

relu91 commented Feb 13, 2024

CI still randomly falling, merging anyway.

@relu91 relu91 merged commit af74c9d into master Feb 13, 2024
11 of 12 checks passed
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.

3 participants