Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/you21979/node-agent-fsm
https://github.com/you21979/node-agent-fsm
Last synced: 3 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/you21979/node-agent-fsm
- Owner: you21979
- Created: 2014-10-27T14:25:49.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2019-06-20T08:01:43.000Z (over 5 years ago)
- Last Synced: 2024-04-24T13:16:58.773Z (7 months ago)
- Language: JavaScript
- Size: 8.79 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
node-agent-fsm
==============install
-------```
npm i agent-fsm
```example
-------```
const rp = require('request-promise');
const FSM = require('..');const STATE = {
NONE : 0,
DOWNLOAD : 1,
RETRY : 2,
FINISH : 3,
}const EVENTS = []
EVENTS[STATE.NONE] = FSM.dummyEvent;
EVENTS[STATE.DOWNLOAD] = FSM.makeEvent({
begin:ctx => {
Promise.all(
["https://api.github.com/users/you21979", "https://api.github.com/users/you21979/repos"].
map(url => rp({url:url,timeout:3000,method:"GET",headers:{"User-Agent":"test"}}))
).then(res => {
ctx.data = res
ctx.m.update(STATE.FINISH)
}).catch( e => {
console.log(e)
ctx.m.update(STATE.RETRY)
})
},
});
EVENTS[STATE.RETRY] = FSM.makeEvent({
begin : ctx => {
setTimeout(() => ctx.m.update(STATE.DOWNLOAD), 2000)
}
})
EVENTS[STATE.FINISH] = FSM.makeEvent({
begin : ctx => {
ctx.isExit = true
console.log(ctx.data)
}
})
const main = () => {
const ctx = {isExit : false}
ctx.m = FSM.makeStateMachine(ctx, EVENTS);
ctx.m.update(STATE.DOWNLOAD)
const update = () => {
if(ctx.isExit) return
ctx.m.tick();
setImmediate(() => update(), 0)
}
update();
}
main()
```