Skip to content

Commit

Permalink
Merge pull request peerlibrary#42 from zodern/fix/meteor-1.8.1
Browse files Browse the repository at this point in the history
Support Meteor 1.8.1
  • Loading branch information
mitar authored Apr 16, 2019
2 parents be84b6d + a8f094b commit 21696e3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Package.onTest(function (api) {
'peerlibrary:[email protected]',
'peerlibrary:[email protected]',
'peerlibrary:[email protected]',
'meteorhacks:unblock@1.1.0'
'lamhieu:unblock@1.0.0'
]);

api.add_files([
Expand Down
27 changes: 23 additions & 4 deletions server.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ checkNames = (publish, allCollectionNames, id, collectionNames) ->

true

iterateObjectOrMapKeys = (objectOrMap, fn) ->
if (objectOrMap instanceof Map)
for [ key ] from objectOrMap
fn(key)
else
for key of objectOrMap
fn(key)

wrapCallbacks = (callbacks, initializingReference) ->
# If observeChanges is called inside a reactive context we have to make extra effort to pass the computation to the
# observeChanges callbacks so that the computation is available to the "added" publish method, if it is called. We use
Expand Down Expand Up @@ -98,8 +106,12 @@ extendPublish (name, publishFunction, options) ->

computation.afterRun =>
# We remove those which are not published anymore.
for collectionName of @_documents
currentlyPublishedDocumentIds = _.keys(@_documents[collectionName] or {})
iterateObjectOrMapKeys @_documents, (collectionName) =>
if @_documents instanceof Map
currentlyPublishedDocumentIds = Array.from(@_documents.get(collectionName))
else
currentlyPublishedDocumentIds = _.keys(@_documents[collectionName] or {})

currentComputationAddedDocumentIds = _.keys(documents[computation._id]?[collectionName] or {})
# If afterRun for other autoruns in the publish function have not yet run, we have to look in "documents" as well.
otherComputationsAddedDocumentsIds = _.union (_.keys(docs[collectionName] or {}) for computationId, docs of documents when computationId isnt "#{computation._id}")...
Expand Down Expand Up @@ -136,14 +148,21 @@ extendPublish (name, publishFunction, options) ->
# This can hide some errors in publish functions if they one calls "added" on an existing document and we could
# make it so that this behavior works only inside reactive computation (if "currentComputation" is set), but we
# can also make it so that publish function tries to do something smarter (sending a diff) in all cases, as we do.
if @_documents[collectionName]?[stringId]
if ((@_documents instanceof Map && @_documents.get(collectionName)?.has(stringId)) || @_documents[collectionName]?[stringId])
oldFields = {}
# If some field existed before, but does not exist anymore, we have to remove it by calling "changed"
# with value set to "undefined". So we look into current session's state and see which fields are currently
# known and create an object of same fields, just all values set to "undefined". We then override some fields
# with new values. Only top-level fields matter.
for field of @_session.getCollectionView(collectionName)?.documents?[stringId]?.dataByKey or {}
_documents = @_session.getCollectionView(collectionName)?.documents or {}
if _documents instanceof Map
dataByKey = _documents.get(stringId)?.dataByKey or {}
else
dataByKey = _documents?[stringId]?.dataByKey or {}

iterateObjectOrMapKeys dataByKey, (field) =>
oldFields[field] = undefined

@changed collectionName, id, _.extend oldFields, fields
else
originalAdded.call @, collectionName, id, fields
Expand Down

0 comments on commit 21696e3

Please sign in to comment.