   ,
     Publication.PUBLIC_FIELDS()
 
-Meteor.publish 'my-publications', ->
+# TODO: Should we use try/except around the code so that if there is any exception we stop handlers?
+publishUsingMyLibrary = (publish, selector, options) ->
   # There are moments when two observes are observing mostly similar list
   # of publications ids so it could happen that one is changing or removing
   # publication just while the other one is adding, so we are making sure

  currentPersonId = null # Just for asserts
  handlePublications = null

  removePublications = (ids) =>
    for id in ids when currentPublications[id]
      delete currentPublications[id]
      @removed 'Publications', id

  publishPublications = (newLibrary) =>
    newLibrary ||= []

    added = {}
    added[id] = true for id in _.difference newLibrary, _.keys(currentPublications)
    removed = {}
    removed[id] = true for id in _.difference _.keys(currentPublications), newLibrary

    # Optimization, happens when a publication document is first deleted and
    # then removed from the library list in the person document
    if _.isEmpty(added) and _.isEmpty(removed)
      return

    oldHandlePublications = handlePublications
    handlePublications = Publications.find(
      _id:
        $in: newLibrary
    ,
      Publication.PUBLIC_FIELDS()
    ).observeChanges
      added: (id, fields) =>
        return if currentPublications[id]
        currentPublications[id] = true

        # We add only the newly added ones, others were added already before
        @added 'Publications', id, fields if added[id]

      changed: (id, fields) =>
        return if not currentPublications[id]

        @changed 'Publications', id, fields

      removed: (id) =>
        return if not currentPublications[id]
        delete currentPublications[id]

        @removed 'Publications', id

    # We stop the handle after we established the new handle,
    # so that any possible changes hapenning in the meantime
    # were still processed by the old handle
    oldHandlePublications.stop() if oldHandlePublications

    # And then we remove those who are not in the library anymore
    removePublications removed

  handlePersons = Persons.find(
    _id: @personId
  ,
    fields:
      # id field is implicitly added


    changed: (id, fields) =>
      # Person should already be added
      assert.equal currentPersonId, id

      publishPublications _.pluck fields.library, '_id'

    removed: (id) =>
      # We cannot remove the person if we never added the person before
      assert.equal currentPersonId, id

      currentPersonId = null
      publishPublications []

  publish.ready()

  publish.onStop =>
    handlePersons.stop() if handlePersons
    handlePublications.stop() if handlePublications

Meteor.publish 'my-publications', ->
  publishUsingMyLibrary @, (library) =>
    _id:
      $in: library
  ,
    Publication.PUBLIC_FIELDS()

# We could try to combine my-publications and my-publications-importing,
# but it is easier to have two and leave to Meteor to merge them together,
# because we are using $ in fields
Meteor.publish 'my-publications-importing', ->
  Publications.find
    'importing.person._id': @personId
