-
Notifications
You must be signed in to change notification settings - Fork 382
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
#!/usr/bin/env coffee | ||
|
||
log = new (require 'log')('debug') | ||
|
||
class Group | ||
on: (@callback)-> | ||
dispatch: (msg)-> | ||
log.info 'dispatch',msg | ||
@callback(msg) if @callback | ||
cb: -> | ||
@callback | ||
|
||
class Bot | ||
constructor:(@list = [])-> | ||
setInterval => | ||
o('bbb' ) for o in @list | ||
,500 | ||
|
||
|
||
listen_group : (name , callback) -> | ||
group = new Group() | ||
@list.push group.dispatch | ||
callback group | ||
|
||
|
||
### DEMO1 | ||
bot = new Bot() | ||
# ggg = null | ||
bot.listen_group 'group', (group)-> | ||
group.on (msg)-> | ||
log.info '- received',msg | ||
group.dispatch 'aaa' | ||
k = group.dispatch | ||
k('single') | ||
# ggg = group | ||
### | ||
|
||
# ggg.dispatch 'ccc' | ||
# | ||
# setInterval -> | ||
# ggg.dispatch 'ddd' | ||
# ,500 | ||
|
||
###期待 | ||
dispatch | ||
- receive | ||
dispatch | ||
- receive | ||
实际结果 | ||
dispatch only | ||
原因猜测 | ||
- js中没class概念 传给list的是个函数,破坏了里面的 this.xx 的概念 | ||
### | ||
|
||
|
||
|
||
# DEMO2 | ||
group = new Group() | ||
group.on (msg)-> | ||
log.info '- received',msg | ||
|
||
group.dispatch 'kk' | ||
|
||
# obj = group | ||
# method = group.dispatch | ||
|
||
group['dispatch'] 'str' | ||
|
||
|
||
|
||
|
||
|
||
|
||
|