diff --git a/package.json b/package.json index dc34fb3..9e87ad6 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.18", + "version": "1.1.0-alpha.19", "dependencies": { "@webkrafters/eagleeye": "^1.0.8", "js-sha512": "^0.9.0", diff --git a/src/main/universe/index.tsx b/src/main/universe/index.tsx index fc276c7..aca0229 100644 --- a/src/main/universe/index.tsx +++ b/src/main/universe/index.tsx @@ -94,6 +94,7 @@ interface Entry { interface Handle { readonly resource : W; + release( force : boolean ) : void; } class ObserverHandle>> implements Handle { @@ -148,17 +149,15 @@ class StreamerHandle implements Handle { // istanbul ignore next get size() { return this.isValid ? this._entry.numConnections : -1 } // istanbul ignore next - dec() { - if( !this.isValid ) { return } - this._entry.numConnections--; - if( this.size ) { return } + dec() { this.size > 0 && this._entry.numConnections-- } + inc() { this.isValid && this._entry.numConnections++ } + release( force = false ) { + if( !force && this.size > 0 ) { return } + this.resource.endStream(); this._chs.finalizer.unregister( this.resource ); - const { channel } = this._entry; this._entry.channel = null; this._chs.finalize( this._sMapHash ); - setTimeout( () => channel.deref()?.endStream(), 0 ); } - inc() { this.isValid && this._entry.numConnections++ } } interface IResource{ @@ -397,10 +396,10 @@ export class ObservableUniverse { ); streamHandle = ctxHandle.getChannelHandleAt( sMapHash ); } - streamHandle.inc(); return streamHandle; }, [ sMapHash, targetId ]); const mounted = useRef( false ); + let [ currentUri ] = useState( getCurrentUri ); const [ store, setStore ] = useState(() => makeStore( handle.resource )); useEffect(() => { const channel = handle.resource; @@ -412,9 +411,13 @@ export class ObservableUniverse { } 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; @@ -422,6 +425,8 @@ export class ObservableUniverse { } } +function getCurrentUri() { return globalThis.location?.pathname } + export class Utility { private _wContext : WeakRef>; constructor( context : ObservableUniverse ) {