Skip to content

Commit f4e3a8e

Browse files
authored
Merge pull request #1 from tusharmath/develop
Develop
2 parents 1a76442 + 462f6b0 commit f4e3a8e

File tree

5 files changed

+88
-37
lines changed

5 files changed

+88
-37
lines changed

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Observable Air
2+
3+
[![Build Status](https://travis-ci.org/tusharmath/rwc.svg?branch=master)](https://travis-ci.org/tusharmath/observable-air)
4+
[![npm](https://img.shields.io/npm/v/observable-air.svg)](https://www.npmjs.com/package/observable-air)
5+
[![Coverage Status](https://coveralls.io/repos/github/tusharmath/observable-air/badge.svg)](https://coveralls.io/github/tusharmath/observable-air)
6+
7+
A light weight and performance focused implementation of [Observables].
8+
Other mainstream (no pun intended) alternatives —
9+
10+
[Observables]: https://github.com/tc39/proposal-observable
11+
12+
- [most]
13+
- [rxjs]
14+
- [kefir]
15+
- [bacon]
16+
- [highland]
17+
- [zen observable]
18+
19+
[most]: https://github.com/cujojs/most
20+
[rxjs]: https://github.com/ReactiveX/rxjs
21+
[kefir]: https://rpominov.github.io/kefir/
22+
[bacon]: https://baconjs.github.io/
23+
[highland]: http://highlandjs.org/
24+
[zen observable]: https://github.com/zenparsing/zen-observable
25+
26+
### Key Features
27+
28+
1. **Ultra high performance**
29+
2. **Small footprint**
30+
3. **Ease of testability**

package.json

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,31 @@
33
"description": "Observables for the calorie conscious",
44
"main": "src/Observable",
55
"scripts": {
6-
"test": "ava",
7-
"lint": "tslint ./src/**.ts",
8-
"hydra": "node --trace-hydrogen --trace-phase=Z --trace-deopt --code-comments --hydrogen-track-positions --redirect-code-traces --redirect-code-traces-to=code.asm chore/benchmark.js",
9-
"rfc": "node chore/rfc",
106
"benchmark": "date >> benchmark.md && node chore/benchmark >> benchmark.md",
7+
"cleanup": "rm -rf ./src/**/*.js && rm -rf ./src/**/*.map",
8+
"coverage": "nyc npm test && nyc report --reporter=text-lcov | coveralls",
9+
"hydra": "node --trace-hydrogen --trace-phase=Z --trace-deopt --code-comments --hydrogen-track-positions --redirect-code-traces --redirect-code-traces-to=code.asm chore/benchmark.js",
10+
"lint": "tslint ./src/**.ts",
1111
"postinstall": "tsc",
12-
"semantic-release": "semantic-release pre && npm publish && semantic-release post"
12+
"rfc": "node chore/rfc",
13+
"semantic-release": "semantic-release pre && npm publish && semantic-release post",
14+
"test": "ava"
1315
},
1416
"author": "",
1517
"license": "ISC",
1618
"devDependencies": {
1719
"ava": "^0.16.0",
1820
"benchmark": "^2.1.1",
21+
"coveralls": "^2.11.14",
1922
"cz-conventional-changelog": "^1.2.0",
2023
"es-observable-tests": "^0.3.0",
24+
"ghooks": "^1.3.2",
25+
"nyc": "^8.3.1",
2126
"semantic-release": "^4.3.5",
27+
"tslint": "^3.15.1",
2228
"tslint-microsoft-contrib": "^2.0.12",
23-
"typescript": "^2.0.3"
29+
"typescript": "^2.0.3",
30+
"validate-commit-msg": "^2.8.2"
2431
},
2532
"repository": {
2633
"type": "git",
@@ -29,6 +36,13 @@
2936
"config": {
3037
"commitizen": {
3138
"path": "./node_modules/cz-conventional-changelog"
39+
},
40+
"ghooks": {
41+
"pre-commit": "npm run lint",
42+
"commit-msg": "validate-commit-msg",
43+
"pre-push": "npm test",
44+
"post-merge": "npm install",
45+
"post-rewrite": "npm install"
3246
}
3347
}
3448
}

src/Observable.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,15 @@ import {Subscription, CompositeSubscription} from './Subscription';
1212
import {SubscriptionObserver} from './SubscriptionObserver';
1313
import {SafeExecutor} from './lib/SafeExecutor';
1414
import {Safety} from './types/ISafeValue';
15+
import {ISubscriptionObserver} from './types/core/ISubscriptionObserver';
1516

1617

18+
function startObserver <T> (observer: IObserver<T>,
19+
subscription: CompositeSubscription) {
20+
if (observer.start)
21+
observer.start(subscription)
22+
}
23+
1724
export class Observable<T> implements IObservable<T> {
1825
constructor (private func: ISubscriberFunction<T>) {
1926
}
@@ -27,21 +34,18 @@ export class Observable<T> implements IObservable<T> {
2734
})
2835
}
2936

30-
safelyExecuteFunc (observer: IObserver<T>, cSub: CompositeSubscription) {
31-
const r = SafeExecutor(() => {
32-
cSub.add(Subscription.from(this.func(SubscriptionObserver.from(observer))))
33-
})
34-
if (r.type === Safety.error && observer.error) {
35-
observer.error(r.value as Error)
36-
}
37+
private safelyExecuteFunc (observer: ISubscriptionObserver<T>, cSub: CompositeSubscription) {
38+
const r = SafeExecutor(() => cSub.add(Subscription.from(this.func(observer))))
39+
if (r.type === Safety.error) observer.error(r.value as Error)
3740
}
3841

39-
subscribe (observer: IObserver<T>, scheduler: IScheduler = new DefaultScheduler()): ISubscription {
40-
if (typeof observer !== 'object') throw new TypeError('Observer should be of object type')
42+
subscribe (observer: IObserver<T> | ((t: T) => void), scheduler: IScheduler = new DefaultScheduler()): ISubscription {
43+
const subObserver = SubscriptionObserver.from(observer)
4144
const subscription = new CompositeSubscription()
42-
const task = () => this.safelyExecuteFunc(observer, subscription);
45+
const task = () => this.safelyExecuteFunc(subObserver, subscription);
4346
subscription.add(scheduler.scheduleNow(task))
44-
if (observer.start) observer.start(subscription)
47+
startObserver(observer as IObserver<T>, subscription);
4548
return subscription
4649
}
50+
4751
}

src/SubscriptionObserver.ts

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,42 +5,45 @@
55
import {ISubscriptionObserver} from './types/core/ISubscriptionObserver';
66
import {IObserver} from './types/core/IObserver';
77

8-
export class SubscriptionObserverStub <T> implements ISubscriptionObserver<T> {
9-
closed: boolean;
10-
11-
next (val: T): void {
12-
}
13-
14-
error (err: Error): void {
15-
}
16-
17-
complete (): void {
18-
}
8+
export function noop () {
9+
}
10+
export function defaultOnError (err: Error) {
11+
throw err
1912
}
2013

14+
2115
export class SubscriptionObserver<T> implements ISubscriptionObserver<T> {
2216
closed: boolean;
2317

24-
constructor (private sink: IObserver<T>) {
18+
constructor (private onNext: (val: T) => void,
19+
private onError: (err: Error) => void,
20+
private onComplete: () => void) {
2521
this.closed = false
2622
}
2723

2824
next (val: T): void {
29-
this.sink.next(val)
25+
this.onNext(val)
3026
}
3127

3228
error (err: Error): void {
33-
this.sink.error(err)
29+
this.onError(err)
3430
}
3531

3632
complete (): void {
37-
this.sink.complete()
33+
this.onComplete()
3834
this.closed = true
3935
}
4036

41-
static from<T> (observer: IObserver<T>) {
42-
if (!observer.next || !observer.complete || !observer.error)
43-
return new SubscriptionObserverStub()
44-
return new SubscriptionObserver(observer)
37+
static from<T> (observer: IObserver<T> | ((t: T) => void)): ISubscriptionObserver<T> {
38+
const type = typeof observer
39+
if (type !== 'object' && type !== 'function') throw new TypeError()
40+
if (observer instanceof SubscriptionObserver) return observer
41+
if (typeof observer === 'function')
42+
return new SubscriptionObserver(observer, defaultOnError, noop)
43+
return new SubscriptionObserver(
44+
observer.next ? (t: T) => observer.next(t) : noop,
45+
observer.error ? (t: Error) => observer.error(t) : defaultOnError,
46+
observer.complete ? () => observer.complete() : noop
47+
)
4548
}
4649
}

src/testing/TestObservable.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ export class TestObservable<T> implements IObservable<T> {
1616
}
1717

1818
subscribe (observer: IObserver<T>, scheduler: IScheduler): ISubscription {
19-
return Subscription.from(this.func(new SubscriptionObserver(observer)))
19+
return Subscription.from(this.func(SubscriptionObserver.from(observer)))
2020
}
2121
}

0 commit comments

Comments
 (0)