Skip to content

Commit e931732

Browse files
Merge pull request #154 from shiftcode/#153-remove-rxjs
refactor(*): replace Observables with Promises
2 parents 699539e + 179c684 commit e931732

File tree

58 files changed

+588
-641
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+588
-641
lines changed

package-lock.json

Lines changed: 69 additions & 69 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@
6262
"prettier": "^1.15.3",
6363
"reflect-metadata": "^0.1.12",
6464
"rimraf": "^2.6.3",
65-
"rxjs": "^6.3.3",
6665
"semantic-release": "^16.0.0-beta.16",
6766
"sort-package-json": "^1.16.0",
6867
"ts-jest": "^23.10.4",
@@ -80,7 +79,6 @@
8079
"aws-sdk": "^2.382.0",
8180
"lodash": "^4.17.11",
8281
"reflect-metadata": "^0.1.12",
83-
"rxjs": "^6.3.3",
8482
"uuid": "^3.3.2"
8583
}
8684
}

src/dynamo/batchget/batch-get-utils.spec.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import * as DynamoDB from 'aws-sdk/clients/dynamodb'
2-
import { of } from 'rxjs'
3-
import { DynamoRx } from '../dynamo-rx'
2+
import { DynamoPromisified } from '../dynamo-promisified'
43
import { batchGetItemsFetchAll, combineBatchGetResponses, hasUnprocessedKeys } from './batch-get-utils'
54

