forked from bocoup/chatter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-args-adjuster.js
99 lines (85 loc) · 3.05 KB
/
create-args-adjuster.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// If this syntax looks unfamiliar, don't worry, it's just JavaScript!
// Learn more about ES2015 here: https://babeljs.io/docs/learn-es2015/
//
// Run "npm install" and then test with this command in your shell:
// node examples/create-args-adjuster.js
const Promise = require('bluebird');
const chalk = require('chalk');
// ES2015 syntax:
// import {processMessage, createCommand, createArgsAdjuster, normalizeMessage} from 'chatter';
// ES5 syntax:
// const chatter = require('chatter');
const chatter = require('..');
const processMessage = chatter.processMessage;
const createCommand = chatter.createCommand;
const createArgsAdjuster = chatter.createArgsAdjuster;
const normalizeMessage = chatter.normalizeMessage;
// ================
// message handlers
// ================
// Increments the counter and returns a string decribing the new state.
const incrementCommand = createCommand({
name: 'increment',
description: 'Increment the counter and show it.',
usage: '<delta>',
}, (message, state) => {
const delta = Number(message);
if (!message) {
return false;
}
else if (isNaN(delta)) {
return `Sorry, but "${message}" doesn't appear to be a number!`;
}
state.counter += delta;
return `The counter is now at ${state.counter}.`;
});
// Returns a message handler that encapsualates some state, and passes that
// state into child commands as an argument.
function getStatefulMessageHandler() {
const state = {counter: 0};
return createArgsAdjuster({
adjustArgs(message) {
return [message, state];
},
}, createCommand({
isParent: true,
description: 'An exciting command, for sure.',
}, [
incrementCommand,
]));
}
// ====================================
// process messages with processMessage
// ====================================
function log(color, prefix, message) {
message = message.replace(/(\n)/g, `$1${' '.repeat(prefix.length + 1)}`);
console.log(chalk[color](`${prefix} ${message}`));
}
function header(message) {
log('cyan', '\n=====', message);
}
function simulate(messageHandler, message) {
log('magenta', '\n[In] ', message);
return processMessage(messageHandler, message)
.then(response => {
const text = response !== false ? normalizeMessage(response) : '-';
log('green', '[Out]', text);
})
.then(() => Promise.delay(100));
}
const firstStatefulHandler = getStatefulMessageHandler('first');
const secondStatefulHandler = getStatefulMessageHandler('second');
Promise.mapSeries([
() => header('firstStatefulHandler'),
() => simulate(firstStatefulHandler, 'help'),
() => simulate(firstStatefulHandler, 'help increment'),
() => simulate(firstStatefulHandler, 'increment 1'),
() => simulate(firstStatefulHandler, 'increment 2'),
() => header('secondStatefulHandler'),
() => simulate(secondStatefulHandler, 'increment 101'),
() => simulate(secondStatefulHandler, 'increment 202'),
() => header('firstStatefulHandler'),
() => simulate(firstStatefulHandler, 'increment 3'),
() => header('secondStatefulHandler'),
() => simulate(secondStatefulHandler, 'increment 303'),
], f => f());