Skip to content

Commit

Permalink
feat: Flag hoc to connect to store
Browse files Browse the repository at this point in the history
  • Loading branch information
ptbrowne committed Mar 28, 2019
1 parent 1f46ad1 commit 98130a9
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 3 deletions.
3 changes: 2 additions & 1 deletion packages/cozy-flags/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
]
},
"dependencies": {
"detect-node": "2.0.4"
"detect-node": "2.0.4",
"microee": "^0.0.6"
}
}
46 changes: 45 additions & 1 deletion packages/cozy-flags/src/browser/flag.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
/* global localStorage */

import MicroEE from 'microee'
import React from 'react'

export const prefix = 'flag__'
export const getKey = name => prefix + name

Expand All @@ -26,6 +29,7 @@ class FlagStore {
for (let flag of flags) {
const val = localStorage.getItem(getKey(flag))
this.store[flag] = val ? JSON.parse(val) : val
this.emit('change')
}
}

Expand All @@ -45,10 +49,23 @@ class FlagStore {
localStorage.setItem(getKey(name), JSON.stringify(value))
}
this.store[name] = value
this.emit('change')
}

remove(name) {
delete this.store[name]
localStorage.removeItem(getKey(name))
this.emit('change')
}
}

MicroEE.mixin(FlagStore)

const store = new FlagStore()

/**
* Public API to use flags
*/
const flag = function() {
if (!window.localStorage) {
return
Expand All @@ -67,11 +84,38 @@ export const listFlags = () => {
}

export const resetFlags = () => {
listFlags().forEach(name => (store[name] = null))
listFlags().forEach(name => store.remove(name))
}

flag.store = store
flag.list = listFlags
flag.reset = resetFlags

export default flag

/**
* Connects a component to the flags. The wrapped component
* will be refreshed when a flag changes.
*/
flag.connect = Component => {
class Wrapped extends React.Component {
constructor(props) {
super(props)
this.handleChange = this.handleChange.bind(this)
}
componentWillMount() {
store.on('change', this.handleChange)
}
componentWillUnmount() {
store.removeListener('change', this.handleChange)
}
handleChange() {
this.forceUpdate()
}
render() {
return <Component {...this.props} />
}
}
Wrapped.displayName = 'flag_' + Component.displayName
return Wrapped
}
12 changes: 12 additions & 0 deletions packages/cozy-flags/src/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ export default function testFlagAPI(flag) {
flag('test', true)
expect(flag('test')).toBe(true)
})

if (flag.store) {
describe('observable', () => {
beforeEach(() => {
jest.spyOn(flag.store, 'emit')
})
it('should emit events', () => {
flag('test', true)
expect(flag.store.emit).toHaveBeenCalled()
})
})
}
})

describe('listFlags', () => {
Expand Down
6 changes: 5 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8473,6 +8473,11 @@ methods@~1.1.2:
resolved "https://registry.yarnpkg.com/methods/-/methods-1.1.2.tgz#5529a4d67654134edcc5266656835b0f851afcee"
integrity sha1-VSmk1nZUE07cxSZmVoNbD4Ua/O4=

microee@^0.0.6:
version "0.0.6"
resolved "https://registry.yarnpkg.com/microee/-/microee-0.0.6.tgz#a12bdb0103681e8b126a9b071eba4c467c78fffe"
integrity sha1-oSvbAQNoHosSapsHHrpMRnx4//4=

micromatch@^2.3.11:
version "2.3.11"
resolved "https://registry.yarnpkg.com/micromatch/-/micromatch-2.3.11.tgz#86677c97d1720b363431d04d0d15293bd38c1565"
Expand Down Expand Up @@ -10527,7 +10532,6 @@ [email protected]:

"pouchdb-adapter-cordova-sqlite@https://github.com/SnceGroup/pouchdb-adapter-cordova-sqlite.git#f3ee23009b70209c611435d57491aa77fb88802a":
version "2.0.2"
uid f3ee23009b70209c611435d57491aa77fb88802a
resolved "https://github.com/SnceGroup/pouchdb-adapter-cordova-sqlite.git#f3ee23009b70209c611435d57491aa77fb88802a"
dependencies:
pouchdb-adapter-websql-core "^6.1.1"
Expand Down

0 comments on commit 98130a9

Please sign in to comment.