4.0.3 • Published 4 years ago

chatflux v4.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
4 years ago

npm.io npm.io npm.io npm.io

ChatFlux

An extended flux implementation designed for usage with chat bots.

Install: npm i chatflux

import { ChatFlux, Reducer, Renderer } from 'ChatFlux';
import { db } from './db';

type CountState = {
  count: number;
}
type CountAction = 'increment' | 'decrement';

const reducer: Reducer<CountState, CountAction> = (state = { count: 0 }, action) => {
  switch (action) {
    case 'increment':
      return ({
        ...state,
        count: state.count + 1,
      });
    case 'decrement':
      return ({
        ...state,
        count: state.count - 1,
      });
    default:
      return state;
  }
};

const renderer: Renderer<CountState, string> = state => `Count: ${state.count}`;

const Counter = new ChatFlux<CountState, CountAction>({
  database: db,
  reduce: reducer,
  render: renderer,
});

app.on('message', async (message) => {
  if (message.content !== '!count') return;
  const msg = message.channel.send('Loading...');
  const body = await Counter.create(msg.id);
  msg.edit(body);

  setInterval(async () => {
    await Counter.dispatch(msg.id, 'increment');
    const newBody = await Counter.render(msg.id);
    msg.edit(newBody);
  }, 3000 /* 3 seconds */);
});
4.0.1

4 years ago

4.0.0

4 years ago

4.0.3

4 years ago

4.0.2

4 years ago

3.0.0

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago