{"id":23878302,"url":"https://github.com/barteh/as-service","last_synced_at":"2025-09-09T04:32:41.637Z","repository":{"id":35178070,"uuid":"140707663","full_name":"barteh/as-service","owner":"barteh","description":"A super simple and easy to use reactive library for subscribing to any data. contains a hook for react js.","archived":false,"fork":false,"pushed_at":"2023-01-06T01:34:44.000Z","size":1193,"stargazers_count":7,"open_issues_count":13,"forks_count":3,"subscribers_count":6,"default_branch":"master","last_synced_at":"2024-12-18T13:07:47.296Z","etag":null,"topics":["barteh","evrything-as-service","injectable-service","javascript","nodejs","observable","reactive-programming","reactjs","state-management","subscriber-pattern","typescript","web-application"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/barteh.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-07-12T12:02:41.000Z","updated_at":"2021-10-07T12:43:49.000Z","dependencies_parsed_at":"2023-01-15T15:31:00.033Z","dependency_job_id":null,"html_url":"https://github.com/barteh/as-service","commit_stats":null,"previous_names":["barteh/btservice"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/barteh%2Fas-service","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/barteh%2Fas-service/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/barteh%2Fas-service/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/barteh%2Fas-service/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/barteh","download_url":"https://codeload.github.com/barteh/as-service/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":232395166,"owners_count":18516595,"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":["barteh","evrything-as-service","injectable-service","javascript","nodejs","observable","reactive-programming","reactjs","state-management","subscriber-pattern","typescript","web-application"],"created_at":"2025-01-03T21:18:32.210Z","updated_at":"2025-01-03T21:18:32.961Z","avatar_url":"https://github.com/barteh.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Build Status](https://travis-ci.org/barteh/as-service.svg?branch=master)](https://travis-ci.org/barteh/as-service)\n\n# AsService\n\n## Use any data as a service\n\n### A super simple and easy to use reactive library for subscribing to any data. contains a hook for react js.\n---\n\n### What does it exactly do?\n\u003e It helps programmers (both back and front end) to avoid complexity of traditional state management systems such as React Redux and reactive programming pattern library like Rxjs. It is lightweight and easy to use library contains one core function and a hook for react. By using it we can subscribe vast the majority of data type such as primitive, complex, promises and function as a service and receive all changes by particular parameter (parametric subscription). We do not even need to control the subscribe-unsubscribe process in react with simple hook it do all the process automatically.\n\n\n\n\n### Install\n\n```cmd\nnpm i @barteh/as-service --save\n```\n\n\n### Usage\n\n\n#### Import library\n\n```js\nimport { AsService, Server } from \"@barteh/as-service\";\n```\n\n\n#### one: Primitive type (number | string | Array) as service\n\n```js\nvar srv1 = new AsService(5); // number as service\nsrv1.Observable()\n.subscribe(a =\u003e console.log(\"ser1 data via observable is:\", a));\n\nsrv1.load().then(a =\u003e console.log(\"ser1 data via promis:\", a));\n```\n\n#### two: Pure object as service\n\n```js\nvar srv2 = new AsService({x: 9}); // object as service\n\nsrv2.Observable()\n.subscribe(a =\u003e console.log(\"ser2 data via observable is:\", a));\n\nsrv2.load().then(a =\u003e console.log(\"ser2 data via promis:\", a));\n```\n\n#### three: Function as service (parametric observable)\n\n```js\nvar srv3 = new AsService(param =\u003e param * 3); // function as service\nsrv3.Observable(2) //parametric observe\n.subscribe(a =\u003e console.log(\"ser3 data via observable is:\", a));\n\n//passing (Number) 2  as parameter\nsrv3.load(2).then(a =\u003e console.log(\"ser3 data via promis:\", a));\n```\n\n#### four: Promise as service \n\n```js\nvar ser4 = new AsService(param =\u003e new Promise((res, rej) =\u003e res(`im promise with parameter: ${param}`)));\n\nser4.Observable(\"myparam\")\n.subscribe(a =\u003e console.log(\"srv4: \", a));\n\nser4.load(\"myparam\");\n```\n\n#### five: XHR as Service\n\n \u003eusing built in advanced methods name [ Server ] wraps axios for retrive data from http server and localforge for cache data.\n Following sample uses class [ Server ]  as input of AsService. You can use your own xhr library instead of this.\n\nif  http://myserver/contacts/getcontact.ctrl http REST service exists.\n\n```js\nimport {AsService,Server} from \"@barteh/as-service\"\n\nvar controller1 = (x, y) =\u003e Server.controller(\"contacts\",\"getcontact\", { name: x, lname: y });\n\nvar srv5 = new AsService(controller1);\n\nsrv5.Observable(\"Ahad\", \"Rafat\")\n.subscribe(a =\u003e console.log(\"srv5:\", a));\n```\n\n\n#### six: observe state\n\u003e current state of a service is observable\n    states can be one of [\"start\",\"loading\",\"idle\"]\n```js\n\nvar srv6=new AsService(8);\n\nsrv6.StateObservable(77).subscribe(a=\u003econsole.log(\"current state is: \",a))\n\nsrv6.load(77);\n\n```\n\n#### Output\n\n```cmd\n\n\u003e ser1 data via observable is: 5\n\u003e ser2 data via observable is: { x: 9 }\n\u003e ser3 data via observable is: 6\n\u003e srv4:  im promise with parameter: myparam\n\n```\n\n#### seven: AsService as AsService (Recursive Service)\n\u003e asn  AsService can use argument of constructor with deferent mapper but same loader. this is usefull to derivate a service from other. it important if you want to decrease number of services complexity and increase reusability of code.\n\n```js\nconst ser1=new AsService([5,6,7,8]);\nconst ser2=new AsService(ser1,/*mapper*/ a=\u003ea.map(b=\u003eb*2)); //=\u003e [10,12,14,16]\n``` \n\n#### eight: derive from a Service using  map() operator.\n\u003e you can create new Service derived from another service using map operator. this operator sends both data and parameter to mapper function. mapper parameters can be more than loader parameters. \n```js\n/*map(data,...params)*/\nconst ser1=new AsService((x,y)=\u003ex+y);\nconst ser2=ser1.map((data,x,y,z)=\u003edata+z);\n\nser1.load(/*x*/1,/*y*/,2,/*z*/,3)\n.then(a=\u003econsole.log(a));\n// output \n// \u003e 6\n\n\n\n```\n\n```js\nconst ser1=new AsService([5,6,7,8]);\nconst ser2=ser1.map(a=\u003ea.filter(b=\u003eb\u003c7)); // ==\u003e [5,6]\n\n```\n\n### Test\n\n `npm  test`\n\n## Using Both for web and node js\n\n### Build\n\n `npm run build`\n\n\n### Use in ES5\n\n ```js\n var { AsService } = require(\"@barteh/as-service\");\n\nvar t = new AsService(8);\n\nt.Observable()\n.subscribe(a =\u003e console.log(a))\n ```\n\nLicense: MIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbarteh%2Fas-service","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbarteh%2Fas-service","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbarteh%2Fas-service/lists"}