Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

A super simple and easy to use reactive library for subscribing to any data. contains a hook for react js.

License

NotificationsYou must be signed in to change notification settings

barteh/as-service

Repository files navigation

Build Status

AsService

Use any data as a service

A super simple and easy to use reactive library for subscribing to any data. contains a hook for react js.


What does it exactly do?

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.

Install

npm i@barteh/as-service --save

Usage

Import library

import{AsService,Server}from"@barteh/as-service";

one: Primitive type (number | string | Array) as service

varsrv1=newAsService(5);// number as servicesrv1.Observable().subscribe(a=>console.log("ser1 data via observable is:",a));srv1.load().then(a=>console.log("ser1 data via promis:",a));

two: Pure object as service

varsrv2=newAsService({x:9});// object as servicesrv2.Observable().subscribe(a=>console.log("ser2 data via observable is:",a));srv2.load().then(a=>console.log("ser2 data via promis:",a));

three: Function as service (parametric observable)

varsrv3=newAsService(param=>param*3);// function as servicesrv3.Observable(2)//parametric observe.subscribe(a=>console.log("ser3 data via observable is:",a));//passing (Number) 2  as parametersrv3.load(2).then(a=>console.log("ser3 data via promis:",a));

four: Promise as service

varser4=newAsService(param=>newPromise((res,rej)=>res(`im promise with parameter:${param}`)));ser4.Observable("myparam").subscribe(a=>console.log("srv4: ",a));ser4.load("myparam");

five: XHR as Service

using built in advanced methods name [ Server ] wraps axios for retrive data from http server and localforge for cache data.Following sample uses class [ Server ] as input of AsService. You can use your own xhr library instead of this.

ifhttp://myserver/contacts/getcontact.ctrl http REST service exists.

import{AsService,Server}from"@barteh/as-service"varcontroller1=(x,y)=>Server.controller("contacts","getcontact",{name:x,lname:y});varsrv5=newAsService(controller1);srv5.Observable("Ahad","Rafat").subscribe(a=>console.log("srv5:",a));

six: observe state

current state of a service is observablestates can be one of ["start","loading","idle"]

varsrv6=newAsService(8);srv6.StateObservable(77).subscribe(a=>console.log("current state is: ",a))srv6.load(77);

Output

> ser1 data via observable is:5> ser2 data via observable is: { x:9 }> ser3 data via observable is:6> srv4:  im promise with parameter: myparam

seven: AsService as AsService (Recursive Service)

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.

constser1=newAsService([5,6,7,8]);constser2=newAsService(ser1,/*mapper*/a=>a.map(b=>b*2));//=> [10,12,14,16]

eight: derive from a Service using map() operator.

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.

/*map(data,...params)*/constser1=newAsService((x,y)=>x+y);constser2=ser1.map((data,x,y,z)=>data+z);ser1.load(/*x*/1,/*y*/,2,/*z*/,3).then(a=>console.log(a));// output// > 6
constser1=newAsService([5,6,7,8]);constser2=ser1.map(a=>a.filter(b=>b<7));// ==> [5,6]

Test

npm test

Using Both for web and node js

Build

npm run build

Use in ES5

var{ AsService}=require("@barteh/as-service");vart=newAsService(8);t.Observable().subscribe(a=>console.log(a))

License: MIT


[8]ページ先頭

©2009-2025 Movatter.jp