-
Notifications
You must be signed in to change notification settings - Fork 149
Apply updates alternative #135
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
Conversation
Thinking of removing JsonSerializer/JsonDeserializer and replacing them with the following signatures in StatefulService.h: enum class StateUpdateResult {
CHANGED = 0, // The update changed the state and propagation should take place if required
UNCHANGED, // The state was unchanged, propagation should not take place
ERROR // There was a problem updating the state, propagation should not take place
};
template <class T>
using StateUpdater = StateUpdateResult (*)(T& settings);
template <class T>
using JsonStateUpdater = StateUpdateResult (*)(JsonObject& root, T& settings);
template <class T>
using StateReader = void (*)(const T& settings);
template <class T>
using JsonStateReader = void (*)(T& settings, JsonObject& root); |
src/LightStateService.h
Outdated
static UpdateOutcome update(JsonObject& root, LightState& lightState) { | ||
boolean newState = root["led_on"] | DEFAULT_LED_STATE; | ||
if (lightState.ledOn != newState) { | ||
lightState.ledOn = newState; | ||
return UpdateOutcome::CHANGED; | ||
} | ||
return UpdateOutcome::UNCHANGED; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about to extend the example with a brightness param. It is a good use case to show how to make validation. Slider can go from 0 to 10 with 10% brightness change per snap. Update function will validate that is brightness in a given range and map it into analogWrite range from 0 to PWMRANGE (1023) with map function. I would also keep on-off switch.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe let's address the larger change first.
lib/framework/StatefulService.h
Outdated
beginTransaction(); | ||
UpdateOutcome outcome = updateFunction(_state); | ||
if (outcome == UpdateOutcome::CHANGED) { | ||
callUpdateHandlers(originId); | ||
} | ||
endTransaction(); | ||
return outcome; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have an idea with auto-transaction. I'll prepare a PR into this branch to show my findings.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll do it later as a separate PR.
… to reflect API in StatefulService Move new definitions to StatefulService.h so it is obvious it is not general purpose
I will sort out updating the docs tomorrow. |
Docs updated, how are we looking? |
README.md
Outdated
```cpp | ||
lightStateService.update([&](LightState& state) { | ||
state.on = true; // turn on the lights! | ||
return StateUpdateResult::CHANGED; // notify StatefulService of the change | ||
}, "timer"); | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
```cpp | |
lightStateService.update([&](LightState& state) { | |
state.on = true; // turn on the lights! | |
return StateUpdateResult::CHANGED; // notify StatefulService of the change | |
}, "timer"); | |
``` | |
```cpp | |
lightStateService.update([&](LightState& state) { | |
if (state.on) { | |
return StateUpdateResult::UNCHANGED; | |
} | |
state.on = true; // turn on the lights! | |
return StateUpdateResult::CHANGED; // notify StatefulService of the change | |
}, "timer"); |
lib/framework/StatefulService.h
Outdated
beginTransaction(); | ||
StateUpdateResult result = stateUpdater(jsonObject, _state); | ||
if (result == StateUpdateResult::CHANGED) { | ||
callUpdateHandlers(originId); | ||
} | ||
endTransaction(); | ||
return result; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
beginTransaction(); | |
StateUpdateResult result = stateUpdater(jsonObject, _state); | |
if (result == StateUpdateResult::CHANGED) { | |
callUpdateHandlers(originId); | |
} | |
endTransaction(); | |
return result; | |
beginTransaction(); | |
StateUpdateResult result = stateUpdater(jsonObject, _state); | |
endTransaction(); | |
if (result == StateUpdateResult::CHANGED) { | |
callUpdateHandlers(originId); | |
} | |
return result; |
We can close transaction much earlier, right after state changed.
lib/framework/StatefulService.h
Outdated
StateUpdateResult update(std::function<StateUpdateResult(T&)> stateUpdater, const String& originId) { | ||
beginTransaction(); | ||
StateUpdateResult result = stateUpdater(_state); | ||
if (result == StateUpdateResult::CHANGED) { | ||
callUpdateHandlers(originId); | ||
} | ||
endTransaction(); | ||
return result; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
StateUpdateResult update(std::function<StateUpdateResult(T&)> stateUpdater, const String& originId) { | |
beginTransaction(); | |
StateUpdateResult result = stateUpdater(_state); | |
if (result == StateUpdateResult::CHANGED) { | |
callUpdateHandlers(originId); | |
} | |
endTransaction(); | |
return result; | |
} | |
StateUpdateResult update(std::function<StateUpdateResult(T&)> stateUpdater, const String& originId) { | |
beginTransaction(); | |
StateUpdateResult result = stateUpdater(_state); | |
endTransaction(); | |
if (result == StateUpdateResult::CHANGED) { | |
callUpdateHandlers(originId); | |
} | |
return result; | |
} |
Thanks for the changes @kasedy - I'll get these changes made and merged into master after work today. My plan is to then update and re-open your access point fixes PR - also fixing the other bugs I've spotted. |
Call update handlers outside of mutex Fix invalid example in README.md
LGTM |
No description provided.