Skip to content

Commit

Permalink
feat: Add triggers realtime to ConnectionFlow
Browse files Browse the repository at this point in the history
With this commit, ConnectionFlow knows to update
its trigger when it is created or deleted
  • Loading branch information
Merkur39 committed Feb 8, 2023
1 parent 94c9e19 commit b46616e
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions packages/cozy-harvest-lib/src/models/ConnectionFlow.js
Expand Up @@ -164,6 +164,8 @@ export class ConnectionFlow {
this.handleAccountUpdated = this.handleAccountUpdated.bind(this)
this.handleCurrentJobUpdated = this.handleCurrentJobUpdated.bind(this)
this.handleTriggerJobUpdated = this.handleTriggerJobUpdated.bind(this)
this.handleTriggerDeleted = this.handleTriggerDeleted.bind(this)
this.handleTriggerCreated = this.handleTriggerCreated.bind(this)
this.handleAccountTwoFA = this.handleAccountTwoFA.bind(this)
this.launch = this.launch.bind(this)
this.sendTwoFACode = this.sendTwoFACode.bind(this)
Expand All @@ -183,6 +185,7 @@ export class ConnectionFlow {

this.watchCurrentJobIfTriggerIsAlreadyRunning()
this.watchTriggerJobs()
this.watchTriggers()
}

getTwoFACodeProvider() {
Expand Down Expand Up @@ -479,6 +482,27 @@ export class ConnectionFlow {
}
}

handleTriggerCreated(trigger) {
if (
this.konnector.slug !== trigger?.message?.konnector ||
this.trigger !== null
) {
return // filter out trigger associated to konnector or if a current trigger already exists
}

this.trigger = trigger
// @ts-ignore
this.emit(UPDATE_EVENT)
}

handleTriggerDeleted(trigger) {
if (this.trigger?._id !== trigger?._id) return // filter out trigger associated to current trigger

this.reset()
// @ts-ignore
this.emit(UPDATE_EVENT)
}

handleAccountUpdated(account) {
const prevAccount = this.account

Expand Down Expand Up @@ -663,6 +687,22 @@ export class ConnectionFlow {
}
}

/**
* Watch all triggers
*/
watchTriggers() {
this.realtime.subscribe(
'deleted',
'io.cozy.triggers',
this.handleTriggerDeleted
)
this.realtime.subscribe(
'created',
'io.cozy.triggers',
this.handleTriggerCreated
)
}

/**
* Watch all jobs related to the current trigger
*/
Expand Down Expand Up @@ -727,6 +767,16 @@ export class ConnectionFlow {
this.job._id,
this.handleCurrentJobUpdated.bind(this)
)
this.realtime.unsubscribe(
'deleted',
'io.cozy.triggers',
this.handleTriggerDeleted
)
this.realtime.unsubscribe(
'created',
'io.cozy.triggers',
this.handleTriggerCreated
)
}

unwatch() {
Expand Down

0 comments on commit b46616e

Please sign in to comment.