项目作者: Idered

项目描述 :
🌲 Manage React state with Behavior Trees
高级语言: TypeScript
项目地址: git://github.com/Idered/behavior-tree.git
创建时间: 2020-04-06T20:55:09Z
项目社区:https://github.com/Idered/behavior-tree

开源协议:MIT License

下载


JavaScript/TypeScript implementation of Behavior Trees).

📕 Read the documentation to learn the API.

Gitter

Note: This software is in early development stage. It’s not production ready. API may change.

Behavior Tree Toolkit

  • 🌲 @btree/core - Framework agnostic behavior trees implementation
  • @btree/react - Hooks and docs how to use BT with React

Quick start

  1. npm install @btree/core
  1. import {nodes} from '@btree/core'
  2. const initialState = {
  3. isLoggedIn: false
  4. }
  5. const AuthBehavior = nodes.root('Auth behavior', () =>
  6. /* Selector runs child one by one until one of them succeeds */
  7. nodes.selector([
  8. /* Sequence runs child one by one and stops if any child return failure status */
  9. nodes.sequence([
  10. /* Condition node run logic checks on current state */
  11. nodes.condition('Is logged in', (state, props) => state.isLoggedIn),
  12. /* Action node is used for side effects and state modifications */
  13. nodes.action('Redirect to dashboard', (state, props) => {
  14. navigate('/dashboard')
  15. }),
  16. ]),
  17. nodes.sequence([
  18. nodes.action('Redirect to login page', () => {
  19. navigate('/login')
  20. }),
  21. ]),
  22. ])
  23. )
  24. // Create instance of tree
  25. const authTree = AuthBehavior(initialState)
  26. // Run tree logic
  27. authTree.tick()
  28. // You can also run it with props
  29. authTree.tick({authKey: 'xyz'})

Why?

Getting app features done is cool but what about future changes? As time passes, you will forgot how given feature works. Writing comments or docs is not something that developers have in mind all the time. This library will handle that for you.

With Behavior Tree notation all your code will be wrapped with nodes - that allows to visualize how it works. Check /packages/react/example for implementation of this logic.