Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
Preserve previous context
Browse files Browse the repository at this point in the history
  • Loading branch information
luisherranz committed Jun 29, 2023
1 parent 2dfa29c commit 4d579a0
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 28 deletions.
28 changes: 16 additions & 12 deletions assets/js/interactivity/directives.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useContext, useMemo, useEffect } from 'preact/hooks';
import { useContext, useMemo, useEffect, useRef } from 'preact/hooks';
import { deepSignal, peek } from 'deepsignal';
import { useSignalEffect } from './utils';
import { directive } from './hooks';
Expand Down Expand Up @@ -32,20 +32,24 @@ export default () => {
'context',
( {
directives: {
context: { default: context },
context: { default: newContext },
},
props: { children },
context: inherited,
context: inheritedContext,
} ) => {
const { Provider } = inherited;
const inheritedValue = useContext( inherited );
const value = useMemo( () => {
const localValue = deepSignal( context );
mergeDeepSignals( localValue, inheritedValue );
return localValue;
}, [ context, inheritedValue ] );

return <Provider value={ value }>{ children }</Provider>;
const { Provider } = inheritedContext;
const inheritedValue = useContext( inheritedContext );
const currentValue = useRef( deepSignal( {} ) );
currentValue.current = useMemo( () => {
const newValue = deepSignal( newContext );
mergeDeepSignals( newValue, currentValue.current );
mergeDeepSignals( newValue, inheritedValue );
return newValue;
}, [ newContext, inheritedValue ] );

return (
<Provider value={ currentValue.current }>{ children }</Provider>
);
},
{ priority: 5 }
);
Expand Down
71 changes: 56 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@
"compare-versions": "4.1.3",
"config": "3.3.7",
"dataloader": "2.1.0",
"deepsignal": "^1.1.0",
"deepsignal": "^1.3.3",
"dinero.js": "1.9.1",
"dompurify": "^2.4.0",
"downshift": "6.1.7",
Expand Down

0 comments on commit 4d579a0

Please sign in to comment.