From ce76335fdabf428ce21e15b2a838b5b7d670710f Mon Sep 17 00:00:00 2001 From: Chi Vinh Le Date: Mon, 12 Jun 2017 23:50:09 +0700 Subject: [PATCH] Add some comments --- plugin-api/beta/client/hocs/withReaction.js | 9 +++++++++ plugin-api/beta/server/getReactionConfig.js | 4 ++++ 2 files changed, 13 insertions(+) diff --git a/plugin-api/beta/client/hocs/withReaction.js b/plugin-api/beta/client/hocs/withReaction.js index 4512d3ac5..42ac79ec5 100644 --- a/plugin-api/beta/client/hocs/withReaction.js +++ b/plugin-api/beta/client/hocs/withReaction.js @@ -18,7 +18,10 @@ export default (reaction) => (WrappedComponent) => { return null; } + // Global instance counter for each `reaction` type. let instances = 0; + + // Track current subscriptions. let createdSubscription = null; let deletedSubscription = null; @@ -148,6 +151,8 @@ export default (reaction) => (WrappedComponent) => { constructor(props, context) { super(props, context); + + // Start subscriptions when it is first needed. if (instances === 0) { createdSubscription = context.client.subscribe({ query: REACTION_CREATED_SUBSCRIPTION, @@ -172,6 +177,7 @@ export default (reaction) => (WrappedComponent) => { instances++; } + // onReactionCreated handles live updates through the subscriptions. onReactionCreated = ({[`${reaction}ActionCreated`]: action}) => { if (this.props.user && action.user && this.props.user.id === action.user.id) { return; @@ -179,6 +185,7 @@ export default (reaction) => (WrappedComponent) => { addReactionToStore(this.context.client, {action, self: false}); }; + // onReactionDeleted handles live updates through the subscriptions. onReactionDeleted = ({[`${reaction}ActionDeleted`]: action}) => { if (this.props.user && action.user && this.props.user.id === action.user.id) { return; @@ -188,6 +195,8 @@ export default (reaction) => (WrappedComponent) => { componentWillUnmount() { instances--; + + // End subscriptions when last component will be unmounted. if (instances === 0) { try { createdSubscription.unsubscribe(); diff --git a/plugin-api/beta/server/getReactionConfig.js b/plugin-api/beta/server/getReactionConfig.js index 077b51261..83c45dcb5 100644 --- a/plugin-api/beta/server/getReactionConfig.js +++ b/plugin-api/beta/server/getReactionConfig.js @@ -130,6 +130,8 @@ function getReactionConfig(reaction) { const response = Comments.get.load(item_id).then((comment) => { return Action.create({item_id, item_type: 'COMMENTS', action_type: REACTION}) .then((action) => { + + // The comment is needed to allow better filtering e.g. by asset_id. pubsub.publish(`${reaction}ActionCreated`, {action, comment}); return Promise.resolve(action); }); @@ -140,6 +142,8 @@ function getReactionConfig(reaction) { const response = Action.delete({id}) .then((action) => { return Comments.get.load(action.item_id).then((comment) => { + + // The comment is needed to allow better filtering e.g. by asset_id. pubsub.publish(`${reaction}ActionDeleted`, {action, comment}); return Promise.resolve(action); });