├── .gitignore ├── .travis.yml ├── Emitter.podspec.json ├── Emitter.xcodeproj ├── project.pbxproj ├── project.xcworkspace │ └── contents.xcworkspacedata └── xcshareddata │ └── xcschemes │ └── Emitter.xcscheme ├── Emitter.xcworkspace └── contents.xcworkspacedata ├── Emitter ├── Emitter+Blocks.h ├── Emitter+Blocks.m ├── Emitter+Selectors.h ├── Emitter+Selectors.m ├── Emitter.h ├── NSInvocation+BlockArguments.h └── NSInvocation+BlockArguments.m ├── EmitterTests ├── Emitter+BlocksTests.m ├── Emitter+SelectorsTests.m └── Emitter-Prefix.pch ├── LICENSE ├── Podfile ├── Podfile.lock └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | # OS X 2 | .DS_Store 3 | 4 | # Xcode 5 | build/ 6 | *.pbxuser 7 | !default.pbxuser 8 | *.mode1v3 9 | !default.mode1v3 10 | *.mode2v3 11 | !default.mode2v3 12 | *.perspectivev3 13 | !default.perspectivev3 14 | xcuserdata 15 | *.xccheckout 16 | profile 17 | *.moved-aside 18 | DerivedData 19 | *.hmap 20 | *.ipa 21 | 22 | # CocoaPods 23 | Pods 24 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: objective-c 2 | xcode_workspace: Emitter.xcworkspace 3 | xcode_scheme: Emitter 4 | before_install: 5 | - gem install cocoapods 6 | notifications: 7 | slack: seegno:AoXFWrkPDMfOuaxvckPs7hc0 8 | -------------------------------------------------------------------------------- /Emitter.podspec.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "Emitter", 3 | "version": "0.0.9", 4 | "summary": "An event emitter implementation for Objective-C with dynamic block invocation based on the node.js EventEmitter API.", 5 | "homepage": "https://github.com/seegno/emitter-objc", 6 | "authors": "Seegno", 7 | "license": { 8 | "type": "MIT", 9 | "file": "LICENSE" 10 | }, 11 | "source": { 12 | "git": "https://github.com/seegno/emitter-objc.git", 13 | "tag": "0.0.9" 14 | }, 15 | "source_files": "Emitter/*.{h,m}", 16 | "requires_arc": true, 17 | "platforms": { 18 | "ios": "6.0", 19 | "osx": "10.8" 20 | }, 21 | "dependencies": { 22 | "BlocksKit/DynamicDelegate": [ 23 | 24 | ] 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Emitter.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 51E206F1FF4145038F60AAF1 /* libPods-EmitterTests.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 6D30EA9E9FE14528BDFB2D2E /* libPods-EmitterTests.a */; }; 11 | 59684AD015F9DF500063CF5E /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 59684ACF15F9DF500063CF5E /* Cocoa.framework */; }; 12 | 59684AE315F9DF500063CF5E /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 59684ACF15F9DF500063CF5E /* Cocoa.framework */; }; 13 | 59684AE615F9DF500063CF5E /* Emitter.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 59684ACC15F9DF500063CF5E /* Emitter.dylib */; }; 14 | 59684AEF15F9DF500063CF5E /* Emitter+BlocksTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 59684AEE15F9DF500063CF5E /* Emitter+BlocksTests.m */; }; 15 | 59ED69701616F891009C400B /* README.md in Resources */ = {isa = PBXBuildFile; fileRef = 59ED696F1616F891009C400B /* README.md */; }; 16 | 643174AA19C84C3C00F609B1 /* XCTest.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 643174A919C84C3C00F609B1 /* XCTest.framework */; }; 17 | CB4919C50D22474F8B4346B1 /* libPods.a in Frameworks */ = {isa = PBXBuildFile; fileRef = D5B6A8F661D74DED8997F8F8 /* libPods.a */; }; 18 | E5682C9B18B52D6D00F5D583 /* NSInvocation+BlockArguments.h in Headers */ = {isa = PBXBuildFile; fileRef = E5682C9918B52D6D00F5D583 /* NSInvocation+BlockArguments.h */; }; 19 | E5682C9C18B52D6D00F5D583 /* NSInvocation+BlockArguments.m in Sources */ = {isa = PBXBuildFile; fileRef = E5682C9A18B52D6D00F5D583 /* NSInvocation+BlockArguments.m */; }; 20 | E578156118F3FA7400698CFA /* Emitter+SelectorsTests.m in Sources */ = {isa = PBXBuildFile; fileRef = E578156018F3FA7400698CFA /* Emitter+SelectorsTests.m */; }; 21 | E5A9453118F2AB2F0076DCE8 /* Emitter+Blocks.h in Headers */ = {isa = PBXBuildFile; fileRef = E5A9452D18F2AB2F0076DCE8 /* Emitter+Blocks.h */; }; 22 | E5A9453218F2AB2F0076DCE8 /* Emitter+Blocks.m in Sources */ = {isa = PBXBuildFile; fileRef = E5A9452E18F2AB2F0076DCE8 /* Emitter+Blocks.m */; }; 23 | E5A9453318F2AB2F0076DCE8 /* Emitter+Selectors.h in Headers */ = {isa = PBXBuildFile; fileRef = E5A9452F18F2AB2F0076DCE8 /* Emitter+Selectors.h */; }; 24 | E5A9453418F2AB2F0076DCE8 /* Emitter+Selectors.m in Sources */ = {isa = PBXBuildFile; fileRef = E5A9453018F2AB2F0076DCE8 /* Emitter+Selectors.m */; }; 25 | E5A9453618F2AB330076DCE8 /* Emitter.h in Headers */ = {isa = PBXBuildFile; fileRef = E5A9453518F2AB330076DCE8 /* Emitter.h */; }; 26 | /* End PBXBuildFile section */ 27 | 28 | /* Begin PBXContainerItemProxy section */ 29 | 59684AE415F9DF500063CF5E /* PBXContainerItemProxy */ = { 30 | isa = PBXContainerItemProxy; 31 | containerPortal = 59684AC315F9DF500063CF5E /* Project object */; 32 | proxyType = 1; 33 | remoteGlobalIDString = 59684ACB15F9DF500063CF5E; 34 | remoteInfo = EventEmitter; 35 | }; 36 | /* End PBXContainerItemProxy section */ 37 | 38 | /* Begin PBXFileReference section */ 39 | 1F0969A822004F25A8D2CE38 /* Pods.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = Pods.xcconfig; path = Pods/Pods.xcconfig; sourceTree = ""; }; 40 | 33715D2D81AB49FB8082B0CE /* Pods-EmitterTests.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-EmitterTests.xcconfig"; path = "Pods/Pods-EmitterTests.xcconfig"; sourceTree = ""; }; 41 | 59684ACC15F9DF500063CF5E /* Emitter.dylib */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.dylib"; includeInIndex = 0; path = Emitter.dylib; sourceTree = BUILT_PRODUCTS_DIR; }; 42 | 59684ACF15F9DF500063CF5E /* Cocoa.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Cocoa.framework; path = System/Library/Frameworks/Cocoa.framework; sourceTree = SDKROOT; }; 43 | 59684AD215F9DF500063CF5E /* AppKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AppKit.framework; path = System/Library/Frameworks/AppKit.framework; sourceTree = SDKROOT; }; 44 | 59684AD315F9DF500063CF5E /* CoreData.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreData.framework; path = System/Library/Frameworks/CoreData.framework; sourceTree = SDKROOT; }; 45 | 59684AD415F9DF500063CF5E /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; }; 46 | 59684AE015F9DF500063CF5E /* EmitterTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = EmitterTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 47 | 59684AEE15F9DF500063CF5E /* Emitter+BlocksTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "Emitter+BlocksTests.m"; sourceTree = ""; }; 48 | 59ED696F1616F891009C400B /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = README.md; sourceTree = ""; }; 49 | 643174A919C84C3C00F609B1 /* XCTest.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = XCTest.framework; path = Platforms/MacOSX.platform/Developer/Library/Frameworks/XCTest.framework; sourceTree = DEVELOPER_DIR; }; 50 | 64634FC9189D44E700B2901F /* Emitter-Prefix.pch */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Emitter-Prefix.pch"; sourceTree = ""; }; 51 | 64634FCA189D508900B2901F /* XCTest.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = XCTest.framework; path = Library/Frameworks/XCTest.framework; sourceTree = DEVELOPER_DIR; }; 52 | 6D30EA9E9FE14528BDFB2D2E /* libPods-EmitterTests.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-EmitterTests.a"; sourceTree = BUILT_PRODUCTS_DIR; }; 53 | 8158A801EEB946FBB6E07D8B /* libPods-EventEmitterTests.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-EventEmitterTests.a"; sourceTree = BUILT_PRODUCTS_DIR; }; 54 | D5B6A8F661D74DED8997F8F8 /* libPods.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libPods.a; sourceTree = BUILT_PRODUCTS_DIR; }; 55 | E5682C9918B52D6D00F5D583 /* NSInvocation+BlockArguments.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSInvocation+BlockArguments.h"; sourceTree = ""; }; 56 | E5682C9A18B52D6D00F5D583 /* NSInvocation+BlockArguments.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSInvocation+BlockArguments.m"; sourceTree = ""; }; 57 | E578156018F3FA7400698CFA /* Emitter+SelectorsTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "Emitter+SelectorsTests.m"; sourceTree = ""; }; 58 | E58C3F7A19869D5400461CE7 /* Emitter.podspec.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = Emitter.podspec.json; sourceTree = ""; }; 59 | E5A9452D18F2AB2F0076DCE8 /* Emitter+Blocks.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "Emitter+Blocks.h"; sourceTree = ""; }; 60 | E5A9452E18F2AB2F0076DCE8 /* Emitter+Blocks.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "Emitter+Blocks.m"; sourceTree = ""; }; 61 | E5A9452F18F2AB2F0076DCE8 /* Emitter+Selectors.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "Emitter+Selectors.h"; sourceTree = ""; }; 62 | E5A9453018F2AB2F0076DCE8 /* Emitter+Selectors.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "Emitter+Selectors.m"; sourceTree = ""; }; 63 | E5A9453518F2AB330076DCE8 /* Emitter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Emitter.h; sourceTree = ""; }; 64 | /* End PBXFileReference section */ 65 | 66 | /* Begin PBXFrameworksBuildPhase section */ 67 | 59684AC915F9DF500063CF5E /* Frameworks */ = { 68 | isa = PBXFrameworksBuildPhase; 69 | buildActionMask = 2147483647; 70 | files = ( 71 | 59684AD015F9DF500063CF5E /* Cocoa.framework in Frameworks */, 72 | CB4919C50D22474F8B4346B1 /* libPods.a in Frameworks */, 73 | ); 74 | runOnlyForDeploymentPostprocessing = 0; 75 | }; 76 | 59684ADC15F9DF500063CF5E /* Frameworks */ = { 77 | isa = PBXFrameworksBuildPhase; 78 | buildActionMask = 2147483647; 79 | files = ( 80 | 643174AA19C84C3C00F609B1 /* XCTest.framework in Frameworks */, 81 | 59684AE315F9DF500063CF5E /* Cocoa.framework in Frameworks */, 82 | 59684AE615F9DF500063CF5E /* Emitter.dylib in Frameworks */, 83 | 51E206F1FF4145038F60AAF1 /* libPods-EmitterTests.a in Frameworks */, 84 | ); 85 | runOnlyForDeploymentPostprocessing = 0; 86 | }; 87 | /* End PBXFrameworksBuildPhase section */ 88 | 89 | /* Begin PBXGroup section */ 90 | 59684AC115F9DF500063CF5E = { 91 | isa = PBXGroup; 92 | children = ( 93 | 59ED696F1616F891009C400B /* README.md */, 94 | 59684AD515F9DF500063CF5E /* Emitter */, 95 | 59684AE715F9DF500063CF5E /* EmitterTests */, 96 | 59684ACE15F9DF500063CF5E /* Frameworks */, 97 | 59684ACD15F9DF500063CF5E /* Products */, 98 | E58C3F7A19869D5400461CE7 /* Emitter.podspec.json */, 99 | 1F0969A822004F25A8D2CE38 /* Pods.xcconfig */, 100 | 33715D2D81AB49FB8082B0CE /* Pods-EmitterTests.xcconfig */, 101 | ); 102 | sourceTree = ""; 103 | }; 104 | 59684ACD15F9DF500063CF5E /* Products */ = { 105 | isa = PBXGroup; 106 | children = ( 107 | 59684ACC15F9DF500063CF5E /* Emitter.dylib */, 108 | 59684AE015F9DF500063CF5E /* EmitterTests.xctest */, 109 | ); 110 | name = Products; 111 | sourceTree = ""; 112 | }; 113 | 59684ACE15F9DF500063CF5E /* Frameworks */ = { 114 | isa = PBXGroup; 115 | children = ( 116 | 643174A919C84C3C00F609B1 /* XCTest.framework */, 117 | 64634FCA189D508900B2901F /* XCTest.framework */, 118 | 59684ACF15F9DF500063CF5E /* Cocoa.framework */, 119 | 59684AD115F9DF500063CF5E /* Other Frameworks */, 120 | D5B6A8F661D74DED8997F8F8 /* libPods.a */, 121 | 8158A801EEB946FBB6E07D8B /* libPods-EventEmitterTests.a */, 122 | 6D30EA9E9FE14528BDFB2D2E /* libPods-EmitterTests.a */, 123 | ); 124 | name = Frameworks; 125 | sourceTree = ""; 126 | }; 127 | 59684AD115F9DF500063CF5E /* Other Frameworks */ = { 128 | isa = PBXGroup; 129 | children = ( 130 | 59684AD215F9DF500063CF5E /* AppKit.framework */, 131 | 59684AD315F9DF500063CF5E /* CoreData.framework */, 132 | 59684AD415F9DF500063CF5E /* Foundation.framework */, 133 | ); 134 | name = "Other Frameworks"; 135 | sourceTree = ""; 136 | }; 137 | 59684AD515F9DF500063CF5E /* Emitter */ = { 138 | isa = PBXGroup; 139 | children = ( 140 | E5A9453518F2AB330076DCE8 /* Emitter.h */, 141 | E5A9452D18F2AB2F0076DCE8 /* Emitter+Blocks.h */, 142 | E5A9452E18F2AB2F0076DCE8 /* Emitter+Blocks.m */, 143 | E5A9452F18F2AB2F0076DCE8 /* Emitter+Selectors.h */, 144 | E5A9453018F2AB2F0076DCE8 /* Emitter+Selectors.m */, 145 | E5682C9918B52D6D00F5D583 /* NSInvocation+BlockArguments.h */, 146 | E5682C9A18B52D6D00F5D583 /* NSInvocation+BlockArguments.m */, 147 | ); 148 | path = Emitter; 149 | sourceTree = ""; 150 | }; 151 | 59684AE715F9DF500063CF5E /* EmitterTests */ = { 152 | isa = PBXGroup; 153 | children = ( 154 | 64634FC8189D44D200B2901F /* Supporting Files */, 155 | 59684AEE15F9DF500063CF5E /* Emitter+BlocksTests.m */, 156 | E578156018F3FA7400698CFA /* Emitter+SelectorsTests.m */, 157 | ); 158 | path = EmitterTests; 159 | sourceTree = ""; 160 | }; 161 | 64634FC8189D44D200B2901F /* Supporting Files */ = { 162 | isa = PBXGroup; 163 | children = ( 164 | 64634FC9189D44E700B2901F /* Emitter-Prefix.pch */, 165 | ); 166 | name = "Supporting Files"; 167 | sourceTree = ""; 168 | }; 169 | /* End PBXGroup section */ 170 | 171 | /* Begin PBXHeadersBuildPhase section */ 172 | 59684ACA15F9DF500063CF5E /* Headers */ = { 173 | isa = PBXHeadersBuildPhase; 174 | buildActionMask = 2147483647; 175 | files = ( 176 | E5A9453318F2AB2F0076DCE8 /* Emitter+Selectors.h in Headers */, 177 | E5A9453618F2AB330076DCE8 /* Emitter.h in Headers */, 178 | E5682C9B18B52D6D00F5D583 /* NSInvocation+BlockArguments.h in Headers */, 179 | E5A9453118F2AB2F0076DCE8 /* Emitter+Blocks.h in Headers */, 180 | ); 181 | runOnlyForDeploymentPostprocessing = 0; 182 | }; 183 | /* End PBXHeadersBuildPhase section */ 184 | 185 | /* Begin PBXNativeTarget section */ 186 | 59684ACB15F9DF500063CF5E /* Emitter */ = { 187 | isa = PBXNativeTarget; 188 | buildConfigurationList = 59684AF215F9DF500063CF5E /* Build configuration list for PBXNativeTarget "Emitter" */; 189 | buildPhases = ( 190 | 2096BF558CD04AB4BC8DA7E8 /* Check Pods Manifest.lock */, 191 | 59684AC815F9DF500063CF5E /* Sources */, 192 | 59684AC915F9DF500063CF5E /* Frameworks */, 193 | 59684ACA15F9DF500063CF5E /* Headers */, 194 | 84489DD248C345E0925F09E4 /* Copy Pods Resources */, 195 | ); 196 | buildRules = ( 197 | ); 198 | dependencies = ( 199 | ); 200 | name = Emitter; 201 | productName = EventEmitter; 202 | productReference = 59684ACC15F9DF500063CF5E /* Emitter.dylib */; 203 | productType = "com.apple.product-type.library.dynamic"; 204 | }; 205 | 59684ADF15F9DF500063CF5E /* EmitterTests */ = { 206 | isa = PBXNativeTarget; 207 | buildConfigurationList = 59684AF515F9DF500063CF5E /* Build configuration list for PBXNativeTarget "EmitterTests" */; 208 | buildPhases = ( 209 | E75F4B4C969C4B8DA672FE18 /* Check Pods Manifest.lock */, 210 | 59684ADB15F9DF500063CF5E /* Sources */, 211 | 59684ADC15F9DF500063CF5E /* Frameworks */, 212 | 59684ADD15F9DF500063CF5E /* Resources */, 213 | 76F8BF29CC93414BB60F302B /* Copy Pods Resources */, 214 | ); 215 | buildRules = ( 216 | ); 217 | dependencies = ( 218 | 59684AE515F9DF500063CF5E /* PBXTargetDependency */, 219 | ); 220 | name = EmitterTests; 221 | productName = EventEmitterTests; 222 | productReference = 59684AE015F9DF500063CF5E /* EmitterTests.xctest */; 223 | productType = "com.apple.product-type.bundle.unit-test"; 224 | }; 225 | /* End PBXNativeTarget section */ 226 | 227 | /* Begin PBXProject section */ 228 | 59684AC315F9DF500063CF5E /* Project object */ = { 229 | isa = PBXProject; 230 | attributes = { 231 | LastUpgradeCheck = 0500; 232 | ORGANIZATIONNAME = buenocode; 233 | }; 234 | buildConfigurationList = 59684AC615F9DF500063CF5E /* Build configuration list for PBXProject "Emitter" */; 235 | compatibilityVersion = "Xcode 3.2"; 236 | developmentRegion = English; 237 | hasScannedForEncodings = 0; 238 | knownRegions = ( 239 | en, 240 | ); 241 | mainGroup = 59684AC115F9DF500063CF5E; 242 | productRefGroup = 59684ACD15F9DF500063CF5E /* Products */; 243 | projectDirPath = ""; 244 | projectRoot = ""; 245 | targets = ( 246 | 59684ACB15F9DF500063CF5E /* Emitter */, 247 | 59684ADF15F9DF500063CF5E /* EmitterTests */, 248 | ); 249 | }; 250 | /* End PBXProject section */ 251 | 252 | /* Begin PBXResourcesBuildPhase section */ 253 | 59684ADD15F9DF500063CF5E /* Resources */ = { 254 | isa = PBXResourcesBuildPhase; 255 | buildActionMask = 2147483647; 256 | files = ( 257 | 59ED69701616F891009C400B /* README.md in Resources */, 258 | ); 259 | runOnlyForDeploymentPostprocessing = 0; 260 | }; 261 | /* End PBXResourcesBuildPhase section */ 262 | 263 | /* Begin PBXShellScriptBuildPhase section */ 264 | 2096BF558CD04AB4BC8DA7E8 /* Check Pods Manifest.lock */ = { 265 | isa = PBXShellScriptBuildPhase; 266 | buildActionMask = 2147483647; 267 | files = ( 268 | ); 269 | inputPaths = ( 270 | ); 271 | name = "Check Pods Manifest.lock"; 272 | outputPaths = ( 273 | ); 274 | runOnlyForDeploymentPostprocessing = 0; 275 | shellPath = /bin/sh; 276 | shellScript = "diff \"${PODS_ROOT}/../Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [[ $? != 0 ]] ; then\n cat << EOM\nerror: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\nEOM\n exit 1\nfi\n"; 277 | showEnvVarsInLog = 0; 278 | }; 279 | 76F8BF29CC93414BB60F302B /* Copy Pods Resources */ = { 280 | isa = PBXShellScriptBuildPhase; 281 | buildActionMask = 2147483647; 282 | files = ( 283 | ); 284 | inputPaths = ( 285 | ); 286 | name = "Copy Pods Resources"; 287 | outputPaths = ( 288 | ); 289 | runOnlyForDeploymentPostprocessing = 0; 290 | shellPath = /bin/sh; 291 | shellScript = "\"${SRCROOT}/Pods/Pods-EmitterTests-resources.sh\"\n"; 292 | showEnvVarsInLog = 0; 293 | }; 294 | 84489DD248C345E0925F09E4 /* Copy Pods Resources */ = { 295 | isa = PBXShellScriptBuildPhase; 296 | buildActionMask = 2147483647; 297 | files = ( 298 | ); 299 | inputPaths = ( 300 | ); 301 | name = "Copy Pods Resources"; 302 | outputPaths = ( 303 | ); 304 | runOnlyForDeploymentPostprocessing = 0; 305 | shellPath = /bin/sh; 306 | shellScript = "\"${SRCROOT}/Pods/Pods-EmitterTests-resources.sh\"\n"; 307 | showEnvVarsInLog = 0; 308 | }; 309 | E75F4B4C969C4B8DA672FE18 /* Check Pods Manifest.lock */ = { 310 | isa = PBXShellScriptBuildPhase; 311 | buildActionMask = 2147483647; 312 | files = ( 313 | ); 314 | inputPaths = ( 315 | ); 316 | name = "Check Pods Manifest.lock"; 317 | outputPaths = ( 318 | ); 319 | runOnlyForDeploymentPostprocessing = 0; 320 | shellPath = /bin/sh; 321 | shellScript = "diff \"${PODS_ROOT}/../Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [[ $? != 0 ]] ; then\n cat << EOM\nerror: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\nEOM\n exit 1\nfi\n"; 322 | showEnvVarsInLog = 0; 323 | }; 324 | /* End PBXShellScriptBuildPhase section */ 325 | 326 | /* Begin PBXSourcesBuildPhase section */ 327 | 59684AC815F9DF500063CF5E /* Sources */ = { 328 | isa = PBXSourcesBuildPhase; 329 | buildActionMask = 2147483647; 330 | files = ( 331 | E5A9453418F2AB2F0076DCE8 /* Emitter+Selectors.m in Sources */, 332 | E5A9453218F2AB2F0076DCE8 /* Emitter+Blocks.m in Sources */, 333 | E5682C9C18B52D6D00F5D583 /* NSInvocation+BlockArguments.m in Sources */, 334 | ); 335 | runOnlyForDeploymentPostprocessing = 0; 336 | }; 337 | 59684ADB15F9DF500063CF5E /* Sources */ = { 338 | isa = PBXSourcesBuildPhase; 339 | buildActionMask = 2147483647; 340 | files = ( 341 | 59684AEF15F9DF500063CF5E /* Emitter+BlocksTests.m in Sources */, 342 | E578156118F3FA7400698CFA /* Emitter+SelectorsTests.m in Sources */, 343 | ); 344 | runOnlyForDeploymentPostprocessing = 0; 345 | }; 346 | /* End PBXSourcesBuildPhase section */ 347 | 348 | /* Begin PBXTargetDependency section */ 349 | 59684AE515F9DF500063CF5E /* PBXTargetDependency */ = { 350 | isa = PBXTargetDependency; 351 | target = 59684ACB15F9DF500063CF5E /* Emitter */; 352 | targetProxy = 59684AE415F9DF500063CF5E /* PBXContainerItemProxy */; 353 | }; 354 | /* End PBXTargetDependency section */ 355 | 356 | /* Begin XCBuildConfiguration section */ 357 | 59684AF015F9DF500063CF5E /* Debug */ = { 358 | isa = XCBuildConfiguration; 359 | buildSettings = { 360 | ALWAYS_SEARCH_USER_PATHS = NO; 361 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 362 | CLANG_CXX_LIBRARY = "libc++"; 363 | CLANG_ENABLE_OBJC_ARC = YES; 364 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 365 | COPY_PHASE_STRIP = NO; 366 | GCC_C_LANGUAGE_STANDARD = gnu99; 367 | GCC_DYNAMIC_NO_PIC = NO; 368 | GCC_ENABLE_OBJC_EXCEPTIONS = YES; 369 | GCC_OPTIMIZATION_LEVEL = 0; 370 | GCC_PREPROCESSOR_DEFINITIONS = ( 371 | "DEBUG=1", 372 | "$(inherited)", 373 | ); 374 | GCC_SYMBOLS_PRIVATE_EXTERN = NO; 375 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 376 | GCC_WARN_ABOUT_RETURN_TYPE = YES; 377 | GCC_WARN_UNINITIALIZED_AUTOS = YES; 378 | GCC_WARN_UNUSED_VARIABLE = YES; 379 | MACOSX_DEPLOYMENT_TARGET = 10.8; 380 | ONLY_ACTIVE_ARCH = YES; 381 | SDKROOT = macosx; 382 | }; 383 | name = Debug; 384 | }; 385 | 59684AF115F9DF500063CF5E /* Release */ = { 386 | isa = XCBuildConfiguration; 387 | buildSettings = { 388 | ALWAYS_SEARCH_USER_PATHS = NO; 389 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 390 | CLANG_CXX_LIBRARY = "libc++"; 391 | CLANG_ENABLE_OBJC_ARC = YES; 392 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 393 | COPY_PHASE_STRIP = YES; 394 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 395 | GCC_C_LANGUAGE_STANDARD = gnu99; 396 | GCC_ENABLE_OBJC_EXCEPTIONS = YES; 397 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 398 | GCC_WARN_ABOUT_RETURN_TYPE = YES; 399 | GCC_WARN_UNINITIALIZED_AUTOS = YES; 400 | GCC_WARN_UNUSED_VARIABLE = YES; 401 | MACOSX_DEPLOYMENT_TARGET = 10.8; 402 | SDKROOT = macosx; 403 | }; 404 | name = Release; 405 | }; 406 | 59684AF315F9DF500063CF5E /* Debug */ = { 407 | isa = XCBuildConfiguration; 408 | baseConfigurationReference = 33715D2D81AB49FB8082B0CE /* Pods-EmitterTests.xcconfig */; 409 | buildSettings = { 410 | COMBINE_HIDPI_IMAGES = YES; 411 | DYLIB_COMPATIBILITY_VERSION = 1; 412 | DYLIB_CURRENT_VERSION = 1; 413 | FRAMEWORK_SEARCH_PATHS = ( 414 | "$(inherited)", 415 | "$(DEVELOPER_LIBRARY_DIR)/Frameworks", 416 | "$(PLATFORM_DIR)/Developer/Library/Frameworks", 417 | ); 418 | PRODUCT_NAME = Emitter; 419 | }; 420 | name = Debug; 421 | }; 422 | 59684AF415F9DF500063CF5E /* Release */ = { 423 | isa = XCBuildConfiguration; 424 | baseConfigurationReference = 33715D2D81AB49FB8082B0CE /* Pods-EmitterTests.xcconfig */; 425 | buildSettings = { 426 | COMBINE_HIDPI_IMAGES = YES; 427 | DYLIB_COMPATIBILITY_VERSION = 1; 428 | DYLIB_CURRENT_VERSION = 1; 429 | FRAMEWORK_SEARCH_PATHS = ( 430 | "$(inherited)", 431 | "$(DEVELOPER_LIBRARY_DIR)/Frameworks", 432 | "$(PLATFORM_DIR)/Developer/Library/Frameworks", 433 | ); 434 | PRODUCT_NAME = Emitter; 435 | }; 436 | name = Release; 437 | }; 438 | 59684AF615F9DF500063CF5E /* Debug */ = { 439 | isa = XCBuildConfiguration; 440 | baseConfigurationReference = 33715D2D81AB49FB8082B0CE /* Pods-EmitterTests.xcconfig */; 441 | buildSettings = { 442 | COMBINE_HIDPI_IMAGES = YES; 443 | FRAMEWORK_SEARCH_PATHS = ( 444 | "$(DEVELOPER_LIBRARY_DIR)/Frameworks", 445 | "$(PLATFORM_DIR)/Developer/Library/Frameworks", 446 | "$(DEVELOPER_DIR)/Platforms/MacOSX.platform/Developer/Library/Frameworks", 447 | ); 448 | GCC_PRECOMPILE_PREFIX_HEADER = YES; 449 | GCC_PREFIX_HEADER = "$(PROJECT_DIR)/EmitterTests/Emitter-Prefix.pch"; 450 | OTHER_LDFLAGS = ( 451 | "-ObjC", 452 | "-lffi", 453 | "-framework", 454 | Foundation, 455 | "-framework", 456 | XCTest, 457 | ); 458 | PRODUCT_NAME = EmitterTests; 459 | WRAPPER_EXTENSION = xctest; 460 | }; 461 | name = Debug; 462 | }; 463 | 59684AF715F9DF500063CF5E /* Release */ = { 464 | isa = XCBuildConfiguration; 465 | baseConfigurationReference = 33715D2D81AB49FB8082B0CE /* Pods-EmitterTests.xcconfig */; 466 | buildSettings = { 467 | COMBINE_HIDPI_IMAGES = YES; 468 | FRAMEWORK_SEARCH_PATHS = ( 469 | "$(DEVELOPER_LIBRARY_DIR)/Frameworks", 470 | "$(PLATFORM_DIR)/Developer/Library/Frameworks", 471 | "$(DEVELOPER_DIR)/Platforms/MacOSX.platform/Developer/Library/Frameworks", 472 | ); 473 | GCC_PRECOMPILE_PREFIX_HEADER = YES; 474 | GCC_PREFIX_HEADER = "$(PROJECT_DIR)/EmitterTests/Emitter-Prefix.pch"; 475 | OTHER_LDFLAGS = ( 476 | "-ObjC", 477 | "-lffi", 478 | "-framework", 479 | Foundation, 480 | "-framework", 481 | XCTest, 482 | ); 483 | PRODUCT_NAME = EmitterTests; 484 | WRAPPER_EXTENSION = xctest; 485 | }; 486 | name = Release; 487 | }; 488 | /* End XCBuildConfiguration section */ 489 | 490 | /* Begin XCConfigurationList section */ 491 | 59684AC615F9DF500063CF5E /* Build configuration list for PBXProject "Emitter" */ = { 492 | isa = XCConfigurationList; 493 | buildConfigurations = ( 494 | 59684AF015F9DF500063CF5E /* Debug */, 495 | 59684AF115F9DF500063CF5E /* Release */, 496 | ); 497 | defaultConfigurationIsVisible = 0; 498 | defaultConfigurationName = Release; 499 | }; 500 | 59684AF215F9DF500063CF5E /* Build configuration list for PBXNativeTarget "Emitter" */ = { 501 | isa = XCConfigurationList; 502 | buildConfigurations = ( 503 | 59684AF315F9DF500063CF5E /* Debug */, 504 | 59684AF415F9DF500063CF5E /* Release */, 505 | ); 506 | defaultConfigurationIsVisible = 0; 507 | defaultConfigurationName = Release; 508 | }; 509 | 59684AF515F9DF500063CF5E /* Build configuration list for PBXNativeTarget "EmitterTests" */ = { 510 | isa = XCConfigurationList; 511 | buildConfigurations = ( 512 | 59684AF615F9DF500063CF5E /* Debug */, 513 | 59684AF715F9DF500063CF5E /* Release */, 514 | ); 515 | defaultConfigurationIsVisible = 0; 516 | defaultConfigurationName = Release; 517 | }; 518 | /* End XCConfigurationList section */ 519 | }; 520 | rootObject = 59684AC315F9DF500063CF5E /* Project object */; 521 | } 522 | -------------------------------------------------------------------------------- /Emitter.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Emitter.xcodeproj/xcshareddata/xcschemes/Emitter.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 29 | 35 | 36 | 37 | 38 | 39 | 44 | 45 | 47 | 53 | 54 | 55 | 56 | 57 | 66 | 67 | 68 | 69 | 75 | 76 | 78 | 79 | 82 | 83 | 84 | -------------------------------------------------------------------------------- /Emitter.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /Emitter/Emitter+Blocks.h: -------------------------------------------------------------------------------- 1 | // 2 | // Emitter+Blocks.h 3 | // Emitter 4 | // 5 | // Created by Nuno Sousa on 2/1/14. 6 | // Copyright (c) 2014 Seegno. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface NSObject(EmitterBlocks) 12 | 13 | /** 14 | * Adds a listener to the end of the listeners array for the specified event. 15 | * 16 | * @param event The event. 17 | * @param listener A block to be called when the event is later emitted. 18 | */ 19 | - (void)addListener:(id)event listener:(id)listener; 20 | 21 | /** 22 | * Adds a listener to the end of the listeners array for the specified event. 23 | * 24 | * @param event The event. 25 | * @param listener A block to be called when the event is later emitted. 26 | */ 27 | - (void)on:(id)event listener:(id)listener; 28 | 29 | /** 30 | * Adds a one time listener for the event. This listener is invoked only the next time the event is fired, after which it is removed. 31 | * 32 | * @param event The event. 33 | * @param listener A block to be called when the event is later emitted. 34 | */ 35 | - (void)once:(id)event listener:(id)listener; 36 | 37 | /** 38 | * Remove a listener from the listener array for the specified event. 39 | * 40 | * @param event The event. 41 | * @param listener A reference to a block already registered with this event. 42 | */ 43 | - (void)removeListener:(id)event listener:(id)listener; 44 | 45 | /** 46 | * Removes all listeners of the specified event. 47 | * 48 | * @param event The event. 49 | */ 50 | - (void)removeAllListeners:(id)event; 51 | 52 | /** 53 | * Removes all listeners. 54 | */ 55 | - (void)removeAllListeners; 56 | 57 | /** 58 | * Execute each of the listeners, in order, with the supplied arguments. 59 | * 60 | * @param event The event. 61 | * @param va_args A list of arguments to be passed to the block. 62 | */ 63 | - (void)emit:(id)event, ...; 64 | 65 | /** 66 | * Execute each of the listeners, in order, with the supplied arguments as an NSArray. 67 | * 68 | * @param event The event. 69 | * @param NSArray An array of arguments to be passed to the block. 70 | */ 71 | - (void)emit:(id)event args:(NSArray *)args; 72 | 73 | @end 74 | -------------------------------------------------------------------------------- /Emitter/Emitter+Blocks.m: -------------------------------------------------------------------------------- 1 | // 2 | // Emitter+Blocks.m 3 | // Emitter 4 | // 5 | // Created by Nuno Sousa on 2/1/14. 6 | // Copyright (c) 2014 Seegno. All rights reserved. 7 | // 8 | 9 | #import 10 | #import 11 | 12 | #import "NSInvocation+BlockArguments.h" 13 | #import "Emitter+Blocks.h" 14 | 15 | @implementation NSObject(EmitterBlocks) 16 | 17 | - (NSMutableDictionary *)eventListeners 18 | { 19 | @synchronized(self) 20 | { 21 | NSMutableDictionary *_eventListeners = objc_getAssociatedObject(self, @"eventListeners"); 22 | 23 | if (!_eventListeners) { 24 | _eventListeners = [[NSMutableDictionary alloc] init]; 25 | 26 | objc_setAssociatedObject(self, @"eventListeners", _eventListeners, OBJC_ASSOCIATION_RETAIN_NONATOMIC); 27 | } 28 | 29 | return _eventListeners; 30 | } 31 | } 32 | 33 | - (void)addListener:(id)event listener:(id)listener once:(BOOL)once 34 | { 35 | if (!self.eventListeners[event]) { 36 | self.eventListeners[event] = [[NSMutableDictionary alloc] init]; 37 | } 38 | 39 | self.eventListeners[event][listener] = @(once); 40 | } 41 | 42 | - (void)addListener:(id)event listener:(id)listener 43 | { 44 | [self addListener:event listener:listener once:NO]; 45 | } 46 | 47 | - (void)on:(id)event listener:(id)listener 48 | { 49 | [self addListener:event listener:listener once:NO]; 50 | } 51 | 52 | - (void)once:(id)event listener:(id)listener 53 | { 54 | [self addListener:event listener:listener once:YES]; 55 | } 56 | 57 | - (void)removeListener:(id)event listener:(id)listener 58 | { 59 | if (!listener) { 60 | return; 61 | } 62 | 63 | [self.eventListeners[event] removeObjectForKey:listener]; 64 | } 65 | 66 | - (void)removeAllListeners:(id)event 67 | { 68 | for (id listener in [self.eventListeners[event] copy]) { 69 | [self removeListener:event listener:listener]; 70 | } 71 | } 72 | 73 | - (void)removeAllListeners 74 | { 75 | for (id event in [self.eventListeners copy]) { 76 | [self removeAllListeners:event]; 77 | } 78 | } 79 | 80 | - (void)emit:(id)event, ... 81 | { 82 | va_list args; 83 | va_start(args, event); 84 | 85 | [self emit:event vargs:args]; 86 | 87 | va_end(args); 88 | } 89 | 90 | - (void)emit:(id)event args:(NSArray *)args 91 | { 92 | NSDictionary *listeners = [self.eventListeners[event] copy]; 93 | 94 | for (id listener in listeners) { 95 | A2BlockInvocation *blockInvocation = [[A2BlockInvocation alloc] initWithBlock:listener]; 96 | NSMethodSignature *signature = blockInvocation.methodSignature; 97 | NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature]; 98 | 99 | for (int i=0; i < MIN(signature.numberOfArguments-2, args.count); i++) { 100 | id arg = args[i]; 101 | 102 | [invocation setArgument:&arg atIndex:i+2]; 103 | } 104 | 105 | [blockInvocation invokeWithInvocation:invocation]; 106 | 107 | // Remove events that are only scheduled to execute once 108 | if (YES == [listeners[listener] boolValue]) { 109 | [self removeListener:event listener:listener]; 110 | } 111 | } 112 | } 113 | 114 | - (void)emit:(id)event vargs:(va_list)args 115 | { 116 | NSDictionary *listeners = [self.eventListeners[event] copy]; 117 | 118 | for (id listener in listeners) { 119 | A2BlockInvocation *blockInvocation = [[A2BlockInvocation alloc] initWithBlock:listener]; 120 | NSMethodSignature *signature = blockInvocation.methodSignature; 121 | NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature]; 122 | 123 | [invocation setArgumentsFromArgumentList:args]; 124 | 125 | [blockInvocation invokeWithInvocation:invocation]; 126 | 127 | // Remove events that are only scheduled to execute once 128 | if (YES == [listeners[listener] boolValue]) { 129 | [self removeListener:event listener:listener]; 130 | } 131 | } 132 | } 133 | 134 | @end 135 | -------------------------------------------------------------------------------- /Emitter/Emitter+Selectors.h: -------------------------------------------------------------------------------- 1 | // 2 | // Emitter+Selectors.h 3 | // Emitter 4 | // 5 | // Created by Nuno Sousa on 2/1/14. 6 | // Copyright (c) 2014 Seegno. All rights reserved. 7 | // 8 | 9 | @interface NSObject(EmitterSelectors) 10 | 11 | /** 12 | * Adds an event listener that will trigger a selector on a given target. 13 | * 14 | * @param event The event to listen to. 15 | * @param selector The selector that will be called. 16 | * @param target The target object. 17 | */ 18 | - (void)addListener:(id)event selector:(SEL)selector target:(__weak id)target; 19 | 20 | /** 21 | * Adds an event listener that will trigger a selector on a given target. 22 | * 23 | * @param event The event to listen to. 24 | * @param selector The selector that will be called. 25 | * @param target The target object. 26 | */ 27 | - (void)on:(id)event selector:(SEL)selector target:(__weak id)target; 28 | 29 | /** 30 | * Adds an event listener that will only trigger a selector on a given target once. 31 | * 32 | * @param event The event to listen to. 33 | * @param selector The selector that will be called. 34 | * @param target The target object. 35 | */ 36 | - (void)once:(id)event selector:(SEL)selector target:(__weak id)target; 37 | 38 | /** 39 | * Removes an event listener for a selector on a given target. 40 | * 41 | * @param event The event to remove. 42 | * @param selector The selector. 43 | * @param target The target. 44 | */ 45 | - (void)removeListener:(id)event selector:(SEL)selector target:(__weak id)target; 46 | 47 | @end 48 | -------------------------------------------------------------------------------- /Emitter/Emitter+Selectors.m: -------------------------------------------------------------------------------- 1 | // 2 | // Emitter+Selectors.m 3 | // Emitter 4 | // 5 | // Created by Nuno Sousa on 2/1/14. 6 | // Copyright (c) 2014 Seegno. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | #import "Emitter+Blocks.h" 12 | #import "Emitter+Selectors.h" 13 | 14 | @implementation NSObject(EmitterEmitterBlocks) 15 | 16 | - (NSMutableDictionary *)eventSelectors 17 | { 18 | @synchronized(self) 19 | { 20 | NSMutableDictionary *_eventSelectors = objc_getAssociatedObject(self, @"eventSelectors"); 21 | 22 | if (!_eventSelectors) { 23 | _eventSelectors = [[NSMutableDictionary alloc] init]; 24 | 25 | objc_setAssociatedObject(self, @"eventSelectors", _eventSelectors, OBJC_ASSOCIATION_RETAIN_NONATOMIC); 26 | } 27 | 28 | return _eventSelectors; 29 | } 30 | } 31 | 32 | - (void)addListener:(id)event selector:(SEL)selector target:(__weak id)target once:(BOOL)once 33 | { 34 | if ([self selectorWithEvent:event selector:selector target:target]) { 35 | return; 36 | } 37 | 38 | id block; 39 | id __weak __block weakBlock; 40 | 41 | weakBlock = block = ^(void *first, void *second, void *third, void *fourth, void *fifth, void *sixth, void *seventh, void *eighth, void *nineth, void *tenth) { 42 | if (nil == target) { 43 | [self removeListener:event listener:weakBlock]; 44 | 45 | return; 46 | } 47 | 48 | NSMethodSignature *signature = [target methodSignatureForSelector:selector]; 49 | NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature]; 50 | 51 | [invocation setTarget:target]; 52 | [invocation setSelector:selector]; 53 | 54 | void *args[] = { first, second, third, fourth, fifth, sixth, seventh, eighth, nineth, tenth }; 55 | 56 | // Set all arguments (skip the first two arguments -- `self` and `_cmd`) 57 | for (int i=2; i < signature.numberOfArguments; i++) { 58 | [invocation setArgument:&(args[i-2]) atIndex:i]; 59 | } 60 | 61 | [invocation invoke]; 62 | }; 63 | 64 | if (! self.eventSelectors[event]) { 65 | self.eventSelectors[event] = [[NSMutableArray alloc] init]; 66 | } 67 | 68 | [self.eventSelectors[event] addObject:@{ 69 | @"selector": NSStringFromSelector(selector), 70 | @"target": [NSValue valueWithNonretainedObject:target], 71 | @"block": block 72 | }]; 73 | 74 | if (once) { 75 | [self once:event listener:block]; 76 | } else { 77 | [self on:event listener:block]; 78 | } 79 | } 80 | 81 | - (void)addListener:(id)event selector:(SEL)selector target:(__weak id)target 82 | { 83 | [self addListener:event selector:selector target:target once:NO]; 84 | } 85 | 86 | - (void)on:(id)event selector:(SEL)selector target:(__weak id)target 87 | { 88 | [self addListener:event selector:selector target:target once:NO]; 89 | } 90 | 91 | - (void)once:(id)event selector:(SEL)selector target:(__weak id)target 92 | { 93 | [self addListener:event selector:selector target:target once:YES]; 94 | } 95 | 96 | - (void)removeListener:(id)event selector:(SEL)selector target:(__weak id)target 97 | { 98 | NSDictionary *listener = [self selectorWithEvent:event selector:selector target:target]; 99 | 100 | [self removeListener:event listener:listener[@"block"]]; 101 | 102 | [self.eventSelectors[event] removeObject:listener]; 103 | 104 | if (0 == [self.eventSelectors[event] count]) { 105 | [self.eventSelectors removeObjectForKey:event]; 106 | } 107 | } 108 | 109 | - (NSDictionary *)selectorWithEvent:(id)event selector:(SEL)selector target:(id)target 110 | { 111 | NSUInteger index = [self.eventSelectors[event] indexOfObjectPassingTest:^BOOL(NSDictionary *obj, NSUInteger idx, BOOL *stop) { 112 | BOOL isSameTarget = [[NSValue valueWithNonretainedObject:target] isEqualToValue:obj[@"target"]]; 113 | BOOL isSameSelector = [NSStringFromSelector(selector) isEqualToString:obj[@"selector"]]; 114 | 115 | return isSameTarget && isSameSelector; 116 | }]; 117 | 118 | if (index == NSNotFound) { 119 | return nil; 120 | } 121 | 122 | return self.eventSelectors[event][index]; 123 | } 124 | 125 | @end 126 | -------------------------------------------------------------------------------- /Emitter/Emitter.h: -------------------------------------------------------------------------------- 1 | // 2 | // Emitter.h 3 | // Emitter 4 | // 5 | // Created by Nuno Sousa on 2/1/14. 6 | // Copyright (c) 2014 Seegno. All rights reserved. 7 | // 8 | 9 | #import "Emitter+Blocks.h" 10 | #import "Emitter+Selectors.h" 11 | -------------------------------------------------------------------------------- /Emitter/NSInvocation+BlockArguments.h: -------------------------------------------------------------------------------- 1 | // 2 | // NSInvocation+BlockArguments.h 3 | // Emitter 4 | // 5 | // Created by Nuno Sousa on 19/02/14. 6 | // Copyright (c) 2014 Seegno. All rights reserved. 7 | // 8 | // Original implementation from libextobjc: https://github.com/jspahrsummers/libextobjc 9 | // 10 | 11 | #import 12 | 13 | @interface NSInvocation (BlockArguments) 14 | 15 | - (BOOL)setArgumentsFromArgumentList:(va_list)cargs; 16 | 17 | @end 18 | -------------------------------------------------------------------------------- /Emitter/NSInvocation+BlockArguments.m: -------------------------------------------------------------------------------- 1 | // 2 | // NSInvocation+BlockArguments.m 3 | // Emitter 4 | // 5 | // Created by Nuno Sousa on 19/02/14. 6 | // Copyright (c) 2014 Seegno. All rights reserved. 7 | // 8 | // Original implementation from libextobjc: https://github.com/jspahrsummers/libextobjc 9 | // 10 | 11 | #import "NSInvocation+BlockArguments.h" 12 | 13 | typedef struct { int i; } *empty_struct_ptr_t; 14 | typedef union { int i; } *empty_union_ptr_t; 15 | 16 | @implementation NSInvocation (BlockArguments) 17 | 18 | - (BOOL)setArgumentsFromArgumentList:(va_list)args 19 | { 20 | va_list cargs; 21 | 22 | va_copy(cargs, args); 23 | 24 | NSMethodSignature *signature = [self methodSignature]; 25 | NSUInteger skip = 2; 26 | NSUInteger count = [signature numberOfArguments]; 27 | 28 | for (NSUInteger i = skip;i < count;++i) { 29 | const char *type = [signature getArgumentTypeAtIndex:i]; 30 | while ( 31 | *type == 'r' || 32 | *type == 'n' || 33 | *type == 'N' || 34 | *type == 'o' || 35 | *type == 'O' || 36 | *type == 'R' || 37 | *type == 'V' 38 | ) { 39 | ++type; 40 | } 41 | 42 | switch (*type) { 43 | case 'c': 44 | { 45 | char val = (char)va_arg(cargs, int); 46 | [self setArgument:&val atIndex:i]; 47 | } 48 | 49 | break; 50 | 51 | case 'i': 52 | { 53 | int val = va_arg(cargs, int); 54 | [self setArgument:&val atIndex:i]; 55 | } 56 | 57 | break; 58 | 59 | case 's': 60 | { 61 | short val = (short)va_arg(cargs, int); 62 | [self setArgument:&val atIndex:i]; 63 | } 64 | 65 | break; 66 | 67 | case 'l': 68 | { 69 | long val = va_arg(cargs, long); 70 | [self setArgument:&val atIndex:i]; 71 | } 72 | 73 | break; 74 | 75 | case 'q': 76 | { 77 | long long val = va_arg(cargs, long long); 78 | [self setArgument:&val atIndex:i]; 79 | } 80 | 81 | break; 82 | 83 | case 'C': 84 | { 85 | unsigned char val = (unsigned char)va_arg(cargs, unsigned int); 86 | [self setArgument:&val atIndex:i]; 87 | } 88 | 89 | break; 90 | 91 | case 'I': 92 | { 93 | unsigned int val = va_arg(cargs, unsigned int); 94 | [self setArgument:&val atIndex:i]; 95 | } 96 | 97 | break; 98 | 99 | case 'S': 100 | { 101 | unsigned short val = (unsigned short)va_arg(cargs, unsigned int); 102 | [self setArgument:&val atIndex:i]; 103 | } 104 | 105 | break; 106 | 107 | case 'L': 108 | { 109 | unsigned long val = va_arg(cargs, unsigned long); 110 | [self setArgument:&val atIndex:i]; 111 | } 112 | 113 | break; 114 | 115 | case 'Q': 116 | { 117 | unsigned long long val = va_arg(cargs, unsigned long long); 118 | [self setArgument:&val atIndex:i]; 119 | } 120 | 121 | break; 122 | 123 | case 'f': 124 | { 125 | float val = (float)va_arg(cargs, double); 126 | [self setArgument:&val atIndex:i]; 127 | } 128 | 129 | break; 130 | 131 | case 'd': 132 | { 133 | double val = va_arg(cargs, double); 134 | [self setArgument:&val atIndex:i]; 135 | } 136 | 137 | break; 138 | 139 | case 'B': 140 | { 141 | _Bool val = (_Bool)va_arg(cargs, int); 142 | [self setArgument:&val atIndex:i]; 143 | } 144 | 145 | break; 146 | 147 | case '*': 148 | { 149 | char *val = va_arg(cargs, char *); 150 | [self setArgument:&val atIndex:i]; 151 | } 152 | 153 | break; 154 | 155 | case '@': 156 | { 157 | __unsafe_unretained id val = va_arg(cargs, id); 158 | [self setArgument:&val atIndex:i]; 159 | 160 | if (type[1] == '?') { 161 | // @? is undocumented, but apparently used to represent 162 | // a block -- not sure how to disambiguate it from 163 | // a separate @ and ?, but I assume that a block parameter 164 | // is a more common case than that 165 | ++type; 166 | } 167 | } 168 | 169 | break; 170 | 171 | case '#': 172 | { 173 | Class val = va_arg(cargs, Class); 174 | [self setArgument:&val atIndex:i]; 175 | } 176 | 177 | break; 178 | 179 | case ':': 180 | { 181 | SEL val = va_arg(cargs, SEL); 182 | [self setArgument:&val atIndex:i]; 183 | } 184 | 185 | break; 186 | 187 | case '[': 188 | NSLog(@"Unexpected array within method argument type code \"%s\", cannot set invocation argument!", type); 189 | va_end(cargs); 190 | return NO; 191 | 192 | case 'b': 193 | NSLog(@"Unexpected bitfield within method argument type code \"%s\", cannot set invocation argument!", type); 194 | va_end(cargs); 195 | return NO; 196 | 197 | case '{': 198 | NSLog(@"Cannot get variable argument for a method that takes a struct argument!"); 199 | va_end(cargs); 200 | return NO; 201 | 202 | case '(': 203 | NSLog(@"Cannot get variable argument for a method that takes a union argument!"); 204 | va_end(cargs); 205 | return NO; 206 | 207 | case '^': 208 | switch (type[1]) { 209 | case 'c': 210 | case 'C': 211 | { 212 | char *val = va_arg(cargs, char *); 213 | [self setArgument:&val atIndex:i]; 214 | } 215 | 216 | break; 217 | 218 | case 'i': 219 | case 'I': 220 | { 221 | int *val = va_arg(cargs, int *); 222 | [self setArgument:&val atIndex:i]; 223 | } 224 | 225 | break; 226 | 227 | case 's': 228 | case 'S': 229 | { 230 | short *val = va_arg(cargs, short *); 231 | [self setArgument:&val atIndex:i]; 232 | } 233 | 234 | break; 235 | 236 | case 'l': 237 | case 'L': 238 | { 239 | long *val = va_arg(cargs, long *); 240 | [self setArgument:&val atIndex:i]; 241 | } 242 | 243 | break; 244 | 245 | case 'q': 246 | case 'Q': 247 | { 248 | long long *val = va_arg(cargs, long long *); 249 | [self setArgument:&val atIndex:i]; 250 | } 251 | 252 | break; 253 | 254 | case 'f': 255 | { 256 | float *val = va_arg(cargs, float *); 257 | [self setArgument:&val atIndex:i]; 258 | } 259 | 260 | break; 261 | 262 | case 'd': 263 | { 264 | double *val = va_arg(cargs, double *); 265 | [self setArgument:&val atIndex:i]; 266 | } 267 | 268 | break; 269 | 270 | case 'B': 271 | { 272 | _Bool *val = va_arg(cargs, _Bool *); 273 | [self setArgument:&val atIndex:i]; 274 | } 275 | 276 | break; 277 | 278 | case 'v': 279 | { 280 | void *val = va_arg(cargs, void *); 281 | [self setArgument:&val atIndex:i]; 282 | } 283 | 284 | break; 285 | 286 | case '*': 287 | case '@': 288 | case '#': 289 | case '^': 290 | case '[': 291 | { 292 | void **val = va_arg(cargs, void **); 293 | [self setArgument:&val atIndex:i]; 294 | } 295 | 296 | break; 297 | 298 | case ':': 299 | { 300 | SEL *val = va_arg(cargs, SEL *); 301 | [self setArgument:&val atIndex:i]; 302 | } 303 | 304 | break; 305 | 306 | case '{': 307 | { 308 | empty_struct_ptr_t val = va_arg(cargs, empty_struct_ptr_t); 309 | [self setArgument:&val atIndex:i]; 310 | } 311 | 312 | break; 313 | 314 | case '(': 315 | { 316 | empty_union_ptr_t val = va_arg(cargs, empty_union_ptr_t); 317 | [self setArgument:&val atIndex:i]; 318 | } 319 | 320 | break; 321 | 322 | case '?': 323 | { 324 | // assume that this is a pointer to a function pointer 325 | // 326 | // even if it's not, the fact that it's 327 | // a pointer-to-something gives us a good chance of not 328 | // causing alignment or size problems 329 | IMP *ptr = va_arg(cargs, IMP *); 330 | [self setArgument:&ptr atIndex:i]; 331 | } 332 | 333 | break; 334 | 335 | case 'b': 336 | default: 337 | NSLog(@"Pointer to unexpected type within method argument type code \"%s\", cannot set method invocation!", type); 338 | va_end(cargs); 339 | return NO; 340 | } 341 | 342 | break; 343 | 344 | case '?': 345 | { 346 | // this is PROBABLY a function pointer, but the documentation 347 | // leaves room open for uncertainty, so at least log a message 348 | NSLog(@"Assuming method argument type code \"%s\" is a function pointer", type); 349 | 350 | IMP ptr = va_arg(cargs, IMP); 351 | [self setArgument:&ptr atIndex:i]; 352 | } 353 | 354 | break; 355 | 356 | default: 357 | NSLog(@"Unexpected method argument type code \"%s\", cannot set method invocation!", type); 358 | va_end(cargs); 359 | return NO; 360 | } 361 | } 362 | 363 | va_end(cargs); 364 | 365 | return YES; 366 | } 367 | 368 | @end 369 | -------------------------------------------------------------------------------- /EmitterTests/Emitter+BlocksTests.m: -------------------------------------------------------------------------------- 1 | // 2 | // Emitter+BlocksTests.m 3 | // Emitter 4 | // 5 | // Created by Nuno Sousa on 2/1/14. 6 | // Copyright (c) 2014 Seegno. All rights reserved. 7 | // 8 | 9 | #import "Emitter.h" 10 | 11 | SpecBegin(EmitterBlocks) 12 | 13 | __block NSObject *emitter; 14 | 15 | beforeEach(^{ 16 | emitter = [[NSObject alloc] init]; 17 | }); 18 | 19 | describe(@"emit:", ^{ 20 | it(@"notifies listener", ^AsyncBlock { 21 | [emitter on:@"key" listener:^{ 22 | done(); 23 | }]; 24 | 25 | [emitter emit:@"key"]; 26 | }); 27 | 28 | it(@"notifies listener multiple times", ^{ 29 | __block int times = 0; 30 | 31 | [emitter on:@"key" listener:^{ 32 | times++; 33 | }]; 34 | 35 | [emitter emit:@"key"]; 36 | [emitter emit:@"key"]; 37 | 38 | expect(times).to.equal(2); 39 | }); 40 | 41 | it(@"notifies listener only once", ^{ 42 | __block int times = 0; 43 | 44 | [emitter once:@"key" listener:^{ 45 | times++; 46 | }]; 47 | 48 | [emitter emit:@"key"]; 49 | [emitter emit:@"key"]; 50 | 51 | expect(times).to.equal(1); 52 | }); 53 | 54 | it(@"notifies listener with one parameter", ^AsyncBlock { 55 | NSArray *param1 = @[@"one", @"two"]; 56 | 57 | [emitter on:@"key" listener:^(NSArray *value) { 58 | expect(value).to.equal(param1); 59 | 60 | done(); 61 | }]; 62 | 63 | [emitter emit:@"key", param1]; 64 | }); 65 | 66 | it(@"notifies listener with two parameters", ^AsyncBlock { 67 | [emitter on:@"key" listener:^(BOOL param1, NSString *param2) { 68 | expect(param1).to.equal(YES); 69 | expect(param2).to.equal(@"two"); 70 | 71 | done(); 72 | }]; 73 | 74 | [emitter emit:@"key", YES, @"two"]; 75 | }); 76 | 77 | it(@"notifies listener with two parameters multiple times", ^AsyncBlock { 78 | NSString *param = @"example-parameter"; 79 | 80 | __block int times = 0; 81 | __block int expected = 1000; 82 | 83 | [emitter on:@"key" listener:^(NSString *value, int i) { 84 | expect(value).to.equal(param); 85 | expect(i).to.equal(times); 86 | 87 | if (++times == expected) { 88 | done(); 89 | }; 90 | }]; 91 | 92 | for (int i=0; i < expected; i++) { 93 | [emitter emit:@"key", param, i]; 94 | } 95 | }); 96 | 97 | it(@"notifies listener and ignores extra parameters", ^AsyncBlock { 98 | [emitter on:@"key" listener:^(int param1, BOOL param2){ 99 | done(); 100 | }]; 101 | 102 | [emitter emit:@"key"]; 103 | }); 104 | 105 | it(@"notifies listener and ignores extra parameters when using array args", ^AsyncBlock { 106 | [emitter on:@"key" listener:^(NSNumber *param1, NSString *param2){ 107 | done(); 108 | }]; 109 | 110 | [emitter emit:@"key" args:@[@123]]; 111 | }); 112 | 113 | it(@"notifies multiple listeners with one parameter", ^AsyncBlock { 114 | NSString *param = @"example-parameter"; 115 | 116 | __block BOOL listener1; 117 | __block BOOL listener2; 118 | 119 | [emitter on:@"key" listener:^(NSString *value) { 120 | listener1 = YES; 121 | 122 | expect(value).to.equal(param); 123 | 124 | if (listener1 && listener2) { 125 | done(); 126 | } 127 | }]; 128 | 129 | [emitter on:@"key" listener:^(NSString *value) { 130 | listener2 = YES; 131 | 132 | expect(value).to.equal(param); 133 | 134 | if (listener1 && listener2) { 135 | done(); 136 | } 137 | }]; 138 | 139 | [emitter emit:@"key", param]; 140 | }); 141 | 142 | it(@"notifies listener with an array of arguments", ^AsyncBlock { 143 | NSString *param1 = @"example-parameter"; 144 | NSNumber *param2 = @123; 145 | 146 | [emitter on:@"key" listener:^(NSString *value1, NSNumber *value2) { 147 | expect(value1).to.equal(param1); 148 | expect(value2).to.equal(param2); 149 | 150 | done(); 151 | }]; 152 | 153 | [emitter emit:@"key" args:@[param1, param2]]; 154 | }); 155 | }); 156 | 157 | describe(@"removeListener:", ^{ 158 | it(@"removes listener", ^{ 159 | id listener = ^{ 160 | NSAssert(NO, @"listener should not fire"); 161 | }; 162 | 163 | [emitter on:@"key" listener:listener]; 164 | 165 | [emitter removeListener:@"key" listener:listener]; 166 | 167 | [emitter emit:@"key"]; 168 | }); 169 | 170 | it(@"removes listener while iterating listeners", ^{ 171 | id listener = ^{ 172 | NSAssert(NO, @"listener should not fire"); 173 | }; 174 | 175 | [emitter on:@"key" listener:listener]; 176 | 177 | [emitter on:@"key2" listener:^{ 178 | [emitter removeListener:@"key" listener:listener]; 179 | }]; 180 | 181 | [emitter emit:@"key2"]; 182 | [emitter emit:@"key"]; 183 | }); 184 | 185 | it(@"removes all listeners for an event", ^{ 186 | id listener = ^{ 187 | NSAssert(NO, @"listener should not fire"); 188 | }; 189 | 190 | [emitter on:@"key" listener:listener]; 191 | 192 | [emitter removeAllListeners:@"key"]; 193 | 194 | [emitter emit:@"key"]; 195 | }); 196 | 197 | it(@"removes all listeners for multiple events", ^{ 198 | [emitter on:@"key" listener:^{ 199 | NSAssert(NO, @"listener should not fire"); 200 | }]; 201 | 202 | [emitter on:@"key" listener:^{ 203 | NSAssert(NO, @"listener should not fire"); 204 | }]; 205 | 206 | [emitter removeAllListeners:@"key"]; 207 | 208 | [emitter emit:@"key"]; 209 | }); 210 | 211 | it(@"removes all listeners", ^{ 212 | [emitter on:@"key" listener:^{ 213 | NSAssert(NO, @"listener should not fire"); 214 | }]; 215 | 216 | [emitter on:@"key" listener:^{ 217 | NSAssert(NO, @"listener should not fire"); 218 | }]; 219 | 220 | [emitter removeAllListeners]; 221 | 222 | [emitter emit:@"key"]; 223 | }); 224 | }); 225 | 226 | SpecEnd -------------------------------------------------------------------------------- /EmitterTests/Emitter+SelectorsTests.m: -------------------------------------------------------------------------------- 1 | // 2 | // Emitter+SelectorsTests.m 3 | // Emitter 4 | // 5 | // Created by Nuno Sousa on 2/1/14. 6 | // Copyright (c) 2014 Seegno. All rights reserved. 7 | // 8 | 9 | #import "Emitter.h" 10 | 11 | @interface TestObject : NSObject 12 | 13 | - (void)listener; 14 | - (void)listenerWithParam1:(id)value1; 15 | - (void)listenerWithParam1:(id)value1 param2:(id)value2; 16 | 17 | @end 18 | 19 | @implementation TestObject 20 | 21 | - (void)listener {} 22 | - (void)listenerWithParam1:(id)value1 {} 23 | - (void)listenerWithParam1:(id)value1 param2:(id)value2 {} 24 | 25 | @end 26 | 27 | SpecBegin(EmitterSelectors) 28 | 29 | __block NSObject *emitter; 30 | __block TestObject *target1; 31 | __block TestObject *target2; 32 | 33 | beforeEach(^{ 34 | emitter = [[NSObject alloc] init]; 35 | target1 = mock(TestObject.class); 36 | target2 = mock(TestObject.class); 37 | }); 38 | 39 | describe(@"on:", ^{ 40 | it(@"does not retain the `target`", ^{ 41 | expect(CFGetRetainCount((__bridge CFTypeRef)target1)).to.equal(1); 42 | 43 | [emitter on:@"key" selector:@selector(listener) target:target1]; 44 | 45 | expect(CFGetRetainCount((__bridge CFTypeRef)target1)).to.equal(1); 46 | }); 47 | }); 48 | 49 | describe(@"emit:", ^{ 50 | it(@"releases the listener if the `target` is nil", ^{ 51 | expect(CFGetRetainCount((__bridge CFTypeRef)target1)).to.equal(1); 52 | 53 | [emitter on:@"key" selector:@selector(listener) target:target1]; 54 | 55 | target1 = nil; 56 | 57 | [emitter emit:@"key"]; 58 | }); 59 | 60 | it(@"notifies selector", ^{ 61 | [emitter on:@"key" selector:@selector(listener) target:target1]; 62 | 63 | [emitter emit:@"key"]; 64 | 65 | [verify(target1) listener]; 66 | }); 67 | 68 | it(@"notifies listener multiple times", ^{ 69 | [emitter on:@"key" selector:@selector(listener) target:target1]; 70 | 71 | [emitter emit:@"key"]; 72 | [emitter emit:@"key"]; 73 | 74 | [verifyCount(target1, times(2)) listener]; 75 | }); 76 | 77 | it(@"notifies listener only once", ^{ 78 | [emitter once:@"key" selector:@selector(listener) target:target1]; 79 | 80 | [emitter emit:@"key"]; 81 | [emitter emit:@"key"]; 82 | 83 | [verifyCount(target1, times(1)) listener]; 84 | }); 85 | 86 | it(@"notifies listener with one parameter", ^{ 87 | NSArray *param1 = @[@"one", @"two"]; 88 | 89 | [emitter on:@"key" selector:@selector(listenerWithParam1:) target:target1]; 90 | 91 | [emitter emit:@"key", param1]; 92 | 93 | [verify(target1) listenerWithParam1:param1]; 94 | }); 95 | 96 | it(@"notifies listener with two parameters", ^{ 97 | [emitter on:@"key" selector:@selector(listenerWithParam1:param2:) target:target1]; 98 | 99 | [emitter emit:@"key", @1, @"two"]; 100 | 101 | [verify(target1) listenerWithParam1:@1 param2:@"two"]; 102 | }); 103 | 104 | it(@"notifies listener with two parameters multiple times", ^{ 105 | __block int expected = 1000; 106 | 107 | [emitter on:@"key" selector:@selector(listenerWithParam1:param2:) target:target1]; 108 | 109 | for(int i=0; i < expected; i++) { 110 | [emitter emit:@"key", @"example-parameter", @(expected)]; 111 | } 112 | 113 | [verifyCount(target1, times(expected)) listenerWithParam1:@"example-parameter" param2:@(expected)]; 114 | }); 115 | 116 | it(@"notifies multiple listeners with one parameter", ^{ 117 | NSString *param = @"example-parameter"; 118 | 119 | [emitter on:@"key" selector:@selector(listenerWithParam1:) target:target1]; 120 | [emitter on:@"key" selector:@selector(listenerWithParam1:) target:target2]; 121 | 122 | [emitter emit:@"key", param]; 123 | 124 | [verify(target1) listenerWithParam1:param]; 125 | [verify(target2) listenerWithParam1:param]; 126 | }); 127 | 128 | it(@"notifies listener with an array of arguments", ^{ 129 | NSString *param1 = @"example-parameter"; 130 | NSNumber *param2 = @123; 131 | 132 | [emitter on:@"key" selector:@selector(listenerWithParam1:param2:) target:target1]; 133 | 134 | [emitter emit:@"key" args:@[param1, param2]]; 135 | 136 | [verify(target1) listenerWithParam1:param1 param2:param2]; 137 | }); 138 | }); 139 | 140 | describe(@"removeListener:", ^{ 141 | it(@"does not retain the `target`", ^{ 142 | expect(CFGetRetainCount((__bridge CFTypeRef)target1)).to.equal(1); 143 | 144 | [emitter on:@"key" selector:@selector(listener) target:target1]; 145 | [emitter removeListener:@"key" selector:@selector(listener) target:target1]; 146 | 147 | expect(CFGetRetainCount((__bridge CFTypeRef)target1)).to.equal(1); 148 | }); 149 | 150 | it(@"removes listener", ^{ 151 | [emitter on:@"key" selector:@selector(listener) target:target1]; 152 | 153 | [emitter removeListener:@"key" selector:@selector(listener) target:target1]; 154 | 155 | [emitter emit:@"key"]; 156 | 157 | [verifyCount(target1, never()) listener]; 158 | }); 159 | 160 | it(@"removes listener while iterating listeners", ^{ 161 | [emitter on:@"key" selector:@selector(listener) target:target1]; 162 | [emitter on:@"key2" listener:^{ 163 | [emitter removeListener:@"key" selector:@selector(listener) target:target1]; 164 | }]; 165 | 166 | [emitter removeListener:@"key" selector:@selector(listener) target:target1]; 167 | 168 | [emitter emit:@"key2"]; 169 | [emitter emit:@"key"]; 170 | 171 | [verifyCount(target1, never()) listener]; 172 | }); 173 | 174 | it(@"removes all listeners for an event", ^{ 175 | [emitter on:@"key" selector:@selector(listener) target:target1]; 176 | 177 | [emitter removeAllListeners:@"key"]; 178 | 179 | [emitter emit:@"key"]; 180 | 181 | [verifyCount(target1, never()) listener]; 182 | }); 183 | 184 | it(@"removes all listeners for multiple events", ^{ 185 | [emitter on:@"key" selector:@selector(listener) target:target1]; 186 | [emitter on:@"key" selector:@selector(listener) target:target2]; 187 | 188 | [emitter removeAllListeners:@"key"]; 189 | 190 | [emitter emit:@"key"]; 191 | 192 | [verifyCount(target1, never()) listener]; 193 | [verifyCount(target2, never()) listener]; 194 | }); 195 | 196 | it(@"removes all listeners", ^{ 197 | [emitter on:@"key" selector:@selector(listener) target:target1]; 198 | [emitter on:@"key" selector:@selector(listener) target:target2]; 199 | 200 | [emitter removeAllListeners]; 201 | 202 | [emitter emit:@"key"]; 203 | 204 | [verifyCount(target1, never()) listener]; 205 | [verifyCount(target2, never()) listener]; 206 | }); 207 | }); 208 | 209 | SpecEnd -------------------------------------------------------------------------------- /EmitterTests/Emitter-Prefix.pch: -------------------------------------------------------------------------------- 1 | // 2 | // Emitter-Prefix.pch 3 | // Emitter 4 | // 5 | // Created by Nuno Sousa on 2/1/14. 6 | // Copyright (c) 2014 Seegno. All rights reserved. 7 | // 8 | 9 | #define EXP_SHORTHAND 10 | #define MOCKITO_SHORTHAND 11 | 12 | #import 13 | #import 14 | #import -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Seegno 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /Podfile: -------------------------------------------------------------------------------- 1 | platform :osx, '10.9' 2 | inhibit_all_warnings! 3 | 4 | pod 'BlocksKit/DynamicDelegate' 5 | 6 | target :EmitterTests, :exclusive => true do 7 | pod 'BlocksKit/DynamicDelegate' 8 | 9 | pod 'Specta' 10 | pod 'Expecta' 11 | pod 'OCMockito' 12 | end 13 | -------------------------------------------------------------------------------- /Podfile.lock: -------------------------------------------------------------------------------- 1 | PODS: 2 | - BlocksKit/DynamicDelegate (2.2.5) 3 | - Expecta (0.3.1) 4 | - OCHamcrest (4.0.1) 5 | - OCMockito (1.3.0): 6 | - OCHamcrest (~> 4.0) 7 | - TPWeakProxy (~> 1.0) 8 | - Specta (0.2.1) 9 | - TPWeakProxy (1.0.0) 10 | 11 | DEPENDENCIES: 12 | - BlocksKit/DynamicDelegate 13 | - Expecta 14 | - OCMockito 15 | - Specta 16 | 17 | SPEC CHECKSUMS: 18 | BlocksKit: 4439e7f30a9f90743462ca63545a15c1f4304cef 19 | Expecta: 03aabd0a89d8dea843baecb19a7fd7466a69a31d 20 | OCHamcrest: b464725bbb48d0f1cd9c6f4ec3cb35fe0c4f2b94 21 | OCMockito: eca2670b3ce4a457d316b046a36d6f2b980a4da7 22 | Specta: 9141310f46b1f68b676650ff2854e1ed0b74163a 23 | TPWeakProxy: a23251ba7fbe268c93f809cc072ef834f5063798 24 | 25 | COCOAPODS: 0.33.1 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | EventEmitter for Objective-C that implements the NodeJS EventEmitter API as closely as possible. 2 | 3 | The library adds a category to NSObject that allows any object to `emit` or `listen` to events. 4 | 5 | [![Build Status](https://travis-ci.org/seegno/emitter-objc.png)](https://travis-ci.org/seegno/emitter-objc) 6 | 7 | ## Install it 8 | 9 | Add it as a Cocoapods dependency to your Podfile: 10 | 11 | pod 'Emitter' 12 | 13 | ## Use it 14 | 15 | ### Quick Start 16 | 17 | Register an event listener on any object: 18 | 19 | ```objective-c 20 | #import 21 | 22 | NSObject *server = [[NSObject alloc] init]; 23 | 24 | [server on:@"connection" listener:^(id stream) { 25 | NSLog(@"someone connected!"); 26 | }]; 27 | 28 | [server once:@"connection" listener:^(id stream) { 29 | NSLog(@"Ah, we have our first user!"); 30 | }]; 31 | ``` 32 | 33 | Later, fire an event to the same object: 34 | 35 | ```objective-c 36 | [server emit:@"connection", stream]; 37 | 38 | [server emit:@"event", @{ 39 | @"type": @"somethinghappened", 40 | @"another key": @"another value", 41 | }]; 42 | ``` 43 | 44 | ### Listening for events 45 | 46 | You can listen for events using: 47 | 48 | ```objective-c 49 | - (void)addListener:(NSString *)event listener:(id)listener; 50 | - (void)on:(NSString *)event listener:(id)listener; 51 | - (void)once:(NSString *)event listener:(id)listener; 52 | ``` 53 | 54 | Stop listening with: 55 | 56 | ```objective-c 57 | - (void)removeListener:(NSString *)event listener:(id)listener; 58 | - (void)removeAllListeners:(NSString *)event; 59 | ``` 60 | 61 | ### Emitting events 62 | 63 | Emit events with: 64 | 65 | ```objective-c 66 | - (void)emit:(NSString *)event, ...; 67 | ``` 68 | 69 | ### Selector/Target syntax 70 | 71 | It is also possible to listen for events with a selector and target object using: 72 | 73 | ```objective-c 74 | - (void)addListener(NSString *)event selector:(SEL)selector target:(id)target; 75 | - (void)on:(NSString *)event selector:(SEL)selector target:(id)target; 76 | - (void)once:(NSString *)event selector:(SEL)selector target:(id)target; 77 | ``` 78 | 79 | Stop listening with: 80 | 81 | ```objective-c 82 | - (void)removeListener:(NSString *)event selector:(SEL)selector target:(id)target; 83 | - (void)removeAllListeners:(NSString *)event; 84 | ``` 85 | 86 | ### Dynamic block invocation 87 | 88 | Any callback block you provide will be called with the correct parameters (we used the fantastic [BlocksKit](https://github.com/pandamonia/BlocksKit) to dynamically invoke the callback block). 89 | 90 | This means you can emit events with any number of parameters such as: 91 | 92 | ```objective-c 93 | [object emit:@"event", @"hello", @123, NO, @{ @"key": @"value" }]; 94 | ``` 95 | 96 | and listen for them with: 97 | 98 | ```objective-c 99 | [object on:@"event", ^(NSString *param1, NSNumber *param2, BOOL param3, NSDictionary *param4){ 100 | NSLog(@"Every parameter was passed correctly!"); 101 | }]; 102 | ``` 103 | 104 | ## Credits 105 | 106 | The original idea was taken from jerolimov's [EventEmitter](https://github.com/jerolimov/EventEmitter) and expanded to support dynamic block arguments and a single method to emit events. 107 | --------------------------------------------------------------------------------