diff --git a/package.json b/package.json index 9e87ad6..96d6c1c 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/main/index.tsx b/src/main/index.tsx index 91e24c3..e08baf4 100644 --- a/src/main/index.tsx +++ b/src/main/index.tsx @@ -107,23 +107,16 @@ export class ObservableContext extends AbstractObservable { return ( 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; }; } diff --git a/src/main/universe/index.test.tsx b/src/main/universe/index.test.tsx index 426fd6d..4b47bdd 100644 --- a/src/main/universe/index.test.tsx +++ b/src/main/universe/index.test.tsx @@ -1,6 +1,7 @@ import { act, render, + renderHook, RenderResult } from '@testing-library/react'; import { @@ -21,6 +22,7 @@ import Immutable from '@webkrafters/auto-immutable'; import { Dispatch, FC, + ReactNode, RefObject, SetStateAction, useMemo, @@ -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(); + const { useStream } = Context; + const wrapper = ( props : { children : ReactNode }) => ( + + { props.children } + + ); + 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', () => { diff --git a/src/main/universe/index.tsx b/src/main/universe/index.tsx index aca0229..462596b 100644 --- a/src/main/universe/index.tsx +++ b/src/main/universe/index.tsx @@ -384,49 +384,39 @@ export class ObservableUniverse { private defineStreamHook() { return ( 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>(() => 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; + currentUri === getCurrentUri() && + handle.release(); + }; + }, []); + return store; }; } } -function getCurrentUri() { return globalThis.location?.pathname } - export class Utility { private _wContext : WeakRef>; constructor( context : ObservableUniverse ) { @@ -467,3 +457,21 @@ export class Utility { } export function createContext() { return new ObservableUniverse() } + +function getCurrentUri() { return globalThis.location?.pathname } + +function getStreamHandle( + contextHandle : ObserverHandle>, + 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>; +}