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/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..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() { @@ -113,15 +114,25 @@ export default class ApiMakerCableConnectionPool { return subscription } - connectUpdate(modelName, modelId, callback) { + 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] = {} @@ -129,17 +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: callback, - modelName: modelName, - modelId: modelId - }) - - this.upcomingSubscriptions[modelName]["updates"][modelId].push(subscription) - + this.upcomingSubscriptions[modelName]["updates"][uniqueId] = {modelId, subscription} this.scheduleConnectUpcoming() return 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