Skip to content

Commit

Permalink
Set-based alternative to #3979
Browse files Browse the repository at this point in the history
  • Loading branch information
developit authored Apr 25, 2023
1 parent 6cc8688 commit 9606107
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions src/create-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ export function createContext(defaultValue, contextId) {
Provider(props) {
if (!this.getChildContext) {
/** @type {import('./internal').Component[]} */
let subs = [];
let subs = new Set();
let ctx = {};
ctx[contextId] = this;

this.getChildContext = () => ctx;

this.shouldComponentUpdate = function (_props) {
this.shouldComponentUpdate = function(_props) {
if (this.props.value !== _props.value) {
// I think the forced value propagation here was only needed when `options.debounceRendering` was being bypassed:
// https://github.com/preactjs/preact/commit/4d339fb803bea09e9f198abf38ca1bf8ea4b7771#diff-54682ce380935a717e41b8bfc54737f6R358
Expand All @@ -41,18 +41,18 @@ export function createContext(defaultValue, contextId) {
// c.context[contextId] = _props.value;
// enqueueRender(c);
// });
subs.some(c => {
subs.forEach(c => {
c._force = true;
enqueueRender(c);
});
}
};

this.sub = c => {
subs.push(c);
subs.add(c);
let old = c.componentWillUnmount;
c.componentWillUnmount = () => {
subs.splice(subs.indexOf(c), 1);
subs.delete(c);
if (old) old.call(c);
};
};
Expand All @@ -68,6 +68,5 @@ export function createContext(defaultValue, contextId) {
// of on the component itself. See:
// https://reactjs.org/docs/context.html#contextdisplayname

return (context.Provider._contextRef = context.Consumer.contextType =
context);
return (context.Provider._contextRef = context.Consumer.contextType = context);
}

0 comments on commit 9606107

Please sign in to comment.