-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/** | ||
* @license Use of this source code is governed by an MIT-style license that | ||
* can be found in the LICENSE file at https://github.com/cartant/rxjs-etc | ||
*/ | ||
/*tslint:disable:no-unused-expression*/ | ||
|
||
import { expect } from "chai"; | ||
import { defer, of, queueScheduler } from "rxjs"; | ||
import { publish } from "rxjs/operators"; | ||
import { refCountOn } from "./refCountOn"; | ||
|
||
describe("refCountOn", () => { | ||
|
||
it("should support queue-scheduled actions", () => { | ||
|
||
let received = false; | ||
let subscribed = false; | ||
|
||
const source = defer(() => { subscribed = true; return of("foo"); }); | ||
|
||
queueScheduler.schedule(() => { | ||
const subscription = source.pipe( | ||
publish(), | ||
refCountOn(queueScheduler) | ||
).subscribe(() => received = true); | ||
subscription.unsubscribe(); | ||
}); | ||
|
||
expect(received).to.be.false; | ||
expect(subscribed).to.be.false; | ||
|
||
queueScheduler.schedule(() => { | ||
const subscription = source.pipe( | ||
publish(), | ||
refCountOn(queueScheduler) | ||
).subscribe(() => received = true); | ||
queueScheduler.schedule(() => { | ||
subscription.unsubscribe(); | ||
}); | ||
}); | ||
|
||
expect(received).to.be.true; | ||
expect(subscribed).to.be.true; | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/** | ||
* @license Use of this source code is governed by an MIT-style license that | ||
* can be found in the LICENSE file at https://github.com/cartant/rxjs-etc | ||
*/ | ||
|
||
import { | ||
ConnectableObservable, | ||
MonoTypeOperatorFunction, | ||
Observable, | ||
SchedulerLike, | ||
Subscription, | ||
using | ||
} from "rxjs"; | ||
|
||
export function refCountOn<T>(scheduler: SchedulerLike): MonoTypeOperatorFunction<T> { | ||
|
||
return (source: Observable<T>) => { | ||
|
||
const connectable: ConnectableObservable<T> = source as any; | ||
let count = 0; | ||
let subscription: Subscription | null = null; | ||
|
||
return using(() => { | ||
++count; | ||
scheduler.schedule(() => { | ||
if (!subscription && (count > 0)) { | ||
subscription = connectable.connect(); | ||
} | ||
}); | ||
return { | ||
unsubscribe: () => { | ||
--count; | ||
scheduler.schedule(() => { | ||
if (subscription && (count === 0)) { | ||
subscription.unsubscribe(); | ||
subscription = null; | ||
} | ||
}); | ||
} | ||
}; | ||
}, () => source); | ||
}; | ||
} |