Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update events with custom query #309

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions app/channels/api_maker/subscriptions_channel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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|
Expand Down
4 changes: 2 additions & 2 deletions lib/api_maker/javascript/base-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
31 changes: 16 additions & 15 deletions lib/api_maker/javascript/cable-connection-pool.js
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down Expand Up @@ -113,33 +114,33 @@ 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] = {}

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
Expand Down
6 changes: 4 additions & 2 deletions lib/api_maker/javascript/event-updated.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import Collection from "./collection"
import PropTypes from "prop-types"
import PropTypesExact from "prop-types-exact"
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() {
Expand All @@ -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() {
Expand Down
25 changes: 15 additions & 10 deletions lib/api_maker/javascript/updated-attribute.jsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Collection from "./collection"
import PropTypes from "prop-types"
import PropTypesExact from "prop-types-exact"
import React from "react"
Expand All @@ -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) {
Expand All @@ -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
})
}

Expand Down
1 change: 0 additions & 1 deletion lib/api_maker/model_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down