-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine.coffee
42 lines (34 loc) · 1.29 KB
/
engine.coffee
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
if typeof(process) is "undefined"
packets = require 'packets'
else
if process.env.NODE_ENV is 'test'
packets = require './components/calce-packets'
engine = module.exports =
_commands: {}
_plugins: []
engine.init = (@io, @host) ->
@socket = @io.connect @host
@socket.on 'connect', @_onConnect
@socket.on 'disconnect', @_onDisconnect
engine._onConnect = (socket) ->
engine._pluginOnConnect(plugin, socket) for plugin in engine._plugins
engine._onDisconnect = (socket) ->
engine._pluginOnDisconnect(plugin, socket) for plugin in engine._plugins
engine.use = (c, plugin) ->
self = this
cmd = c
if not Array.isArray @_commands[cmd]
@_commands[cmd] = []
@socket.on cmd, (data) ->
data = packets.toObject data
if data
data.cmd = cmd
self._pluginOnCommand(plugin, data, self.socket) for plugin in self._commands[cmd]
@_commands[cmd].push plugin if @_commands[cmd].indexOf plugin is -1
@_plugins.push plugin if @_plugins.indexOf plugin is -1
engine._pluginOnConnect = (plugin, socket) ->
plugin.onConnect socket if plugin.onDisconnect
engine._pluginOnDisconnect = (plugin, socket) ->
plugin.onDisconnect socket if plugin.onDisconnect
engine._pluginOnCommand = (plugin, data, socket) ->
plugin.onCommand data, socket if plugin.onCommand