Skip to content

Commit 1cb9882

Browse files
committed
refactor(*): replace Observables with Promises
BREAKING CHANGE: all requests no work with and return promises
1 parent 699539e commit 1cb9882

File tree

55 files changed

+457
-518
lines changed

Some content is hidden

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

55 files changed

+457
-518
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: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import * as DynamoDB from 'aws-sdk/clients/dynamodb'
2-
import { of } from 'rxjs'
32
import { DynamoRx } from '../dynamo-rx'
43
import { batchGetItemsFetchAll, combineBatchGetResponses, hasUnprocessedKeys } from './batch-get-utils'
54

@@ -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: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import * as DynamoDB from 'aws-sdk/clients/dynamodb'
2-
import { Observable, of } from 'rxjs'
3-
import { delay, map, mergeMap } from 'rxjs/operators'
2+
import { promiseDelay } from '../../helper'
43
import { DynamoRx } from '../dynamo-rx'
54

65
/**
@@ -16,27 +15,25 @@ export function batchGetItemsFetchAll(
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: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
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'
65
import { DynamoRx } from '../dynamo-rx'
@@ -117,7 +116,7 @@ describe('batch get', () => {
117116
request = new BatchGetRequest()
118117
request.forModel(SimpleWithPartitionKeyModel, [jsItem1, jsItem2])
119118

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

123122
Object.assign(request, { dynamoRx })
@@ -126,7 +125,7 @@ describe('batch get', () => {
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))
184+
batchGetItemsSpy = jasmine.createSpy().and.returnValue(Promise.resolve(sampleResponse))
186185
const dynamoRx: DynamoRx = <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: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
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'
@@ -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: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import * as DynamoDB from 'aws-sdk/clients/dynamodb'
2-
import { of } from 'rxjs'
32
import { DynamoRx } from '../dynamo-rx'
43
import { batchWriteItemsWriteAll, hasUnprocessedItems } from './batch-write-utils'
54

@@ -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: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import * as DynamoDB from 'aws-sdk/clients/dynamodb'
2-
import { Observable, of } from 'rxjs'
3-
import { delay, mergeMap } from 'rxjs/operators'
2+
import { promiseDelay } from '../../helper'
43
import { DynamoRx } from '../dynamo-rx'
54

65
/**
@@ -16,27 +15,24 @@ export function batchWriteItemsWriteAll(
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
})

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

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import * as DynamoDB from 'aws-sdk/clients/dynamodb'
2-
import { Observable } from 'rxjs'
3-
import { map } from 'rxjs/operators'
42
import { randomExponentialBackoffTimer } from '../../helper'
53
import { createToKeyFn, toDb } from '../../mapper'
64
import { ModelConstructor } from '../../model'
@@ -49,18 +47,15 @@ export class BatchWriteRequest {
4947
exec(
5048
backoffTimer = randomExponentialBackoffTimer,
5149
throttleTimeSlot = BATCH_WRITE_DEFAULT_TIME_SLOT,
52-
): Observable<void> {
53-
return this.write(backoffTimer, throttleTimeSlot).pipe(
54-
map(() => {
55-
return
56-
}),
57-
)
50+
): Promise<void> {
51+
return this.write(backoffTimer, throttleTimeSlot)
52+
.then(() => { return })
5853
}
5954

6055
execFullResponse(
6156
backoffTimer = randomExponentialBackoffTimer,
6257
throttleTimeSlot = BATCH_WRITE_DEFAULT_TIME_SLOT,
63-
): Observable<DynamoDB.BatchWriteItemOutput> {
58+
): Promise<DynamoDB.BatchWriteItemOutput> {
6459
return this.write(backoffTimer, throttleTimeSlot)
6560
}
6661

0 commit comments

Comments
 (0)