2018-11-22 21:30:05 +01:00
---
2018-11-23 10:25:11 +01:00
id: custom_activation_strategy
2018-11-22 21:30:05 +01:00
title: Custom Activation Strategy
---
2018-11-23 10:25:11 +01:00
Even though Unleash comes with a few powerful [activation strategies ](activation-strategies.md ) there might be scenarios where you would like to extend Unleash with your own custom strategies.
2018-11-22 21:30:05 +01:00
### Example: TimeStamp Strategy
2018-11-23 10:25:11 +01:00
In this example we want to define an activation strategy offers a scheduled release of a feature toggle. This means that we want the feature toggle to be activated after a given date and time.
2018-11-22 21:30:05 +01:00
#### Define custom strategy
2018-11-23 10:25:11 +01:00
First we need to "define" our new strategy. To add a new "Strategy", open the Strategies tab from the sidebar.
2018-11-22 21:30:05 +01:00
2019-08-27 21:40:20 +02:00
![timestamp_create_strategy ](../assets/timestamp_create_strategy.png )
2018-11-22 21:30:05 +01:00
2018-11-23 10:25:11 +01:00
We name our strategy `TimeStamp` and add one required parameter of type string, which we call `enableAfter` .
2018-11-22 21:30:05 +01:00
#### Use custom strategy
2018-11-23 10:25:11 +01:00
After we have created the strategy definition, we can now decide to use that activation strategy for our feature toggle.
2018-11-22 21:30:05 +01:00
2019-08-27 21:40:20 +02:00
![timestamp_use_strategy ](../assets/timestamp_use_strategy.png )
2018-11-22 21:30:05 +01:00
In the example we want to use our custom strategy for the feature toggle named `demo.TimeStampRollout` .
2018-11-23 10:25:11 +01:00
#### Client implementation
2018-11-22 21:30:05 +01:00
2018-11-23 10:25:11 +01:00
All official client SDK's for Unleash provides abstractions for you to implement support for custom strategies.
2018-11-22 21:30:05 +01:00
> Before you have provided support for the custom strategy; the client will return false, because it does not understand the activation strategy.
In Node.js the implementation for the `TimeStampStrategy` would be:
```javascript
class TimeStampStrategy extends Strategy {
constructor() {
super('TimeStamp');
}
isEnabled(parameters, context) {
2018-11-23 14:03:52 +01:00
Date.parse(parameters.enableAfter) < Date.now ( ) ;
2018-11-22 21:30:05 +01:00
}
}
```
2018-11-23 10:25:11 +01:00
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.
2018-11-23 14:03:52 +01:00
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.
2018-11-22 21:30:05 +01:00
2018-11-23 10:25:11 +01:00
We also have to remember to register the custom strategy when initializing the Unleash client. Full working code example:
2018-11-22 21:30:05 +01:00
```javascript
const { Strategy, initialize, isEnabled } = require('unleash-client');
class TimeStampStrategy extends Strategy {
constructor() {
super('TimeStamp');
}
isEnabled(parameters, context) {
2018-11-23 14:03:52 +01:00
return Date.parse(parameters.enableAfter) < Date.now ( ) ;
2018-11-22 21:30:05 +01:00
}
}
const instance = initialize({
url: 'http://unleash.herokuapp.com/api/',
appName: 'unleash-demo',
instanceId: '1',
strategies: [new TimeStampStrategy()],
});
instance.on('ready', () => {
setInterval(() => {
console.log(isEnabled('demo.TimeStampRollout'));
}, 1000);
});
```