diff --git a/spec/helpers/marble-testing.ts b/spec/helpers/marble-testing.ts index bb75d5b89c..3ba9927f5a 100644 --- a/spec/helpers/marble-testing.ts +++ b/spec/helpers/marble-testing.ts @@ -1,5 +1,5 @@ import { Observable } from 'rxjs'; -import { SubscriptionLog } from '../../src/internal/testing/SubscriptionLog'; +import { SubscriptionLog } from '../../src/internal/testing/subscription-logging'; import { ColdObservable } from '../../src/internal/testing/ColdObservable'; import { HotObservable } from '../../src/internal/testing/HotObservable'; import { observableToBeFn, subscriptionLogsToBeFn } from '../../src/internal/testing/TestScheduler'; diff --git a/src/internal/testing/ColdObservable.ts b/src/internal/testing/ColdObservable.ts index 40cbe49a0a..845189039d 100644 --- a/src/internal/testing/ColdObservable.ts +++ b/src/internal/testing/ColdObservable.ts @@ -1,35 +1,30 @@ import { Observable } from '../Observable'; import { Subscription } from '../Subscription'; -import { Scheduler } from '../Scheduler'; import { TestMessage } from './TestMessage'; -import { SubscriptionLog } from './SubscriptionLog'; -import { SubscriptionLoggable } from './SubscriptionLoggable'; -import { applyMixins } from '../util/applyMixins'; import { Subscriber } from '../Subscriber'; import { observeNotification } from '../Notification'; +import { SchedulerLike, TeardownLogic } from '../types'; +import { logSubscribedFrame, logUnsubscribedFrame, SubscriptionLog } from './subscription-logging'; -export class ColdObservable extends Observable implements SubscriptionLoggable { +export class ColdObservable extends Observable { public subscriptions: SubscriptionLog[] = []; - scheduler: Scheduler; - // @ts-ignore: Property has no initializer and is not definitely assigned - logSubscribedFrame: () => number; - // @ts-ignore: Property has no initializer and is not definitely assigned - logUnsubscribedFrame: (index: number) => void; + logSubscribedFrame = logSubscribedFrame; + logUnsubscribedFrame = logUnsubscribedFrame; - constructor(public messages: TestMessage[], scheduler: Scheduler) { - super(function (this: Observable, subscriber: Subscriber) { - const observable: ColdObservable = this as any; - const index = observable.logSubscribedFrame(); - const subscription = new Subscription(); - subscription.add( - new Subscription(() => { - observable.logUnsubscribedFrame(index); - }) - ); - observable.scheduleMessages(subscriber); - return subscription; - }); - this.scheduler = scheduler; + protected _subscribe(subscriber: Subscriber): TeardownLogic { + const index = this.logSubscribedFrame(); + const subscription = new Subscription(); + subscription.add( + new Subscription(() => { + this.logUnsubscribedFrame(index); + }) + ); + this.scheduleMessages(subscriber); + return subscription; + } + + constructor(public messages: TestMessage[], public scheduler: SchedulerLike) { + super(); } scheduleMessages(subscriber: Subscriber) { @@ -39,7 +34,10 @@ export class ColdObservable extends Observable implements SubscriptionLogg subscriber.add( this.scheduler.schedule( (state) => { - const { message: { notification }, subscriber: destination } = state!; + const { + message: { notification }, + subscriber: destination, + } = state!; observeNotification(notification, destination); }, message.frame, @@ -49,4 +47,3 @@ export class ColdObservable extends Observable implements SubscriptionLogg } } } -applyMixins(ColdObservable, [SubscriptionLoggable]); diff --git a/src/internal/testing/HotObservable.ts b/src/internal/testing/HotObservable.ts index c151480620..075c29d430 100644 --- a/src/internal/testing/HotObservable.ts +++ b/src/internal/testing/HotObservable.ts @@ -3,32 +3,27 @@ import { Subscriber } from '../Subscriber'; import { Subscription } from '../Subscription'; import { Scheduler } from '../Scheduler'; import { TestMessage } from './TestMessage'; -import { SubscriptionLog } from './SubscriptionLog'; -import { SubscriptionLoggable } from './SubscriptionLoggable'; -import { applyMixins } from '../util/applyMixins'; import { observeNotification } from '../Notification'; +import { logSubscribedFrame, logUnsubscribedFrame, SubscriptionLog } from './subscription-logging'; -export class HotObservable extends Subject implements SubscriptionLoggable { +export class HotObservable extends Subject { public subscriptions: SubscriptionLog[] = []; - scheduler: Scheduler; - // @ts-ignore: Property has no initializer and is not definitely assigned - logSubscribedFrame: () => number; - // @ts-ignore: Property has no initializer and is not definitely assigned - logUnsubscribedFrame: (index: number) => void; - constructor(public messages: TestMessage[], scheduler: Scheduler) { + logSubscribedFrame = logSubscribedFrame; + + logUnsubscribedFrame = logUnsubscribedFrame; + + constructor(public messages: TestMessage[], public scheduler: Scheduler) { super(); - this.scheduler = scheduler; } /** @internal */ protected _subscribe(subscriber: Subscriber): Subscription { - const subject: HotObservable = this; - const index = subject.logSubscribedFrame(); + const index = this.logSubscribedFrame(); const subscription = new Subscription(); subscription.add( new Subscription(() => { - subject.logUnsubscribedFrame(index); + this.logUnsubscribedFrame(index); }) ); subscription.add(super._subscribe(subscriber)); @@ -36,18 +31,10 @@ export class HotObservable extends Subject implements SubscriptionLoggable } setup() { - const subject = this; - const messagesLength = subject.messages.length; - /* tslint:disable:no-var-keyword */ - for (let i = 0; i < messagesLength; i++) { - (() => { - const { notification, frame } = subject.messages[i]; - /* tslint:enable */ - subject.scheduler.schedule(() => { - observeNotification(notification, subject); - }, frame); - })(); + for (const { notification, frame } of this.messages) { + this.scheduler.schedule(() => { + observeNotification(notification, this); + }, frame); } } } -applyMixins(HotObservable, [SubscriptionLoggable]); diff --git a/src/internal/testing/SubscriptionLog.ts b/src/internal/testing/SubscriptionLog.ts deleted file mode 100644 index 367a6d9691..0000000000 --- a/src/internal/testing/SubscriptionLog.ts +++ /dev/null @@ -1,5 +0,0 @@ -export class SubscriptionLog { - constructor(public subscribedFrame: number, - public unsubscribedFrame: number = Infinity) { - } -} \ No newline at end of file diff --git a/src/internal/testing/SubscriptionLoggable.ts b/src/internal/testing/SubscriptionLoggable.ts deleted file mode 100644 index e8def0419f..0000000000 --- a/src/internal/testing/SubscriptionLoggable.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { Scheduler } from '../Scheduler'; -import { SubscriptionLog } from './SubscriptionLog'; - -export class SubscriptionLoggable { - public subscriptions: SubscriptionLog[] = []; - // @ts-ignore: Property has no initializer and is not definitely assigned - scheduler: Scheduler; - - logSubscribedFrame(): number { - this.subscriptions.push(new SubscriptionLog(this.scheduler.now())); - return this.subscriptions.length - 1; - } - - logUnsubscribedFrame(index: number) { - const subscriptionLogs = this.subscriptions; - const oldSubscriptionLog = subscriptionLogs[index]; - subscriptionLogs[index] = new SubscriptionLog( - oldSubscriptionLog.subscribedFrame, - this.scheduler.now() - ); - } -} diff --git a/src/internal/testing/TestScheduler.ts b/src/internal/testing/TestScheduler.ts index 0045166740..f4ad50d796 100644 --- a/src/internal/testing/TestScheduler.ts +++ b/src/internal/testing/TestScheduler.ts @@ -2,7 +2,7 @@ import { Observable } from '../Observable'; import { ColdObservable } from './ColdObservable'; import { HotObservable } from './HotObservable'; import { TestMessage } from './TestMessage'; -import { SubscriptionLog } from './SubscriptionLog'; +import { SubscriptionLog } from './subscription-logging'; import { Subscription } from '../Subscription'; import { VirtualTimeScheduler, VirtualAction } from '../scheduler/VirtualTimeScheduler'; import { ObservableNotification } from '../types'; diff --git a/src/internal/testing/subscription-logging.ts b/src/internal/testing/subscription-logging.ts new file mode 100644 index 0000000000..6f05905e60 --- /dev/null +++ b/src/internal/testing/subscription-logging.ts @@ -0,0 +1,19 @@ +interface SubscriptionLoggingProps { + subscriptions: SubscriptionLog[]; + scheduler: { now(): number }; +} + +export class SubscriptionLog { + constructor(public subscribedFrame: number, public unsubscribedFrame: number = Infinity) {} +} + +export function logUnsubscribedFrame(this: SubscriptionLoggingProps, index: number) { + const subscriptionLogs = this.subscriptions; + const oldSubscriptionLog = subscriptionLogs[index]; + subscriptionLogs[index] = new SubscriptionLog(oldSubscriptionLog.subscribedFrame, this.scheduler.now()); +} + +export function logSubscribedFrame(this: SubscriptionLoggingProps): number { + this.subscriptions.push(new SubscriptionLog(this.scheduler.now())); + return this.subscriptions.length - 1; +} diff --git a/src/internal/util/applyMixins.ts b/src/internal/util/applyMixins.ts deleted file mode 100644 index 7c1ed24232..0000000000 --- a/src/internal/util/applyMixins.ts +++ /dev/null @@ -1,10 +0,0 @@ -export function applyMixins(derivedCtor: any, baseCtors: any[]) { - for (let i = 0, len = baseCtors.length; i < len; i++) { - const baseCtor = baseCtors[i]; - const propertyKeys = Object.getOwnPropertyNames(baseCtor.prototype); - for (let j = 0, len2 = propertyKeys.length; j < len2; j++) { - const name = propertyKeys[j]; - derivedCtor.prototype[name] = baseCtor.prototype[name]; - } - } -} \ No newline at end of file