Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(TestScheduler): remove applyMixins use #7325

Merged
merged 1 commit into from Aug 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion 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';
Expand Down
49 changes: 23 additions & 26 deletions 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<T> extends Observable<T> implements SubscriptionLoggable {
export class ColdObservable<T> extends Observable<T> {
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;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of mixins I'm just adding the methods as function properties on the classes.

logUnsubscribedFrame = logUnsubscribedFrame;

constructor(public messages: TestMessage[], scheduler: Scheduler) {
super(function (this: Observable<T>, subscriber: Subscriber<any>) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved this super argument out to just be declared as a method. It's just less complicated.

const observable: ColdObservable<T> = this as any;
const index = observable.logSubscribedFrame();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In particular, in some environments with CJS and ES2022, applyMixins was not adding the mixing properties to the prototype, and we were getting errors here.

const subscription = new Subscription();
subscription.add(
new Subscription(() => {
observable.logUnsubscribedFrame(index);
})
);
observable.scheduleMessages(subscriber);
return subscription;
});
this.scheduler = scheduler;
protected _subscribe(subscriber: Subscriber<any>): 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<any>) {
Expand All @@ -39,7 +34,10 @@ export class ColdObservable<T> extends Observable<T> 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,
Expand All @@ -49,4 +47,3 @@ export class ColdObservable<T> extends Observable<T> implements SubscriptionLogg
}
}
}
applyMixins(ColdObservable, [SubscriptionLoggable]);
39 changes: 13 additions & 26 deletions src/internal/testing/HotObservable.ts
Expand Up @@ -3,51 +3,38 @@ 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<T> extends Subject<T> implements SubscriptionLoggable {
export class HotObservable<T> extends Subject<T> {
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<any>): Subscription {
const subject: HotObservable<T> = 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));
return subscription;
}

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]);
5 changes: 0 additions & 5 deletions src/internal/testing/SubscriptionLog.ts

This file was deleted.

22 changes: 0 additions & 22 deletions src/internal/testing/SubscriptionLoggable.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/internal/testing/TestScheduler.ts
Expand Up @@ -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';
Expand Down
19 changes: 19 additions & 0 deletions 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;
}
10 changes: 0 additions & 10 deletions src/internal/util/applyMixins.ts

This file was deleted.