A demonstration of a simple stateless implementation of "flag dependencies" with Optimizely Full Stack
Check out the demo here.
The source code is in the docs directory in this repository.
Imagine you're working with three Optimizely Full Stack Feature Flags:,flag_1
, flag_2
, and flag_3
:
flag_1
andflag_2
each have their own flag rules.flag_3
should beon
for everyone for whomflag_1
andflag_2
are also on.
In this scenario, flag_3
depends on flag_1
and flag_2
. When a decision for flag_3
is made, these dependencies can be evaluated statelessly
or statefully
:
- Stateless dependency evaluation works by checking whether
flag_1
andflag_2
areon
for a given user at the moment that a decision forflag_3
is required. - Stateful dependency evaluation works by checking whether
flag_1
andflag_2
were explicitly evaluated ason
for a given user at some point before a decision is required forflag_3
.
This demo implements stateless flag dependencies.
With this approach, a Full Stack user can specify flag dependencies in the Optimizely Full Stack UI by adding a special _depends
flag variable:
Dependencies are specified in the default value of the _depends
variable using a comma-separated list of flag keys.
optimizely_flag_dependencies.js
exports one function: createInstance()
.
This function behaves exactly like the Optimizely Full Stack createInstance()
method with one difference:
optimizely_flag_dependencies.createInstance()
returns an Optimizely client instance with an additional bound method, decideWithDependencies()
.
The following code example captures the relationship between optimizelyClient.decideDependencies()
and user.decide()
:
import * as optimizely_flag_dependencies from "./optimizely_flag_dependencies.js";
// Instantiate an Optimizely client object
var optimizelyClient = optimizely_flag_dependencies.createInstance({ sdkKey: "LbmzK7viE2J2bP5ozmZR9" });
// Create a user context object
var user = optimizelyClient.createUserContext("user123");
// Decide whether `flag_3` is enabled for `user123`, ignoring any dependencies
var decisionWithoutDependencies = user.decide("flag_3");
// Decide whether `flag_3` is enabled for `user123` only if all dependencies are also enabled right now
var decisionWithDependencies = optimizelyClient.decideWithDependencies(user, "flag_3")