Skip to content

Commit 5e661c1

Browse files
committed
Added subscribe option
1 parent 5097b18 commit 5e661c1

File tree

14 files changed

+2292
-1854
lines changed

14 files changed

+2292
-1854
lines changed

.eslintrc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
{
22
"parser": "babel-eslint",
3-
"extends": "airbnb",
3+
"extends": ["prettier"],
44
"plugins": [
5+
"prettier",
56
"babel"
67
],
78
"rules": {
9+
"prettier/prettier": ["error"],
810
"import/first": 0,
911
"eol-last": 0,
1012
"arrow-body-style": 0,

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,4 @@ jspm_packages
3838

3939
.eslintcache
4040

41-
.idea
41+
.idea

.prettierrc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"singleQuote": true,
3+
"trailingComma": "all",
4+
"printWidth": 120
5+
}

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ yarn add ld-redux
9494
```
9595

9696
## API
97-
### init({clientSideId, dispatch, flags, user, options})
97+
### init({clientSideId, dispatch, flags, user, subscribe, options})
9898
The init method accepts an object with the above properties. `clientSideId`, `dispatch` are mandatory.
9999

100100
The `flags` property is optional. This is an object containing all the flags you want to use and subscribe to in your app.
@@ -113,6 +113,10 @@ ldRedux.init({
113113
});
114114
```
115115
116+
The `subscribe` property is optional. This defaults to true which means by default you'll get automatic live updates
117+
of flag changes from the server. You can turn this off and manually subscribe to flag changes through the ldClient
118+
object if for some reason you don't want to get live updates.
119+
116120
The `user` property is optional. You can initialise the sdk with a custom user by specifying one. This must be an object containing
117121
at least a "key" property. If you don't specify a user object, ldRedux will create a default one that looks like this:
118122

example/src/client/index.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import React from 'react';
2-
import {hydrate} from 'react-dom';
3-
import {Router, browserHistory} from 'react-router';
2+
import { hydrate } from 'react-dom';
3+
import { Router, browserHistory } from 'react-router';
44
import routes from '../universal/routes';
5-
import {Provider} from 'react-redux';
5+
import { Provider } from 'react-redux';
66
import createStore from '../universal/redux/store';
77
import ldRedux from 'ld-redux';
88
import flags from '../../flags.json';
@@ -11,14 +11,14 @@ const reduxState = window.__INITIAL_STATE__ || undefined;
1111
const store = createStore(reduxState);
1212

1313
ldRedux.init({
14-
clientSideId: '57df4354dd79c70721bcb507',
14+
clientSideId: '59b2b2596d1a250b1c78baa4',
1515
dispatch: store.dispatch,
1616
flags,
1717
});
1818

1919
hydrate(
2020
<Provider store={store}>
21-
<Router routes={routes} history={browserHistory}/>
21+
<Router routes={routes} history={browserHistory} />
2222
</Provider>,
23-
document.getElementById('reactDiv')
23+
document.getElementById('reactDiv'),
2424
);

example/yarn.lock

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22
# yarn lockfile v1
33

44

5-
6-
version "1.0.1"
7-
resolved "https://registry.yarnpkg.com/Base64/-/Base64-1.0.1.tgz#def45cc50c961bcc9bf2321d0f52bcbfec1f1bb1"
8-
95
abbrev@1:
106
version "1.1.1"
117
resolved "https://registry.yarnpkg.com/abbrev/-/abbrev-1.1.1.tgz#f8f2c887ad10bf67f634f005b6987fed3179aac8"
@@ -1332,7 +1328,7 @@ escape-html@~1.0.3:
13321328
version "1.0.3"
13331329
resolved "https://registry.yarnpkg.com/escape-html/-/escape-html-1.0.3.tgz#0258eae4d3d0c0974de1c169188ef0051d1d1988"
13341330

1335-
escape-string-regexp@1.0.5, escape-string-regexp@^1.0.2:
1331+
escape-string-regexp@^1.0.2:
13361332
version "1.0.5"
13371333
resolved "https://registry.yarnpkg.com/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz#1b61c0562190a8dff6ae3bb2cf0200ca130b86d4"
13381334

@@ -1774,10 +1770,6 @@ invert-kv@^1.0.0:
17741770
version "1.0.0"
17751771
resolved "https://registry.yarnpkg.com/invert-kv/-/invert-kv-1.0.0.tgz#104a8e4aaca6d3d8cd157a8ef8bfab2d7a3ffdb6"
17761772

1777-
ip@^1.1.5:
1778-
version "1.1.5"
1779-
resolved "https://registry.yarnpkg.com/ip/-/ip-1.1.5.tgz#bdded70114290828c0a039e72ef25f5aaec4354a"
1780-
17811773
17821774
version "1.8.0"
17831775
resolved "https://registry.yarnpkg.com/ipaddr.js/-/ipaddr.js-1.8.0.tgz#eaa33d6ddd7ace8f7f6fe0c9ca0440e706738b1e"
@@ -1972,22 +1964,8 @@ lcid@^1.0.0:
19721964
dependencies:
19731965
invert-kv "^1.0.0"
19741966

1975-
ld-redux@^3.1.0:
1976-
version "3.2.0"
1977-
resolved "https://registry.yarnpkg.com/ld-redux/-/ld-redux-3.2.0.tgz#6631b2ca5ea4288924fb34254ec4902f232b645c"
1978-
dependencies:
1979-
ip "^1.1.5"
1980-
ldclient-js "^2.6.0"
1981-
lodash.camelcase "^4.3.0"
1982-
ua-parser-js "^0.7.18"
1983-
uuid "^3.3.2"
1984-
1985-
ldclient-js@^2.6.0:
1986-
version "2.6.0"
1987-
resolved "https://registry.yarnpkg.com/ldclient-js/-/ldclient-js-2.6.0.tgz#39875ae1b05fda2b4ffee4d53c103cabc2602bba"
1988-
dependencies:
1989-
Base64 "1.0.1"
1990-
escape-string-regexp "1.0.5"
1967+
"ld-redux@file:../lib":
1968+
version "0.0.0"
19911969

19921970
load-json-file@^1.0.0:
19931971
version "1.1.0"
@@ -2016,10 +1994,6 @@ lodash-es@^4.17.5:
20161994
version "4.17.11"
20171995
resolved "https://registry.yarnpkg.com/lodash-es/-/lodash-es-4.17.11.tgz#145ab4a7ac5c5e52a3531fb4f310255a152b4be0"
20181996

2019-
lodash.camelcase@^4.3.0:
2020-
version "4.3.0"
2021-
resolved "https://registry.yarnpkg.com/lodash.camelcase/-/lodash.camelcase-4.3.0.tgz#b28aa6288a2b9fc651035c7711f65ab6190331a6"
2022-
20231997
lodash@^4.14.0, lodash@^4.6.1:
20241998
version "4.17.4"
20251999
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.4.tgz#78203a4d1c328ae1d86dca6460e369b57f4055ae"
@@ -3105,10 +3079,6 @@ uuid@^3.0.0:
31053079
version "3.2.1"
31063080
resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.2.1.tgz#12c528bb9d58d0b9265d9a2f6f0fe8be17ff1f14"
31073081

3108-
uuid@^3.3.2:
3109-
version "3.3.2"
3110-
resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.3.2.tgz#1b4af4955eb3077c501c23872fc6513811587131"
3111-
31123082
validate-npm-package-license@^3.0.1:
31133083
version "3.0.1"
31143084
resolved "https://registry.yarnpkg.com/validate-npm-package-license/-/validate-npm-package-license-3.0.1.tgz#2804babe712ad3379459acfbe24746ab2c303fbc"

lib/init.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,14 @@ exports.default = function (_ref) {
9191
dispatch = _ref.dispatch,
9292
flags = _ref.flags,
9393
user = _ref.user,
94+
subscribe = _ref.subscribe,
9495
options = _ref.options;
9596

9697
initFlags(flags, dispatch);
9798

99+
// default subscribe to true
100+
var sanitisedSubscribe = typeof subscribe === 'undefined' ? true : subscribe;
101+
98102
if (!user) {
99103
user = initUser();
100104
}
@@ -103,6 +107,9 @@ exports.default = function (_ref) {
103107
window.ldClient.on('ready', function () {
104108
var flagsSanitised = flags || ldClient.allFlags();
105109
setFlags(flagsSanitised, dispatch);
106-
subscribeToChanges(flagsSanitised, dispatch);
110+
111+
if (sanitisedSubscribe) {
112+
subscribeToChanges(flagsSanitised, dispatch);
113+
}
107114
});
108115
};

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,15 @@
3838
"babel-preset-stage-0": "^6.24.1",
3939
"eslint": "^5.5.0",
4040
"eslint-config-airbnb": "^17.1.0",
41+
"eslint-config-prettier": "4.3.0",
4142
"eslint-friendly-formatter": "^4.0.1",
4243
"eslint-plugin-babel": "^5.2.0",
4344
"eslint-plugin-import": "^2.14.0",
4445
"eslint-plugin-jsx-a11y": "^6.1.1",
46+
"eslint-plugin-prettier": "3.1.0",
4547
"eslint-plugin-react": "^7.11.1",
46-
"jest": "^23.5.0",
48+
"jest": "23.6.0",
49+
"prettier": "1.17.1",
4750
"rimraf": "^2.6.2",
4851
"testdouble": "^3.8.1"
4952
},

src/actions.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
export const setFlags = (flags) => {
1+
export const setFlags = flags => {
22
return {
33
type: 'SET_FLAGS',
44
data: flags,
55
};
6-
};
6+
};

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ import reducer from './reducer';
44
export default {
55
init,
66
reducer: () => reducer,
7-
};
7+
};

