• Stars
    star
    571
  • Rank 75,160 (Top 2 %)
  • Language
    Swift
  • License
    MIT License
  • Created almost 10 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Type-safe event handling for Swift

emitter-kit v5.2.2

stable CocoaPods Compatible Carthage Compatible Platform

A replacement for NSNotificationCenter#addObserver and NSObject#addObserver that is type-safe and not verbose.

import EmitterKit

// A generic event emitter (but type-safe)!
var event = Event<T>()

// Any emitted data must be the correct type.
event.emit(data)

// This listener will only be called once.
// You are *not* required to retain it.
event.once { data: T in
  print(data)
}

// This listener won't stop listening;
// unless you stop it manually,
// or its Event<T> is deallocated.
// You *are* required to retain it.
var listener = event.on { data: T in
  print(data)
}

// Stop the listener manually.
listener.isListening = false

// Restart the listener (if it was stopped).
listener.isListening = true

 

Targeting

A target allows you to associate a specific AnyObject with an emit call. This is useful when emitting events associated with classes you can't add properties to (like UIView).

When calling emit with a target, you must also call on or once with the same target in order to receive the emitted event.

let myView = UIView()
let didTouch = Event<UITouch>()

didTouch.once(myView) { touch in
  print(touch)
}

didTouch.emit(myView, touch)

 

NSNotification

The Notifier class helps when you are forced to use NSNotificationCenter (for example, if you want to know when the keyboard has appeared).

// You are **not** required to retain this after creating your listener.
var event = Notifier(UIKeyboardWillShowNotification)

// Handle NSNotifications with style!
listener = event.on { (notif: Notification) in
  print(notif.userInfo)
}

 

Key-Value Observation (KVO)

// Any NSObject descendant will work.
var view = UIView()

// "Make KVO great again!" - Donald Trump
listener = view.on("bounds") { (change: Change<CGRect>) in
  print(change)
}

 

Thread Safety

⚠️ None of the classes in EmitterKit are thread-safe!

The following actions must be done on the same thread, or you need manual locking:

  • Emit an event
  • Add/remove a listener
  • Set the isListening property of a listener

 

