Skip to content

Commit

Permalink
Updated api-reactive doc.
Browse files Browse the repository at this point in the history
  • Loading branch information
puckowski committed Dec 5, 2019
1 parent 400cb25 commit ab1daf2
Showing 1 changed file with 51 additions and 9 deletions.
60 changes: 51 additions & 9 deletions docs/api-reactive.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Reactive API

## s.stream
__object s.stream( )__
## s.Stream
__object s.Stream( )__

Returns a Sling stream. A stream is a sequence of values over time and the associated operations which are automatically applied as those values change.

Expand All @@ -10,7 +10,7 @@ Example stream usage using Sling XHR API:
```javascript
s.get('https://jsonplaceholder.typicode.com/posts').then(xhrResp => {
let postArr = JSON.parse(xhrResp.response);
let postStream = s.stream().from(postArr).transform(function(arr) {
let postStream = s.Stream().from(postArr).transform(function(arr) {
return arr.filter(v => v.userId === 1);
}).transform(function(arr) {
return arr.filter(v => v.body.includes('quo'));
Expand All @@ -21,7 +21,7 @@ s.get('https://jsonplaceholder.typicode.com/posts').then(xhrResp => {
Equivalent stream usage using preexisting stream object and Sling XHR API:

```javascript
let postStream2 = s.stream();
let postStream2 = s.Stream();
postStream2.transform(function(arr) {
return arr.filter(v => v.userId === 1);
}).transform(function(arr) {
Expand Down Expand Up @@ -83,7 +83,7 @@ __object from ( newArray )__

Set stream data to ```newArray``` and apply all existing transformers. Returns the stream.

## s.observable
## s.Observable
__object s.observable( array )__

Returns a Sling observable. An observable is an array which may be listened to.
Expand All @@ -92,13 +92,13 @@ Example observable usage:

```javascript
let myArray = [1, 2, 3];
let myObservable = s.observable(myArray);
let myObservable = s.Observable(myArray);
myObservable.subscribe(function(arr) {
console.log('New length: ' + arr.length);
console.log('New length: ' + arr.length);
});

myObservable.getData().push(1);
obs.getData()[myObservable.getData().length] = 3;
myObservable.getData().push(4);
obs.getData()[myObservable.getData().length] = 5;
```

## Observable Functions
Expand All @@ -122,3 +122,45 @@ Remove all subscribed functions. Returns the observable.
__[ ] getData( )__

Get the underlying array data.

## s.BehaviorSubject
__object s.BehaviorSubject( value )__

Returns a Sling behavior subject. A behavior subject is a value that emits changes to subscribers.

Example behavior subject usage:

```javascript
let subject = s.BehaviorSubject(5);
subject.next(subject.getData() + 1);
let value = subject.getData(); // 6

subject.subscribe(function (value) { console.log('Value: ' + value); });
```

## BehaviorSubject Functions

## subscribe
__void subscribe ( listenerFunction )__

Listener function will be automatically called whenever the subject's value changes. Returns the behavior subject.

## clearSubscription
__object clearSubscription( functionToClear )__

Remove ```functionToClear``` from the list of subscribed functions. Returns the behavior subject.

## clearSubscriptions
__object clearSubscriptions( )__

Remove all subscribed functions. Returns the behavior subject.

## next
__object next( value )__

Set the next value of the subject. All subscribers are automatically called. Returns the behavior subject.

## getData
__[ ] getData( )__

Get the underlying value.

0 comments on commit ab1daf2

Please sign in to comment.