src/init.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
import {initialize as ldClientInitialize} from 'ldclient-js';
1+
import { initialize as ldClientInitialize } from 'ldclient-js';
22
import camelCase from 'lodash.camelcase';
33
import uuid from 'uuid';
44
import ip from 'ip';
55
import UAParser from 'ua-parser-js';
6-
import {setFlags as setFlagsAction} from './actions';
6+
import { setFlags as setFlagsAction } from './actions';
77

88
const userAgentParser = new UAParser();
99
const isMobileDevice = typeof window !== 'undefined' && userAgentParser.getDevice().type === 'mobile';
1010
const isTabletDevice = typeof window !== 'undefined' && userAgentParser.getDevice().type === 'tablet';
1111

1212
// initialise flags with default values in ld redux store
1313
const initFlags = (flags, dispatch) => {
14-
const flagValues = {isLDReady: false};
14+
const flagValues = { isLDReady: false };
1515
for (const flag in flags) {
1616
const camelCasedKey = camelCase(flag);
1717
flagValues[camelCasedKey] = flags[flag];
@@ -21,7 +21,7 @@ const initFlags = (flags, dispatch) => {
2121

2222
// set flags with real values from ld server
2323
const setFlags = (flags, dispatch) => {
24-
const flagValues = {isLDReady: true};
24+
const flagValues = { isLDReady: true };
2525
for (const flag in flags) {
2626
const camelCasedKey = camelCase(flag);
2727
flagValues[camelCasedKey] = ldClient.variation(flag, flags[flag]);
@@ -32,7 +32,7 @@ const setFlags = (flags, dispatch) => {
3232
const subscribeToChanges = (flags, dispatch) => {
3333
for (const flag in flags) {
3434
const camelCasedKey = camelCase(flag);
35-
ldClient.on(`change:${flag}`, (current) => {
35+
ldClient.on(`change:${flag}`, current => {
3636
const newFlagValue = {};
3737
newFlagValue[camelCasedKey] = current;
3838
dispatch(setFlagsAction(newFlagValue));
@@ -61,9 +61,12 @@ const initUser = () => {
6161
};
6262
};
6363

64-
export default ({clientSideId, dispatch, flags, user, options}) => {
64+
export default ({ clientSideId, dispatch, flags, user, subscribe, options }) => {
6565
initFlags(flags, dispatch);
6666

67+
// default subscribe to true
68+
const sanitisedSubscribe = typeof subscribe === 'undefined' ? true : subscribe;
69+
6770
if (!user) {
6871
user = initUser();
6972
}
@@ -72,6 +75,9 @@ export default ({clientSideId, dispatch, flags, user, options}) => {
7275
window.ldClient.on('ready', () => {
7376
const flagsSanitised = flags || ldClient.allFlags();
7477
setFlags(flagsSanitised, dispatch);
75-
subscribeToChanges(flagsSanitised, dispatch);
78+
79+
if (sanitisedSubscribe) {
80+
subscribeToChanges(flagsSanitised, dispatch);
81+
}
7682
});
7783
};

0 commit comments

Comments
 (0)