{"id":13413478,"url":"https://github.com/mudler/anagent","last_synced_at":"2025-04-19T18:59:40.211Z","repository":{"id":57496877,"uuid":"115740699","full_name":"mudler/anagent","owner":"mudler","description":"Minimalistic, pluggable Golang evloop/timer handler with dependency-injection","archived":false,"fork":false,"pushed_at":"2018-08-12T17:51:33.000Z","size":40,"stargazers_count":15,"open_issues_count":0,"forks_count":4,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-29T12:02:38.178Z","etag":null,"topics":[],"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/mudler.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":"2017-12-29T17:16:25.000Z","updated_at":"2024-01-02T22:21:21.000Z","dependencies_parsed_at":"2022-08-28T11:51:03.608Z","dependency_job_id":null,"html_url":"https://github.com/mudler/anagent","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/mudler%2Fanagent","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mudler%2Fanagent/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mudler%2Fanagent/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mudler%2Fanagent/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mudler","download_url":"https://codeload.github.com/mudler/anagent/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249771302,"owners_count":21323077,"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-07-30T20:01:41.283Z","updated_at":"2025-04-19T18:59:40.171Z","avatar_url":"https://github.com/mudler.png","language":"Go","readme":"# Anagent [![Build Status](https://travis-ci.org/mudler/anagent.svg?branch=master)](https://travis-ci.org/mudler/anagent) [![codecov](https://codecov.io/gh/mudler/anagent/branch/master/graph/badge.svg)](https://codecov.io/gh/mudler/anagent) [![Go Report Card](https://goreportcard.com/badge/github.com/mudler/anagent)](https://goreportcard.com/report/github.com/mudler/anagent) [![godoc](https://godoc.org/github.com/mudler/anagent?status.svg)](http://godoc.org/github.com/mudler/anagent)\n\nMinimalistic, pluggable Golang evloop/timer handler with dependency-injection - based on [codegangsta/inject](github.com/codegangsta/inject) - [go-macaron/inject](github.com/go-macaron/inject) and [chuckpreslar/emission](https://github.com/chuckpreslar/emission).\n\n*Anagent* is a lightweight library that allows you to plug inside to other event loops, or allows you to handle and create your own within your application - leaving the control to you.\n\nIt comes with dependency-injection from codegangsta/inject, and it's also a soft-wrapper to chuckpreslar/emission, adding to it dependency injection capabilities and timer handlers.\n\n## Usage\n\n### Event Emitter with Dependency injection\n\n```go\n    package main\n\n    import (\n    \t\"log\"\n    \t\"github.com/mudler/anagent\"\n    )\n\n    type TestTest struct {\n    \tTest string\n    }\n\n    func main() {\n    \tagent := anagent.New()\n    \tmytest := \u0026TestTest{Test: \"PONG!\"}\n    \tagent.Map(mytest)\n\n    \tagent.Once(\"test\", func(te *TestTest, l *log.Logger) {\n    \t\tif te.Test == \"PONG!\" {\n    \t\t\tl.Println(\"It just works!\")\n    \t\t}\n    \t})\n\n    \tagent.Emit(\"test\")\n    }\n```\n\nWhat happened here? we mapped our structure instance (```TestTest```) inside the agent with (```agent.Map()```), and all fired events can access to them.\n\n### Timer / Reactor\n\n```go\n    package main\n\n    import \"github.com/mudler/anagent\"\n    import \"fmt\"\n\n    type TestTest struct {\n            Test string\n    }\n\n    func main() {\n            agent := anagent.New()\n            mytest := \u0026TestTest{Test: \"PONG!\"}\n            agent.Map(mytest)\n\n            agent.Emitter().On(\"test\", func(s string) { fmt.Println(\"Received: \" + s) })\n\n            // Not recurring timer\n            agent.TimerSeconds(int64(3), false, func(a *anagent.Anagent, te *TestTest) {\n                    a.Emitter().Emit(\"test\", te.Test)\n                    go a.Stop()\n            })\n\n            agent.Start() // Loops here and never returns\n    }\n ```\n\nThe code portion will start and wait for 3 seconds, then it will execute the callback (not recurring, that's why the ```false```) that will fire a custom event defined before (note, it's not using the dependency-injection capabilities, thus it's accessing the emitter handler directly with ```agent.Emitter()```).\n\nThe difference is that when we access to ```On()``` provided by ```agent.On()```, we access to the agent dependencies, that have been mapped with ```agent.Map()``` - otherwise, with ```agent.Emitter().On()``` we are free to bind any arguments to the event callback.\n\n\nAfter the event is fired, the timer stops the eventloop (```a.Stop()```), so the program returns.\n\n### Hook into other loops\n\nIt is often in other framework to use loop patterns, as example in framework for game development, network agents, and such.\nWe can hook into other loops, and run the agent Step function, so we can still leverage the evloop functionalities.\n\n```go\n    package main\n\n    import \"github.com/mudler/anagent\"\n    import \"fmt\"\n\n    type TestTest struct {\n    \tTest string\n    }\n\n    func main() {\n    \tagent := anagent.New()\n    \tmytest := \u0026TestTest{Test: \"PONG!\"}\n    \tagent.Map(mytest)\n\n        // Reply with a delay of 2s\n    \tagent.Emitter().On(\"test\", func(s string) {\n    \t\tagent.TimerSeconds(int64(2), false, func() {\n    \t\t\tfmt.Println(\"Received: \" + s)\n    \t\t})\n    \t})\n\n    \t// Recurring timer\n    \tagent.TimerSeconds(int64(3), true, func(a *anagent.Anagent, te *TestTest) {\n    \t\tfmt.Println(\"PING!\")\n    \t\ta.Emitter().Emit(\"test\", te.Test)\n    \t})\n\n    \tfor { // Infinite loop\n    \t\tagent.Step()\n    \t}\n    }\n ```\n","funding_links":[],"categories":["杂项","Miscellaneous","其他杂项","Uncategorized","Microsoft Office","其他"],"sub_categories":["Advanced Console UIs","Uncategorized","Strings","未分类的","暂未分类","暂未分类这些库被放在这里是因为其他类别似乎都不适合。","交流"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmudler%2Fanagent","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmudler%2Fanagent","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmudler%2Fanagent/lists"}