Skip to content
Merged
28 changes: 18 additions & 10 deletions packages/react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,22 @@ Object.defineProperties(Signal.prototype, {
});

// Track the current owner (roughly equiv to current vnode)
// let currentOwner: ReactOwner;
// Object.defineProperty(internals.ReactCurrentOwner, "current", {
// get() { return currentOwner; },
// set(owner) { currentOwner = owner; },
// });
let lastOwner: ReactOwner;
let currentOwner: ReactOwner;
Object.defineProperty(internals.ReactCurrentOwner, "current", {
get() {
return currentOwner;
},
set(owner) {
currentOwner = owner;
if (currentOwner) lastOwner = currentOwner;
},
});
Comment thread
CodyJasonBennett marked this conversation as resolved.
Outdated

// Track the current dispatcher (roughly equiv to current component impl)
let lock = false;
const UPDATE = () => ({});
let lastDispatcher: ReactDispatcher;
let currentDispatcher: ReactDispatcher;
Object.defineProperty(internals.ReactCurrentDispatcher, "current", {
get() {
Expand All @@ -108,17 +115,18 @@ Object.defineProperty(internals.ReactCurrentDispatcher, "current", {
set(api) {
currentDispatcher = api;
if (lock) return;
if (api && !isInvalidHookAccessor(api)) {
if (lastOwner && api && !isInvalidHookAccessor(api)) {
// prevent re-injecting useReducer when the Dispatcher
// context changes to run the reducer callback:
lock = true;
const rerender = api.useReducer(UPDATE, {})[1];
lock = false;
const currentOwner = internals.ReactCurrentOwner.current;
let updater = updaterForComponent.get(currentOwner);
if (!updater) {

let updater = updaterForComponent.get(lastOwner);
if (!updater || !lastDispatcher !== api) {
updater = createUpdater(rerender);
updaterForComponent.set(currentOwner, updater);
updaterForComponent.set(lastOwner, updater);
lastDispatcher = api;
}
setCurrentUpdater(updater);
} else {
Expand Down