{"id":13367307,"url":"https://github.com/sadlil/Go-trigger","last_synced_at":"2025-03-12T18:32:18.137Z","repository":{"id":57479989,"uuid":"44525126","full_name":"sadlil/go-trigger","owner":"sadlil","description":"A Global event triggerer for golang. Defines functions as event with id string. Trigger the event anywhere from your project.","archived":false,"fork":false,"pushed_at":"2017-03-28T16:18:42.000Z","size":12,"stargazers_count":248,"open_issues_count":1,"forks_count":41,"subscribers_count":14,"default_branch":"master","last_synced_at":"2025-03-03T02:52:19.875Z","etag":null,"topics":["event-handlers","trigger"],"latest_commit_sha":null,"homepage":"","language":"Go","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/sadlil.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":"2015-10-19T09:42:17.000Z","updated_at":"2025-01-29T02:54:36.000Z","dependencies_parsed_at":"2022-09-26T17:41:34.573Z","dependency_job_id":null,"html_url":"https://github.com/sadlil/go-trigger","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sadlil%2Fgo-trigger","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sadlil%2Fgo-trigger/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sadlil%2Fgo-trigger/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sadlil%2Fgo-trigger/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sadlil","download_url":"https://codeload.github.com/sadlil/go-trigger/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243271567,"owners_count":20264475,"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-handlers","trigger"],"created_at":"2024-07-30T00:01:44.113Z","updated_at":"2025-03-12T18:32:17.867Z","avatar_url":"https://github.com/sadlil.png","language":"Go","readme":"# go-trigger\nGo Trigger is a global event trigger for golang. Define an event with a task specified to that\nevent and Trigger it from anywhere you want.\n\n### Get The Package \n```bash\n$ go get -u github.com/sadlil/go-trigger\n```\n\n### How to switch to a specific version\n`go get` the package. Go to the package directory in your $GOPATH/src.\nChange the tag using git.\n`go install` the package.\n \n```bash\n$ go get -u github.com/sadlil/go-trigger\n$ cd $GOPATH/src/github.com/sadlil/go-trigger\n$ git checkout tags/\u003ctag_name\u003e\n$ go install\n```\n#### Currently [available Tags](https://github.com/sadlil/go-trigger/releases)    \n - [v0.01](https://github.com/sadlil/go-trigger/releases/tag/v0.01)\n     - Global event with unique key Id.\n     - Trigger Event,\n     - List Events,\n     - Clear and Delete Events,\n     - Trigger event in background,\n     - Local Event,\n     - Thread Safe Mutex Lock.\n\n### How To Use\n#### 1. Global Events\nImport the package into your code. Add events with `trigger.On` method.\nAnd call that event handler with `trigger.Fire` method. All the events added\nlike this will be global events. You can call `Fire` from anywhere.\n\n```go\npackage main\n\nimport (\n  \"github.com/sadlil/go-trigger\"\n  \"fmt\"\n)\n\n\nfunc main() {\n  trigger.On(\"first-event\", func() {\n    // Do Some Task Here.\n    fmt.Println(\"Done\")\n  })\n  trigger.Fire(\"first-event\")\n}\n```\n\n\nYou can define your events from another package\n```go\n  trigger.On(\"second-event\", packagename.FunctionName)\n  trigger.Fire(\"second-event\")\n```\n\n\nYou can define events with parameteres and return types.\n```go\nfunc TestFunc(a, b int) int {\n    return a + b\n}\n\n// Call them using\ntrigger.On(\"third-event\", TestFunc)\nvalues, err := trigger.Fire(\"third-event\", 5, 6)\n\n// IMPORTANT : You need to type convert Your Returned Values using\n// values[0].Int()\n```\n\n\nYou can define your event in one package and trigger it another package. Your event and trigger are global.\nDefine anywhere, fire anywhere. You can define any function in any package as event you only need to\nimport the function's specified package where you define the event. Where you trigger the event, you do not\nneed to import it there.\n```go\n//---------------------------------------------\n  package a\n  \n  func AFunction(one, two int) int {\n    return one + two\n  }\n  \n  \n//---------------------------------------------\n  package b\n  import (\n    \"yourdirectory/a\"\n    \"github.com/sadlil/go-trigger\"\n  )\n  \n  func() {\n    trigger.On(\"new-event\", a.AFunction)\n  }\n  \n  \n//---------------------------------------------\n  package c\n  import (\n    \"github.com/sadlil/go-trigger\"\n  )\n  \n  func() {\n    values, err := trigger.Fire(\"new-event\", 10, 10) \n    // You don't need to import package a here.\n    fmt.Println(values[0].Int())\n  }\n```\n\nYou can run events in background with `FireBackground()`\n```go\nfunc main() {\n  trigger.On(\"first-event\", func() {\n    for i := 1; i \u003c= 1000; i++ {\n      fmt.Println(i)\n    }\n  })\n  channel, err := trigger.FireBackground(\"first-event\")\n  fmt.Println(\"Event runs\")\n  //read the returned channel\n  values := \u003c- channel\n  \n  trigger.FireBackground(\"first-event\")\n  fmt.Println(\"Running 2nd Event\")\n}\n\n```\n\n#### 2. Local Events\nTrigger instance that will not effecct the global event. All event added to\nan local event instace can call only via this trigger instance. This is\nimplementation of plugable `Trigger` interface.\n\nCreate a local trigger instance,\n\n```go\npackage main\n\nimport (\n  \"github.com/sadlil/go-trigger\"\n  \"fmt\"\n)\n\n\nfunc main() {\n  t := trigger.New()\n  t.On(\"first-event\", func() {\n    // Do Some Task Here.\n    fmt.Println(\"Done\")\n  })\n  t.Fire(\"first-event\")\n  \n  // t2 is another trigger instance that will be separate from t1.\n  t2 := trigger.New()\n  t2.On(\"first-event\", func() {\n    // Do Some Task Here.\n    fmt.Println(\"Done\")\n  })\n  t2.Fire(\"first-event\")\n}\n\n```\n**All other methods are availabe on any local trigger instance**\n\n\n### Available methods\n```go\nOn(event string, task interface{}) error\n  - Add a Event. task must be function. Throws an error if the event is duplicated.\n   \nFire(event string, params ...interface{}) ([]reflect.Value, error)\n  - Fires the task specified with the event key. params are the parameter and\n  [] is the returned values of task. Fire Triggers the event and wait for it to\n  end until it goes to execute the following codes.\n  \nFireBackground(event string, params ...interface{}) (chan []reflect.Value, error)\n  - Fires the task specified with the event key. Unlike Fire it runs the event in\n  background in go routine. It triggers the event but does not wait for the event\n  to end. It writes the returned values of the event in a channel and returns the\n  channel of reflect.Values. You can get the returned values by reading the\n  channel (IE. ret := \u003c- returned channel).\n  \n  - As FireBackground does not wait for the event to end first, if your program \n  exits it will stop any running event that did not finishes. So make sure your\n  background events exits before ending the program.   \n\n\nClear(event string) error\n  - Delete a event from the event list. throws an error if event not found.\n  \nClearEvents()\n  - Deletes all event from the event list.\n  \nHasEvent(event string) bool\n  - Checks if a event exists or not. Return true if the event list have a event with \n  that key.  false otherwise.\n  \nEvents() []string\n  - Returns all the events added.\n  \nEventCount() int\n  - Returns count of the events. If none found return 0;\n  \n```\n\n\n### Under Development Features\n 1. Multiple event handler for a event.\n\n### Licence\n    Licenced under MIT Licence\n\n\n##### Any Suggestions and Bug Report will be gladly appreciated.\n","funding_links":[],"categories":["实用工具","實用工具"],"sub_categories":["高级控制台界面","高級控制台界面"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsadlil%2FGo-trigger","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsadlil%2FGo-trigger","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsadlil%2FGo-trigger/lists"}