react-pubst
Advanced tools
Comparing version 0.0.1 to 0.0.2
{ | ||
"name": "react-pubst", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Utilities for creating Pubst-aware React components.", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -5,2 +5,9 @@ import {Component, createElement} from 'react'; | ||
const DEFAULT_CONFIG = { | ||
topic: '', | ||
default: undefined, | ||
doPrime: true, | ||
allowRepeats: false | ||
}; | ||
function pubstWrappedComponentBuilder(componentToWrap, subMap) { | ||
@@ -13,4 +20,35 @@ | ||
this.subConfigs = {}; | ||
Object.keys(subMap).forEach(propName => { | ||
this.state[propName] = pubst.currentVal(subMap[propName]); | ||
const passedConfig = subMap[propName]; | ||
const subConfig = {}; | ||
if (typeof passedConfig === 'object') { | ||
Object.keys(passedConfig).forEach(configItem => { | ||
if (DEFAULT_CONFIG.hasOwnProperty(configItem)) { | ||
subConfig[configItem] = passedConfig[configItem]; | ||
} | ||
}); | ||
} else if (typeof passedConfig === 'string' || passedConfig instanceof RegExp) { | ||
subConfig.topic = passedConfig; | ||
} else { | ||
throw new Error(`Pubst subscriber components must be configured with a string, regular expression, or object. Received '${passedConfig}' for prop '${propName}'`); | ||
} | ||
Object.keys(DEFAULT_CONFIG).forEach(configItem => { | ||
if (!subConfig.hasOwnProperty(configItem)) { | ||
subConfig[configItem] = DEFAULT_CONFIG[configItem]; | ||
} | ||
}); | ||
subConfig.handler = (value) => { | ||
const stateUpdate = {}; | ||
stateUpdate[propName] = value; | ||
this.setState(stateUpdate); | ||
} | ||
this.subConfigs[propName] = subConfig; | ||
this.state[propName] = pubst.currentVal(subConfig.topic, subConfig.default); | ||
}); | ||
@@ -24,13 +62,8 @@ | ||
componentDidMount() { | ||
Object.keys(this.subMap).forEach(propName => { | ||
Object.keys(this.subConfigs).forEach(propName => { | ||
const subConfig = this.subConfigs[propName]; | ||
this.unsubs.push( | ||
pubst.subscribe( | ||
this.subMap[propName], | ||
(value) => { | ||
if (value !== this.state[propName]) { | ||
const stateUpdate = {}; | ||
stateUpdate[propName] = value; | ||
this.setState(stateUpdate); | ||
} | ||
} | ||
subConfig.topic, | ||
subConfig | ||
) | ||
@@ -37,0 +70,0 @@ ); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
14765
4
72
1
6