Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Protect the emitter #19

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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