Skip to content

Commit

Permalink
Update README and the example files
Browse files Browse the repository at this point in the history
  • Loading branch information
maniator committed May 4, 2017
1 parent c84b7b6 commit 6cf9cda
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 53 deletions.
3 changes: 3 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
.idea
npm-debug.log
node_modules
example
config
release.js
143 changes: 101 additions & 42 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,55 +10,114 @@ npm i servable --save

---------

It can be used as following:
It can be used like so:

```
const { Observable } = require('servable');
const Observable = require('servable').Observable;
const countObservable$ = new Observable(function ({ next, error, complete }) {
let number = 0;
const runNext = function () {
next(number+=1);
const countObservable$ = Observable.interval(1000, 1);
const countSubscription = countObservable$
.take(10)
.map((n) => n * 5)
.filter((n) => n > 10)
.subscribe({
next (number) {
console.log('NEXT NUMBER: ', number);
},
error (errors) {
console.warn('I HAVE ERRORS', errors)
},
if (number < 10) {
setTimeout(runNext, 1000);
} else {
complete();
complete () {
console.log('I AM COMPLETE');
}
};
runNext();
// can (optionally) return a function to run when the observer is unsubscribed to
return function () {
console.log('I AM UNSUBSCRIBED');
};
}); // function will not run until subscribed to
const subscription = countObservable$.subscribe({
next (number) {
console.log('NEXT NUMBER: ', number);
},
complete () {
console.log('I AM COMPLETE');
}
});
});
```

This will log out to the console (if subscribe like above is called):
```
NEXT NUMBER: 1
NEXT NUMBER: 2 // after a 1 second delay
NEXT NUMBER: 3 // after a 1 second delay
NEXT NUMBER: 4 // after a 1 second delay
NEXT NUMBER: 5 // after a 1 second delay
NEXT NUMBER: 6 // after a 1 second delay
NEXT NUMBER: 7 // after a 1 second delay
NEXT NUMBER: 8 // after a 1 second delay
NEXT NUMBER: 9 // after a 1 second delay
NEXT NUMBER: 10 // after a 1 second delay
I AM UNSUBSCRIBED // same time as previous line
NEXT NUMBER: 15
NEXT NUMBER: 20 // after a 1 second delay
NEXT NUMBER: 25 // after a 1 second delay
NEXT NUMBER: 30 // after a 1 second delay
NEXT NUMBER: 35 // after a 1 second delay
NEXT NUMBER: 40 // after a 1 second delay
NEXT NUMBER: 45 // after a 1 second delay
NEXT NUMBER: 50 // after a 1 second delay
I AM COMPLETE // same time as previous line
```
```

### There a few plugins that are available - each return a new Observable instance:

- They are called by doing: `observableInstance$.<pluginFunction>`

`.map(<mapCallback>)`

- will map each value to a new value using the callback

`.filter(<filterCallback)`

- will filter out values that you do not want to subscribe to

`.do(<doCallback>)`

- will run some callback before passing the current value to the subscription

`.take(<amount>[, <callback>])`

- will take the amount of values you want (or less if it completes first) and then complete -- this is useful for infinitely running observables
- a callback may be passed to filter out the valeus you want to take

`.first([<callback>])`

- this is an alias for `.take(1[, <callback>])`

`.toPromise()`

- this will do a `.first()` on the observable object and return the value from that to the response from a promise

`.flatMap(<mapCallback>)`

- same as `.map(<mapCallback>)` but will take the value of the callback and turn it from an observable to a value

`.switchMap(<mapCallback>)`

- the value from the mapCallback is an observable and if another value comes through the previous observable is cancelled
- this is useful for things like typeaheads where you dont want a request for every keypress

`.delay(<time>)`

- will delay output from the observable until a specific time interval has passed

`.debounceTime(<time>)`

- will only output values if there has not been any new values in the past time interval passed

### There are also a few Observables that can be created from the Observable object itself:

- They are called by doing: `Observable.<observableFunction>`

`.fromEvent(<eventName>, <element>[, <mapCallback>)`

- will create an observable that will listen for an event on a DOM element
- there is also an optional mappCallback that can be used to map the event that comes through the next observer function

`.fromPromise(promise)`

- turns a promise into an observable that emits the value of the promise and then completes

`.interval(<time>[, <start = 0>])`

- emits a number at the specified time interval and increases by one every time
- there is an optional start value that can be passed

`.of(<...values>)`

- takes any number of arguments and emits them in an observable (in order) and then completes

`.range(<start>[, <end>])`

- emits a range of numbers from start to end and then completes
- if only start is given than the range starts at 0 and ends at the start value
4 changes: 1 addition & 3 deletions example/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,18 @@ const { Observable } = Servable;
// });

// test event binding
const inputObservable$ = Observable.fromEvent('input', document.getElementById('myInput'));
const inputObservable$ = Observable.fromEvent('input', document.getElementById('myInput'), (event) => event.currentTarget.value);
const divReverse = document.getElementById('myTextReverse');
const div = document.getElementById('myText');

console.log(inputObservable$);

inputObservable$
.map((event, element) => element.value)
.map((text) => text.split('').reverse().join(''))
.do((text) => divReverse.textContent = text)
.subscribe();

inputObservable$
.map((event, element) => element.value)
.do((text) => div.textContent = text)
.subscribe();

8 changes: 2 additions & 6 deletions release.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,12 @@ inquirer.prompt(questions).then(function(answers) {
bumpVersion('package.json', pkg, dryRun) &&
run('npm run build', dryRun) &&
run('git add .', dryRun) &&
run('git add -f dist', dryRun) &&
run('git commit -m "' + newVerison + '"', dryRun) &&
run('git push', dryRun) &&
run('git tag -a ' + newVerison + ' -m "v' + newVerison + '"', dryRun) &&
run('git push origin --tags', dryRun) &&
run('npm publish', dryRun) &&
run('git rm -r dist', dryRun) &&
run('git commit -m "cleanup repository after release"', dryRun) &&
run('git push', dryRun)
})
run('npm publish', dryRun)
});

function bumpVersion(fileName, obj, dry) {
console.log('Bumping version in `' + fileName + '` to ' + obj.version)
Expand Down
4 changes: 2 additions & 2 deletions src/observables/fromEvent.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Observable } from '../Observable';
import { makeHot } from '../utilities/makeHot';

export const fromEvent = function (eventName, element) {
export const fromEvent = function (eventName, element, mapCallback = (v) => v) {
return makeHot(new Observable(function ({ next }) {
const listener = function (event) {
next(event, element);
next(mapCallback(event));
};

element.addEventListener(eventName, listener, false);
Expand Down
1 change: 1 addition & 0 deletions src/operators/toPromise.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const toPromise = function (source$) {
return first(source$).subscribe({
next: respond,
error: reject,
complete: respond,
});
});
};
Expand Down

0 comments on commit 6cf9cda

Please sign in to comment.