{"id":21524199,"url":"https://github.com/gxsshallot/react-native-general-listener","last_synced_at":"2025-04-09T23:02:37.882Z","repository":{"id":57337194,"uuid":"146841632","full_name":"gxsshallot/react-native-general-listener","owner":"gxsshallot","description":"A general event listener support multi-level key and listening sub events.","archived":false,"fork":false,"pushed_at":"2019-02-16T08:23:27.000Z","size":27,"stargazers_count":6,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-18T13:11:44.184Z","etag":null,"topics":["event-listener","react-native"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","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/gxsshallot.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-08-31T04:26:06.000Z","updated_at":"2019-11-06T05:18:34.000Z","dependencies_parsed_at":"2022-09-10T02:53:08.383Z","dependency_job_id":null,"html_url":"https://github.com/gxsshallot/react-native-general-listener","commit_stats":null,"previous_names":["gaoxiaosong/react-native-general-listener"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gxsshallot%2Freact-native-general-listener","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gxsshallot%2Freact-native-general-listener/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gxsshallot%2Freact-native-general-listener/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gxsshallot%2Freact-native-general-listener/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gxsshallot","download_url":"https://codeload.github.com/gxsshallot/react-native-general-listener/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248125581,"owners_count":21051768,"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":["event-listener","react-native"],"created_at":"2024-11-24T01:21:29.632Z","updated_at":"2025-04-09T23:02:37.847Z","avatar_url":"https://github.com/gxsshallot.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# react-native-general-listener\n\n[![npm version](https://img.shields.io/npm/v/react-native-general-listener.svg?style=flat)](https://www.npmjs.com/package/react-native-general-listener)\n[![Build Status](https://travis-ci.org/gaoxiaosong/react-native-general-listener.svg?branch=master)](https://travis-ci.org/gaoxiaosong/react-native-general-listener)\n\n[中文说明](https://www.jianshu.com/p/43c2f39992b4)\n\nIt is a wrapper of `DeviceEventListener` to support following functions:\n\n* Event level to make event name more readable.\n* Subscribe all sub level event and unsubscribe.\n\n## Install\n\nInstall by Yarn:\n\n```shell\nyarn add react-native-general-listener\n```\n\nInstall by NPM:\n\n```shell\nnpm install --save react-native-general-listener\n```\n\n## Usage\n\nImport the module in file:\n\n```javascript\nimport Listener from 'react-native-general-listener';\n```\n\nThen you can register, unregister or trigger an event type.\n\n### Event Type Structure\n\nAn event type can be a string, an array of string, or an object.\n\n* a string: Use the string as event name directly, it is not supported to trigger parent or child event.\n* an array of string: We will use seperator to connect these strings to generate event name. And you can register with listen to its child event, or trigger its parent event.\n* an object: Use the json string of object as event name. We do not recommend this usage.\n\n### Register\n\nWe have two register function:\n\n* register\n* registerWithSubEvent\n\nThey both have two parameters:\n\n* type: Event type.\n* func: Event callback, will be called when the event is triggered.\n\nExample:\n\n```javascript\nthis.loginListener1 = Listener.register('LoginEvent', this.loginFn);\nthis.loginListener2 = Listener.register(['TestApp', 'Login', userId], this.loginFn);\n```\n\nThe only difference between `register` and `registerWithSubEvent` is: the second one register event type with all sub level event types.\n\nThe function will return an object to used when `unregister` event type.\n\n### Unregister\n\nYou can unregister one event listener or all of an event type.\n\nThe function `unregister` has three parameters:\n\n* type: Event type.\n* listenerObj: Listener object which is the returned value of `register` or `registerWithSubEvent`. If it is `undefined`, we will remove all event listeners of the event type.\n\nExample:\n\n```javascript\nListener.unregister('LoginEvent', this.loginListener1);\nListener.unregister('LoginEvent');\nListener.unregister(['TestApp', 'Login', userId], this.loginListener);\nListener.unregister(['TestApp', 'Login', userId]);\n```\n\n### Trigger\n\nYou can trigger event with an event type and an event param.\n\nThe function `trigger` has three parameters:\n\n* type: Event type.\n* state: Event param. It can be an object, a number, a boolean or any other data type.\n\nWhen trigger an event with an array of string event type `[str0, str1, ... strn]`, we will find its parent event types to see if they use `registerWithSubEvent` function:\n\n```javascript\nconst upperType = [str0, str1, ..., strn];\nwhile ( /* upperType is not empty */ ) {\n    // Convert upperType to upperName\n    if ( /* upperName is registered with registerWithSubEvent function */ ) {\n        // Trigger upper event with state\n    }\n    // Pop the last element of upperType\n}\n```\n\n### Global Constant\n\nThere are two constant in the module:\n\n* defaultSeperator: Default seperator, used as connector of event type to generate event name. Default is `'$'`。\n* innerEventType: Inner event type key, when event is triggered, the event type will be set into event param if event param is an object. This key is `'_##_inner_##_event_##_type_##_'` to avoid key duplication in event param.\n\nYou can modify them globally:\n\n```javascript\nListener.defaultSeperator = '#';\nListener.innerEventType = '123321';\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgxsshallot%2Freact-native-general-listener","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgxsshallot%2Freact-native-general-listener","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgxsshallot%2Freact-native-general-listener/lists"}