Skip to content
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 23 additions & 24 deletions src/implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function createEventEmitter(value) {
},

off(handler) {
handlers = handlers.filter(h => h !== handler);
handlers = handlers.filter(h => h !== handler);
},

get() {
Expand All @@ -70,21 +70,21 @@ function createReactContext<T>(
defaultValue: T,
calculateChangedBits: ?(a: T, b: T) => number
): Context<T> {
const contextProp = '__create-react-context-' + gud() + '__';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where did contextProp go to?


let emitter; // Provider and Consumer share an internal inaccessible emitter
class Provider extends Component<ProviderProps<T>> {
emitter = createEventEmitter(this.props.value);

static childContextTypes = {
[contextProp]: PropTypes.object.isRequired
[contextProp]: PropTypes.string.isRequired
};

constructor(props, context){
super(props, context);
emitter = createEventEmitter(props.value);
}

getChildContext() {
return {
[contextProp]: this.emitter
};
return {
[contextProp]: contextProp
}
}

componentWillReceiveProps(nextProps) {
if (this.props.value !== nextProps.value) {
let oldValue = this.props.value;
Expand All @@ -110,7 +110,7 @@ function createReactContext<T>(
changedBits |= 0;

if (changedBits !== 0) {
this.emitter.set(nextProps.value, changedBits);
emitter.set(nextProps.value, changedBits);//Notify all consumers to update
}
}
}
Expand All @@ -123,15 +123,14 @@ function createReactContext<T>(

class Consumer extends Component<ConsumerProps<T>, ConsumerState<T>> {
static contextTypes = {
[contextProp]: PropTypes.object
[contextProp]: PropTypes.string
};

observedBits: number;

state: ConsumerState<T> = {
value: this.getValue()
};

componentWillReceiveProps(nextProps) {
let { observedBits } = nextProps;
this.observedBits =
Expand All @@ -142,7 +141,7 @@ function createReactContext<T>(

componentDidMount() {
if (this.context[contextProp]) {
this.context[contextProp].on(this.onUpdate);
emitter.on(this.onUpdate);
}
let { observedBits } = this.props;
this.observedBits =
Expand All @@ -152,17 +151,17 @@ function createReactContext<T>(
}

componentWillUnmount() {
if (this.context[contextProp]) {
this.context[contextProp].off(this.onUpdate);
}
if (this.context[contextProp]) {
emitter.off(this.onUpdate);
}
}

getValue(): T {
if (this.context[contextProp]) {
return this.context[contextProp].get();
} else {
return defaultValue;
}
if (this.context[contextProp]) {
return emitter.get();
} else {
return defaultValue;
}
}

onUpdate = (newValue, changedBits: number) => {
Expand Down