Skip to content

Commit b7d8211

Browse files
authored
Merge pull request #55 from ReactiveDB/chore/upgrade-ts
chore(dependency): upgrade `typescript` to 2.8
2 parents 8a233fc + 6da3d09 commit b7d8211

File tree

8 files changed

+48
-26
lines changed

8 files changed

+48
-26
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989
"tslint": "^5.9.1",
9090
"tslint-eslint-rules": "^5.1.0",
9191
"tslint-loader": "^3.6.0",
92-
"typescript": "^2.7.2",
92+
"typescript": "^2.8.3",
9393
"webpack": "^4.1.1",
9494
"webpack-cli": "^2.0.10",
9595
"webpack-dev-server": "^3.1.0"

src/storage/Database.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import Version from '../version'
1515
import { Traversable } from '../shared'
1616
import { Mutation, Selector, QueryToken, PredicateProvider } from './modules'
1717
import { dispose, contextTableName, fieldIdentifier, hiddenColName } from './symbols'
18-
import { forEach, clone, contains, tryCatch, hasOwn, getType, assert, identity, warn } from '../utils'
18+
import { forEach, clone, contains, tryCatch, hasOwn, getType, assert, assertValue, warn, isNonNullable } from '../utils'
1919
import { createPredicate, createPkClause, mergeTransactionResult, predicatableQuery, lfFactory } from './helper'
2020
import { Relationship, RDBType, DataStoreType, LeafType, StatementType, JoinMode } from '../interface/enum'
2121
import { SchemaDef, ColumnDef, ParsedSchema, Association, ScopedHandler } from '../interface'
@@ -51,8 +51,7 @@ export class Database {
5151

5252
private findSchema = (name: string): ParsedSchema => {
5353
const schema = this.schemas.get(name)
54-
assert(schema, Exception.NonExistentTable(name))
55-
return schema!
54+
return assertValue(schema, Exception.NonExistentTable(name))
5655
}
5756

5857
/**
@@ -500,7 +499,7 @@ export class Database {
500499
indexes.push(key)
501500
}
502501

503-
const isNullable = ![def.primaryKey, def.index, def.unique].some(identity)
502+
const isNullable = ![def.primaryKey, def.index, def.unique].some(isNonNullable)
504503
if (isNullable) {
505504
nullable.push(key)
506505
}

src/storage/modules/Mutation.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import * as lf from 'lovefield'
22

3-
import { forEach, assert, warn } from '../../utils'
3+
import { forEach, assertValue, warn } from '../../utils'
44
import { fieldIdentifier } from '../symbols'
55
import * as Exception from '../../exception'
66

77
export class Mutation {
88

99
private params: Object
10-
private meta!: {
10+
private meta: {
1111
key: string,
1212
val: any
13-
}
13+
} | undefined
1414

1515
constructor(
1616
private db: lf.Database,
@@ -67,10 +67,9 @@ export class Mutation {
6767
}
6868

6969
private toUpdater() {
70-
assert(this.meta, Exception.PrimaryKeyNotProvided())
71-
70+
const meta = assertValue(this.meta, Exception.PrimaryKeyNotProvided())
7271
const query = this.db.update(this.table)
73-
query.where(this.table[this.meta.key].eq(this.meta.val))
72+
query.where(this.table[meta.key].eq(meta.val))
7473

7574
forEach(this.params, (val, key) => {
7675
const column = this.table[key]
@@ -85,12 +84,11 @@ export class Mutation {
8584
}
8685

8786
private toRow() {
88-
assert(this.meta, Exception.PrimaryKeyNotProvided())
89-
87+
const meta = assertValue(this.meta, Exception.PrimaryKeyNotProvided())
9088
return {
9189
table: this.table,
9290
row: this.table.createRow({
93-
[this.meta.key]: this.meta.val,
91+
[meta.key]: meta.val,
9492
...this.params
9593
})
9694
}

src/storage/modules/Selector.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Observer } from 'rxjs/Observer'
22
import { Observable } from 'rxjs/Observable'
33
import { OperatorFunction } from 'rxjs/interfaces'
4+
import { filter } from 'rxjs/operators/filter'
45
import { from } from 'rxjs/observable/from'
56
import { fromPromise } from 'rxjs/observable/fromPromise'
67
import { combineAll } from 'rxjs/operators/combineAll'
@@ -10,12 +11,13 @@ import { mergeMap } from 'rxjs/operators/mergeMap'
1011
import { publishReplay } from 'rxjs/operators/publishReplay'
1112
import { reduce } from 'rxjs/operators/reduce'
1213
import { refCount } from 'rxjs/operators/refCount'
14+
import { scan } from 'rxjs/operators/scan'
1315
import { switchMap } from 'rxjs/operators/switchMap'
1416
import { async } from 'rxjs/scheduler/async'
1517
import * as lf from 'lovefield'
1618
import * as Exception from '../../exception'
1719
import { predicatableQuery, graph } from '../helper'
18-
import { identity, forEach, assert, warn } from '../../utils'
20+
import { identity, forEach, assert, warn, isNonNullable } from '../../utils'
1921
import { PredicateProvider } from './PredicateProvider'
2022
import { ShapeMatcher, OrderInfo, StatementType } from '../../interface'
2123
import { mapFn } from './mapFn'
@@ -120,9 +122,10 @@ export class Selector <T> {
120122
)
121123
: observeOn(this.getQuery())
122124

123-
return lfIssueFix(changesOnQuery)
124-
.publishReplay(1)
125-
.refCount()
125+
return lfIssueFix(changesOnQuery).pipe(
126+
publishReplay(1),
127+
refCount()
128+
)
126129
}
127130

128131
private set change$ (dist$: Observable<T[]>) {
@@ -315,5 +318,8 @@ const lfIssueFix = <T>(changes: Observable<T[]>) => {
315318
? null
316319
: curr
317320

318-
return (changes as any).scan(doKeep, null).filter(Boolean)
321+
return changes.pipe(
322+
scan(doKeep, null),
323+
filter(isNonNullable)
324+
)
319325
}

src/utils/assert.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
1-
export function assert(condition: any, error: Error | string) {
2-
if (condition) {
3-
return
1+
import { Truthy } from './truthy'
2+
3+
export function assert(condition: boolean, error: Error | string): void {
4+
truthyOrThrow(condition, error)
5+
}
6+
7+
export function assertValue<T>(
8+
value: T,
9+
error: Error | string
10+
): Truthy<T> | never {
11+
return truthyOrThrow(value, error)
12+
}
13+
14+
function truthyOrThrow<T>(x: T, error: Error | string): Truthy<T> | never {
15+
if (x) {
16+
return x as Truthy<T>
417
}
518

619
if (error instanceof Error) {
720
throw error
8-
} else if (typeof error === 'string') {
21+
} else {
922
throw new Error(error)
1023
}
1124
}

src/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ export * from './contains'
88
export * from './for-each'
99
export * from './identity'
1010
export * from './get-type'
11+
export * from './truthy'
1112
export * from './try-catch'
1213
export { warn } from './warn'

src/utils/truthy.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export function isNonNullable<T = any>(x: T): x is NonNullable<T> {
2+
return x != null
3+
}
4+
5+
export type Truthy<T> = Exclude<T, undefined | null | false | 0 | ''>

yarn.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7335,9 +7335,9 @@ typescript@^2.4.2, typescript@^2.6.1:
73357335
version "2.6.2"
73367336
resolved "https://registry.npmjs.org/typescript/-/typescript-2.6.2.tgz#3c5b6fd7f6de0914269027f03c0946758f7673a4"
73377337

7338-
typescript@^2.7.2:
7339-
version "2.7.2"
7340-
resolved "https://registry.npmjs.org/typescript/-/typescript-2.7.2.tgz#2d615a1ef4aee4f574425cdff7026edf81919836"
7338+
typescript@^2.8.3:
7339+
version "2.8.3"
7340+
resolved "https://registry.npmjs.org/typescript/-/typescript-2.8.3.tgz#5d817f9b6f31bb871835f4edf0089f21abe6c170"
73417341

73427342
uglify-es@^3.3.4:
73437343
version "3.3.9"

0 commit comments

Comments
 (0)