Skip to content

Commit 2f839a4

Browse files
committed
refactor: (Counters) Counters Service
- refactor Counters Service - add tests for Counters namespace accessor
1 parent 190d98d commit 2f839a4

26 files changed

+436
-246
lines changed

src/counters/add-and-get.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/counters/compare-and-set.js

Lines changed: 0 additions & 32 deletions
This file was deleted.

src/counters/counter.js

Lines changed: 47 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,72 @@
11
import Utils from '../utils'
2+
import { deprecated } from '../decorators'
3+
4+
import {
5+
incrementAndGet,
6+
decrementAndGet,
7+
get,
8+
getAndIncrement,
9+
getAndDecrement,
10+
getAndAdd,
11+
addAndGet,
12+
compareAndSet,
13+
reset
14+
} from './methods'
215

3-
import Counters from './counters'
416

517
export default class Counter {
618
constructor(name) {
7-
if (!name) {
8-
throw new Error('Missing value for the "counterName" argument. The argument must contain a string value.')
9-
}
10-
11-
if (!Utils.isString(name)) {
12-
//TODO: fix me
13-
throw new Error('Invalid value for the "name" argument. The argument must contain only string values')
19+
if (!name || !Utils.isString(name)) {
20+
throw new Error('Counter Name must be non empty String')
1421
}
1522

1623
this.name = name
1724
}
1825
}
1926

20-
const proxyToCounters = method => function(...args){
21-
return Counters[method](this.name, ...args)
22-
}
27+
const withCounterName = method => (...args) => method(this.name, ...args)
28+
29+
//TODO: will be removed when remove sync methods
30+
const namespaceLabel = 'Backendless.Counter.of(<CounterName>)'
31+
2332

2433
Object.setPrototypeOf(Counter.prototype, {
2534

26-
incrementAndGet: proxyToCounters('incrementAndGet'),
27-
incrementAndGetSync: proxyToCounters('incrementAndGetSync'),
35+
@deprecated(namespaceLabel, `${namespaceLabel}.incrementAndGet`)
36+
incrementAndGetSync: Utils.synchronized(withCounterName(incrementAndGet)),
37+
incrementAndGet : Utils.promisified(withCounterName(incrementAndGet)),
2838

29-
getAndIncrement: proxyToCounters('getAndIncrement'),
30-
getAndIncrementSync: proxyToCounters('getAndIncrementSync'),
39+
@deprecated(namespaceLabel, `${namespaceLabel}.getAndIncrement`)
40+
getAndIncrementSync: Utils.synchronized(withCounterName(getAndIncrement)),
41+
getAndIncrement : Utils.promisified(withCounterName(getAndIncrement)),
3142

32-
decrementAndGet: proxyToCounters('decrementAndGet'),
33-
decrementAndGetSync: proxyToCounters('decrementAndGetSync'),
43+
@deprecated(namespaceLabel, `${namespaceLabel}.decrementAndGet`)
44+
decrementAndGetSync: Utils.synchronized(withCounterName(decrementAndGet)),
45+
decrementAndGet : Utils.promisified(withCounterName(decrementAndGet)),
3446

35-
getAndDecrement: proxyToCounters('getAndDecrement'),
36-
getAndDecrementSync: proxyToCounters('getAndDecrementSync'),
47+
@deprecated(namespaceLabel, `${namespaceLabel}.getAndDecrement`)
48+
getAndDecrementSync: Utils.synchronized(withCounterName(getAndDecrement)),
49+
getAndDecrement : Utils.promisified(withCounterName(getAndDecrement)),
3750

38-
reset: proxyToCounters('reset'),
39-
resetSync: proxyToCounters('resetSync'),
51+
@deprecated(namespaceLabel, `${namespaceLabel}.reset`)
52+
resetSync: Utils.synchronized(withCounterName(reset)),
53+
reset : Utils.promisified(withCounterName(reset)),
4054

41-
get: proxyToCounters('get'),
42-
getSync: proxyToCounters('getSync'),
55+
@deprecated(namespaceLabel, `${namespaceLabel}.get`)
56+
getSync: Utils.synchronized(withCounterName(get)),
57+
get : Utils.promisified(withCounterName(get)),
4358

44-
addAndGet: proxyToCounters('addAndGet'),
45-
addAndGetSync: proxyToCounters('addAndGetSync'),
59+
@deprecated(namespaceLabel, `${namespaceLabel}.addAndGet`)
60+
addAndGetSync: Utils.synchronized(withCounterName(addAndGet)),
61+
addAndGet : Utils.promisified(withCounterName(addAndGet)),
4662

47-
getAndAdd: proxyToCounters('getAndAdd'),
48-
getAndAddSync: proxyToCounters('getAndAddSync'),
63+
@deprecated(namespaceLabel, `${namespaceLabel}.getAndAdd`)
64+
getAndAddSync: Utils.synchronized(withCounterName(getAndAdd)),
65+
getAndAdd : Utils.promisified(withCounterName(getAndAdd)),
4966

50-
compareAndSet: proxyToCounters('compareAndSet'),
51-
compareAndSetSync: proxyToCounters('compareAndSetSync'),
67+
@deprecated(namespaceLabel, `${namespaceLabel}.compareAndSet`)
68+
compareAndSetSync: Utils.synchronized(withCounterName(compareAndSet)),
69+
compareAndSet : Utils.promisified(withCounterName(compareAndSet)),
5270

5371
})
5472

src/counters/decrement-and-get.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/counters/get-and-add.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/counters/get-and-decrement.js

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/counters/get-and-increment.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/counters/get.js

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/counters/implement-method-with-value.js

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/counters/implement-method.js

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)