GithubHelp home page GithubHelp logo

idered / behavior-tree Goto Github PK

View Code? Open in Web Editor NEW
115.0 3.0 4.0 3.37 MB

๐ŸŒฒ Manage React state with Behavior Trees

License: MIT License

TypeScript 93.17% JavaScript 1.63% CSS 5.20%
behavior-tree behavior tree react react-hooks state-management

behavior-tree's Introduction

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

npm install @btree/core
import {nodes} from '@btree/core'

const initialState = {
  isLoggedIn: false
}

const AuthBehavior = nodes.root('Auth behavior', () =>
  /* Selector runs child one by one until one of them succeeds */
  nodes.selector([
    /* Sequence runs child one by one and stops if any child return failure status */
    nodes.sequence([
      /* Condition node run logic checks on current state */
      nodes.condition('Is logged in', (state, props) => state.isLoggedIn),
      /* Action node is used for side effects and state modifications */
      nodes.action('Redirect to dashboard', (state, props) => {
        navigate('/dashboard')
      }),
    ]),
    nodes.sequence([
      nodes.action('Redirect to login page', () => {
        navigate('/login')
      }),
    ]),
  ])
)

// Create instance of tree
const authTree = AuthBehavior(initialState)

// Run tree logic
authTree.tick()

// You can also run it with props
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.

behavior-tree's People

Contributors

dependabot[bot] avatar idered avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

behavior-tree's Issues

Create TodoMVC

Creating TodoMVC example will help spotting potential bugs. It will also help to determine good use cases for behavior trees - because you probably should not use it everywhere, right?

Add tests

Without a suit of tests, this should not be used in production.

Move DevTools to browser extension

Currently DevTools is just a React component and it has some cons:

  • It's unusable in pure JS environment
  • You need to add it to your app tree
  • It can collide with application UI

Browser extension like React DevTools would be a perfect solution.

Custom nodes - plugins

All current nodes should be in form of a plugin.

Example: nodes.sequence() returns an object without function to interpret it. If we want pluggable nodes then it should contain interpretation logic.

Here are some ideas after pluggable nodes will be implemented:

  1. Move default nodes to separate package @btree/nodes
  2. @btree/core should only contain functions for creating nodes and executing tree
  3. Allow people to create custom set of nodes
  4. Default nodes implementation should be optional
  5. If default nodes will be in separate package then nodes.root should not be a node but a normal function eg. createTree - it should be in core package.

Implementing this is crucial for library future. People have different needs and there's no way to fulfill them all. With solid plugin system we could keep this lib alive.

Alternative names

selector - or
sequence - and
condition - if
action - do

useTree - useBehavior as suggested in HN comment

Selector, sequence is typical naming for behavior trees but might be a little hard to understand for beginners.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.