rxjs-interop
Advanced tools
Comparing version 1.0.3 to 2.0.0
@@ -0,1 +1,8 @@ | ||
<a name="2.0.0"></a> | ||
## [2.0.0](https://github.com/cartant/rxjs-interop/compare/v1.0.3...v2.0.0) (2021-10-30) | ||
### Breaking Changes | ||
* Update `Subscribable` so that it's compatible with RxJS. ([0e8cee0](https://github.com/cartant/rxjs-interop/commit/0e8cee0)). | ||
<a name="1.0.3"></a> | ||
@@ -2,0 +9,0 @@ ## [1.0.3](https://github.com/cartant/rxjs-interop/compare/v1.0.2...v1.0.3) (2020-12-04) |
import { Observer, PartialObserver } from "./types"; | ||
export declare function toObserver<T>(nextOrObserver?: PartialObserver<T> | ((value: T) => void) | null, error?: ((error: any) => void) | null, complete?: (() => void) | null): Observer<T>; | ||
export declare function toObserver<T>(observer?: PartialObserver<T>): Observer<T>; |
@@ -8,19 +8,17 @@ "use strict"; | ||
}; | ||
function toObserver(nextOrObserver, error, complete) { | ||
if (nextOrObserver && typeof nextOrObserver !== "function") { | ||
if (nextOrObserver.next && | ||
nextOrObserver.error && | ||
nextOrObserver.complete) { | ||
return nextOrObserver; | ||
function toObserver(observer) { | ||
if (observer) { | ||
if (observer.next && observer.error && observer.complete) { | ||
return observer; | ||
} | ||
return { | ||
complete: (nextOrObserver.complete || noop).bind(nextOrObserver), | ||
error: (nextOrObserver.error || rethrow).bind(nextOrObserver), | ||
next: (nextOrObserver.next || noop).bind(nextOrObserver), | ||
complete: (observer.complete ?? noop).bind(observer), | ||
error: (observer.error ?? rethrow).bind(observer), | ||
next: (observer.next ?? noop).bind(observer), | ||
}; | ||
} | ||
return { | ||
complete: complete || noop, | ||
error: error || rethrow, | ||
next: nextOrObserver || noop, | ||
complete: noop, | ||
error: rethrow, | ||
next: noop, | ||
}; | ||
@@ -27,0 +25,0 @@ } |
@@ -10,12 +10,5 @@ export declare type InteropObservable<T> = { | ||
}; | ||
export declare type PartialObserver<T> = Partial<Observer<T>> & ({ | ||
complete(): void; | ||
} | { | ||
error(error: any): void; | ||
} | { | ||
next(value: T): void; | ||
}); | ||
export declare type PartialObserver<T> = Partial<Observer<T>>; | ||
export declare type Subscribable<T> = { | ||
subscribe(observer?: PartialObserver<T>): Unsubscribable; | ||
subscribe(next?: ((value: T) => void) | null, error?: ((error: any) => void) | null, complete?: (() => void) | null): Unsubscribable; | ||
}; | ||
@@ -22,0 +15,0 @@ export declare type Unsubscribable = { |
import { Observer, PartialObserver } from "./types"; | ||
export declare function toObserver<T>(nextOrObserver?: PartialObserver<T> | ((value: T) => void) | null, error?: ((error: any) => void) | null, complete?: (() => void) | null): Observer<T>; | ||
export declare function toObserver<T>(observer?: PartialObserver<T>): Observer<T>; |
@@ -5,21 +5,19 @@ const noop = () => { }; | ||
}; | ||
export function toObserver(nextOrObserver, error, complete) { | ||
if (nextOrObserver && typeof nextOrObserver !== "function") { | ||
if (nextOrObserver.next && | ||
nextOrObserver.error && | ||
nextOrObserver.complete) { | ||
return nextOrObserver; | ||
export function toObserver(observer) { | ||
if (observer) { | ||
if (observer.next && observer.error && observer.complete) { | ||
return observer; | ||
} | ||
return { | ||
complete: (nextOrObserver.complete || noop).bind(nextOrObserver), | ||
error: (nextOrObserver.error || rethrow).bind(nextOrObserver), | ||
next: (nextOrObserver.next || noop).bind(nextOrObserver), | ||
complete: (observer.complete ?? noop).bind(observer), | ||
error: (observer.error ?? rethrow).bind(observer), | ||
next: (observer.next ?? noop).bind(observer), | ||
}; | ||
} | ||
return { | ||
complete: complete || noop, | ||
error: error || rethrow, | ||
next: nextOrObserver || noop, | ||
complete: noop, | ||
error: rethrow, | ||
next: noop, | ||
}; | ||
} | ||
//# sourceMappingURL=to-observer.js.map |
@@ -10,12 +10,5 @@ export declare type InteropObservable<T> = { | ||
}; | ||
export declare type PartialObserver<T> = Partial<Observer<T>> & ({ | ||
complete(): void; | ||
} | { | ||
error(error: any): void; | ||
} | { | ||
next(value: T): void; | ||
}); | ||
export declare type PartialObserver<T> = Partial<Observer<T>>; | ||
export declare type Subscribable<T> = { | ||
subscribe(observer?: PartialObserver<T>): Unsubscribable; | ||
subscribe(next?: ((value: T) => void) | null, error?: ((error: any) => void) | null, complete?: (() => void) | null): Unsubscribable; | ||
}; | ||
@@ -22,0 +15,0 @@ export declare type Unsubscribable = { |
@@ -9,17 +9,17 @@ { | ||
"devDependencies": { | ||
"@cartant/tslint-config": "^2.0.0", | ||
"@cartant/tslint-config-etc": "^2.0.0", | ||
"@cartant/eslint-config": "^3.0.0", | ||
"@cartant/eslint-config-etc": "^3.0.0", | ||
"@types/chai": "^4.0.0", | ||
"@types/mocha": "^8.0.0", | ||
"@types/mocha": "^9.0.0", | ||
"@typescript-eslint/parser": "^5.2.0", | ||
"chai": "^4.1.2", | ||
"cpy-cli": "^3.0.0", | ||
"husky": "^4.2.3", | ||
"lint-staged": "^10.0.8", | ||
"mocha": "^8.0.0", | ||
"prettier": "~2.2.0", | ||
"eslint": "^8.1.0", | ||
"husky": "^7.0.0", | ||
"lint-staged": "^11.0.0", | ||
"mocha": "^9.0.0", | ||
"prettier": "~2.4.0", | ||
"rimraf": "^3.0.0", | ||
"rxjs": "^6.5.4", | ||
"tslint": "^6.0.0", | ||
"tslint-etc": "^1.5.1", | ||
"typescript": "~4.1.2" | ||
"rxjs": "^7.0.0", | ||
"typescript": "~4.4.2" | ||
}, | ||
@@ -31,7 +31,2 @@ "files": [ | ||
"homepage": "https://github.com/cartant/rxjs-interop", | ||
"husky": { | ||
"hooks": { | ||
"pre-commit": "lint-staged" | ||
} | ||
}, | ||
"keywords": [ | ||
@@ -62,3 +57,4 @@ "interop", | ||
"dist:clean": "rimraf dist", | ||
"lint": "tslint --project tsconfig.json source/**/*.ts", | ||
"lint": "eslint \"source/**/*.{js,ts}\"", | ||
"prepare": "husky install", | ||
"prepublishOnly": "yarn run test && yarn run dist", | ||
@@ -73,3 +69,3 @@ "prettier": "prettier --write \"./**/{scripts,source}/**/*.{js,json,ts}\"", | ||
"types": "./dist/esm/index.d.ts", | ||
"version": "1.0.3" | ||
"version": "2.0.0" | ||
} |
@@ -5,3 +5,3 @@ /** | ||
*/ | ||
/*tslint:disable:no-unused-expression*/ | ||
/* eslint-disable no-unused-expressions */ | ||
@@ -12,11 +12,7 @@ import { expect } from "chai"; | ||
import { toObserver } from "./to-observer"; | ||
import { PartialObserver, Subscribable } from "./types"; | ||
import { InteropObservable, PartialObserver, Subscribable } from "./types"; | ||
const subscribable: Subscribable<number> = { | ||
subscribe( | ||
nextOrObserver?: PartialObserver<number> | ((value: number) => void) | null, | ||
error?: ((error: any) => void) | null, | ||
complete?: (() => void) | null | ||
) { | ||
const observer = toObserver(nextOrObserver, error, complete); | ||
subscribe(partialObserver?: PartialObserver<number>) { | ||
const observer = toObserver(partialObserver); | ||
observer.next(42); | ||
@@ -51,5 +47,8 @@ return { unsubscribe: () => {} }; | ||
it("should return a passed function", () => { | ||
const interop = ( | ||
const interop = (( | ||
...args: Parameters<Subscribable<number>["subscribe"]> | ||
) => subscribable.subscribe(...args); | ||
) => | ||
subscribable.subscribe( | ||
...args | ||
)) as unknown as InteropObservable<number>; | ||
interop[Symbol.observable] = (): Subscribable<number> => { | ||
@@ -107,5 +106,8 @@ return subscribable; | ||
const values: number[] = []; | ||
const interop = ( | ||
const interop = (( | ||
...args: Parameters<Subscribable<number>["subscribe"]> | ||
) => subscribable.subscribe(...args); | ||
) => | ||
subscribable.subscribe( | ||
...args | ||
)) as unknown as InteropObservable<number>; | ||
interop[Symbol.observable] = (): Subscribable<number> => { | ||
@@ -121,3 +123,3 @@ return subscribable; | ||
describe("with Symbol.observable", () => { | ||
let symbol: Symbol; | ||
let symbol: symbol; | ||
@@ -169,5 +171,8 @@ beforeEach(() => { | ||
const values: number[] = []; | ||
const interop = ( | ||
const interop = (( | ||
...args: Parameters<Subscribable<number>["subscribe"]> | ||
) => subscribable.subscribe(...args); | ||
) => | ||
subscribable.subscribe( | ||
...args | ||
)) as unknown as InteropObservable<number>; | ||
interop[Symbol.observable] = (): Subscribable<number> => { | ||
@@ -174,0 +179,0 @@ return subscribable; |
@@ -7,3 +7,2 @@ /** | ||
declare global { | ||
/*tslint:disable-next-line:no-unused-declaration*/ | ||
interface SymbolConstructor { | ||
@@ -10,0 +9,0 @@ readonly observable: symbol; |
@@ -48,14 +48,4 @@ /** | ||
it("should support separate arguments", () => { | ||
const next = () => {}; | ||
const error = () => {}; | ||
const complete = () => {}; | ||
const observer = toObserver(next, error, complete); | ||
expect(observer.next).to.equal(next); | ||
expect(observer.error).to.equal(error); | ||
expect(observer.complete).to.equal(complete); | ||
}); | ||
it("should support undefined arguments", () => { | ||
const observer = toObserver(undefined, undefined, undefined); | ||
it("should support an undefined argument", () => { | ||
const observer = toObserver(undefined); | ||
expect(observer.next).to.be.a("function"); | ||
@@ -66,9 +56,2 @@ expect(observer.error).to.be.a("function"); | ||
it("should support null arguments", () => { | ||
const observer = toObserver(null, null, null); | ||
expect(observer.next).to.be.a("function"); | ||
expect(observer.error).to.be.a("function"); | ||
expect(observer.complete).to.be.a("function"); | ||
}); | ||
it("should throw if an error method is not specified", () => { | ||
@@ -81,7 +64,2 @@ const observer = toObserver({ | ||
}); | ||
it("should throw if an error handler is not specified", () => { | ||
const observer = toObserver(() => {}); | ||
expect(() => observer.error("Kaboom!")).to.throw("Kaboom!"); | ||
}); | ||
}); |
@@ -10,30 +10,22 @@ /** | ||
const rethrow = (error: unknown) => { | ||
/* tslint:disable-next-line throw-error */ | ||
/* eslint-disable-next-line etc/throw-error */ | ||
throw error; | ||
}; | ||
export function toObserver<T>( | ||
nextOrObserver?: PartialObserver<T> | ((value: T) => void) | null, | ||
error?: ((error: any) => void) | null, | ||
complete?: (() => void) | null | ||
): Observer<T> { | ||
if (nextOrObserver && typeof nextOrObserver !== "function") { | ||
if ( | ||
nextOrObserver.next && | ||
nextOrObserver.error && | ||
nextOrObserver.complete | ||
) { | ||
return nextOrObserver as Observer<T>; | ||
export function toObserver<T>(observer?: PartialObserver<T>): Observer<T> { | ||
if (observer) { | ||
if (observer.next && observer.error && observer.complete) { | ||
return observer as Observer<T>; | ||
} | ||
return { | ||
complete: (nextOrObserver.complete || noop).bind(nextOrObserver), | ||
error: (nextOrObserver.error || rethrow).bind(nextOrObserver), | ||
next: (nextOrObserver.next || noop).bind(nextOrObserver), | ||
complete: (observer.complete ?? noop).bind(observer), | ||
error: (observer.error ?? rethrow).bind(observer), | ||
next: (observer.next ?? noop).bind(observer), | ||
}; | ||
} | ||
return { | ||
complete: complete || noop, | ||
error: error || rethrow, | ||
next: nextOrObserver || noop, | ||
complete: noop, | ||
error: rethrow, | ||
next: noop, | ||
}; | ||
} |
@@ -17,16 +17,6 @@ /** | ||
export type PartialObserver<T> = Partial<Observer<T>> & | ||
( | ||
| { complete(): void } | ||
| { error(error: any): void } | ||
| { next(value: T): void } | ||
); | ||
export type PartialObserver<T> = Partial<Observer<T>>; | ||
export type Subscribable<T> = { | ||
subscribe(observer?: PartialObserver<T>): Unsubscribable; | ||
subscribe( | ||
next?: ((value: T) => void) | null, | ||
error?: ((error: any) => void) | null, | ||
complete?: (() => void) | null | ||
): Unsubscribable; | ||
}; | ||
@@ -33,0 +23,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Unidentified License
License(Experimental) Something that seems like a license was found, but its contents could not be matched with a known license.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Unidentified License
License(Experimental) Something that seems like a license was found, but its contents could not be matched with a known license.
Found 1 instance in 1 package
27264
488