{"id":13537239,"url":"https://github.com/zombieleet/emitter","last_synced_at":"2025-04-02T04:30:27.075Z","repository":{"id":86824785,"uuid":"74792976","full_name":"zombieleet/emitter","owner":"zombieleet","description":"event listeners for bash ","archived":false,"fork":false,"pushed_at":"2023-04-24T11:25:29.000Z","size":21,"stargazers_count":85,"open_issues_count":0,"forks_count":3,"subscribers_count":7,"default_branch":"master","last_synced_at":"2024-11-03T02:32:43.931Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Shell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zombieleet.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-11-25T23:08:09.000Z","updated_at":"2024-08-09T12:36:55.000Z","dependencies_parsed_at":null,"dependency_job_id":"e7633805-c813-409b-a1f0-b2a7686957fa","html_url":"https://github.com/zombieleet/emitter","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zombieleet%2Femitter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zombieleet%2Femitter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zombieleet%2Femitter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zombieleet%2Femitter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zombieleet","download_url":"https://codeload.github.com/zombieleet/emitter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246756762,"owners_count":20828762,"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":[],"created_at":"2024-08-01T09:00:56.693Z","updated_at":"2025-04-02T04:30:27.070Z","avatar_url":"https://github.com/zombieleet.png","language":"Shell","funding_links":[],"categories":["Helper Function Things"],"sub_categories":["Reusable Things"],"readme":"# Emitter\nEmitter is a bash library that helps you to be able to create custom event listeners and assign a function to be executed whenever the listener is been emitted\n\n**Usage**\n`event` takes three subcommands which are `attach` `emit` and `detach`\n\n**event attach**\n`event attach` takes two argument which is the name of the event you want to listen for and the function to execute when the event is emitted\n\n```bash\n1  #!/usr/bin/env bash\n2  source ../evt.sh\n3  CreateFile() {\n4    local ftocreate=$1\n5    echo -e \"Love is a name \\nSex is a game\\nPlay the Game\\nForget the name \" \u003e \"$ftocreate\"\n6    printf \"%s\\n\" \"$ftocreate have been created\"\n7  }\n8  event attach createfile CreateFile ;# createfile is the event to listen for, CreateFile is the callback function to execute when the event is emitted\n9  event attach showfile ReadFile ;# showfile is the event to listen for, ReadFile is the callback function to execute when the event is emitted\n```\n\n\n**event emit**\n`event emit` the first argument to `event emit` must be the the event you want to listen for and it must have already been added to the stack with `event attach` , the second argument must be the list  of all the argument which will be passed to the callback function (behind the hood) assigned to the event you want to listen for\n\nNote:- You must not pass the arguments to the callback function directly when createing the event listener with `event attach`\n\nNote:- When assigning the arguments to the event listener using `event emit` you must wrap the arguments with double quotes , each argument must be separated with a single space, if a single argument have spaces or tabs , wrap that specific argument with single quotes.\n\nNote:- Under the hood, 2 other argument are passed to the callback function which is the name of the event listend for and the function that will be fired if the event is emitted\n\nfor example:\n\n```bash\n  event attach talk SayShell # talk is the event to listen for, SayShell is the callback function to execute when the event talk is been emitted\n\n  event emit talk \"bash ksh zsh sh\" # talk is the event to listen for, \"bash ksh zsh sh\" is the argument passed to SayShell\n  \n  loveWhat=\"These are the shells i love\"\n  event emit talk \"'$loveWhat' bash ksh zsh sh\" # loveWhat variable contains space, and it's wrapped in single quotes so that it will be parsed as a single argument\n\n```\n\n\nsee line 7 and line 27\n\n```bash\n1  #!/usr/bin/env bash\n2  source ../evt.sh\n3  CreateFile() {\n4    local text=$1 ftocreate=$2\n5    echo -e \"Love is a name \\nSex is a game\\nPlay the Game\\nForget the name \" \u003e \"$ftocreate\"\n6    printf \"%s\\n\" \"$ftocreate $text\"\n7    event emit showfile \"$ftocreate\"\n8  }\n9  event attach createfile CreateFile ; #createfile is the event to listen for, CreateFile is the callback function to execute when the event is emitted\n10 event attach showfile ReadFile  ; #showfile is the event to listen for, ReadFile is the callback function execute when the event is emitted\n11 ReadFile() {\n12    local file=$1\n13    echo \"------------------------- READING $file -----------------------\"\n14    while read log;do\n15      printf \"%s\\n\" \"$log\"\n16    done \u003c \"$file\"\n17    echo \"------------------------- DONE READING $file ------------------\"\n18 }\n19\n20  File() {\n21\n22    if [[ ! -f \"log.txt\" ]];then\n23    \t event emit createfile \"'has been created' log.txt\" ;# 'has been created' and log.txt will be passed to CreateFile\n24\t     return 1;\n25    fi\n26\n27  event emit showfile \"log.txt\" ; #log.txt will be passed to ReadFile\n28}\n\n```\n**once**\n`event once` works like `event emit`. The only difference is that once an event is emitted in `event once` that event is removed as an event listener i.e it is only executed ones\n\n\n```bash\n\t# check test/once.sh\n\t\t\n\tsource ../emitter.bash;\n\n\ts() {\n\t\techo \"${1}\";\n\t}\n\tevent attach sayHoss s\n\n\tevent emit sayHoss 'victory'\n\tevent emit sayHoss 'victory'\n\tevent emit sayHoss 'victory'\n\n\n\tff() {\n\t\tlocal _num1=${1};\n\t\tlocal _num2=${2};\n\n\t\tprintf \"%d\\n\" \"$(( _num1 + _num2 ))\";\n\t}\n\n\tevent attach addNumber ff\n\n\tevent once addNumber 2 2\t\n\n```\n\n**NOTE:** once the event `addNumber` is emitted trying to emit again will cause an error\n\n\n**removeall listener** \n`event removeAll` This subcommands removes all listener by unsetting the Stack array \n\n**List Event**\n`event list` takes no argument. It lists the total number of event listener in the Stack\n\nexample\n`event list`\n\n\n**setmaxlistener**\n`event setMaxlistener` takes one argument which must be an integer. It resets the default maximum listener by the specified ones\n\n\n**Detach Event**\n`event detach` takes a single argument, it detaches/removes the event you don't want to listen for again from the Stack.\n\n`event detach eventName`\n\n\n**maxlisteners**\nThe total number of listeners allowed is 1000, and it's readonly, you cannot modify it in your script, except you want to modify it directly from the emitter library\n\n***License***\nGNU General Public License\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzombieleet%2Femitter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzombieleet%2Femitter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzombieleet%2Femitter/lists"}