Skip to content
Merged
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@
"test:watch": "eslint --fix && jest --updateSnapshot --watchAll"
},
"types": "dist/index.d.ts",
"version": "1.1.0-alpha.19",
"version": "1.1.0-alpha.20",
"dependencies": {
"@webkrafters/eagleeye": "^1.0.8",
"js-sha512": "^0.9.0",
Expand Down
9 changes: 1 addition & 8 deletions src/main/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,23 +107,16 @@ export class ObservableContext<T extends State> extends AbstractObservable<T> {
return <const S extends SelectorMap>( selectorMap? : S ) => {
const [ channel ] = useState(() => stream( selectorMap ));
const [ store, setStore ] = useState(() => makeStore( channel ));
const mounted = useRef( false );
useEffect(() => {
channel.selectorMap = selectorMap;
}, [ selectorMap ]);
useEffect(() => {
// istanbul ignore next
if( mounted.current ) {
setStore( makeStore( channel ) );
} else {
mounted.current = true;
}
channel.addListener(
'data-changed',
() => setStore({ ...store, data: channel.data })
);
return () => channel.endStream();
}, [ channel ]);
}, []);
return store;
};
}
Expand Down
20 changes: 20 additions & 0 deletions src/main/universe/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
act,
render,
renderHook,
RenderResult
} from '@testing-library/react';
import {
Expand All @@ -21,6 +22,7 @@ import Immutable from '@webkrafters/auto-immutable';
import {
Dispatch,
FC,
ReactNode,
RefObject,
SetStateAction,
useMemo,
Expand Down Expand Up @@ -214,6 +216,24 @@ describe( 'ReactObservableUniverse', () => {
expect( clientData.eyes ).toBe( 'blue' );
expect( clientData.fruit ).toBe( 'banana' );
} );
test( 'selectorMap update not supported in SSR Mode', () => {
const Context = createContext<SourceData>();
const { useStream } = Context;
const wrapper = ( props : { children : ReactNode }) => (
<Context.Provider observableConfig={{ value: createSourceData() }}>
{ props.children }
</Context.Provider>
);
const { rerender } = renderHook(
props => { useStream( props.sMap ) }, {
initialProps: { sMap: { c: 'company' } },
wrapper
}
);
expect(() => rerender({ sMap: { c: 'company' }})).toThrow(
'Selector map change not supported in SSR Mode.'
);
} );
} );
describe( 'Provider', () => {
test( 'creates and provides annonymous context instance when none available', () => {
Expand Down
66 changes: 37 additions & 29 deletions src/main/universe/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -384,49 +384,39 @@ export class ObservableUniverse<T extends State> {

private defineStreamHook() {
return <const S extends SelectorMap>( selectorMap? : S ) => {
const [ sMapHash, updateSMapHash ] = useState(() => this._util.hashSelectorMap( selectorMap ));
useEffect(() => updateSMapHash( this._util.hashSelectorMap( selectorMap ) ), [ selectorMap ]);
const targetId = use( this._context );
const handle = useMemo(() => {
const ctxHandle = this._obs.createHandleFor( targetId );
let streamHandle = ctxHandle.getChannelHandleAt( sMapHash );
if( !streamHandle.isValid ) {
ctxHandle.addChannelAt(
sMapHash, ctxHandle.resource.stream( selectorMap )
);
streamHandle = ctxHandle.getChannelHandleAt( sMapHash );
}
return streamHandle;
}, [ sMapHash, targetId ]);
const mounted = useRef( false );
const targetId = use( this._context );
const [ handle ] = useState(() => getStreamHandle(
this._obs.createHandleFor( targetId ),
selectorMap,
this._util.hashSelectorMap( selectorMap )
));
let [ currentUri ] = useState( getCurrentUri );
const [ store, setStore ] = useState(() => makeStore( handle.resource ));
const [ store, setStore ] = useState<Store<T, S>>(() => makeStore( handle.resource ));
useEffect(() => {
if( !mounted.current ) { return }
throw new Error( 'Selector map change not supported in SSR Mode.' );
}, [ selectorMap ]);
useEffect(() => {
const channel = handle.resource;
// istanbul ignore next
if( mounted.current ) {
setStore( makeStore( channel ) );
} else {
mounted.current = true;
}
if( mounted.current ) { return }
mounted.current = true;
const channel = handle.resource;
const fn = () => setStore({ ...store, data: channel.data });
channel.addListener( 'data-changed', fn );
handle.inc();
return () => {
channel.removeListener( 'data-changed', fn );
handle.dec();
const uri = getCurrentUri();
currentUri === uri && handle.release();
currentUri = uri;
}
}, [ handle.resource ]);
return store as Store<T, S>;
currentUri === getCurrentUri() &&
handle.release();
};
}, []);
return store;
};
}
}

function getCurrentUri() { return globalThis.location?.pathname }

export class Utility<T extends State> {
private _wContext : WeakRef<ObservableUniverse<T>>;
constructor( context : ObservableUniverse<T> ) {
Expand Down Expand Up @@ -467,3 +457,21 @@ export class Utility<T extends State> {
}

export function createContext<T extends State>() { return new ObservableUniverse<T>() }

function getCurrentUri() { return globalThis.location?.pathname }

function getStreamHandle<T extends State, S extends SelectorMap>(
contextHandle : ObserverHandle<AbstractObservable<T>>,
selectorMap : S,
sMapHash : string
) {
let streamHandle = contextHandle.getChannelHandleAt( sMapHash );
if( !streamHandle.isValid ) {
contextHandle.addChannelAt(
sMapHash,
contextHandle.resource.stream( selectorMap )
);
streamHandle = contextHandle.getChannelHandleAt( sMapHash );
}
return streamHandle as StreamerHandle<Channel<T, S>>;
}
Loading