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

Feature/improve typing #93

Merged
merged 2 commits into from
Aug 26, 2022
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
1 change: 1 addition & 0 deletions config/tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"importHelpers": true,
"sourceMap": true,
"strict": true,
"exactOptionalPropertyTypes": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"noUnusedLocals": true,
Expand Down
1 change: 1 addition & 0 deletions config/tsconfig.main.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"importHelpers": true,
"sourceMap": true,
"strict": true,
"exactOptionalPropertyTypes": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"noUnusedLocals": true,
Expand Down
1 change: 1 addition & 0 deletions config/tsconfig.module.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"importHelpers": true,
"sourceMap": true,
"strict": true,
"exactOptionalPropertyTypes": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"noUnusedLocals": true,
Expand Down
1 change: 1 addition & 0 deletions config/tsconfig.types.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"importHelpers": true,
"sourceMap": true,
"strict": true,
"exactOptionalPropertyTypes": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"noUnusedLocals": true,
Expand Down
24 changes: 14 additions & 10 deletions deno_dist/actor/obs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import { Update } from '../common/mod.ts';

class NotifierBase<T> {
readonly _subscribers = new Set<Obs.StateUpdate<T>>();
_onNoMoreSubscribers?: Obs.UnsubscribeFn;
_onNoMoreSubscribers?: undefined | Obs.UnsubscribeFn;

constructor(
readonly options?: { onFirstSubscription?: () => Obs.UnsubscribeFn }
readonly options?: {
onFirstSubscription?: undefined | (() => Obs.UnsubscribeFn);
}
) {}

get hasSubscribers(): boolean {
Expand Down Expand Up @@ -43,14 +45,16 @@ class Impl<T, D> extends NotifierBase<Protected<T & D>> {
constructor(
public pureState: T,
readonly options?: {
onGetState?: () => void;
onSetState?: (newState: Protected<T & D>) => void;
derive?: (
newState: Protected<T>,
oldState: Protected<T>,
oldDerived?: Protected<D>
) => D;
onFirstSubscription?: () => Obs.UnsubscribeFn;
onGetState?: undefined | (() => void);
onSetState?: undefined | ((newState: Protected<T & D>) => void);
derive?:
| undefined
| ((
newState: Protected<T>,
oldState: Protected<T>,
oldDerived?: Protected<D>
) => D);
onFirstSubscription?: undefined | (() => Obs.UnsubscribeFn);
},
public derivedState = options?.derive?.(
pureState as Protected<T>,
Expand Down
4 changes: 2 additions & 2 deletions deno_dist/common/range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ export namespace Range {
* @param range - the `Range` to use
*/
export function getNormalizedRange<T>(range: Range<T>): {
start?: [T, boolean];
end?: [T, boolean];
start?: [T, boolean] | undefined;
end?: [T, boolean] | undefined;
} {
let start: [T, boolean] | undefined = undefined;
let end: [T, boolean] | undefined = undefined;
Expand Down
24 changes: 13 additions & 11 deletions deno_dist/hashed/base/hashed-custom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { List } from '../../list/mod.ts';
type GenBlockBuilderEntry<E> = BlockBuilderBase<E> | CollisionBuilderBase<E>;

export abstract class BlockBuilderBase<E> {
abstract source?: GenSource<E>;
abstract _entries?: E[];
abstract _entrySets?: GenBlockBuilderEntry<E>[];
abstract source?: undefined | GenSource<E>;
abstract _entries?: undefined | E[];
abstract _entrySets?: undefined | GenBlockBuilderEntry<E>[];
abstract get size(): number;

get isEmpty(): boolean {
Expand Down Expand Up @@ -38,14 +38,16 @@ export abstract class BlockBuilderBase<E> {
}

export abstract class CollisionBuilderBase<E> {
abstract source?: {
size: number;
entries: List.NonEmpty<E>;
forEach(
f: (entry: E, index: number, halt: () => void) => void,
state: TraverseState
): void;
};
abstract source?:
| undefined
| {
size: number;
entries: List.NonEmpty<E>;
forEach(
f: (entry: E, index: number, halt: () => void) => void,
state: TraverseState
): void;
};
abstract _entries?: List.Builder<E> | undefined;

get size(): number {
Expand Down
10 changes: 5 additions & 5 deletions deno_dist/hashed/map-custom/implementation/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ export class HashMapBlockBuilder<K, V>
{
constructor(
readonly context: HashMapContext<K>,
public source?: HashMapBlock<K, V>,
public _entries?: (readonly [K, V])[],
public _entrySets?: MapBlockBuilderEntry<K, V>[],
public source?: undefined | HashMapBlock<K, V>,
public _entries?: undefined | (readonly [K, V])[],
public _entrySets?: undefined | MapBlockBuilderEntry<K, V>[],
public size = source?.size ?? 0,
public level = source?.level ?? 0
) {
Expand Down Expand Up @@ -445,8 +445,8 @@ export class HashMapCollisionBuilder<K, V> extends CollisionBuilderBase<
> {
constructor(
readonly context: HashMapContext<K>,
public source?: HashMapCollision<K, V>,
public _entries?: List.Builder<readonly [K, V]>
public source?: undefined | HashMapCollision<K, V>,
public _entries?: undefined | List.Builder<readonly [K, V]>
) {
super();
}
Expand Down
10 changes: 5 additions & 5 deletions deno_dist/hashed/set-custom/implementation/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ export class HashSetBlockBuilder<T>
{
constructor(
readonly context: HashSetContext<T>,
public source?: HashSetBlock<T>,
public _entries?: T[],
public _entrySets?: SetBlockBuilderEntry<T>[],
public source?: undefined | HashSetBlock<T>,
public _entries?: undefined | T[],
public _entrySets?: undefined | SetBlockBuilderEntry<T>[],
public size = source?.size ?? 0,
public level = source?.level ?? 0
) {
Expand Down Expand Up @@ -288,8 +288,8 @@ export class HashSetBlockBuilder<T>
export class HashSetCollisionBuilder<T> extends CollisionBuilderBase<T> {
constructor(
readonly context: HashSetContext<T>,
public source?: HashSetCollision<T>,
public _entries?: List.Builder<T>
public source?: undefined | HashSetCollision<T>,
public _entries?: undefined | List.Builder<T>
) {
super();
}
Expand Down
32 changes: 17 additions & 15 deletions deno_dist/sorted/common/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -663,26 +663,28 @@ export function innerStreamSliceIndex<E>(

export abstract class SortedBuilder<E> {
abstract get context(): { minEntries: number; maxEntries: number };
abstract source?: {
min<O>(otherwise?: OptLazy<O>): E | O;
max<O>(otherwise?: OptLazy<O>): E | O;
getAtIndex<O>(index: number, otherwise?: OptLazy<O>): E | O;
forEach(
f: (entry: E, index: number, halt: () => void) => void,
state?: TraverseState
): void;
};
abstract _entries?: E[];
abstract _children?: SortedBuilder<E>[];
abstract source?:
| undefined
| {
min<O>(otherwise?: OptLazy<O>): E | O;
max<O>(otherwise?: OptLazy<O>): E | O;
getAtIndex<O>(index: number, otherwise?: OptLazy<O>): E | O;
forEach(
f: (entry: E, index: number, halt: () => void) => void,
state?: TraverseState
): void;
};
abstract _entries?: undefined | E[];
abstract _children?: undefined | SortedBuilder<E>[];
abstract get children(): SortedBuilder<E>[];
abstract set children(value: SortedBuilder<E>[]);
abstract size: number;
abstract prepareMutate(): void;
abstract createNew(
source?: unknown,
entries?: E[],
children?: SortedBuilder<E>[],
size?: number
source?: undefined | unknown,
entries?: undefined | E[],
children?: undefined | SortedBuilder<E>[],
size?: undefined | number
): SortedBuilder<E>;

_lock = 0;
Expand Down
14 changes: 7 additions & 7 deletions deno_dist/sorted/map-custom/implementation/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ export class SortedMapBuilder<K, V>
{
constructor(
readonly context: SortedMapContext<K>,
public source?: SortedMap<K, V>,
public _entries?: (readonly [K, V])[],
public _children?: SortedMapBuilder<K, V>[],
public source?: undefined | SortedMap<K, V>,
public _entries?: undefined | (readonly [K, V])[],
public _children?: undefined | SortedMapBuilder<K, V>[],
public size = source?.size ?? 0
) {
super();
}

createNew(
source?: SortedMap<K, V>,
_entries?: (readonly [K, V])[],
_children?: SortedMapBuilder<K, V>[],
size?: number
source?: undefined | SortedMap<K, V>,
_entries?: undefined | (readonly [K, V])[],
_children?: undefined | SortedMapBuilder<K, V>[],
size?: undefined | number
): SortedMapBuilder<K, V> {
return new SortedMapBuilder(
this.context,
Expand Down
14 changes: 7 additions & 7 deletions deno_dist/sorted/set-custom/implementation/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ import { SortedIndex, SortedBuilder } from '../../common/index.ts';
export class SortedSetBuilder<T> extends SortedBuilder<T> {
constructor(
readonly context: SortedSetContext<T>,
public source?: SortedSet<T>,
public _entries?: T[],
public _children?: SortedSetBuilder<T>[],
public source?: undefined | SortedSet<T>,
public _entries?: undefined | T[],
public _children?: undefined | SortedSetBuilder<T>[],
public size = source?.size ?? 0
) {
super();
}

createNew(
source?: SortedSet<T>,
entries?: T[],
children?: SortedSetBuilder<T>[],
size?: number
source?: undefined | SortedSet<T>,
entries?: undefined | T[],
children?: undefined | SortedSetBuilder<T>[],
size?: undefined | number
): SortedSetBuilder<T> {
return new SortedSetBuilder(this.context, source, entries, children, size);
}
Expand Down
6 changes: 3 additions & 3 deletions deno_dist/stream/async-custom/async-fast-iterator-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export const emptyAsyncFastIterator: AsyncFastIterator<any> = Object.freeze({

export abstract class AsyncFastIteratorBase<T> implements AsyncFastIterator<T> {
abstract fastNext<O>(otherwise?: AsyncOptLazy<O>): MaybePromise<T | O>;
return?: () => Promise<any>;
return?: undefined | (() => Promise<any>);

async next(): Promise<IteratorResult<T>> {
const done = Symbol('Done');
Expand Down Expand Up @@ -286,7 +286,7 @@ export class FromIterator<T> extends AsyncFastIteratorBase<T> {
this.return = close;
}

return?: () => MaybePromise<any>;
return: undefined | (() => MaybePromise<any>);

async fastNext<O>(otherwise?: AsyncOptLazy<O>): Promise<T | O> {
const result = this.iterator.next();
Expand Down Expand Up @@ -907,7 +907,7 @@ export class AsyncDropIterator<T> extends AsyncFastIteratorBase<T> {

export class AsyncRepeatIterator<T> extends AsyncFastIteratorBase<T> {
iterator: AsyncFastIterator<T>;
remain?: number;
remain: number | undefined;

constructor(readonly source: AsyncStream<T>, readonly amount?: number) {
super();
Expand Down
10 changes: 5 additions & 5 deletions deno_dist/stream/async/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -730,11 +730,11 @@ export interface AsyncStream<T>
* @note O(N)
*/
join(options?: {
sep?: string;
start?: string;
end?: string;
valueToString?: (value: T) => MaybePromise<string>;
ifEmpty?: string;
sep?: string | undefined;
start?: string | undefined;
end?: string | undefined;
valueToString?: ((value: T) => MaybePromise<string>) | undefined;
ifEmpty?: string | undefined;
}): Promise<string>;
/**
* Returns an AsyncStream starting with `options.sep`, then returning the elements of this Stream interspersed with `options.sep`, and ending with
Expand Down
2 changes: 1 addition & 1 deletion deno_dist/stream/custom/fast-iterator-custom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ export class DropIterator<T> extends FastIteratorBase<T> {

export class RepeatIterator<T> extends FastIteratorBase<T> {
iterator: FastIterator<T>;
remain?: number;
remain: number | undefined;

constructor(readonly source: Stream<T>, readonly amount?: number) {
super();
Expand Down
30 changes: 26 additions & 4 deletions deno_dist/stream/custom/stream-custom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,28 @@ import {
} from '../../stream/custom/index.ts';
import { RimbuError, Token } from '../../base/mod.ts';

function* yieldObjKeys<K extends string | number | symbol>(
obj: Record<K, any>
): Generator<K> {
for (const key in obj) {
yield key;
}
}

function* yieldObjValues<V>(obj: Record<any, V>): Generator<V> {
for (const key in obj) {
yield (obj as any)[key];
}
}

function* yieldObjEntries<K extends string | number | symbol, V>(
obj: Record<K, V>
): Generator<[K, V]> {
for (const key in obj) {
yield [key, obj[key]];
}
}

export abstract class StreamBase<T> implements Stream<T> {
abstract [Symbol.iterator](): FastIterator<T>;

Expand Down Expand Up @@ -1868,15 +1890,15 @@ export const StreamConstructorsImpl: StreamConstructors =
fromObjectKeys<K extends string | number | symbol>(
obj: Record<K, any>
): Stream<K> {
return StreamConstructorsImpl.fromArray(Object.keys(obj) as K[]);
return StreamConstructorsImpl.from(yieldObjKeys(obj));
},
fromObjectValues<V>(obj: Record<any, V> | readonly V[]): Stream<V> {
return StreamConstructorsImpl.fromArray(Object.values(obj));
fromObjectValues<V>(obj: Record<any, V>): Stream<V> {
return StreamConstructorsImpl.from(yieldObjValues(obj));
},
fromObject<K extends string | number | symbol, V>(
obj: Record<K, V>
): Stream<[K, V]> {
return StreamConstructorsImpl.fromArray(Object.entries(obj) as [K, V][]);
return StreamConstructorsImpl.from(yieldObjEntries(obj));
},
fromString(source: string, range?: IndexRange, reversed = false) {
return StreamConstructorsImpl.fromArray(
Expand Down
10 changes: 5 additions & 5 deletions deno_dist/stream/main/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -694,11 +694,11 @@ export interface Stream<T> extends FastIterable<T>, Streamable<T> {
* @note O(N)
*/
join(options?: {
sep?: string;
start?: string;
end?: string;
valueToString?: (value: T) => string;
ifEmpty?: string;
sep?: string | undefined;
start?: string | undefined;
end?: string | undefined;
valueToString?: ((value: T) => string) | undefined;
ifEmpty?: string | undefined;
}): string;
/**
* Returns a Stream starting with `options.sep`, then returning the elements of this Stream interspersed with `options.sep`, and ending with
Expand Down