65
describe('batch-get utils', () => {
@@ -52,7 +51,7 @@ describe('batch-get utils', () => {
5251

5352
describe('batchGetItemsFetchAll', () => {
5453
let batchGetItemsSpy: jasmine.Spy
55-
let dynamoRx: DynamoRx
54+
let dynamoRx: DynamoPromisified
5655
let backoffTimerMock: { next: jasmine.Spy }
5756

5857
const output1: DynamoDB.BatchGetItemOutput = {
@@ -72,11 +71,11 @@ describe('batch-get utils', () => {
7271
}
7372

7473
beforeEach(async () => {
75-
batchGetItemsSpy = jasmine.createSpy().and.returnValues(of(output1), of(output2))
74+
batchGetItemsSpy = jasmine.createSpy().and.returnValues(Promise.resolve(output1), Promise.resolve(output2))
7675
dynamoRx = <any>{ batchGetItems: batchGetItemsSpy }
7776
backoffTimerMock = { next: jasmine.createSpy().and.returnValue({ value: 0 }) }
7877

79-
await batchGetItemsFetchAll(dynamoRx, <any>{}, <IterableIterator<number>>(<any>backoffTimerMock), 0).toPromise()
78+
await batchGetItemsFetchAll(dynamoRx, <any>{}, <IterableIterator<number>>(<any>backoffTimerMock), 0)
8079
})
8180

8281
it('should use UnprocessedKeys for next request', () => {

src/dynamo/batchget/batch-get-utils.ts

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import * as DynamoDB from 'aws-sdk/clients/dynamodb'
2-
import { Observable, of } from 'rxjs'
3-
import { delay, map, mergeMap } from 'rxjs/operators'
4-
import { DynamoRx } from '../dynamo-rx'
2+
import { promiseDelay } from '../../helper'
3+
import { DynamoPromisified } from '../dynamo-promisified'
54

65
/**
76
* Function which executes batchGetItem operations until all given items (as params) are processed (fetched).
@@ -12,31 +11,29 @@ import { DynamoRx } from '../dynamo-rx'
1211
* @param throttleTimeSlot used to calculate the effective wait time
1312
*/
1413
export function batchGetItemsFetchAll(
15-
dynamoRx: DynamoRx,
14+
dynamoRx: DynamoPromisified,
1615
params: DynamoDB.BatchGetItemInput,
1716
backoffTimer: IterableIterator<number>,
1817
throttleTimeSlot: number,
19-
): Observable<DynamoDB.BatchGetItemOutput> {
20-
return dynamoRx.batchGetItems(params).pipe(
21-
mergeMap(response => {
22-
if (hasUnprocessedKeys(response)) {
23-
// in case of unprocessedItems do a follow-up requests
24-
return of(response.UnprocessedKeys).pipe(
18+
): Promise<DynamoDB.BatchGetItemOutput> {
19+
return dynamoRx.batchGetItems(params)
20+
.then(response => {
21+
if (hasUnprocessedKeys(response)) {
22+
// in case of unprocessedKeys do a follow-up requests
23+
return Promise.resolve(response.UnprocessedKeys)
2524
// delay before doing the follow-up request
26-
delay(backoffTimer.next().value * throttleTimeSlot),
27-
28-
mergeMap((UnprocessedKeys: DynamoDB.BatchGetRequestMap) => {
29-
const nextParams = { ...params, RequestItems: UnprocessedKeys }
30-
// call recursively batchGetItemsFetchAll with the returned UnprocessedItems params
31-
return batchGetItemsFetchAll(dynamoRx, nextParams, backoffTimer, throttleTimeSlot)
32-
}),
33-
map(combineBatchGetResponses(response)),
34-
)
35-
}
36-
// no follow-up request necessary, return result
37-
return of(response)
38-
}),
39-
)
25+
.then(promiseDelay(backoffTimer.next().value * throttleTimeSlot))
26+
.then(UnprocessedKeys => {
27+
const nextParams = { ...params, RequestItems: UnprocessedKeys }
28+
// call recursively batchGetItemsFetchAll with the returned UnprocessedItems params
29+
return batchGetItemsFetchAll(dynamoRx, nextParams, backoffTimer, throttleTimeSlot)
30+
})
31+
.then(combineBatchGetResponses(response))
32+
}
33+
// no follow-up request necessary, return result
34+
return response
35+
},
36+
)
4037
}
4138

4239
export type BatchGetItemOutputWithUnprocessedKeys = DynamoDB.BatchGetItemOutput & {

src/dynamo/batchget/batch-get.request.spec.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
// tslint:disable:no-non-null-assertion
22
import * as DynamoDB from 'aws-sdk/clients/dynamodb'
3-
import { of } from 'rxjs'
43
import { Organization, SimpleWithCompositePartitionKeyModel, SimpleWithPartitionKeyModel } from '../../../test/models'
54
import { Attributes, toDb } from '../../mapper'
6-
import { DynamoRx } from '../dynamo-rx'
5+
import { DynamoPromisified } from '../dynamo-promisified'
76
import { getTableName } from '../get-table-name.function'
87
import { BatchGetRequest } from './batch-get.request'
98

@@ -117,16 +116,16 @@ describe('batch get', () => {
117116
request = new BatchGetRequest()
118117
request.forModel(SimpleWithPartitionKeyModel, [jsItem1, jsItem2])
119118

120-
batchGetItemsSpy = jasmine.createSpy().and.returnValues(of(output1), of(output2))
121-
const dynamoRx: DynamoRx = <any>{ batchGetItems: batchGetItemsSpy }
119+
batchGetItemsSpy = jasmine.createSpy().and.returnValues(Promise.resolve(output1), Promise.resolve(output2))
120+
const dynamoRx: DynamoPromisified = <any>{ batchGetItems: batchGetItemsSpy }
122121

123122
Object.assign(request, { dynamoRx })
124123

125124
nextSpyFn = jest.fn().mockImplementation(() => ({ value: 0 }))
126125
})
127126

128127
it('[execNoMap] should backoff and retry when UnprocessedItems are returned', async () => {
129-
const result = await request.execNoMap(generatorMock).toPromise()
128+
const result = await request.execNoMap(generatorMock)
130129
expect(nextSpyFn).toHaveBeenCalledTimes(1)
131130
expect(batchGetItemsSpy).toHaveBeenCalledTimes(2)
132131
expect(result).toBeDefined()
@@ -140,7 +139,7 @@ describe('batch get', () => {
140139
})
141140

142141
it('[execFullResponse] should backoff and retry when UnprocessedItems are returned', async () => {
143-
const result = await request.execFullResponse(generatorMock).toPromise()
142+
const result = await request.execFullResponse(generatorMock)
144143
expect(nextSpyFn).toHaveBeenCalledTimes(1)
145144
expect(batchGetItemsSpy).toHaveBeenCalledTimes(2)
146145
expect(result).toBeDefined()
@@ -154,7 +153,7 @@ describe('batch get', () => {
154153
})
155154

156155
it('[exec] should backoff and retry when UnprocessedItems are returned', async () => {
157-
const result = await request.exec(generatorMock).toPromise()
156+
const result = await request.exec(generatorMock)
158157
expect(nextSpyFn).toHaveBeenCalledTimes(1)
159158
expect(batchGetItemsSpy).toHaveBeenCalledTimes(2)
160159
expect(result).toBeDefined()
@@ -182,21 +181,21 @@ describe('batch get', () => {
182181
}
183182

184183
beforeEach(() => {
185-
batchGetItemsSpy = jasmine.createSpy().and.returnValue(of(sampleResponse))
186-
const dynamoRx: DynamoRx = <any>{ batchGetItems: batchGetItemsSpy }
184+
batchGetItemsSpy = jasmine.createSpy().and.returnValue(Promise.resolve(sampleResponse))
185+
const dynamoRx: DynamoPromisified = <any>{ batchGetItems: batchGetItemsSpy }
187186
request = new BatchGetRequest()
188187
Object.assign(request, { dynamoRx })
189188
request.forModel(SimpleWithPartitionKeyModel, [{ id: 'idVal' }])
190189
})
191190

192191
it('exec', async () => {
193-
const result = await request.exec().toPromise()
192+
const result = await request.exec()
194193
expect(batchGetItemsSpy).toHaveBeenCalled()
195194
expect(result).toEqual({ [getTableName(SimpleWithPartitionKeyModel)]: [jsItem] })
196195
})
197196

198197
it('execFullResponse', async () => {
199-
const result = await request.execFullResponse().toPromise()
198+
const result = await request.execFullResponse()
200199
expect(batchGetItemsSpy).toHaveBeenCalled()
201200
expect(result).toEqual({
202201
Responses: { [getTableName(SimpleWithPartitionKeyModel)]: [jsItem] },

src/dynamo/batchget/batch-get.request.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import * as DynamoDB from 'aws-sdk/clients/dynamodb'
2-
import { Observable } from 'rxjs'
3-
import { map } from 'rxjs/operators'
42
import { metadataForClass } from '../../decorator/metadata/metadata-helper'
53
import { randomExponentialBackoffTimer } from '../../helper'
64
import { createToKeyFn, fromDb } from '../../mapper'
75
import { Attributes } from '../../mapper/type/attribute.type'
86
import { ModelConstructor } from '../../model/model-constructor'
9-
import { DynamoRx } from '../dynamo-rx'
7+
import { DynamoPromisified } from '../dynamo-promisified'
108
import { getTableName } from '../get-table-name.function'
119
import { BatchGetFullResponse } from './batch-get-full.response'
1210
import { batchGetItemsFetchAll } from './batch-get-utils'
@@ -15,12 +13,12 @@ import { BatchGetResponse } from './batch-get.response'
1513

1614
export class BatchGetRequest {
1715
readonly params: DynamoDB.BatchGetItemInput
18-
private readonly dynamoRx: DynamoRx
16+
private readonly dynamoRx: DynamoPromisified
1917
private readonly tables: Map<string, ModelConstructor<any>> = new Map()
2018
private itemCounter = 0
2119

2220
constructor() {
23-
this.dynamoRx = new DynamoRx()
21+
this.dynamoRx = new DynamoPromisified()
2422
this.params = {
2523
RequestItems: {},
2624
}
@@ -64,25 +62,25 @@ export class BatchGetRequest {
6462
execNoMap(
6563
backoffTimer = randomExponentialBackoffTimer,
6664
throttleTimeSlot = BATCH_GET_DEFAULT_TIME_SLOT,
67-
): Observable<DynamoDB.BatchGetItemOutput> {
65+
): Promise<DynamoDB.BatchGetItemOutput> {
6866
return this.fetch(backoffTimer, throttleTimeSlot)
6967
}
7068

7169
execFullResponse(
7270
backoffTimer = randomExponentialBackoffTimer,
7371
throttleTimeSlot = BATCH_GET_DEFAULT_TIME_SLOT,
74-
): Observable<BatchGetFullResponse> {
75-
return this.fetch(backoffTimer, throttleTimeSlot).pipe(map(this.mapResponse))
72+
): Promise<BatchGetFullResponse> {
73+
return this.fetch(backoffTimer, throttleTimeSlot)
74+
.then(this.mapResponse)
7675
}
7776

7877
exec(
7978
backoffTimer = randomExponentialBackoffTimer,
8079
throttleTimeSlot = BATCH_GET_DEFAULT_TIME_SLOT,
81-
): Observable<BatchGetResponse> {
82-
return this.fetch(backoffTimer, throttleTimeSlot).pipe(
83-
map(this.mapResponse),
84-
map(r => r.Responses),
85-
)
80+
): Promise<BatchGetResponse> {
81+
return this.fetch(backoffTimer, throttleTimeSlot)
82+
.then(this.mapResponse)
83+
.then(r => r.Responses)
8684
}
8785

8886
private fetch(backoffTimer = randomExponentialBackoffTimer, throttleTimeSlot = BATCH_GET_DEFAULT_TIME_SLOT) {

src/dynamo/batchwrite/batch-write-utils.spec.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import * as DynamoDB from 'aws-sdk/clients/dynamodb'
2-
import { of } from 'rxjs'
3-
import { DynamoRx } from '../dynamo-rx'
2+
import { DynamoPromisified } from '../dynamo-promisified'
43
import { batchWriteItemsWriteAll, hasUnprocessedItems } from './batch-write-utils'
54

65
describe('batch-write-utils', () => {
76
describe('batchWriteItemsWriteAll', () => {
87
let batchWriteItemSpy: jasmine.Spy
9-
let dynamoRx: DynamoRx
8+
let dynamoRx: DynamoPromisified
109
let backoffTimerMock: { next: jasmine.Spy }
1110

1211
const output1: DynamoDB.BatchWriteItemOutput = {
@@ -21,11 +20,11 @@ describe('batch-write-utils', () => {
2120
const output2: DynamoDB.BatchWriteItemOutput = {}
2221

2322
beforeEach(async () => {
24-
batchWriteItemSpy = jasmine.createSpy().and.returnValues(of(output1), of(output2))
23+
batchWriteItemSpy = jasmine.createSpy().and.returnValues(Promise.resolve(output1), Promise.resolve(output2))
2524
dynamoRx = <any>{ batchWriteItem: batchWriteItemSpy }
2625
backoffTimerMock = { next: jasmine.createSpy().and.returnValue({ value: 0 }) }
2726

28-
await batchWriteItemsWriteAll(dynamoRx, <any>{}, <IterableIterator<number>>(<any>backoffTimerMock), 0).toPromise()
27+
await batchWriteItemsWriteAll(dynamoRx, <any>{}, <IterableIterator<number>>(<any>backoffTimerMock), 0)
2928
})
3029

3130
it('should use UnprocessedKeys for next request', () => {

src/dynamo/batchwrite/batch-write-utils.ts

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import * as DynamoDB from 'aws-sdk/clients/dynamodb'
2-
import { Observable, of } from 'rxjs'
3-
import { delay, mergeMap } from 'rxjs/operators'
4-
import { DynamoRx } from '../dynamo-rx'
2+
import { promiseDelay } from '../../helper'
3+
import { DynamoPromisified } from '../dynamo-promisified'
54

65
/**
76
* Function which executes batchWriteItem operations until all given items (as params) are processed (written).
@@ -12,31 +11,28 @@ import { DynamoRx } from '../dynamo-rx'
1211
* @param throttleTimeSlot used to calculate the effective wait time
1312
*/
1413
export function batchWriteItemsWriteAll(
15-
dynamoRx: DynamoRx,
14+
dynamoRx: DynamoPromisified,
1615
params: DynamoDB.BatchWriteItemInput,
1716
backoffTimer: IterableIterator<number>,
1817
throttleTimeSlot: number,
19-
): Observable<DynamoDB.BatchGetItemOutput> {
20-
return dynamoRx.batchWriteItem(params).pipe(
21-
mergeMap(response => {
18+
): Promise<DynamoDB.BatchGetItemOutput> {
19+
return dynamoRx.batchWriteItem(params)
20+
.then(response => {
2221
if (hasUnprocessedItems(response)) {
2322
// in case of unprocessedItems do a follow-up requests
24-
return of(response.UnprocessedItems).pipe(
23+
return Promise.resolve(response.UnprocessedItems)
2524
// delay before doing the follow-up request
26-
delay(backoffTimer.next().value * throttleTimeSlot),
27-
28-
mergeMap((unprocessedKeys: DynamoDB.BatchWriteItemRequestMap) => {
25+
.then(promiseDelay(backoffTimer.next().value * throttleTimeSlot))
26+
.then(unprocessedKeys => {
2927
const nextParams: DynamoDB.BatchWriteItemInput = { ...params, RequestItems: unprocessedKeys }
3028
// call recursively batchWriteItemsWriteAll with the returned UnprocessedItems params
3129
return batchWriteItemsWriteAll(dynamoRx, nextParams, backoffTimer, throttleTimeSlot)
32-
}),
33-
// no combining of responses necessary, only the last response is returned
34-
)
30+
})
31+
// no combining of responses necessary, only the last response is returned
3532
}
3633
// no follow-up request necessary, return result
37-
return of(response)
38-
}),
39-
)
34+
return response
35+
})
4036
}
4137

4238
export type BatchWriteItemOutputWithUnprocessedItems = DynamoDB.BatchWriteItemOutput & {

src/dynamo/batchwrite/batch-write.request.spec.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { of } from 'rxjs'
21
import { ComplexModel, SimpleWithPartitionKeyModel } from '../../../test/models'
32
import { getTableName } from '../get-table-name.function'
43
import { BatchWriteRequest } from './batch-write.request'
@@ -133,18 +132,18 @@ describe('batchWriteRequest', () => {
133132
const output = {
134133
myResponse: true,
135134
}
136-
batchWriteItemSpy = jasmine.createSpy().and.returnValue(of(output))
135+
batchWriteItemSpy = jasmine.createSpy().and.returnValue(Promise.resolve(output))
137136
const dynamoRx = <any>{ batchWriteItem: batchWriteItemSpy }
138137
req = new BatchWriteRequest()
139138
Object.assign(req, { dynamoRx })
140139
})
141140

142141
it('exec should return void', async () => {
143-
expect(await req.exec().toPromise()).toBeUndefined()
142+
expect(await req.exec()).toBeUndefined()
144143
})
145144

146145
it('execFullResponse should return the full response', async () => {
147-
expect(await req.execFullResponse().toPromise()).toEqual({ myResponse: true })
146+
expect(await req.execFullResponse()).toEqual({ myResponse: true })
148147
})
149148
})
150149
})

0 commit comments

Comments
 (0)