{"id":18264741,"url":"https://github.com/reactcomponentkit/bkeventbus","last_synced_at":"2025-08-13T01:47:52.259Z","repository":{"id":56903021,"uuid":"123133585","full_name":"ReactComponentKit/BKEventBus","owner":"ReactComponentKit","description":"EventBus is tiny, simple typed event bus to communicate between components.","archived":false,"fork":false,"pushed_at":"2019-01-17T15:08:47.000Z","size":1051,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-09-17T20:47:48.626Z","etag":null,"topics":["component","eventbus","ios","nsnotification","swift"],"latest_commit_sha":null,"homepage":"https://github.com/ReactComponentKit/EventBus","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ReactComponentKit.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-02-27T13:33:40.000Z","updated_at":"2019-01-17T15:08:31.000Z","dependencies_parsed_at":"2022-08-20T18:50:57.506Z","dependency_job_id":null,"html_url":"https://github.com/ReactComponentKit/BKEventBus","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ReactComponentKit%2FBKEventBus","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ReactComponentKit%2FBKEventBus/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ReactComponentKit%2FBKEventBus/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ReactComponentKit%2FBKEventBus/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ReactComponentKit","download_url":"https://codeload.github.com/ReactComponentKit/BKEventBus/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247958710,"owners_count":21024821,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["component","eventbus","ios","nsnotification","swift"],"created_at":"2024-11-05T11:15:47.454Z","updated_at":"2025-04-09T01:42:11.795Z","avatar_url":"https://github.com/ReactComponentKit.png","language":"Swift","readme":"# EventBus\n\nEventBus is tiny, simple typed event bus to communicate between components. It is built on NSNotificationCenter. \n\n## How to install\n\n```\npod 'BKEventBus'\n```\n\n## Why Use EventBus?\n\nIf you want to remove dependencies between components or layers, you'd better to use event bus. Removing dependency is always important thing when you make software. EventBus helps you it more easily. It is typed EventBus. You can define Event type and arguments and you can use it somewhere.\n\n## How to use EventBus\n\n### Define EventType\n\nYou can define event type using EventType protocol. It is just marking as event type. You can use every types like as class, struct or enum. I prefer to use enum :) You can define events like as below:\n\n\n```swift\nenum Actions: EventType {\n\tcase push(item: String)\n\tcase pop\n\tcase loading(item: String)\n\tcase delete(item: String)\n}\n```\n\n### Make Event Poster\n\n```swift\n...\nlet eventBus: EventBus\u003cActions\u003e = EventBus()\n...\n```\n\n### Post Events\n\n```swift\neventBus.post(event: .push(item: \"Something\"))\neventBus.post(event: .pop)\neventBus.post(event: .loding(item: \"1234\"))\neventBus.pose(event: .delete(item: \"1234\"))\n```\n\n\n### Subscribe Events\n\n```swift\n// Somewhere\n{\n...\n\n\tlet eventBus: EventBus\u003cActions\u003e = EventBus()\n\teventBus.on { [weak self] (event: Actions) in\n\t\tswitch event {\n\t\tcase let .push(item):\n\t\t\t...\n\t\tcase pop:\n\t\t\t...\n\t\tcase loding(item):\n\t\t\t...\n\t\tcase delete(item):\n\t\t\t...\n\t\t}\n\t}\n\t\n...\n}\n```\n\n## Life cycle\n\nYou can stop or resume event bus with life cycle methods. \n\n### Resume EventBus\n\n```swift\neventBus.resume()\n```\n\n### Stop EventBus\n\n```swift\neventBus.stop()\n```\n\nOr You can stop event bus by deleting it.\n\n```swift\n// Define Event Bus\nvar eventBus: EventBus\u003cActions\u003e? = EventBus()\n...\nUse it\n...\n\n// Delete it\neventBus = nil\n```\n\n## Simple Example\n\n### Commumicate between CollectionViewCell and ViewController.\n\n#### Define Events at Cell\n\n```swift\nclass ItemCollectionViewCell: UICollectionViewCell {\n    \n    // Define Events\n    enum Event: EventType {\n        case clickedDeleteButton(indexPath: IndexPath)\n        case clickedDetailButton(indexPath: IndexPath)\n    }\n    \n    static var className: String {\n        return String(describing: self)\n    }\n\n    @IBOutlet weak var dateLabel: UILabel!\n\n    private var indexPath: IndexPath? = nil\n    private var eventBus: EventBus\u003cItemCollectionViewCell.Event\u003e? = nil\n    \n    override func awakeFromNib() {\n        super.awakeFromNib()\n    }\n    \n    override func prepareForReuse() {\n        super.prepareForReuse()\n        eventBus = nil\n    }\n    \n    func configure(item: String, indexPath: IndexPath) {\n        self.dateLabel.text = item\n        self.indexPath = indexPath\n        eventBus = EventBus()\n    }\n    \n    // Post Events\n    @IBAction func clickedDetailButton(_ sender: Any) {\n        guard let indexPath = indexPath else { return }\n        eventBus?.post(event: .clickedDetailButton(indexPath: indexPath))\n    }\n    \n    @IBAction func clickedDeleteButton(_ sender: Any) {\n        guard let indexPath = indexPath else { return }\n        eventBus?.post(event: .clickedDeleteButton(indexPath: indexPath))\n    }\n}\n```\n\n\n#### Subscribe events on ViewController\n\n```swift\nimport UIKit\n\nclass ListViewController: UIViewController {\n    \n    @IBOutlet weak var collectionView: UICollectionView!\n    \n    private var items: [String] = []\n    private let eventBus = EventBus\u003cItemCollectionViewCell.Event\u003e()\n    \n    override func viewDidLoad() {\n        super.viewDidLoad()\n        self.title = \"Date List\"\n        collectionView.register(UINib(nibName: ItemCollectionViewCell.className, bundle: nil), forCellWithReuseIdentifier: ItemCollectionViewCell.className)\n        collectionView.dataSource = self\n        collectionView.delegate = self\n        \n        // Subscribe events\n        eventBus.on { [weak self] (event: ItemCollectionViewCell.Event) in\n            guard let strongSelf = self else { return }\n            switch event {\n            case let .clickedDeleteButton(indexPath):\n                strongSelf.items.remove(at: indexPath.row)\n                strongSelf.collectionView.reloadData()\n            case let .clickedDetailButton(indexPath):\n                let item = strongSelf.items[indexPath.row]\n                let vc = DetailViewController.viewController(item: item)\n                strongSelf.navigationController?.pushViewController(vc, animated: true)\n            }\n        }\n    }\n\n    override func didReceiveMemoryWarning() {\n        super.didReceiveMemoryWarning()\n    }\n\n    \n    @IBAction func clickedAddButtonItem(_ sender: Any) {\n        let date = Date()\n        items.append(date.description)\n        collectionView.reloadData()\n    }\n}\n\nextension ListViewController: UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {\n    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -\u003e Int {\n        return items.count\n    }\n    \n    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -\u003e CGSize {\n        return CGSize(width: collectionView.bounds.width, height: 50)\n    }\n    \n    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -\u003e UICollectionViewCell {\n        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ItemCollectionViewCell.className, for: indexPath)\n        if let itemCell = cell as? ItemCollectionViewCell {\n            itemCell.configure(item: items[indexPath.row], indexPath: indexPath)\n        }\n        return cell\n    }\n}\n```\n\n## MIT License\n\nThe MIT License\n\nCopyright © 2018 Sungcheol Kim, http://github.com/ReactComponentKit/BKEventBus\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in\nall copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\nTHE SOFTWARE.","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Freactcomponentkit%2Fbkeventbus","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Freactcomponentkit%2Fbkeventbus","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Freactcomponentkit%2Fbkeventbus/lists"}