Skip to content

Commit

Permalink
start strictifying..
Browse files Browse the repository at this point in the history
  • Loading branch information
tantaman committed Aug 10, 2022
1 parent 327b6c1 commit 0f08a4e
Show file tree
Hide file tree
Showing 26 changed files with 134 additions and 89 deletions.
3 changes: 2 additions & 1 deletion packages/absurd-sql-connector/src/worker/worker.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import initSqlJs from '@aphro/sql.js';
import { SQLiteFS } from '@aphro/absurd-sql';
// @ts-ignore
import IndexedDBBackend from '@aphro/absurd-sql/dist/indexeddb-backend.js';
import thisPackage from '../pkg.js';
import tracer from '../tracer.js';
Expand All @@ -11,7 +12,7 @@ import tracer from '../tracer.js';
*/
async function init() {
let SQL = await initSqlJs({
locateFile: file => {
locateFile: (file: string) => {
return file;
},
});
Expand Down
6 changes: 4 additions & 2 deletions packages/cache-runtime-ts/src/__tests__/Cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import Cache from '../cache.js';
import { asId, SID_of } from '@strut/sid';
import fc from 'fast-check';

declare const global;
declare const global: {
gc: () => void;
};

// TODO: cache will need to be per viewer......
// Why would we have many viewers in local first P2P app? For privacy respecting replications to other peers.
Expand Down Expand Up @@ -81,7 +83,7 @@ test('purges refs after garbage collection', async () => {
expect(cache.get(id, db, tablish)).toBe(null);
});

function mkModel(id) {
function mkModel(id: SID_of<TestModel>) {
return new TestModel(id);
}

Expand Down
3 changes: 3 additions & 0 deletions packages/codegen-cli/src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#!/usr/bin/env node

// @ts-ignore
import commandLineArgs from 'command-line-args';
// @ts-ignore
import commandLineUsage from 'command-line-usage';

import { CodegenPipeline } from '@aphro/codegen';
import * as process from 'process';
import * as path from 'path';
Expand Down
2 changes: 1 addition & 1 deletion packages/context-runtime-ts/src/INode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export interface IModel<T extends {} = Object> {
subscribe(c: () => void): Disposer;
subscribeTo(keys: (keyof T)[], c: () => void): Disposer;

destroy();
destroy(): void;

// Internal only APIs. Exposed since TS doesn't understand package friends.
// TODO: Or does it? I can extend a type that exists in a package from another package...
Expand Down
1 change: 1 addition & 0 deletions packages/context-runtime-ts/src/OptimisticPromise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* Complicates the code and is only a problem because React can't handle async correctly.
*/
export default class OptimisticPromise<T> extends Promise<T> {
// @ts-ignore
#optimistic: T;

get optimistic(): T {
Expand Down
8 changes: 4 additions & 4 deletions packages/context-runtime-ts/src/resolvers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { StorageEngine, StorageType } from '@aphro/schema-api';
import { DBResolver, EngineToResolved, ResolvedDB } from './DBResolver.js';

export const printResolver: DBResolver = spyResolver(function() {
export const printResolver: DBResolver = spyResolver(function () {
console.log(arguments);
});

Expand Down Expand Up @@ -31,14 +31,14 @@ export function spyResolver(spy: (...args: any) => any): DBResolver {

function spyProxy(spy: (...args: any[]) => any) {
const objProxyDef = {
get(target, prop, receiver) {
get(target: any, prop: any, receiver: any): any {
spy(prop);
return new Proxy(() => {}, fnProxyDef);
},
};

const fnProxyDef = {
apply(target, thisArg, args) {
apply(target: any, thisArg: any, args: any): any {
spy(args);
return new Proxy({}, objProxyDef);
},
Expand All @@ -53,7 +53,7 @@ export function basicResolver<X extends StorageEngine>(resolved: EngineToResolve
return {
db(db: string) {
// TOOD: some sort of invariant to ensure E === X?
return (resolved as unknown) as EngineToResolved[E];
return resolved as unknown as EngineToResolved[E];
},
};
},
Expand Down
1 change: 0 additions & 1 deletion packages/crr-sqlite/README.md

This file was deleted.

38 changes: 33 additions & 5 deletions packages/instrument/src/tracer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Span, trace } from '@opentelemetry/api';
import { Span, SpanStatusCode, trace } from '@opentelemetry/api';

export type Tracer = ReturnType<typeof getTracer>;

Expand All @@ -14,7 +14,14 @@ export default function getTracer(pkg: string, version: string) {
try {
return cb(span);
} catch (e) {
span.recordException(e);
if (e instanceof Error) {
span.recordException(e);
} else {
span.setStatus({
code: SpanStatusCode.ERROR,
message: (e as any)?.message,
});
}
throw e;
} finally {
span.end();
Expand All @@ -27,7 +34,14 @@ export default function getTracer(pkg: string, version: string) {
try {
return await cb(span);
} catch (e) {
span.recordException(e);
if (e instanceof Error) {
span.recordException(e);
} else {
span.setStatus({
code: SpanStatusCode.ERROR,
message: (e as any)?.message,
});
}
throw e;
} finally {
span.end();
Expand All @@ -40,7 +54,14 @@ export default function getTracer(pkg: string, version: string) {
try {
return await cb();
} catch (e) {
span.recordException(e);
if (e instanceof Error) {
span.recordException(e);
} else {
span.setStatus({
code: SpanStatusCode.ERROR,
message: (e as any)?.message,
});
}
throw e;
} finally {
span.end();
Expand All @@ -52,7 +73,14 @@ export default function getTracer(pkg: string, version: string) {
try {
return cb();
} catch (e) {
span.recordException(e);
if (e instanceof Error) {
span.recordException(e);
} else {
span.setStatus({
code: SpanStatusCode.ERROR,
message: (e as any)?.message,
});
}
throw e;
} finally {
span.end();
Expand Down
2 changes: 1 addition & 1 deletion packages/migration-runtime-ts/src/autoMigrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ async function migrateAllTasksForDB(db: SQLResolvedDB, tasks: MigrationTask[]) {
await db.query(sql`ROLLBACK`);
throw {
cause: e,
message: 'Failed to commit the migration. Rolling it back. ' + e.message,
message: 'Failed to commit the migration. Rolling it back. ' + (e as any)?.message,
};
}
}
Expand Down
13 changes: 7 additions & 6 deletions packages/migration-runtime-ts/src/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { autoMigrate, CreateError } from './autoMigrate.js';
export type SQLExports = { sqlite: DBs };
type DBs = { [dbName: string]: Schemas };
type Schemas = { [key: string]: string };
type Engine = 'sqlite';

export const bootstrap = {
/**
Expand All @@ -16,7 +17,7 @@ export const bootstrap = {
async createIfNotExists(
resolver: DBResolver,
sqlExports: SQLExports,
engine?: string,
engine?: Engine,
db?: string,
) {
await create(
Expand All @@ -41,7 +42,7 @@ export const bootstrap = {
async createThrowIfExists(
resolver: DBResolver,
sqlExports: SQLExports,
engine?: string,
engine?: Engine,
db?: string,
) {
await create(
Expand All @@ -68,7 +69,7 @@ export const bootstrap = {
async createAutomigrateIfExists(
resolver: DBResolver,
sqlExports: SQLExports,
engine?: string,
engine?: Engine,
db?: string,
) {
let tryToMigrate: CreateError[] = [];
Expand Down Expand Up @@ -99,21 +100,21 @@ async function create(
resolver: DBResolver,
sqlExports: SQLExports,
errorHandler: (r: PromiseSettledResult<void>) => void,
engine?: string,
engine?: Engine,
db?: string,
) {
if (engine != null) {
await createForEngine(resolver, engine, sqlExports[engine], errorHandler, db);
return;
}
for (const [engine, dbs] of Object.entries(sqlExports)) {
for (const [engine, dbs] of Object.entries(sqlExports) as [Engine, DBs][]) {
await createForEngine(resolver, engine, dbs, errorHandler);
}
}

async function createForEngine(
resolver: DBResolver,
engine: string,
engine: Engine,
dbs: DBs,
errorHandler: (r: PromiseSettledResult<void>) => void,
db?: string,
Expand Down
7 changes: 5 additions & 2 deletions packages/model-runtime-ts/src/Model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export default abstract class Model<T extends {}> implements IModel<T> {
private subscriptions: Set<() => void> = new Set();
private keyedSubscriptions: Map<keyof T, Set<() => void>> = new Map();

// @ts-ignore
#generatorChange: (x: this) => this;
#generator: ReturnType<typeof observe>;
#generatorUsed: boolean = false;
Expand Down Expand Up @@ -69,7 +70,7 @@ export default abstract class Model<T extends {}> implements IModel<T> {

let unchangedKeys = new Set();
if (newData != null) {
Object.entries(newData).forEach(entry => {
(Object.entries(newData) as [keyof Partial<T>, any][]).forEach(entry => {
if (lastData[entry[0]] === entry[1]) {
unchangedKeys.add(entry[0]);
}
Expand All @@ -87,7 +88,9 @@ export default abstract class Model<T extends {}> implements IModel<T> {
}

_isNoop(updates: Partial<T>) {
return Object.entries(updates).every(entry => this.data[entry[0]] === entry[1]);
return (Object.entries(updates) as [keyof Partial<T>, any][]).every(
entry => this.data[entry[0]] === entry[1],
);
}

private gatherNotifications(changedKeys?: (keyof T)[]): Set<() => void> {
Expand Down
8 changes: 5 additions & 3 deletions packages/model-runtime-ts/src/modelDataEncoding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ export function decodeModelData<D>(data: any, spec: FieldsSpec): D {
}

export function encodeModelData<D>(data: D, spec: FieldsSpec): any {
const encodedFields = Object.entries(spec).filter(([key, value]) => value.encoding !== 'none');
const encodedFields = (Object.entries(spec) as [keyof D, any][]).filter(
([key, value]) => value.encoding !== 'none',
);
if (encodedFields.length === 0) {
return data;
}

const ret = {
const ret: { [Prop in keyof D]: any } = {
...data,
};

Expand All @@ -34,7 +36,7 @@ export function encodeModelData<D>(data: D, spec: FieldsSpec): any {
if (value.encoding === 'json') {
ret[key] = JSON.stringify(ret[key]);
} else {
throw new Error(`Unsupported encoding ${value} on field ${key}`);
throw new Error(`Unsupported encoding ${value} on field ${String(key)}`);
}
});

Expand Down
2 changes: 1 addition & 1 deletion packages/model-runtime-ts/src/that.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export default function that() {
export default function that(this: any) {
return this;
}
2 changes: 1 addition & 1 deletion packages/mutator-runtime-ts/src/commit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export function commit<T extends readonly Changeset<any, any>[]>(
const transaction = new ChangesetExecutor(ctx, changesets).execute();

const optimistic = changesets.map(cs => transaction.nodes.get(cs.id));
let result;
let result: any;
if (singular) {
result = optimistic[0];
} else {
Expand Down
4 changes: 2 additions & 2 deletions packages/parse-ts/src/nodeChildren.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import ts from 'typescript';

export function filter<S extends ts.Node>(n: ts.Node, pred: (n: ts.Node) => n is S): S[] {
const ret: S[] = [];
n.forEachChild(c => {
n.forEachChild((c: ts.Node) => {
if (pred(c)) {
ret.push(c);
}
Expand All @@ -13,7 +13,7 @@ export function filter<S extends ts.Node>(n: ts.Node, pred: (n: ts.Node) => n is

export function map<R>(n: ts.Node, m: (n: ts.Node) => R): R[] {
const ret: R[] = [];
n.forEachChild(c => {
n.forEachChild((c: ts.Node) => {
ret.push(m(c));
});
return ret;
Expand Down
8 changes: 4 additions & 4 deletions packages/query-runtime-ts/src/ChunkIterable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export class PromiseSourceSingleChunkIterable<T> extends BaseChunkIterable<T> {
}

export class MappedChunkIterable<TIn, TOut> extends BaseChunkIterable<TOut> {
constructor(private source: ChunkIterable<TIn>, private fn: (TIn) => Promise<TOut>) {
constructor(private source: ChunkIterable<TIn>, private fn: (x: TIn) => Promise<TOut>) {
super();
}

Expand All @@ -103,7 +103,7 @@ export class MappedChunkIterable<TIn, TOut> extends BaseChunkIterable<TOut> {
}

export class SyncMappedChunkIterable<TIn, TOut> extends BaseChunkIterable<TOut> {
constructor(private source: ChunkIterable<TIn>, private fn: (TIn) => TOut) {
constructor(private source: ChunkIterable<TIn>, private fn: (x: TIn) => TOut) {
super();
}

Expand All @@ -115,7 +115,7 @@ export class SyncMappedChunkIterable<TIn, TOut> extends BaseChunkIterable<TOut>
}

export class FilteredChunkIterable<T> extends BaseChunkIterable<T> {
constructor(private source: ChunkIterable<T>, private fn: (T) => Promise<boolean>) {
constructor(private source: ChunkIterable<T>, private fn: (x: T) => Promise<boolean>) {
super();
}

Expand All @@ -136,7 +136,7 @@ export class FilteredChunkIterable<T> extends BaseChunkIterable<T> {
}

export class SyncFilteredChunkIterable<T> extends BaseChunkIterable<T> {
constructor(private source: ChunkIterable<T>, private fn: (T) => boolean) {
constructor(private source: ChunkIterable<T>, private fn: (x: T) => boolean) {
super();
}

Expand Down
24 changes: 12 additions & 12 deletions packages/query-runtime-ts/src/Expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,19 @@ export type ExpressionType =

export type Direction = 'asc' | 'desc';
export type Expression =
| ReturnType<typeof take>
| ReturnType<typeof before>
| ReturnType<typeof after>
| ReturnType<typeof filter>
| ReturnType<typeof filterAsync>
| ReturnType<typeof orderBy>
| ReturnType<typeof orderByLambda>
| ReturnType<typeof hop>
| ReturnType<typeof modelLoad>
| ReturnType<typeof count>
| ReturnType<typeof take<any>>
| ReturnType<typeof before<any>>
| ReturnType<typeof after<any>>
| ReturnType<typeof filter<any, any>>
| ReturnType<typeof filterAsync<any, any>>
| ReturnType<typeof orderBy<any, any>>
| ReturnType<typeof orderByLambda<any>>
| ReturnType<typeof hop<any, any>>
| ReturnType<typeof modelLoad<any, any>>
| ReturnType<typeof count<any>>
| CountLoadExpression<any>
| ReturnType<typeof map>
| ReturnType<typeof mapAsync>;
| ReturnType<typeof map<any, any>>
| ReturnType<typeof mapAsync<any, any>>;
/*
declare module '@mono/model/query' {
interface Expressions<ReturnType> {
Expand Down
Loading

0 comments on commit 0f08a4e

Please sign in to comment.