mirror of
https://github.com/Unleash/unleash.git
synced 2025-01-25 00:07:47 +01:00
chore: simplify custom strategy example
This commit is contained in:
parent
dab134ea22
commit
05de035163
@ -34,28 +34,24 @@ All official client SDK's for Unleash provides abstractions for you to implement
|
|||||||
In Node.js the implementation for the `TimeStampStrategy` would be:
|
In Node.js the implementation for the `TimeStampStrategy` would be:
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
const moment = require('moment');
|
|
||||||
|
|
||||||
class TimeStampStrategy extends Strategy {
|
class TimeStampStrategy extends Strategy {
|
||||||
constructor() {
|
constructor() {
|
||||||
super('TimeStamp');
|
super('TimeStamp');
|
||||||
}
|
}
|
||||||
|
|
||||||
isEnabled(parameters, context) {
|
isEnabled(parameters, context) {
|
||||||
const enableAfterTimeStamp = moment(parameters.enableAfter);
|
Date.parse(parameters.enableAfter) < Date.now();
|
||||||
return moment().isAfter(enableAfterTimeStamp);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
In the example implementation we make use of the library called moment to parse the timestamp and verify that current time is after the specified `enabledAfter` parameter.
|
In the example implementation we make use of the library called moment to parse the timestamp and verify that current time is after the specified `enabledAfter` parameter.
|
||||||
|
|
||||||
All parameter injected to the strategy are handled as `string` objects. This means that the strategies needs to parse it to a more suitable format. In this example we make use of the [moment](https://github.com/moment/moment) library. This library makes it also very convenient to check if we have passed the date/time specified via the `enabledAfter` parameter.
|
All parameter injected to the strategy are handled as `string` objects. This means that the strategies needs to parse it to a more suitable format. In this example we just parse it directly to a `Date` type and do the comparison directly. You might want to also consider timezone in a real implementation.
|
||||||
|
|
||||||
We also have to remember to register the custom strategy when initializing the Unleash client. Full working code example:
|
We also have to remember to register the custom strategy when initializing the Unleash client. Full working code example:
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
const moment = require('moment');
|
|
||||||
const { Strategy, initialize, isEnabled } = require('unleash-client');
|
const { Strategy, initialize, isEnabled } = require('unleash-client');
|
||||||
|
|
||||||
class TimeStampStrategy extends Strategy {
|
class TimeStampStrategy extends Strategy {
|
||||||
@ -64,8 +60,7 @@ class TimeStampStrategy extends Strategy {
|
|||||||
}
|
}
|
||||||
|
|
||||||
isEnabled(parameters, context) {
|
isEnabled(parameters, context) {
|
||||||
const enableAfterTimeStamp = moment(parameters.enableAfter);
|
return Date.parse(parameters.enableAfter) < Date.now();
|
||||||
return moment().isAfter(enableAfterTimeStamp);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user