{"action":"created","issue":{"url":"https://api.github.com/repos/Reactive-Extensions/RxJS/issues/763","labels_url":"https://api.github.com/repos/Reactive-Extensions/RxJS/issues/763/labels{/name}","comments_url":"https://api.github.com/repos/Reactive-Extensions/RxJS/issues/763/comments","events_url":"https://api.github.com/repos/Reactive-Extensions/RxJS/issues/763/events","html_url":"https://github.com/Reactive-Extensions/RxJS/issues/763","id":87553438,"number":763,"title":"Why shareReplay() subscribes to source observable twice?","user":{"login":"xwk","id":1052240,"avatar_url":"https://avatars.githubusercontent.com/u/1052240?v=3","gravatar_id":"","url":"https://api.github.com/users/xwk","html_url":"https://github.com/xwk","followers_url":"https://api.github.com/users/xwk/followers","following_url":"https://api.github.com/users/xwk/following{/other_user}","gists_url":"https://api.github.com/users/xwk/gists{/gist_id}","starred_url":"https://api.github.com/users/xwk/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/xwk/subscriptions","organizations_url":"https://api.github.com/users/xwk/orgs","repos_url":"https://api.github.com/users/xwk/repos","events_url":"https://api.github.com/users/xwk/events{/privacy}","received_events_url":"https://api.github.com/users/xwk/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-06-12T00:37:07Z","updated_at":"2015-06-12T02:28:46Z","closed_at":null,"body":"Hi,\r\n\r\nFirst, the problem is different from another issue with shareReplay() I mentioned in https://github.com/Reactive-Extensions/RxJS/issues/758, and I tested the code below with Rx version 2.5.3.\r\n\r\nI was expecting the createJob observable, as shown in the code below, should only be subscribed once, since I made it a shared one by doing .shareReplay(). However, it actually was subscribed twice.\r\n\r\n````\r\nvar Rx = require('rx');\r\n\r\nvar createJob = function() {\r\n  return Rx.Observable.create(function(subscriber) {\r\n\r\n    console.log(\"subscribed to create job\");\r\n    var resolve = function() {\r\n      console.log(\"resolve createJob\");\r\n      subscriber.onNext(\"job123\");\r\n      return subscriber.onCompleted();\r\n    };\r\n    return setTimeout(resolve, 10);\r\n  });\r\n};\r\n\r\nvar createJobEvtObservable = createJob()\r\n.doOnNext(function(data) {\r\n  return console.log(\"Side effect \" + data);\r\n})\r\n.shareReplay();\r\n\r\nvar doJob = function(jobId) {\r\n  console.log(\"start job \" + jobId);\r\n  return Rx.Observable.interval(300).take(3).map(function(i) {\r\n    return \"completed step \" + i;\r\n  });\r\n};\r\n\r\nvar doJobEvtObservable = createJobEvtObservable.flatMap(doJob);\r\n\r\ncreateJobEvtObservable.concat(doJobEvtObservable).subscribe(function(evt) {\r\n  return console.log(\"evt=\", evt);\r\n});\r\n\r\n````\r\nThe output is as below\r\n````\r\nsubscribed to create job\r\nresolve createJob\r\nSide effect job123\r\nevt= job123\r\nsubscribed to create job\r\nstart job job123\r\nresolve createJob\r\nevt= completed step 0\r\nevt= completed step 1\r\nevt= completed step 2\r\n````\r\nYou can see 'subscribed to create job' is printed twice, despite of shareReplay(). However, more strangely, the \"Side effect job123\" indeed only is printed once.\r\n"},"comment":{"url":"https://api.github.com/repos/Reactive-Extensions/RxJS/issues/comments/111332357","html_url":"https://github.com/Reactive-Extensions/RxJS/issues/763#issuecomment-111332357","issue_url":"https://api.github.com/repos/Reactive-Extensions/RxJS/issues/763","id":111332357,"user":{"login":"paulpdaniels","id":2528918,"avatar_url":"https://avatars.githubusercontent.com/u/2528918?v=3","gravatar_id":"","url":"https://api.github.com/users/paulpdaniels","html_url":"https://github.com/paulpdaniels","followers_url":"https://api.github.com/users/paulpdaniels/followers","following_url":"https://api.github.com/users/paulpdaniels/following{/other_user}","gists_url":"https://api.github.com/users/paulpdaniels/gists{/gist_id}","starred_url":"https://api.github.com/users/paulpdaniels/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/paulpdaniels/subscriptions","organizations_url":"https://api.github.com/users/paulpdaniels/orgs","repos_url":"https://api.github.com/users/paulpdaniels/repos","events_url":"https://api.github.com/users/paulpdaniels/events{/privacy}","received_events_url":"https://api.github.com/users/paulpdaniels/received_events","type":"User","site_admin":false},"created_at":"2015-06-12T02:28:46Z","updated_at":"2015-06-12T02:28:46Z","body":"The problem is that `shareReplay()` is really `replay().refCount()`, and `concat()` is really `enumerableOf(sources).merge(1)`. \r\n\r\nIf you look at the [source of refCount](https://github.com/Reactive-Extensions/RxJS/blob/master/src/core/linq/connectableobservable.js#L25-L30) you'll see that once the count dips to zero, the subscription disconnects, and once it goes up to one again, it reconnects.  In `concat()` it waits for the previous observable to complete (i.e. count == 0) then it starts the new one (count == 1, reconnect!).\r\n\r\nAll this is a roundabout way of saying don't use `shareReplay()` here, use the `replay()/connect()` combo instead.\r\n\r\n@mattpodwysocki is refCount supposed to restart like that? I never noticed it before.\r\n"}}