From b70ec23f6afdcf817af6d2ed0435eda5cab01b1a Mon Sep 17 00:00:00 2001 From: kaspernj Date: Tue, 7 Apr 2020 10:21:11 +0200 Subject: [PATCH 1/2] Update events with custom query --- lib/api_maker/javascript/base-model.js | 4 +-- .../javascript/cable-connection-pool.js | 11 ++++---- lib/api_maker/javascript/event-updated.jsx | 6 +++-- .../javascript/updated-attribute.jsx | 25 +++++++++++-------- lib/api_maker/model_extensions.rb | 1 - 5 files changed, 26 insertions(+), 21 deletions(-) diff --git a/lib/api_maker/javascript/base-model.js b/lib/api_maker/javascript/base-model.js index f21b96606..d2457f057 100644 --- a/lib/api_maker/javascript/base-model.js +++ b/lib/api_maker/javascript/base-model.js @@ -74,8 +74,8 @@ export default class BaseModel { return cableSubscription } - connectUpdated(callback) { - const cableSubscription = CableConnectionPool.current().connectUpdate(this.modelClassData().name, this.primaryKey(), callback) + connectUpdated(args) { + const cableSubscription = CableConnectionPool.current().connectUpdate(this.modelClassData().name, this.primaryKey(), args) return cableSubscription } diff --git a/lib/api_maker/javascript/cable-connection-pool.js b/lib/api_maker/javascript/cable-connection-pool.js index 10aa0328a..99a32e13d 100644 --- a/lib/api_maker/javascript/cable-connection-pool.js +++ b/lib/api_maker/javascript/cable-connection-pool.js @@ -113,7 +113,10 @@ export default class ApiMakerCableConnectionPool { return subscription } - connectUpdate(modelName, modelId, callback) { + connectUpdate(modelName, modelId, args) { + const callback = args.callback + const query = args.query + if (!this.upcomingSubscriptionData[modelName]) this.upcomingSubscriptionData[modelName] = {} @@ -132,11 +135,7 @@ export default class ApiMakerCableConnectionPool { if (!this.upcomingSubscriptions[modelName]["updates"][modelId]) this.upcomingSubscriptions[modelName]["updates"][modelId] = [] - const subscription = new CableSubscription({ - callback: callback, - modelName: modelName, - modelId: modelId - }) + const subscription = new CableSubscription({callback, modelName, modelId}) this.upcomingSubscriptions[modelName]["updates"][modelId].push(subscription) diff --git a/lib/api_maker/javascript/event-updated.jsx b/lib/api_maker/javascript/event-updated.jsx index a8cc04bd7..3c9360f19 100644 --- a/lib/api_maker/javascript/event-updated.jsx +++ b/lib/api_maker/javascript/event-updated.jsx @@ -1,3 +1,4 @@ +import Collection from "./collection" import PropTypes from "prop-types" import PropTypesExact from "prop-types-exact" import React from "react" @@ -5,7 +6,8 @@ import React from "react" export default class ApiMakerEventUpdated extends React.Component { static propTypes = PropTypesExact({ model: PropTypes.object.isRequired, - onUpdated: PropTypes.func.isRequired + onUpdated: PropTypes.func.isRequired, + query: PropTypes.instanceOf(Collection) }) componentDidMount() { @@ -17,7 +19,7 @@ export default class ApiMakerEventUpdated extends React.Component { } connect() { - this.connectUpdated = this.props.model.connectUpdated(this.props.onUpdated) + this.connectUpdated = this.props.model.connectUpdated({callback: this.props.onUpdated, query: this.props.query}) } render() { diff --git a/lib/api_maker/javascript/updated-attribute.jsx b/lib/api_maker/javascript/updated-attribute.jsx index 7106d0d82..17cd58549 100644 --- a/lib/api_maker/javascript/updated-attribute.jsx +++ b/lib/api_maker/javascript/updated-attribute.jsx @@ -1,3 +1,4 @@ +import Collection from "./collection" import PropTypes from "prop-types" import PropTypesExact from "prop-types-exact" import React from "react" @@ -6,7 +7,8 @@ export default class ApiMakerUpdatedAttribute extends React.Component { static propTypes = PropTypesExact({ attribute: PropTypes.string, model: PropTypes.object.isRequired, - onValue: PropTypes.func + onValue: PropTypes.func, + query: PropTypes.instanceOf(Collection) }) constructor(props) { @@ -26,15 +28,18 @@ export default class ApiMakerUpdatedAttribute extends React.Component { } connect() { - this.connectUpdated = this.props.model.connectUpdated(args => { - if (!this.props.attribute || args.model.isAttributeLoaded(this.props.attribute)) { - this.setState( - {model: args.model}, - () => this.setAttribute() - ) - } else { - this.loadModelWithAttribute() - } + this.connectUpdated = this.props.model.connectUpdated({ + callback: (args) => { + if (!this.props.attribute || args.model.isAttributeLoaded(this.props.attribute)) { + this.setState( + {model: args.model}, + () => this.setAttribute() + ) + } else { + this.loadModelWithAttribute() + } + }, + query: this.props.query }) } diff --git a/lib/api_maker/model_extensions.rb b/lib/api_maker/model_extensions.rb index cd2d3c707..e0fb2c35e 100644 --- a/lib/api_maker/model_extensions.rb +++ b/lib/api_maker/model_extensions.rb @@ -78,7 +78,6 @@ def api_maker_broadcast_destroy_channel_name def api_maker_broadcast_update serializer = ApiMaker::Serializer.new(model: self) data_to_broadcast = ApiMaker::ResultParser.parse( - model: self, model_id: id, model_type: serializer.resource.collection_name, type: :update From ab9521783e3b7b60a56d10ed0bf0ea834f2aa75b Mon Sep 17 00:00:00 2001 From: kaspernj Date: Wed, 8 Apr 2020 08:23:55 +0200 Subject: [PATCH 2/2] Worked more on the refactor --- .../api_maker/subscriptions_channel.rb | 9 +++++--- .../javascript/cable-connection-pool.js | 22 ++++++++++--------- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/app/channels/api_maker/subscriptions_channel.rb b/app/channels/api_maker/subscriptions_channel.rb index 4e605fc8a..4958b7e9a 100644 --- a/app/channels/api_maker/subscriptions_channel.rb +++ b/app/channels/api_maker/subscriptions_channel.rb @@ -8,8 +8,8 @@ def subscribed connect_creates(model_name) if subscription_types.key?("creates") if subscription_types.key?("updates") - model_ids = subscription_types.fetch("updates") - connect_updates(model_name, model_ids) + models_data = subscription_types.fetch("updates") + connect_updates(model_name, models_data) end if subscription_types.key?("destroys") @@ -56,7 +56,10 @@ def connect_event(model_name, model_ids, event_name) end end - def connect_updates(model_name, model_ids) + def connect_updates(model_name, models_data) + binding.pry + + model_ids = models_data.keys model_class = model_for_resource_name(model_name) models = model_class.accessible_by(current_ability, :update_events).where(model_class.primary_key => model_ids) models.each do |model| diff --git a/lib/api_maker/javascript/cable-connection-pool.js b/lib/api_maker/javascript/cable-connection-pool.js index 99a32e13d..3526ef654 100644 --- a/lib/api_maker/javascript/cable-connection-pool.js +++ b/lib/api_maker/javascript/cable-connection-pool.js @@ -1,5 +1,6 @@ import CableSubscriptionPool from "./cable-subscription-pool" import CableSubscription from "./cable-subscription" +import {v4 as uuidv4} from "uuid" export default class ApiMakerCableConnectionPool { static current() { @@ -116,15 +117,22 @@ export default class ApiMakerCableConnectionPool { connectUpdate(modelName, modelId, args) { const callback = args.callback const query = args.query + const subscription = new CableSubscription({callback, modelName, modelId}) + const uniqueId = uuidv4() if (!this.upcomingSubscriptionData[modelName]) this.upcomingSubscriptionData[modelName] = {} if (!this.upcomingSubscriptionData[modelName]["updates"]) - this.upcomingSubscriptionData[modelName]["updates"] = [] + this.upcomingSubscriptionData[modelName]["updates"] = {} + + if (!this.upcomingSubscriptionData[modelName]["updates"][modelId]) + this.upcomingSubscriptionData[modelName]["updates"][modelId] = [] - if (!this.upcomingSubscriptionData[modelName]["updates"].includes(modelId)) - this.upcomingSubscriptionData[modelName]["updates"].push(modelId) + this.upcomingSubscriptionData[modelName]["updates"][modelId].push({ + query_params: query.params(), + unique_id: uniqueId + }) if (!this.upcomingSubscriptions[modelName]) this.upcomingSubscriptions[modelName] = {} @@ -132,13 +140,7 @@ export default class ApiMakerCableConnectionPool { if (!this.upcomingSubscriptions[modelName]["updates"]) this.upcomingSubscriptions[modelName]["updates"] = {} - if (!this.upcomingSubscriptions[modelName]["updates"][modelId]) - this.upcomingSubscriptions[modelName]["updates"][modelId] = [] - - const subscription = new CableSubscription({callback, modelName, modelId}) - - this.upcomingSubscriptions[modelName]["updates"][modelId].push(subscription) - + this.upcomingSubscriptions[modelName]["updates"][uniqueId] = {modelId, subscription} this.scheduleConnectUpcoming() return subscription