1
0
mirror of https://github.com/oceanprotocol-archive/squid-js.git synced 2024-02-02 15:31:51 +01:00

Add more events on order execution.

This commit is contained in:
Pedro Gutiérrez 2019-04-25 14:15:32 +02:00 committed by Pedro Gutiérrez
parent 4c4a4f64ce
commit c8411e29d2
3 changed files with 25 additions and 17 deletions

View File

@ -41,7 +41,7 @@ describe("Consume Asset (Brizo)", () => {
.next((step) => steps.push(step)) .next((step) => steps.push(step))
assert.isDefined(agreementId) assert.isDefined(agreementId)
assert.deepEqual(steps, [0, 1, 2, 3]) assert.deepEqual(steps, [0, 1, 2, 3, 4, 5])
}) })
it("should consume and store the assets", async () => { it("should consume and store the assets", async () => {

View File

@ -8,9 +8,11 @@ import { fillConditionsWithDDO, noZeroX, SubscribablePromise } from "../utils"
import { Instantiable, InstantiableConfig } from "../Instantiable.abstract" import { Instantiable, InstantiableConfig } from "../Instantiable.abstract"
export enum OrderProgressStep { export enum OrderProgressStep {
Preparing,
Prepared, Prepared,
AgreementSent, SendingAgreement,
AgreementInitialized, AgreementInitialized,
LockingPayment,
LockedPayment, LockedPayment,
} }
@ -215,6 +217,7 @@ export class OceanAssets extends Instantiable {
return new SubscribablePromise(async (observer) => { return new SubscribablePromise(async (observer) => {
const oceanAgreements = this.ocean.agreements const oceanAgreements = this.ocean.agreements
observer.next(OrderProgressStep.Preparing)
this.logger.log("Asking for agreement signature") this.logger.log("Asking for agreement signature")
const {agreementId, signature} = await oceanAgreements.prepare(did, serviceDefinitionId, consumer) const {agreementId, signature} = await oceanAgreements.prepare(did, serviceDefinitionId, consumer)
this.logger.log(`Agreement ${agreementId} signed`) this.logger.log(`Agreement ${agreementId} signed`)
@ -239,6 +242,7 @@ export class OceanAssets extends Instantiable {
const accessGranted = accessCondition.getConditionFulfilledEvent(agreementId).once() const accessGranted = accessCondition.getConditionFulfilledEvent(agreementId).once()
observer.next(OrderProgressStep.LockingPayment)
const paid = await oceanAgreements.conditions.lockReward(agreementId, metadata.base.price, consumer) const paid = await oceanAgreements.conditions.lockReward(agreementId, metadata.base.price, consumer)
observer.next(OrderProgressStep.LockedPayment) observer.next(OrderProgressStep.LockedPayment)
@ -257,7 +261,7 @@ export class OceanAssets extends Instantiable {
resolve() resolve()
}) })
observer.next(OrderProgressStep.AgreementSent) observer.next(OrderProgressStep.SendingAgreement)
this.logger.log("Sending agreement request") this.logger.log("Sending agreement request")
await oceanAgreements.send(did, agreementId, serviceDefinitionId, signature, consumer) await oceanAgreements.send(did, agreementId, serviceDefinitionId, signature, consumer)
this.logger.log("Agreement request sent") this.logger.log("Agreement request sent")

View File

@ -13,20 +13,8 @@ export class SubscribablePromise<T extends any, P extends any> {
) )
constructor(executor: (observer: SubscribableObserver<T, P>) => void | Promise<P>) { constructor(executor: (observer: SubscribableObserver<T, P>) => void | Promise<P>) {
const execution = executor(this.observer) // Defear
setTimeout(() => this.init(executor), 1)
Promise.resolve(execution as any)
.then((result) => {
if (Promise.resolve(execution as any) === execution) {
this.observer.complete(result)
}
})
.catch((result) => {
if (Promise.resolve(execution as any) === execution) {
this.observer.error(result)
}
})
} }
public subscribe(onNext: (next: T) => void) { public subscribe(onNext: (next: T) => void) {
@ -49,4 +37,20 @@ export class SubscribablePromise<T extends any, P extends any> {
public finally(onfinally?: () => any) { public finally(onfinally?: () => any) {
return Object.assign(this.promise.finally(onfinally), this) return Object.assign(this.promise.finally(onfinally), this)
} }
private init(executor: (observer: SubscribableObserver<T, P>) => void | Promise<P>) {
const execution = executor(this.observer)
Promise.resolve(execution as any)
.then((result) => {
if (Promise.resolve(execution as any) === execution) {
this.observer.complete(result)
}
})
.catch((result) => {
if (Promise.resolve(execution as any) === execution) {
this.observer.error(result)
}
})
}
} }