Skip to content

Commit

Permalink
chore: address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
benlesh committed Jun 15, 2020
1 parent 07b5746 commit 8ab5b7b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 23 deletions.
2 changes: 1 addition & 1 deletion spec-dtslint/Notification-spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Notification } from "../dist/types/internal/Notification";
import { Notification } from 'rxjs';

describe('Notification', () => {
// Basic method tests
Expand Down
22 changes: 11 additions & 11 deletions src/internal/Notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ export class Notification<T> {
throw new Error('unexpected notification kind value');
}

private static completeNotification: Notification<never> & CompleteNotification = new Notification('C') as any;
private static completeNotification = new Notification('C') as Notification<never> & CompleteNotification;
/**
* A shortcut to create a Notification instance of the type `next` from a
* given value.
Expand All @@ -205,8 +205,8 @@ export class Notification<T> {
* and use them. Rather, try to create POJOs matching the signature outlined in {@link ObservableNotification}.
* For example: `{ kind: 'N', value: 1 }`, `{kind: 'E', error: new Error('bad') }`, or `{ kind: 'C' }`.
*/
static createNext<T>(value: T): Notification<T> & NextNotification<T> {
return new Notification('N', value) as any;
static createNext<T>(value: T) {
return new Notification('N', value) as Notification<T> & NextNotification<T>;
}

/**
Expand All @@ -220,8 +220,8 @@ export class Notification<T> {
* and use them. Rather, try to create POJOs matching the signature outlined in {@link ObservableNotification}.
* For example: `{ kind: 'N', value: 1 }`, `{kind: 'E', error: new Error('bad') }`, or `{ kind: 'C' }`.
*/
static createError(err?: any): Notification<never> & ErrorNotification {
return new Notification('E', undefined, err) as any;
static createError(err?: any) {
return new Notification('E', undefined, err) as Notification<never> & ErrorNotification;
}

/**
Expand Down Expand Up @@ -266,7 +266,7 @@ export function observeNotification<T>(notification: ObservableNotification<T>,
* same "shape" as other notifications in v8.
* @internal
*/
export const COMPLETE_NOTIFICATION = (() => createNotification('C', undefined, undefined))();
export const COMPLETE_NOTIFICATION = (() => createNotification('C', undefined, undefined) as CompleteNotification)();

/**
* Internal use only. Creates an optimized error notification that is the same "shape"
Expand All @@ -282,8 +282,8 @@ export function errorNotification(error: any): ErrorNotification {
* as other notifications.
* @internal
*/
export function nextNotification<T>(value: T): NextNotification<T> {
return createNotification('N', value, undefined) as any;
export function nextNotification<T>(value: T) {
return createNotification('N', value, undefined) as NextNotification<T>;
}

/**
Expand All @@ -292,10 +292,10 @@ export function nextNotification<T>(value: T): NextNotification<T> {
* TODO: This is only exported to support a crazy legacy test in `groupBy`.
* @internal
*/
export function createNotification(kind: 'N'|'E'|'C', value: any, error: any) {
export function createNotification(kind: 'N' | 'E' | 'C', value: any, error: any) {
return {
kind,
value,
error
error,
};
}
}
14 changes: 4 additions & 10 deletions src/internal/operators/dematerialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Operator } from '../Operator';
import { Observable } from '../Observable';
import { Subscriber } from '../Subscriber';
import { observeNotification } from '../Notification';
import { OperatorFunction, ObservableNotification, NextNotification } from '../types';
import { OperatorFunction, ObservableNotification, ValueFromNotification } from '../types';

/**
* Converts an Observable of {@link ObservableNotification} objects into the emissions
Expand Down Expand Up @@ -51,13 +51,13 @@ import { OperatorFunction, ObservableNotification, NextNotification } from '../t
* @return {Observable} An Observable that emits items and notifications
* embedded in Notification objects emitted by the source Observable.
*/
export function dematerialize<N extends ObservableNotification<any>>(): OperatorFunction<N, GetNotificationValueOf<N>> {
export function dematerialize<N extends ObservableNotification<any>>(): OperatorFunction<N, ValueFromNotification<N>> {
return function dematerializeOperatorFunction(source: Observable<N>) {
return source.lift(new DeMaterializeOperator<N>());
};
}

class DeMaterializeOperator<N extends ObservableNotification<any>> implements Operator<N, GetNotificationValueOf<N>> {
class DeMaterializeOperator<N extends ObservableNotification<any>> implements Operator<N, ValueFromNotification<N>> {
call(subscriber: Subscriber<any>, source: any): any {
return source.subscribe(new DeMaterializeSubscriber<N>(subscriber));
}
Expand All @@ -69,17 +69,11 @@ class DeMaterializeOperator<N extends ObservableNotification<any>> implements Op
* @extends {Ignored}
*/
class DeMaterializeSubscriber<N extends ObservableNotification<any>> extends Subscriber<N> {
constructor(destination: Subscriber<GetNotificationValueOf<N>>) {
constructor(destination: Subscriber<ValueFromNotification<N>>) {
super(destination);
}

protected _next(notification: N) {
observeNotification(notification, this.destination);
}
}

type GetNotificationValueOf<T> = T extends { kind: 'N'|'E'|'C' } ?
(T extends NextNotification<any> ?
(T extends { value: infer V } ? V : undefined )
: never)
: never;
11 changes: 10 additions & 1 deletion src/internal/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,4 +237,13 @@ export type Tail<X extends any[]> =
* If you have `T extends Array<any>`, and pass a `string[]` to it,
* `ValueFromArray<T>` will return the actual type of `string`.
*/
export type ValueFromArray<A> = A extends Array<infer T> ? T : never;
export type ValueFromArray<A> = A extends Array<infer T> ? T : never;

/**
* Gets the value type from an {@link ObservableNotification}, if possible.
*/
export type ValueFromNotification<T> = T extends { kind: 'N'|'E'|'C' } ?
(T extends NextNotification<any> ?
(T extends { value: infer V } ? V : undefined )
: never)
: never;

0 comments on commit 8ab5b7b

Please sign in to comment.