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

feat: Allow plugins to declare an action invalid #963

Merged
merged 3 commits into from
Jul 21, 2021
Merged

Conversation

delucis
Copy link
Member

@delucis delucis commented Jul 19, 2021

Closes #962

This adds support for an isInvalid method in boardgame.io plugins. Similar to the current noClient plugin method, this allows the reducer to ask plugins if everything is OK or if the current event/move is invalid from their point of view. If one or more plugins returns a truthy value from its isInvalid method, the reducer will fall back to its initial state, analogous to what it does when a move returns INVALID_MOVE.

Currently this is not in use but the intention would be to use it for the events plugin, following on from #957. It also allows for other custom use cases, like state validators.

Notes

  • isInvalid should return a string if the action is invalid. This could be less strict if that seems desirable.

  • The error returned to the client is of the form

    {
      type: 'action/plugin_invalid',
      payload: {
        plugin: 'name of plugin that declared action invalid',
        message: 'string returned by plugin’s isInvalid method'
      },
    }
  • If more than one plugin’s isInvalid method returns a string, we just error with the first of them, rather than reporting all of them.

vdfdev
vdfdev previously approved these changes Jul 20, 2021
Copy link
Contributor

@vdfdev vdfdev left a comment

Choose a reason for hiding this comment

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

10/10 very readable and well tested code, thank you!

src/core/reducer.ts Outdated Show resolved Hide resolved
src/plugins/main.test.ts Outdated Show resolved Hide resolved
src/plugins/main.test.ts Outdated Show resolved Hide resolved
@delucis delucis merged commit afee0b7 into main Jul 21, 2021
@delucis delucis deleted the delucis/feat/962 branch July 21, 2021 07:39
@delucis
Copy link
Member Author

delucis commented Jul 21, 2021

Thanks for the review @vdfdev!

delucis added a commit that referenced this pull request Aug 1, 2021
#963 introduced an `isInvalid` method to the plugin API allowing plugins to declare updates invalid. #963 used this to check updates in the reducer, but didn’t handle plugins being flushed during game initialisation. This commit refactors use of plugins so that flushing and validation always happens together via a consolidated `flushAndValidate` method.
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.

Add API allowing plugins to declare the current action invalid
2 participants