https://github.com/kcreate/events
Event API written for Crystal
https://github.com/kcreate/events
Last synced: 11 months ago
JSON representation
Event API written for Crystal
- Host: GitHub
- URL: https://github.com/kcreate/events
- Owner: KCreate
- License: mit
- Created: 2016-08-10T21:09:35.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2016-12-12T16:16:50.000Z (about 9 years ago)
- Last Synced: 2025-03-29T11:52:51.508Z (11 months ago)
- Language: Crystal
- Homepage: http://crystal-lang.org
- Size: 23.4 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[
](https://github.com/KCreate/events)
[](https://travis-ci.org/KCreate/events)
# Events
Super basic event emitter written for crystal-lang.
## Installation
Add this to your application's `shard.yml`:
```yaml
dependencies:
events:
github: kcreate/events
branch: master
```
## Example
Just include the module inside any classes you want to have access to the event methods
```crystal
require "events"
# Some class
class Person
include Events
def initialize
add_event "fart"
end
def fart
invoke_event "fart"
end
end
leonard = Person.new
leonard.on "fart", do
puts "daaamn"
end
leonard.fart
```
You should see "daaamn" pop up in your console
## Usage
The [test suite](spec/Events_spec.cr) has all the examples you need. I usually forget to update the README so just look there.
You first need a class that will emit some events. Add the module to your class like this:
```crystal
class Person
include Events
end
```
You should add all your events inside your initialize function to prevent unexpected behaviour. Use *add_event* for this.
```crystal
class Person
include Events
def initialize
add_event "wakeup"
add_event "sleep"
end
def wakeup
invoke_event "wakeup"
end
def gotosleep
invoke_event "sleep"
end
# ... The rest of your class
end
```
Similarly you can remove an event using *remove_event*. I don't know why you'd want to remove an event, but you can.
```crystal
class Person
include Events
def initialize
add_event "wakeup"
add_event "sleep"
end
def wakeup
invoke_event "wakeup"
end
def gotosleep
# This first invokes the event and deletes it afterwards
invoke_event "sleep"
remove_event "sleep"
end
end
```
You can subscribe to these events from the outside like this:
```crystal
class Person
...
end
leonard = Person.new
leonard.on "wakeup" do
puts "leonard woke up"
end
leonard.on "sleep" do
puts "leonard went to sleep"
end
```
If you now call the *wakeup* and *gotosleep* functions,
```crystal
leonard.gotosleep
leonard.wakeup
```
This will be the output in your console
```sh
leonard went to sleep
leonard woke up
```
Calling the *on* function will add your handler to the event, but also return a proc that removes the handler from the event again.
This means you can do stuff like this:
```crystal
sleephandler = leonard.on "sleep" do
puts "going to sleep"
end
leonard.gotosleep # going to sleep
leonard.gotosleep # going to sleep
sleephandler.call # removing the handler
leonard.gotosleep # ... nothing will be printed
```
## Test
Run `crystal spec` to run the test suite.
## Todos
- Allow passing arguments to callbacks
- Allow the usage of subclasses of `Event`
- Add docs to a public website
## Contributing
1. Fork it ( https://github.com/kcreate/events/fork )
2. Create your feature branch (git checkout -b my-new-feature)
3. Commit your changes (git commit -am 'Add some feature')
4. Make sure your changes don't break anything (crystal spec)
5. Push to the branch (git push origin my-new-feature)
6. Create a new Pull Request
## Contributors
- [kcreate](https://github.com/kcreate) Leonard Schuetz - creator, maintainer