Fix some bugs with emit calls.
Fix a possible race with connection/subscribe.
Add some simple test scripts.