Skip to content

Commit

Permalink
adhoc: support deferred clientSideId and possibly secure mode? (#27)
Browse files Browse the repository at this point in the history
* adhoc: support deferred clientSideId

also an attempt at secure mode.

Thus, one wouldn’t have to put the clientSideId into the config/environment.js

The UI could hit an API endpoint, or possibly even the response body from a login, and then do something like this:

    return get(this, 'launchDarkly').initialize(userStuffHere, {
       clientSideId: FROM_THE_SERVER,
       hash: ALSO_FROM_THE_SERVER
    );

* remove dead code path involving LDClient check

* cleanup

* add documentation around options configuration

Co-authored-by: Jared Galanis <[email protected]>
Co-authored-by: Jared Galanis <[email protected]>
  • Loading branch information
3 people authored Sep 6, 2020
1 parent 08b1b81 commit f294a31
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ The `initialize()` function returns a promise that resolves when the Launch Dark

The user `key` is the only required attribute, see the [Launch Darkly documentation](https://docs.launchdarkly.com/docs/js-sdk-reference#section-users) for the other attributes you can provide.

The `initialize()` function accepts a second parameter, an object, which is **optional**. It allows to you configure the Launch Darkly client, see the [Launch Darkly documentation](https://docs.launchdarkly.com/sdk/client-side/javascript#customizing-your-client) to learn more about available options and default values.

```js
// /app/application/route.js

Expand All @@ -123,7 +125,12 @@ export default Route.extend({
anonymous: true
};

return this.launchDarkly.initialize(user);
let options = {
sendEvents: false, // default is true, see https://docs.launchdarkly.com/sdk/client-side/javascript#analytics-events
hash: "SERVER_GENERATED_HASH", // see https://docs.launchdarkly.com/sdk/client-side/javascript#secure-mode
};

return this.launchDarkly.initialize(user, options);
}
});
```
Expand Down
16 changes: 10 additions & 6 deletions addon/services/launch-darkly-client-remote.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@ export default Service.extend(Evented, {
this._super(...arguments);
},

initialize(user = {}) {
initialize(user = {}, options = {}) {
let { clientSideId, streaming = false } = this._config();

assert('ENV.launchDarkly.clientSideId must be specified in config/environment.js', clientSideId);
if (!clientSideId) {
clientSideId = options.clientSideId; // let this be deferred, maybe an API provided it
delete options.clientSideId;
}

if (!clientSideId) {
warn('ENV.launchDarkly.clientSideId not specified. Defaulting all feature flags to "false"', false, { id: 'ember-launch-darkly.client-id-not-specified' });
warn('ENV.launchDarkly.clientSideId not specified or provided via user hash. Defaulting all feature flags to "false"', false, { id: 'ember-launch-darkly.client-id-not-specified' });

this.set('_client', NullClient);

Expand All @@ -39,7 +42,7 @@ export default Service.extend(Evented, {
return RSVP.resolve();
}

return this._initialize(clientSideId, user, streaming);
return this._initialize(clientSideId, user, streaming, options);
},

identify(user) {
Expand All @@ -60,9 +63,10 @@ export default Service.extend(Evented, {
return appConfig.launchDarkly || {};
},

_initialize(id, user, streamingOptions) {
_initialize(id, user, streamingOptions, options = {}) {
return new RSVP.Promise((resolve, reject) => {
let client = initialize(id, user);

let client = initialize(id, user, options);

client.on('ready', () => {
this.set('_client', client);
Expand Down

0 comments on commit f294a31

Please sign in to comment.