Skip to content

Commit

Permalink
Merge pull request #788 from orbitjs/strict
Browse files Browse the repository at this point in the history
Enable TS strict mode
  • Loading branch information
dgeb authored Sep 27, 2020
2 parents e1372b4 + 8d3c271 commit c68e808
Show file tree
Hide file tree
Showing 162 changed files with 1,717 additions and 1,307 deletions.
12 changes: 7 additions & 5 deletions packages/@orbit/coordinator/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ module.exports = {
'prefer-rest-params': ['off'],
'prefer-spread': ['off']
},
overrides: [{
files: ['test/**/*.ts'],
rules: {
'@typescript-eslint/no-unused-vars': ['off']
overrides: [
{
files: ['test/**/*.ts'],
rules: {
'@typescript-eslint/no-unused-vars': ['off']
}
}
}]
]
};
2 changes: 1 addition & 1 deletion packages/@orbit/coordinator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"types": "dist/modules/index.d.ts",
"scripts": {
"build": "rm -rf ./dist && yarn build:modules && yarn build:commonjs",
"build:modules": "tsc",
"build:modules": "tsc --project ./tsconfig.modules.json",
"build:commonjs": "tsc --project ./tsconfig.commonjs.json",
"clean": "git clean -x -f",
"lint": "eslint . --ext .ts",
Expand Down
31 changes: 16 additions & 15 deletions packages/@orbit/coordinator/src/coordinator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ export interface ActivationOptions {
export class Coordinator {
protected _sources: Dict<Source>;
protected _strategies: Dict<Strategy>;
protected _activated: Promise<any>;
protected _activated?: Promise<void>;
protected _defaultActivationOptions: ActivationOptions;
protected _currentActivationOptions: ActivationOptions;
protected _currentActivationOptions?: ActivationOptions;

constructor(options: CoordinatorOptions = {}) {
this._sources = {};
Expand All @@ -54,18 +54,19 @@ export class Coordinator {

addSource(source: Source): void {
const name = source.name;

assert(`Sources require a 'name' to be added to a coordinator.`, !!name);
assert(
`A source named '${name}' has already been added to this coordinator.`,
!this._sources[name]
);
assert(
`A coordinator's sources can not be changed while it is active.`,
!this._activated
);

this._sources[name] = source;
if (name) {
assert(
`A source named '${name}' has already been added to this coordinator.`,
!this._sources[name]
);
assert(
`A coordinator's sources can not be changed while it is active.`,
!this._activated
);
this._sources[name] = source;
} else {
assert(`Sources require a 'name' to be added to a coordinator.`, !!name);
}
}

removeSource(name: string): void {
Expand Down Expand Up @@ -137,7 +138,7 @@ export class Coordinator {
return Object.keys(this._strategies);
}

get activated(): Promise<void> {
get activated(): Promise<void> | undefined {
return this._activated;
}

Expand Down
28 changes: 15 additions & 13 deletions packages/@orbit/coordinator/src/strategies/connection-strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,37 +28,40 @@ export interface ConnectionStrategyOptions extends StrategyOptions {
* invoked in the context of this strategy (and thus will have access to
* both `this.source` and `this.target`).
*/
action: string | ((...args: any[]) => any);
action: string | ((...args: unknown[]) => unknown);

/**
* A handler for any errors thrown as a result of performing the action.
*/
catch?: (error: Error, ...args: any[]) => void;
catch?: (error: Error, ...args: unknown[]) => void;

/**
* A filter function that returns `true` if the `action` should be performed.
*
* `filter` will be invoked in the context of this strategy (and thus will
* have access to both `this.source` and `this.target`).
*/
filter?: (...args: any[]) => boolean;
filter?: (...args: unknown[]) => boolean;

/**
* Should resolution of `action` on the the target block the completion
* of the source's event?
*
* By default, `blocking` is false.
*/
blocking?: boolean | ((...args: any[]) => boolean);
blocking?: boolean | ((...args: unknown[]) => boolean);
}

declare type CatchFn = (error: Error, ...args: unknown[]) => void;
declare type FilterFn = (...args: unknown[]) => boolean;

export class ConnectionStrategy extends Strategy {
protected _blocking: boolean | ((...args: any[]) => boolean);
protected _blocking: boolean | ((...args: unknown[]) => boolean);
protected _event: string;
protected _action: string | ((...args: any[]) => any);
protected _catch: (error: Error, ...args: any[]) => void;
protected _listener: Listener;
protected _filter: (...args: any[]) => boolean;
protected _action: string | ((...args: unknown[]) => unknown);
protected _catch?: CatchFn;
protected _listener?: Listener;
protected _filter?: FilterFn;

constructor(options: ConnectionStrategyOptions) {
assert(
Expand All @@ -75,7 +78,7 @@ export class ConnectionStrategy extends Strategy {
);
options.sources = [options.source];
let defaultName = `${options.source}:${options.on}`;
delete options.source;
delete (options as any).source;
if (options.target) {
assert(
'`target` should be a Source name specified as a string',
Expand Down Expand Up @@ -125,7 +128,7 @@ export class ConnectionStrategy extends Strategy {
async deactivate(): Promise<void> {
await super.deactivate();
this.source.off(this._event, this._listener);
this._listener = null;
this._listener = undefined;
}

protected generateListener(): Listener {
Expand All @@ -148,8 +151,7 @@ export class ConnectionStrategy extends Strategy {

if (this._catch && result && result.catch) {
result = result.catch((e: Error) => {
args.unshift(e);
return this._catch.apply(this, args);
return (this._catch as CatchFn).apply(this, [e, ...args]);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ export interface EventLoggingStrategyOptions extends StrategyOptions {
export class EventLoggingStrategy extends Strategy {
protected _events?: string[];
protected _interfaces?: string[];
protected _eventListeners: Dict<Dict<Listener>>;
protected _eventListeners?: Dict<Dict<Listener>>;

constructor(options: EventLoggingStrategyOptions = {}) {
options.name = options.name || 'event-logging';
super(options);
super({
...options,
name: options.name || 'event-logging'
});

this._events = options.events;
this._interfaces = options.interfaces;
Expand All @@ -44,7 +46,7 @@ export class EventLoggingStrategy extends Strategy {
async deactivate(): Promise<void> {
await super.deactivate();
this._sources.forEach((source) => this._deactivateSource(source));
this._eventListeners = null;
this._eventListeners = undefined;
}

protected _activateSource(source: Source): void {
Expand Down Expand Up @@ -110,7 +112,7 @@ export class EventLoggingStrategy extends Strategy {
case 'transformable':
return ['transform'];
}
} else if (this._logLevel > LogLevel.None) {
} else if (this._logLevel !== undefined && this._logLevel > LogLevel.None) {
switch (interfaceName) {
case 'pullable':
return ['pullFail'];
Expand All @@ -124,23 +126,30 @@ export class EventLoggingStrategy extends Strategy {
return ['updateFail'];
}
}
return [];
}

protected _addListener(source: Source, event: string): void {
const listener = this._generateListener(source, event);
deepSet(this._eventListeners, [source.name, event], listener);
const sourceName = this.getSourceName(source);
deepSet(this._eventListeners, [sourceName, event], listener);
source.on(event, listener);
}

protected _removeListener(source: Source, event: string): void {
const listener = deepGet(this._eventListeners, [source.name, event]);
const sourceName = this.getSourceName(source);
const listener = deepGet(this._eventListeners, [
sourceName,
event
]) as Listener;
source.off(event, listener);
this._eventListeners[source.name][event] = null;
deepSet(this._eventListeners, [sourceName, event], undefined);
}

protected _generateListener(source: Source, event: string): Listener {
const sourceName = this.getSourceName(source);
return (...args: any[]) => {
console.log(this._logPrefix, source.name, event, ...args);
console.log(this._logPrefix, sourceName, event, ...args);
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import { Source, Transform } from '@orbit/data';
import { Dict } from '@orbit/utils';

export class LogTruncationStrategy extends Strategy {
protected _reviewing: Promise<void>;
protected _extraReviewNeeded: boolean;
protected _transformListeners: Dict<(transform: Transform) => void>;
protected _transformListeners: Dict<(transform: Transform) => void> = {};

constructor(options: StrategyOptions = {}) {
options.name = options.name || 'log-truncation';
super(options);
super({
...options,
name: options.name || 'log-truncation'
});
}

async activate(
Expand All @@ -29,10 +29,10 @@ export class LogTruncationStrategy extends Strategy {
for (let source of this._sources) {
this._disconnectSource(source);
}
this._transformListeners = null;
this._transformListeners = {};
}

_review(source: Source): Promise<void> {
async _review(source: Source): Promise<void> {
let sources = this._sources;
let transformId = source.transformLog.head;

Expand Down Expand Up @@ -70,22 +70,22 @@ export class LogTruncationStrategy extends Strategy {
}

_connectSource(source: Source): void {
const listener = (this._transformListeners[source.name] = (): Promise<
void
> => {
const listener = async (): Promise<void> => {
if (source.requestQueue.empty && source.syncQueue.empty) {
return this._review(source);
}
});

};
const sourceName = this.getSourceName(source);
this._transformListeners[sourceName] = listener;
source.syncQueue.on('complete', listener);
source.requestQueue.on('complete', listener);
}

_disconnectSource(source: Source): void {
const listener = this._transformListeners[source.name];
const sourceName = this.getSourceName(source);
const listener = this._transformListeners[sourceName];
source.syncQueue.off('complete', listener);
source.requestQueue.off('complete', listener);
delete this._transformListeners[source.name];
delete this._transformListeners[sourceName];
}
}
19 changes: 11 additions & 8 deletions packages/@orbit/coordinator/src/strategies/request-strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ export class RequestStrategy extends ConnectionStrategy {
constructor(options: RequestStrategyOptions) {
super(options);

this.passHints = options.passHints;
this.passHints = !!options.passHints;
}

protected generateListener(): Listener {
const target = this.target as any;
const target = this.target;

return (...args: any[]): any => {
return (...args: unknown[]): unknown => {
let result;

if (this._filter) {
Expand All @@ -43,14 +43,14 @@ export class RequestStrategy extends ConnectionStrategy {
}

if (typeof this._action === 'string') {
result = target[this._action](args[0]);
result = (target as any)[this._action](args[0]);
} else {
result = this._action(...args);
}

if (this._catch && result && result.catch) {
result = result.catch((e: Error) => {
return this._catch(e, ...args);
return this._catch && this._catch(e, ...args);
});
}

Expand All @@ -68,7 +68,7 @@ export class RequestStrategy extends ConnectionStrategy {
if (this.passHints) {
const hints = args[1];
if (typeof hints === 'object') {
return this.applyHint(hints, result);
return this.applyHint(hints as { data?: unknown }, result);
}
}
return result;
Expand All @@ -77,7 +77,10 @@ export class RequestStrategy extends ConnectionStrategy {
};
}

protected async applyHint(hints: any, result: Promise<any>): Promise<void> {
return (hints.data = await result);
protected async applyHint(
hints: { data?: unknown },
result: Promise<unknown>
): Promise<void> {
hints.data = await result;
}
}
Loading

0 comments on commit c68e808

Please sign in to comment.