@@ -1823,6 +1823,8 @@ it.layer(
18231823 models : [ ] ,
18241824 } satisfies ServerProvider ;
18251825 const changes = yield * PubSub . unbounded < ServerProvider > ( ) ;
1826+ let activeSubscriptions = 0 ;
1827+ const instanceChanges = yield * PubSub . unbounded < void > ( ) ;
18261828 const instance = {
18271829 instanceId : cursorInstanceId ,
18281830 driverKind : cursorDriver ,
@@ -1842,23 +1844,33 @@ it.layer(
18421844 ) ,
18431845 getSnapshot : Effect . succeed ( initialProvider ) ,
18441846 refresh : Effect . succeed ( refreshedProvider ) ,
1845- streamChanges : Stream . fromPubSub ( changes ) ,
1847+ streamChanges : Stream . unwrap (
1848+ Effect . sync ( ( ) => {
1849+ activeSubscriptions ++ ;
1850+ return Stream . fromPubSub ( changes ) ;
1851+ } ) ,
1852+ ) . pipe (
1853+ Stream . ensuring (
1854+ Effect . sync ( ( ) => {
1855+ activeSubscriptions -- ;
1856+ } ) ,
1857+ ) ,
1858+ ) ,
18461859 applyUsageLimits : ( ) => Effect . void ,
18471860 } ,
18481861 orchestrationAdapter : { } as ProviderInstance [ "orchestrationAdapter" ] ,
18491862 textGeneration : { } as ProviderInstance [ "textGeneration" ] ,
18501863 } satisfies ProviderInstance ;
1864+ let instances : ReadonlyArray < ProviderInstance > = [ instance ] ;
18511865 const layerInstanceRegistry = Layer . succeed (
18521866 ProviderInstanceRegistry . ProviderInstanceRegistry ,
18531867 {
18541868 getInstance : ( instanceId ) =>
1855- Effect . succeed ( instanceId === cursorInstanceId ? instance : undefined ) ,
1856- listInstances : Effect . succeed ( [ instance ] ) ,
1869+ Effect . sync ( ( ) => instances . find ( ( instance ) => instance . instanceId === instanceId ) ) ,
1870+ listInstances : Effect . sync ( ( ) => instances ) ,
18571871 listUnavailable : Effect . succeed ( [ ] ) ,
18581872 streamChanges : Stream . empty ,
1859- subscribeChanges : Effect . flatMap ( PubSub . unbounded < void > ( ) , ( pubsub ) =>
1860- PubSub . subscribe ( pubsub ) ,
1861- ) ,
1873+ subscribeChanges : PubSub . subscribe ( instanceChanges ) ,
18621874 } ,
18631875 ) ;
18641876 const scope = yield * Scope . make ( ) ;
@@ -1900,6 +1912,32 @@ it.layer(
19001912 models : [ ...initialProvider . models ] ,
19011913 } ) ,
19021914 ) ;
1915+ for ( const iconUrl of [ "https://example.com/one.png" , "https://example.com/two.png" ] ) {
1916+ const next = { ...initialProvider , iconUrl } ;
1917+ instances = [
1918+ {
1919+ ...instance ,
1920+ snapshot : { ...instance . snapshot , getSnapshot : Effect . succeed ( next ) } ,
1921+ } ,
1922+ ] ;
1923+ const updated = yield * registry . streamChanges . pipe (
1924+ Stream . filter ( ( providers ) => providers [ 0 ] ?. iconUrl === iconUrl ) ,
1925+ Stream . runHead ,
1926+ Effect . forkChild ( { startImmediately : true } ) ,
1927+ ) ;
1928+ yield * PubSub . publish ( instanceChanges , undefined ) ;
1929+ yield * Fiber . join ( updated ) ;
1930+ assert . strictEqual ( activeSubscriptions , 1 ) ;
1931+ }
1932+ instances = [ ] ;
1933+ const removed = yield * registry . streamChanges . pipe (
1934+ Stream . filter ( ( providers ) => providers . length === 0 ) ,
1935+ Stream . runHead ,
1936+ Effect . forkChild ( { startImmediately : true } ) ,
1937+ ) ;
1938+ yield * PubSub . publish ( instanceChanges , undefined ) ;
1939+ yield * Fiber . join ( removed ) ;
1940+ assert . strictEqual ( activeSubscriptions , 0 ) ;
19031941 } ) . pipe ( Effect . provide ( runtimeServices ) ) ;
19041942 } ) ,
19051943 ) ;
0 commit comments