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

[WIP] New context API #963

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
64 changes: 64 additions & 0 deletions src/create-context.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { Component } from "./component";
import { extendComponent } from "./util";

/**
*
* @param {string} key
*/
export function createContext(key) {
/** Provider Component class.
* prop `value` is provided to every corresponding `Consumer` component.
* @public
*
*/
function Provider(props, context) {
Component.call(this, props, context);

this.c = [];
}

extendComponent(Provider.prototype, {
s(subscriber) {
this.c.push(subscriber);
subscriber(this.props.value);
},

getChildContext() {
return { [key]: this.s };
},

componentWillReceiveProps(nextProps) {
if (!Object.is(this.props.value, nextProps.value)) {
this.c.forEach(subscriber => {
subscriber(nextProps.value);
});
}
},

render() {
return this.props.children;
}
});

/** Consumer Component class.
* `children` must be a function that accepts the value from the `Consumer`'s
corresponding `Provider` component.
* @public
*
*/
function Consumer(props, context) {
Component.call(this, props, context);

context[key](value => {
this.setState({ value });
});
}

extendComponent(Consumer.prototype, {
render() {
return this.props.children(this.state.value);
}
});

return { Provider, Consumer };
}
3 changes: 3 additions & 0 deletions src/preact.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { h, h as createElement } from './h';
import { cloneElement } from './clone-element';
import { Component } from './component';
import { createContext } from './create-context';
import { render } from './render';
import { rerender } from './render-queue';
import options from './options';
Expand All @@ -10,6 +11,7 @@ export default {
createElement,
cloneElement,
Component,
createContext,
render,
rerender,
options
Expand All @@ -20,6 +22,7 @@ export {
createElement,
cloneElement,
Component,
createContext,
render,
rerender,
options
Expand Down
15 changes: 15 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {Component} from './component';

/**
* Copy all properties from `props` onto `obj`.
* @param {Object} obj Object onto which properties should be copied.
Expand All @@ -18,3 +20,16 @@ export function extend(obj, props) {
* @param {Function} callback
*/
export const defer = typeof Promise=='function' ? Promise.resolve().then.bind(Promise.resolve()) : setTimeout;

/**
* Extend an object from `Component`.
* @param {Object} obj Object onto `Component` should be applied to.
* @param {Object} props Object from which to copy properties.
* @private
*/
export function extendComponent(obj, props) {
obj = Object.create(Component.prototype);
obj.constructor = Component;
extend(obj, props);
}