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

chore: use @internal and _ for internals; remove protected modifiers #6215

Merged
merged 15 commits into from
Apr 19, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 4 additions & 15 deletions api_guard/dist/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,13 @@ export declare const async: AsyncScheduler;
export declare const asyncScheduler: AsyncScheduler;

export declare class AsyncSubject<T> extends Subject<T> {
protected _checkFinalizedStatuses(subscriber: Subscriber<T>): void;
complete(): void;
next(value: T): void;
}

export declare class BehaviorSubject<T> extends Subject<T> {
get value(): T;
constructor(_value: T);
protected _subscribe(subscriber: Subscriber<T>): Subscription;
getValue(): T;
next(value: T): void;
}
Expand Down Expand Up @@ -88,7 +86,6 @@ export declare class ConnectableObservable<T> extends Observable<T> {
source: Observable<T>;
protected subjectFactory: () => Subject<T>;
constructor(source: Observable<T>, subjectFactory: () => Subject<T>);
protected _subscribe(subscriber: Subscriber<T>): Subscription;
protected _teardown(): void;
connect(): Subscription;
protected getSubject(): Subject<T>;
Expand Down Expand Up @@ -253,14 +250,12 @@ export declare const ObjectUnsubscribedError: ObjectUnsubscribedErrorCtor;
export declare const observable: string | symbol;

export declare class Observable<T> implements Subscribable<T> {
protected operator: Operator<any, T> | undefined;
protected source: Observable<any> | undefined;
operator: Operator<any, T> | undefined;
source: Observable<any> | undefined;
constructor(subscribe?: (this: Observable<T>, subscriber: Subscriber<T>) => TeardownLogic);
protected _subscribe(subscriber: Subscriber<any>): TeardownLogic;
protected _trySubscribe(sink: Subscriber<T>): TeardownLogic;
forEach(next: (value: T) => void): Promise<void>;
forEach(next: (value: T) => void, promiseCtor: PromiseConstructorLike): Promise<void>;
protected lift<R>(operator?: Operator<T, R>): Observable<R>;
lift<R>(operator?: Operator<T, R>): Observable<R>;
pipe(): Observable<T>;
pipe<A>(op1: OperatorFunction<T, A>): Observable<A>;
pipe<A, B>(op1: OperatorFunction<T, A>, op2: OperatorFunction<A, B>): Observable<B>;
Expand Down Expand Up @@ -365,8 +360,7 @@ export interface ReadableStreamLike<T> {
}

export declare class ReplaySubject<T> extends Subject<T> {
constructor(bufferSize?: number, windowTime?: number, timestampProvider?: TimestampProvider);
protected _subscribe(subscriber: Subscriber<T>): Subscription;
constructor(_bufferSize?: number, _windowTime?: number, _timestampProvider?: TimestampProvider);
next(value: T): void;
}

Expand Down Expand Up @@ -399,11 +393,6 @@ export declare class Subject<T> extends Observable<T> implements SubscriptionLik
observers: Observer<T>[];
thrownError: any;
constructor();
protected _checkFinalizedStatuses(subscriber: Subscriber<any>): void;
protected _innerSubscribe(subscriber: Subscriber<any>): Subscription;
protected _subscribe(subscriber: Subscriber<T>): Subscription;
protected _throwIfClosed(): void;
protected _trySubscribe(subscriber: Subscriber<T>): TeardownLogic;
asObservable(): Observable<T>;
complete(): void;
error(err: any): void;
Expand Down
2 changes: 0 additions & 2 deletions api_guard/dist/types/webSocket/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
export declare function webSocket<T>(urlConfigOrSource: string | WebSocketSubjectConfig<T>): WebSocketSubject<T>;

export declare class WebSocketSubject<T> extends AnonymousSubject<T> {
_output: Subject<T>;
constructor(urlConfigOrSource: string | WebSocketSubjectConfig<T> | Observable<T>, destination?: Observer<T>);
_subscribe(subscriber: Subscriber<T>): Subscription;
lift<R>(operator: Operator<T, R>): WebSocketSubject<R>;
multiplex(subMsg: () => any, unsubMsg: () => any, messageFilter: (value: T) => boolean): Observable<any>;
unsubscribe(): void;
Expand Down
2 changes: 1 addition & 1 deletion docs_app/src/app/custom-elements/toc/toc.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ class TestTocService {
activeItemIndex = new BehaviorSubject<number | null>(null);
setActiveIndex(index: number|null) {
this.activeItemIndex.next(index);
if (asap.scheduled !== undefined) {
if (asap._scheduled !== undefined) {
asap.flush();
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"test:side-effects": "check-side-effects --test integration/side-effects/side-effects.json",
"test:side-effects:update": "npm run test:side-effects -- --update",
"test:import": "ts-node ./integration/import/runner.ts",
"compile": "tsc -b ./src/tsconfig.cjs.json ./src/tsconfig.cjs.spec.json ./src/tsconfig.esm.json ./src/tsconfig.esm5.json ./src/tsconfig.esm5.rollup.json ./src/tsconfig.types.json ./spec/tsconfig.json",
"compile": "tsc -b ./src/tsconfig.cjs.json ./src/tsconfig.cjs.spec.json ./src/tsconfig.esm.json ./src/tsconfig.esm5.json ./src/tsconfig.esm5.rollup.json ./src/tsconfig.types.json ./src/tsconfig.types.spec.json ./spec/tsconfig.json",
"build:clean": "shx rm -rf ./dist",
"build:global": "node ./tools/make-umd-bundle.js && node ./tools/make-closure-core.js",
"build:package": "npm-run-all build:clean compile build:global && node ./tools/prepare-package.js && node ./tools/generate-alias.js",
Expand Down
4 changes: 2 additions & 2 deletions spec/Scheduler-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe('Scheduler.queue', () => {
it('should schedule things recursively', () => {
let call1 = false;
let call2 = false;
(queue as QueueScheduler).active = false;
(queue as QueueScheduler)._active = false;
queue.schedule(() => {
call1 = true;
queue.schedule(() => {
Expand All @@ -21,7 +21,7 @@ describe('Scheduler.queue', () => {
it('should schedule things recursively via this.schedule', () => {
let call1 = false;
let call2 = false;
(queue as QueueScheduler).active = false;
(queue as QueueScheduler)._active = false;
queue.schedule(function (state) {
call1 = state!.call1;
call2 = state!.call2;
Expand Down
6 changes: 3 additions & 3 deletions spec/observables/interval-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ describe('interval', () => {
},
complete() {
expect(asapScheduler.actions.length).to.equal(0);
expect(asapScheduler.scheduled).to.equal(undefined);
expect(asapScheduler._scheduled).to.equal(undefined);
sandbox.restore();
done();
}
Expand All @@ -104,7 +104,7 @@ describe('interval', () => {
},
complete() {
expect(queueScheduler.actions.length).to.equal(0);
expect(queueScheduler.scheduled).to.equal(undefined);
expect(queueScheduler._scheduled).to.equal(undefined);
sandbox.restore();
done();
}
Expand All @@ -131,7 +131,7 @@ describe('interval', () => {
},
complete() {
expect(animationFrameScheduler.actions.length).to.equal(0);
expect(animationFrameScheduler.scheduled).to.equal(undefined);
expect(animationFrameScheduler._scheduled).to.equal(undefined);
sandbox.restore();
done();
}
Expand Down
4 changes: 2 additions & 2 deletions spec/schedulers/AnimationFrameScheduler-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,12 @@ describe('Scheduler.animationFrame', () => {
let animationFrameExec2 = false;
const action1 = animationFrame.schedule(() => { animationFrameExec1 = true; });
const action2 = animationFrame.schedule(() => { animationFrameExec2 = true; });
expect(animationFrame.scheduled).to.exist;
expect(animationFrame._scheduled).to.exist;
expect(animationFrame.actions.length).to.equal(2);
action1.unsubscribe();
action2.unsubscribe();
expect(animationFrame.actions.length).to.equal(0);
expect(animationFrame.scheduled).to.equal(undefined);
expect(animationFrame._scheduled).to.equal(undefined);
animationFrame.schedule(() => {
expect(animationFrameExec1).to.equal(false);
expect(animationFrameExec2).to.equal(false);
Expand Down
4 changes: 2 additions & 2 deletions spec/schedulers/AsapScheduler-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,12 @@ describe('Scheduler.asap', () => {
let asapExec2 = false;
const action1 = asap.schedule(() => { asapExec1 = true; });
const action2 = asap.schedule(() => { asapExec2 = true; });
expect(asap.scheduled).to.exist;
expect(asap._scheduled).to.exist;
expect(asap.actions.length).to.equal(2);
action1.unsubscribe();
action2.unsubscribe();
expect(asap.actions.length).to.equal(0);
expect(asap.scheduled).to.equal(undefined);
expect(asap._scheduled).to.equal(undefined);
asap.schedule(() => {
expect(asapExec1).to.equal(false);
expect(asapExec2).to.equal(false);
Expand Down
2 changes: 1 addition & 1 deletion spec/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
},
"references": [
{
"path": "../src/tsconfig.types.json"
"path": "../src/tsconfig.types.spec.json"
}
]
}
10 changes: 7 additions & 3 deletions src/internal/AnyCatcher.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
/*
* Note that we cannot apply the `internal` tag here because the declaration
* needs to survive the `stripInternal` option. Otherwise, `AnyCatcher` will
* be `any` in the `.d.ts` files.
*/
declare const anyCatcherSymbol: unique symbol;

/**
* This is just a type that we're using to identify `any` being passed to
* function overloads. This is used because of situations like {@link forkJoin},
* where it could return an `Observable<T[]>` or an `Observable<{ [key: K]: T }>`,
* so `forkJoin(any)` would mean we need to return `Observable<unknown>`.
*
* @internal
*/
declare const anyCatcherSymbol: unique symbol;
export type AnyCatcher = typeof anyCatcherSymbol;
23 changes: 12 additions & 11 deletions src/internal/AsyncSubject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,33 @@ import { Subscriber } from './Subscriber';
* @class AsyncSubject<T>
*/
export class AsyncSubject<T> extends Subject<T> {
private value: T | null = null;
private hasValue = false;
private isComplete = false;
private _value: T | null = null;
private _hasValue = false;
private _isComplete = false;

/** @internal */
protected _checkFinalizedStatuses(subscriber: Subscriber<T>) {
const { hasError, hasValue, value, thrownError, isStopped } = this;
const { hasError, _hasValue, _value, thrownError, isStopped } = this;
if (hasError) {
subscriber.error(thrownError);
} else if (isStopped) {
hasValue && subscriber.next(value!);
_hasValue && subscriber.next(_value!);
subscriber.complete();
}
}

next(value: T): void {
if (!this.isStopped) {
this.value = value;
this.hasValue = true;
this._value = value;
this._hasValue = true;
}
}

complete(): void {
const { hasValue, value, isComplete } = this;
if (!isComplete) {
this.isComplete = true;
hasValue && super.next(value!);
const { _hasValue, _value, _isComplete } = this;
if (!_isComplete) {
this._isComplete = true;
_hasValue && super.next(_value!);
super.complete();
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/internal/BehaviorSubject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class BehaviorSubject<T> extends Subject<T> {
return this.getValue();
}

/** @deprecated This is an internal implementation detail, do not use. */
/** @internal */
protected _subscribe(subscriber: Subscriber<T>): Subscription {
const subscription = super._subscribe(subscriber);
!subscription.closed && subscriber.next(this._value);
Expand Down
18 changes: 11 additions & 7 deletions src/internal/Observable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ import { isFunction } from './util/isFunction';
* @class Observable<T>
*/
export class Observable<T> implements Subscribable<T> {
/** @deprecated This is an internal implementation detail, do not use. */
protected source: Observable<any> | undefined;
/**
* @deprecated This is an internal implementation detail, do not use.
*/
source: Observable<any> | undefined;

/** @deprecated This is an internal implementation detail, do not use. */
protected operator: Operator<any, T> | undefined;
/**
* @deprecated This is an internal implementation detail, do not use.
*/
operator: Operator<any, T> | undefined;

/**
* @constructor
Expand Down Expand Up @@ -61,7 +65,7 @@ export class Observable<T> implements Subscribable<T> {
* using `lift`, it is recommended that you create an operator by simply returning `new Observable()` directly.
* See "Creating new operators from scratch" section here: https://rxjs.dev/guide/operators
*/
protected lift<R>(operator?: Operator<T, R>): Observable<R> {
lift<R>(operator?: Operator<T, R>): Observable<R> {
const observable = new Observable<R>();
observable.source = this;
observable.operator = operator;
Expand Down Expand Up @@ -250,7 +254,7 @@ export class Observable<T> implements Subscribable<T> {
return subscriber;
}

/** @deprecated This is an internal implementation detail, do not use. */
/** @internal */
protected _trySubscribe(sink: Subscriber<T>): TeardownLogic {
try {
return this._subscribe(sink);
Expand Down Expand Up @@ -343,7 +347,7 @@ export class Observable<T> implements Subscribable<T> {
}) as Promise<void>;
}

/** @internal This is an internal implementation detail, do not use. */
/** @internal */
protected _subscribe(subscriber: Subscriber<any>): TeardownLogic {
return this.source?.subscribe(subscriber);
}
Expand Down
50 changes: 25 additions & 25 deletions src/internal/ReplaySubject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ import { dateTimestampProvider } from './scheduler/dateTimestampProvider';
* @see {@link shareReplay}
*/
export class ReplaySubject<T> extends Subject<T> {
private buffer: (T | number)[] = [];
private infiniteTimeWindow = true;
private _buffer: (T | number)[] = [];
private _infiniteTimeWindow = true;

/**
* @param bufferSize The size of the buffer to replay on subscription
Expand All @@ -45,38 +45,38 @@ export class ReplaySubject<T> extends Subject<T> {
* calculate the amount of time something has been buffered.
*/
constructor(
private bufferSize = Infinity,
private windowTime = Infinity,
private timestampProvider: TimestampProvider = dateTimestampProvider
private _bufferSize = Infinity,
private _windowTime = Infinity,
private _timestampProvider: TimestampProvider = dateTimestampProvider
) {
super();
this.infiniteTimeWindow = windowTime === Infinity;
this.bufferSize = Math.max(1, bufferSize);
this.windowTime = Math.max(1, windowTime);
this._infiniteTimeWindow = _windowTime === Infinity;
this._bufferSize = Math.max(1, _bufferSize);
this._windowTime = Math.max(1, _windowTime);
}

next(value: T): void {
const { isStopped, buffer, infiniteTimeWindow, timestampProvider, windowTime } = this;
const { isStopped, _buffer, _infiniteTimeWindow, _timestampProvider, _windowTime } = this;
if (!isStopped) {
buffer.push(value);
!infiniteTimeWindow && buffer.push(timestampProvider.now() + windowTime);
_buffer.push(value);
!_infiniteTimeWindow && _buffer.push(_timestampProvider.now() + _windowTime);
}
this.trimBuffer();
this._trimBuffer();
super.next(value);
}

/** @deprecated Remove in v8. This is an internal implementation detail, do not use. */
/** @internal */
protected _subscribe(subscriber: Subscriber<T>): Subscription {
this._throwIfClosed();
this.trimBuffer();
this._trimBuffer();

const subscription = this._innerSubscribe(subscriber);

const { infiniteTimeWindow, buffer } = this;
const { _infiniteTimeWindow, _buffer } = this;
// We use a copy here, so reentrant code does not mutate our array while we're
// emitting it to a new subscriber.
const copy = buffer.slice();
for (let i = 0; i < copy.length && !subscriber.closed; i += infiniteTimeWindow ? 1 : 2) {
const copy = _buffer.slice();
for (let i = 0; i < copy.length && !subscriber.closed; i += _infiniteTimeWindow ? 1 : 2) {
subscriber.next(copy[i] as T);
}

Expand All @@ -85,26 +85,26 @@ export class ReplaySubject<T> extends Subject<T> {
return subscription;
}

private trimBuffer() {
const { bufferSize, timestampProvider, buffer, infiniteTimeWindow } = this;
private _trimBuffer() {
const { _bufferSize, _timestampProvider, _buffer, _infiniteTimeWindow } = this;
// If we don't have an infinite buffer size, and we're over the length,
// use splice to truncate the old buffer values off. Note that we have to
// double the size for instances where we're not using an infinite time window
// because we're storing the values and the timestamps in the same array.
const adjustedBufferSize = (infiniteTimeWindow ? 1 : 2) * bufferSize;
bufferSize < Infinity && adjustedBufferSize < buffer.length && buffer.splice(0, buffer.length - adjustedBufferSize);
const adjustedBufferSize = (_infiniteTimeWindow ? 1 : 2) * _bufferSize;
_bufferSize < Infinity && adjustedBufferSize < _buffer.length && _buffer.splice(0, _buffer.length - adjustedBufferSize);

// Now, if we're not in an infinite time window, remove all values where the time is
// older than what is allowed.
if (!infiniteTimeWindow) {
const now = timestampProvider.now();
if (!_infiniteTimeWindow) {
const now = _timestampProvider.now();
let last = 0;
// Search the array for the first timestamp that isn't expired and
// truncate the buffer up to that point.
for (let i = 1; i < buffer.length && (buffer[i] as number) <= now; i += 2) {
for (let i = 1; i < _buffer.length && (_buffer[i] as number) <= now; i += 2) {
last = i;
}
last && buffer.splice(0, last + 1);
last && _buffer.splice(0, last + 1);
}
}
}
Loading