{"id":17680745,"url":"https://github.com/kcreate/silver","last_synced_at":"2025-03-30T19:13:03.619Z","repository":{"id":57359589,"uuid":"47966300","full_name":"KCreate/silver","owner":"KCreate","description":"Silver is a fast, powerful and lightweight 1KB! event management library for node. It's written in plain Javascript, has no dependencies and great documentation.","archived":false,"fork":false,"pushed_at":"2016-01-08T20:17:35.000Z","size":25,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-29T11:52:51.963Z","etag":null,"topics":[],"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/KCreate.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-12-14T09:56:07.000Z","updated_at":"2017-04-10T16:18:58.000Z","dependencies_parsed_at":"2022-09-06T21:41:34.735Z","dependency_job_id":null,"html_url":"https://github.com/KCreate/silver","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/KCreate%2Fsilver","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KCreate%2Fsilver/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KCreate%2Fsilver/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KCreate%2Fsilver/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/KCreate","download_url":"https://codeload.github.com/KCreate/silver/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246365645,"owners_count":20765546,"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-10-24T09:08:45.031Z","updated_at":"2025-03-30T19:13:03.597Z","avatar_url":"https://github.com/KCreate.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"### Silver\n__Silver__ is a fast, powerful and lightweight __1KB!__ event management library for node. It's written in plain Javascript and has __no dependencies__.\n\nNPM page: [silverjs](https://www.npmjs.com/package/silverjs)\n\nGithub page: [kcreate/silver](https://github.com/KCreate/silver)\n\n### How to install\n\n```sh\nnpm install --save silverjs\n```\n```javascript\nvar Silver = require('silverjs');\n```\nOnce you've installed it with npm, you can compress it by just running gulp against the directory and then replacing the index.js with index.min.js. However this is not required.\n\n___\n\n### Creating a new silver object\n___new Silver(name = Date.now()+\"\")___\n\nYou should always pass the same name you took for your variable into the Silver initializer. If you don't pass anything, Silver will just use the current ```Date.now()``` value.\n```javascript\nvar abc = new Silver(\"abc\");\n```\n\n### Add a new event\n___addEvent(eventName)___\n\n```javascript\nvar abc = new Silver(\"abc\");\nabc.addEvent(\"myEvent\");\n```\n\n### Remove an event\n___removeEvent(eventName)___\n\n```javascript\nvar abc = new Silver(\"abc\");\nabc.addEvent(\"myEvent\");\nabc.removeEvent(\"myEvent\");\n```\n\nRemoving an event that has active subscriptions, will fire the callbacks of the subscribers with the following data:\n```json\n{\n\t\"error\":{\n\t\t\"Event got removed while still being subscribed.\"\n\t}\n}\n```\n\n___removeEvent___ also takes a second parameter, but it's only used internally.\n\n### Check if an event exists\n___hasEvent(eventName)___\n\n```javascript\nvar abc = new Silver(\"abc\");\nabc.addEvent(\"myEvent\");\nvar hasEvent = abc.hasEvent(\"myEvent\"); // true\n```\n\n### Subscribe to an event\n___subscribeToEvent(object, eventName, callback)___\n\nTo send data back to the caller of the event, return it in the callback\n```javascript\nvar abc = new Silver(\"abc\");\nvar def = new Silver(\"def\");\n\nabc.addEvent(\"myEvent\");\ndef.subscribeToEvent(abc, \"myEvent\", function(data) {\n        if(data.error){\n\t\t\t// handle error\n\t\t}\n\n\t\treturn data+\" world\";\n});\n```\n\nWhen you try to subscribe to an event that doesn't exist, the callback will be called immediately with the following content:\n```json\n{\n\t\"error\": {\n\t\t\"message\":\"An event called myEvent doesn't exist.\"\n\t}\n}\n```\n\nIf the object already has a subscription, it will be __replaced__.\n\n### Unsubscribing from an event\n___unsubscribeFromEvent(object, eventName)___\n\nUnsubscribing from an event that doesn't exist, or you're not subscribed to in the first place, will return __false__. On success it will return __true__;\n```javascript\nvar abc = new Silver(\"abc\");\nvar def = new Silver(\"def\");\n\nabc.addEvent(\"myEvent\");\ndef.subscribeToEvent(abc, \"myEvent\", function(data){\n        if(data.error){\n\t\t\t// handle error\n\t\t}\n\n\t\treturn data+\" world\";\n});\ndef.unsubscribeFromEvent(abc, \"myEvent\");\n```\n\n### Get all subscribers for an event\n___subscribersForEvent(eventName)___\n\n```javascript\nvar abc = new Silver(\"abc\");\nvar def = new Silver(\"def\");\n\nabc.addEvent(\"myEvent\");\ndef.subscribeToEvent(abc, \"myEvent\", function(data){\n\n});\nvar subscribers = abc.subscribersForEvent(\"myEvent\");\n```\nThe content of subscribers will be:\n```javascript\n[\n\t{\n\t\tobject:[Silver],\n\t\tname:'abc',\n\t\tcallback:[\n\t\t\tFunction\n\t\t]\n\t}\n]\n```\n\n### Check if an object is subscribed to another\n___isSubscribed(object, eventName)___\n\nThis returns false if the object is not subscribed or if the event doesn't exist.\n```javascript\nvar abc = new Silver(\"abc\");\nvar def = new Silver(\"def\");\n\nabc.addEvent(\"myEvent\");\ndef.subscribeToEvent(abc, \"myEvent\", function(data){\n        // do stuff with data\n});\nvar subscribed = def.isSubscribed(abc, \"myEvent\"); // true\n```\n\n### Fire an event\n___fireEvent(eventName, parameters, callback)___\n\nThe responses variable in the callback contains the responses that get returned in the ```subscribeToEvent``` method.\n```javascript\nvar abc = new Silver(\"abc\");\nvar def = new Silver(\"def\");\n\nabc.addEvent(\"myEvent\");\ndef.subscribeToEvent(abc, \"myEvent\", function(data){\n        return data + \" world\";\n});\nabc.fireEvent(\"myEvent\", \"hello\", function(responses) {\n        // [ 'hello world' ]\n});\n```\n\n### Get all events an object has\n___events()___\n\nThe events method returns an array of all events an object has.\n\n### Transfering an event\n___transferEvent(object, eventName, keepSubscriptions, overwriteExisting)___\n\nThis transfer an event from the owner to a new object. If __keepSubscriptions__ is set to true, all subscriptions will be transfered over. If they are being removed, all callbacks will receive an error with the following content:\n```json\n{\n\t\"message\":\"Event got transfered to another object\",\n\t\"newObject\":object\n}\n```\n\nIf __overwriteExisting__ is set to true, it will overwrite an existing event with the same name on the new object. If __keepSubscriptions__ and __overwriteExisting__ are both set to true, Silver will try to merge all subscriptions into one event. If an object is subcribed to both events, the subscription to the overwritten event will be removed.\n\n### Chainability\n\nYou can also chain most methods with each other. So for example if you'd wanted to add a new event and then immediately check if it really got created, you could write that like this:\n```javascript\nvar abc = new Silver('abc');\nvar exists = abc.addEvent('test').hasEvent('test');\n// -\u003e true\n```\n\nOr if you want to fire an event and then immediately remove it afterwards\n```javascript\nabc.fireEvent('test', \"world\", function(response) {\n\t// your code goes here\n}).removeEvent('test');\n// -\u003e abc does not have the event 'test' anymore\n```\n\n### Error handling\n\nMost of the time if an error happens, it will be reported via the callback specified in the subscribeToEvent method.\n\nAn error report always looks like this:\n```json\n{\n\t\"error\":{\n\t\t\"message\":\"Some error message...\"\n\t}\n}\n```\n\nYou should aways check for a error object inside the callback method like that:\n```javascript\nvar abc = new Silver(\"abc\");\nvar def = new Silver(\"def\");\n\nabc.addEvent(\"myEvent\");\ndef.subscribeToEvent(abc, \"myEvent\", function(data) {\n    if (data.error){\n\t\t// check the message here\n\t\tconsole.log(data.error.message);\n\t}\n});\n```\n\n### License\n\nThis library is licensed under the [MIT License](https://opensource.org/licenses/MIT).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkcreate%2Fsilver","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkcreate%2Fsilver","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkcreate%2Fsilver/lists"}