v5.2.2 changelog

  • Fixed protocol casting (#60)

v5.2.1 changelog

  • Fix Carthage compatibility for non iOS platforms

v5.2.0 changelog

  • Added the Event.getListeners method
  • Listeners are now always called in the order they were added
  • Event.emit() can be called without an argument
  • Carthage support has been improved

v5.1.0 changelog

  • The NotificationListener class now takes a Notification instead of an NSDictionary.

  • A NotificationListener without a target will now receive every Notification with its name, regardless of the value of notif.object.

v5.0.0 changelog

  • Swift 3.0 + Xcode 8.0 beta 6 support

  • The Signal class was removed. (use Event<Void> instead)

  • The Emitter abstract class was removed.

  • The EmitterListener class was renamed EventListener<T>.

  • The Event<T> class no longer has a superclass.

  • The Notification class was renamed Notifier (to prevent collision with Foundation.Notification).

  • The on and once methods of Event<T> now return an EventListener<T> (instead of just a Listener)

  • The on and once methods of Notifier now return an NotificationListener (instead of just a Listener)

  • The on and once methods of NSObject now return an ChangeListener<T> (instead of just a Listener)

  • The keyPath, options, and object properties of ChangeListener<T> are now public.

  • A listenerCount: Int computed property was added to the Event<T> class.

  • An event: Event<T> property was added to the EventListener<T> class.

The changelog for older versions can be found here.

More Repositories

1

vite-tsconfig-paths

Support for TypeScript's path mapping in Vite
TypeScript
1,027
star
2

ee-ts

Type-safe, isomorphic event emitters
TypeScript
123
star
3

hex-kit

Convert hex triplets into UIColors and CGColors!
Swift
68
star
4

spec.ts

Write tests for your types!
JavaScript
50
star
5

markdown-ast

Tiny markdown parser
JavaScript
39
star
6

module-extractor

Extract a JS/TS module and its dependencies into a new package
TypeScript
14
star
7

esbuild-extra

Extra features for esbuild plugins (like onTransform hook)
TypeScript
13
star
8

grunt-then

Anonymous tasks and targets in Grunt.
CoffeeScript
13
star
9

meteor-client

The client-side core of Meteor
JavaScript
12
star
10

rethinkdb-mock

In-memory RethinkDB
CoffeeScript
7
star
11

glob-regex

Tiny glob-to-RegExp converter
JavaScript
7
star
12

sql-ast

MySQL parser
JavaScript
7
star
13

get-dev-paths

Search node_modules for locally developed packages
CoffeeScript
7
star
14

immer-undo

Generate inverse Immer patches on-the-fly
TypeScript
6
star
15

uvu-watch

Watch mode for uvu test runner
JavaScript
6
star
16

mini-hb

Tiny handlebars engine (made with TypeScript)
TypeScript
6
star
17

babel-register

JavaScript
5
star
18

vs-auto-typings

Managed @types/* packages for VS Code
TypeScript
5
star
19

nitro-lang

Minimal scripting language that embraces immutability and reactivity (concept)
TypeScript
5
star
20

rafz

Coordinate `requestAnimationFrame` calls across your app and/or libraries
TypeScript
5
star
21

vite-plugin-template

JavaScript
4
star
22

443

Automated SSL certificates using Let's Encrypt
JavaScript
4
star
23

ddp

Meteor's latency-compensated distributed data client
JavaScript
4
star
24

vite-plugin-react-lazy

Seamless conditional imports with React Suspense
TypeScript
4
star
25

nomatter

Remove YAML front matter from a string. No dependencies.
JavaScript
3
star
26

mixpa

Tiny, isomorphic client for Mixpanel
TypeScript
3
star
27

cush-shell

Throwaway shell scripts
TypeScript
3
star
28

check-git-patch

Find errors and conflicts in a git patch
TypeScript
2
star
29

typescript-rewrite-paths

Rewrite import paths in TypeScript code
TypeScript
2
star
30

is-node-env

Know when your code is running in a NodeJS environment.
JavaScript
2
star
31

vscode-lsp-template

TypeScript
2
star
32

lotus-runner

A framework-agnostic test runner for Node.
CoffeeScript
2
star
33

changed-lines

Compare strings just like `git diff` does
JavaScript
2
star
34

steal

Array.prototype.pop for objects
CoffeeScript
2
star
35

yender

Yield for render props
TypeScript
2
star
36

with-scope

Expose the properties of a given `Object` as local variables to a given `Function`.
CoffeeScript
2
star
37

repro

Bug reproductions
1
star
38

measureText

Measure text without rendering it (for React Native)
CoffeeScript
1
star
39

tslint-mango

Opinionated TSLint preset -- Prettier compatible, JSX ready, no semicolons
TypeScript
1
star
40

isNodeJS

Returns true when in NodeJS
JavaScript
1
star
41

lodge

Isomorphic console w/ inspection, colors, namespaces, and configuration
CoffeeScript
1
star
42

mongo

Adaptor for using MongoDB and Minimongo over DDP
JavaScript
1
star
43

vscode-seoul-dark

1
star
44

lua-emitter

Event emitters for Lua
MoonScript
1
star
45

ReactiveVar

Reactive variables for Javascript
CoffeeScript
1
star
46

parse-bool

Convert a string to a boolean
JavaScript
1
star
47

keymirror

A flexible key mirror. { key1: "key1", key2: "key2" }
CoffeeScript
1
star
48

vocus

Memory-conscious, real-time audio manipulator in Node.js
1
star
49

StatusBar

StatusBar singleton for React Native
CoffeeScript
1
star
50

is-open-comment

Returns true if a string has an unclosed comment
JavaScript
1
star
51

bocks

JavaScript
1
star
52

fsx

User-friendly file operations for NodeJS
CoffeeScript
1
star
53

react-dev-ssr

JSX runtime + Babel transform – Step through React component trees with a synchronous stack trace
TypeScript
1
star
54

recrawl

Filesystem crawler
TypeScript
1
star
55

lotus-log

An extensible, platform-agnostic logging library
JavaScript
1
star
56

lotus-jasmine

Use the Jasmine testing framework with lotus-runner
CoffeeScript
1
star
57

resolve-symlink

Follow a trail of symlinks
JavaScript
1
star
58

relative

Ultra fast relative path resolver
TypeScript
1
star
59

fsx-mock

JavaScript
1
star
60

shrink-array

Shrink an array with a subset reducer
JavaScript
1
star
61

slurm

CLI argument parser
JavaScript
1
star
62

lotus-coffee

CoffeeScript
1
star
63

combine

Combine multiple objects into an existing object
CoffeeScript
1
star
64

telegram-tl-node

JavaScript
1
star
65

journey

Client-side routing
JavaScript
1
star
66

condo

Simple, event-based state wrappers with optional validation
JavaScript
1
star
67

Property

A class for property descriptors
CoffeeScript
1
star
68

lotus-lab

CoffeeScript
1
star
69

umd-cli

Generate UMD bundles from NPM packages (using https://packd.now.sh)
JavaScript
1
star
70

define

The better Object.defineProperty
CoffeeScript
1
star
71

spy-on

TypeScript
1
star
72

process

A simple, configurable Process type for Javascript
CoffeeScript
1
star
73

valido

Fast and flexible validation engine (13kb unminified)
CoffeeScript
1
star
74

package-live-reload

Atom service that reloads Atom packages on file changes
CoffeeScript
1
star
75

key-bindings

Bind keystrokes to functions in NodeJS
CoffeeScript
1
star
76

gesture

A gesture library for React & React Native
CoffeeScript
1
star
77

round

Robust fraction rounding
JavaScript
1
star
78

dough

💸 Rich, modern, imperative DOM wrappers
JavaScript
1
star
79

weak-map

A weak map for the browser and Node.
CoffeeScript
1
star
80

diff-sequence

An implementation of a diff algorithm on arrays and objects
JavaScript
1
star
81

save-heap

Save heap snapshots with ease.
CoffeeScript
1
star
82

hasKeys

Know if Object.keys() would return an empty array
JavaScript
1
star
83

sortObject

Sort an object by its keys and/or values
CoffeeScript
1
star
84

minimongo

Meteor's client-side datastore: a port of MongoDB to Javascript
JavaScript
1
star
85

slush

Basic HTTP server for NodeJS
JavaScript
1
star
86

map-filter-ts

Map an iterable, filter out undefined results
JavaScript
1
star
87

telegram-mt-node

JavaScript
1
star
88

lotus-git

UNMAINTAINED - Check out https://github.com/aleclarson/scripts
CoffeeScript
1
star