mirror of
https://github.com/oceanprotocol-archive/squid-js.git
synced 2024-02-02 15:31:51 +01:00
Fix linter errors.
This commit is contained in:
parent
5b5ad23d7a
commit
e76c152bfc
@ -38,7 +38,7 @@ describe("Consume Asset (Brizo)", () => {
|
|||||||
|
|
||||||
const steps = []
|
const steps = []
|
||||||
agreementId = await ocean.assets.order(ddo.id, accessService.serviceDefinitionId, consumer)
|
agreementId = await ocean.assets.order(ddo.id, accessService.serviceDefinitionId, consumer)
|
||||||
.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])
|
||||||
|
@ -212,7 +212,7 @@ export class OceanAssets extends Instantiable {
|
|||||||
consumer: Account,
|
consumer: Account,
|
||||||
): SubscribablePromise<OrderProgressStep, string> {
|
): SubscribablePromise<OrderProgressStep, string> {
|
||||||
|
|
||||||
return new SubscribablePromise(async observer => {
|
return new SubscribablePromise(async (observer) => {
|
||||||
const oceanAgreements = this.ocean.agreements
|
const oceanAgreements = this.ocean.agreements
|
||||||
|
|
||||||
this.logger.log("Asking for agreement signature")
|
this.logger.log("Asking for agreement signature")
|
||||||
|
42
src/utils/SubscribableObserver.ts
Normal file
42
src/utils/SubscribableObserver.ts
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
export class SubscribableObserver<T, P> {
|
||||||
|
public completed: boolean = false
|
||||||
|
private subscriptions = new Set<{onNext?: (next: T) => void, onComplete?: (complete: P) => void, onError?: (error: any) => void}>()
|
||||||
|
|
||||||
|
public subscribe(onNext?: (next: T) => void, onComplete?: (complete: P) => void, onError?: (error: any) => void) {
|
||||||
|
if (this.completed) {
|
||||||
|
throw new Error("Observer completed.")
|
||||||
|
}
|
||||||
|
const subscription = {onNext, onComplete, onError}
|
||||||
|
this.subscriptions.add(subscription)
|
||||||
|
|
||||||
|
return {
|
||||||
|
unsubscribe: () => this.subscriptions.delete(subscription),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public next(next?: T): void {
|
||||||
|
this.emit("onNext", next)
|
||||||
|
}
|
||||||
|
|
||||||
|
public complete(resolve?: P): void {
|
||||||
|
this.emit("onComplete", resolve)
|
||||||
|
this.unsubscribe()
|
||||||
|
}
|
||||||
|
|
||||||
|
public error(error?: any): void {
|
||||||
|
this.emit("onError", error)
|
||||||
|
this.unsubscribe()
|
||||||
|
}
|
||||||
|
|
||||||
|
private emit(type: "onNext" | "onComplete" | "onError", value: any) {
|
||||||
|
Array.from(this.subscriptions)
|
||||||
|
.map((subscription) => subscription[type])
|
||||||
|
.filter((callback: any) => callback && typeof callback === "function")
|
||||||
|
.forEach((callback: any) => callback(value))
|
||||||
|
}
|
||||||
|
|
||||||
|
private unsubscribe() {
|
||||||
|
this.completed = true
|
||||||
|
this.subscriptions.clear()
|
||||||
|
}
|
||||||
|
}
|
@ -1,45 +1,4 @@
|
|||||||
export class SubscribableObserver<T, P> {
|
import { SubscribableObserver } from "./SubscribableObserver"
|
||||||
public completed: boolean = false
|
|
||||||
private subscriptions = new Set<{onNext?: (next: T) => void, onComplete?: (complete: P) => void, onError?: (error: any) => void}>()
|
|
||||||
|
|
||||||
subscribe(onNext?: (next: T) => void, onComplete?: (complete: P) => void, onError?: (error: any) => void) {
|
|
||||||
if (this.completed) {
|
|
||||||
throw new Error("Observer completed.")
|
|
||||||
}
|
|
||||||
const subscription = {onNext, onComplete, onError}
|
|
||||||
this.subscriptions.add(subscription)
|
|
||||||
|
|
||||||
return {
|
|
||||||
unsubscribe: () => this.subscriptions.delete(subscription),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
next(next?: T): void {
|
|
||||||
this.emit('onNext', next)
|
|
||||||
}
|
|
||||||
|
|
||||||
complete(resolve?: P): void {
|
|
||||||
this.emit('onComplete', resolve)
|
|
||||||
this.unsubscribe()
|
|
||||||
}
|
|
||||||
|
|
||||||
error(error?: any): void {
|
|
||||||
this.emit('onError', error)
|
|
||||||
this.unsubscribe()
|
|
||||||
}
|
|
||||||
|
|
||||||
private emit(type: 'onNext' | 'onComplete' | 'onError', value: any) {
|
|
||||||
Array.from(this.subscriptions)
|
|
||||||
.map(subscription => subscription[type])
|
|
||||||
.filter(callback => callback && typeof callback === 'function')
|
|
||||||
.forEach(callback => callback(value))
|
|
||||||
}
|
|
||||||
|
|
||||||
private unsubscribe() {
|
|
||||||
this.completed = true
|
|
||||||
this.subscriptions.clear()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export class SubscribablePromise<T extends any, P extends any> {
|
export class SubscribablePromise<T extends any, P extends any> {
|
||||||
private observer = new SubscribableObserver<T, P>()
|
private observer = new SubscribableObserver<T, P>()
|
||||||
@ -48,7 +7,7 @@ export class SubscribablePromise<T extends any, P extends any> {
|
|||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
this.observer
|
this.observer
|
||||||
.subscribe(undefined, resolve, reject)
|
.subscribe(undefined, resolve, reject)
|
||||||
})
|
}, 0)
|
||||||
}),
|
}),
|
||||||
this,
|
this,
|
||||||
)
|
)
|
||||||
@ -57,31 +16,31 @@ export class SubscribablePromise<T extends any, P extends any> {
|
|||||||
const execution = executor(this.observer)
|
const execution = executor(this.observer)
|
||||||
|
|
||||||
Promise.resolve(execution as any)
|
Promise.resolve(execution as any)
|
||||||
.then(result => {
|
.then((result) => {
|
||||||
if (Promise.resolve(execution as any) == execution) {
|
if (Promise.resolve(execution as any) === execution) {
|
||||||
this.observer.complete(result)
|
this.observer.complete(result)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
subscribe(onNext: (next: T) => void) {
|
public subscribe(onNext: (next: T) => void) {
|
||||||
return this.observer.subscribe(onNext)
|
return this.observer.subscribe(onNext)
|
||||||
}
|
}
|
||||||
|
|
||||||
next(onNext: (next: T) => void) {
|
public next(onNext: (next: T) => void) {
|
||||||
this.observer.subscribe(onNext)
|
this.observer.subscribe(onNext)
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
then(onfulfilled?: (value: P) => any, onrejected?: (error: any) => any) {
|
public then(onfulfilled?: (value: P) => any, onrejected?: (error: any) => any) {
|
||||||
return Object.assign(this.promise.then(onfulfilled, onrejected), this)
|
return Object.assign(this.promise.then(onfulfilled, onrejected), this)
|
||||||
}
|
}
|
||||||
|
|
||||||
catch(onrejected?: (error: any) => any) {
|
public catch(onrejected?: (error: any) => any) {
|
||||||
return Object.assign(this.promise.catch(onrejected), this)
|
return Object.assign(this.promise.catch(onrejected), this)
|
||||||
}
|
}
|
||||||
|
|
||||||
finally(onfinally?: () => any) {
|
public finally(onfinally?: () => any) {
|
||||||
return Object.assign(this.promise.finally(onfinally), this)
|
return Object.assign(this.promise.finally(onfinally), this)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,3 +4,4 @@ export * from "./ConversionTypeHelpers"
|
|||||||
export * from "./GeneratorHelpers"
|
export * from "./GeneratorHelpers"
|
||||||
export * from "./DDOHelpers"
|
export * from "./DDOHelpers"
|
||||||
export * from "./SubscribablePromise"
|
export * from "./SubscribablePromise"
|
||||||
|
export * from "./SubscribableObserver"
|
||||||
|
76
test/utils/SubscribableObserver.test.ts
Normal file
76
test/utils/SubscribableObserver.test.ts
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
import { assert, expect, spy, use } from "chai"
|
||||||
|
import * as spies from "chai-spies"
|
||||||
|
|
||||||
|
import { SubscribableObserver} from "../../src/utils/SubscribableObserver"
|
||||||
|
|
||||||
|
use(spies)
|
||||||
|
|
||||||
|
describe("SubscribableObserver", () => {
|
||||||
|
|
||||||
|
describe("#subscribe()", () => {
|
||||||
|
|
||||||
|
it("should be able to add a subcription", async () => {
|
||||||
|
const observer = new SubscribableObserver()
|
||||||
|
const subscription = observer.subscribe()
|
||||||
|
|
||||||
|
assert.isDefined(subscription.unsubscribe)
|
||||||
|
assert.typeOf(subscription.unsubscribe, "function")
|
||||||
|
})
|
||||||
|
|
||||||
|
it("should be able to unsubscribe", async () => {
|
||||||
|
const observer = new SubscribableObserver()
|
||||||
|
const subscription = observer.subscribe()
|
||||||
|
|
||||||
|
subscription.unsubscribe()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe("#next()", () => {
|
||||||
|
|
||||||
|
it("should be able to emit next value", async () => {
|
||||||
|
const onNextSpy = spy()
|
||||||
|
const observer = new SubscribableObserver()
|
||||||
|
observer.subscribe(onNextSpy)
|
||||||
|
|
||||||
|
observer.next("test")
|
||||||
|
expect(onNextSpy).to.has.been.called.with("test")
|
||||||
|
|
||||||
|
observer.next("test")
|
||||||
|
expect(onNextSpy).to.has.been.called.exactly(2)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe("#complete()", () => {
|
||||||
|
|
||||||
|
it("should be able to complete", async () => {
|
||||||
|
const onCompleteSpy = spy()
|
||||||
|
const observer = new SubscribableObserver()
|
||||||
|
observer.subscribe(undefined, onCompleteSpy)
|
||||||
|
|
||||||
|
observer.complete("test")
|
||||||
|
expect(onCompleteSpy).to.has.been.called.with("test")
|
||||||
|
|
||||||
|
observer.complete("test")
|
||||||
|
expect(onCompleteSpy).to.has.been.called.exactly(1)
|
||||||
|
|
||||||
|
assert.isTrue(observer.completed)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe("#error()", () => {
|
||||||
|
|
||||||
|
it("should be able to emit a error", async () => {
|
||||||
|
const onErrorSpy = spy()
|
||||||
|
const observer = new SubscribableObserver()
|
||||||
|
observer.subscribe(undefined, undefined, onErrorSpy)
|
||||||
|
|
||||||
|
observer.error("test")
|
||||||
|
expect(onErrorSpy).to.has.been.called.with("test")
|
||||||
|
|
||||||
|
observer.error("test")
|
||||||
|
expect(onErrorSpy).to.has.been.called.exactly(1)
|
||||||
|
|
||||||
|
assert.isTrue(observer.completed)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
@ -1,80 +1,10 @@
|
|||||||
import { assert, expect, spy, use } from "chai"
|
import { assert, expect, spy, use } from "chai"
|
||||||
import * as spies from "chai-spies"
|
import * as spies from "chai-spies"
|
||||||
|
|
||||||
import { SubscribableObserver, SubscribablePromise } from "../../src/utils/SubscribablePromise"
|
import { SubscribablePromise } from "../../src/utils/SubscribablePromise"
|
||||||
|
|
||||||
use(spies)
|
use(spies)
|
||||||
|
|
||||||
describe("SubscribableObserver", () => {
|
|
||||||
|
|
||||||
describe("#subscribe()", () => {
|
|
||||||
|
|
||||||
it("should be able to add a subcription", async () => {
|
|
||||||
const observer = new SubscribableObserver()
|
|
||||||
const subscription = observer.subscribe()
|
|
||||||
|
|
||||||
assert.isDefined(subscription.unsubscribe)
|
|
||||||
assert.typeOf(subscription.unsubscribe, 'function')
|
|
||||||
})
|
|
||||||
|
|
||||||
it("should be able to unsubscribe", async () => {
|
|
||||||
const observer = new SubscribableObserver()
|
|
||||||
const subscription = observer.subscribe()
|
|
||||||
|
|
||||||
subscription.unsubscribe()
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
describe("#next()", () => {
|
|
||||||
|
|
||||||
it("should be able to emit next value", async () => {
|
|
||||||
const onNextSpy = spy()
|
|
||||||
const observer = new SubscribableObserver()
|
|
||||||
observer.subscribe(onNextSpy)
|
|
||||||
|
|
||||||
observer.next('test')
|
|
||||||
expect(onNextSpy).to.has.been.called.with('test')
|
|
||||||
|
|
||||||
observer.next('test')
|
|
||||||
expect(onNextSpy).to.has.been.called.exactly(2)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
describe("#complete()", () => {
|
|
||||||
|
|
||||||
it("should be able to complete", async () => {
|
|
||||||
const onCompleteSpy = spy()
|
|
||||||
const observer = new SubscribableObserver()
|
|
||||||
observer.subscribe(undefined, onCompleteSpy)
|
|
||||||
|
|
||||||
observer.complete('test')
|
|
||||||
expect(onCompleteSpy).to.has.been.called.with('test')
|
|
||||||
|
|
||||||
observer.complete('test')
|
|
||||||
expect(onCompleteSpy).to.has.been.called.exactly(1)
|
|
||||||
|
|
||||||
assert.isTrue(observer.completed)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
describe("#error()", () => {
|
|
||||||
|
|
||||||
it("should be able to emit a error", async () => {
|
|
||||||
const onErrorSpy = spy()
|
|
||||||
const observer = new SubscribableObserver()
|
|
||||||
observer.subscribe(undefined, undefined, onErrorSpy)
|
|
||||||
|
|
||||||
observer.error('test')
|
|
||||||
expect(onErrorSpy).to.has.been.called.with('test')
|
|
||||||
|
|
||||||
observer.error('test')
|
|
||||||
expect(onErrorSpy).to.has.been.called.exactly(1)
|
|
||||||
|
|
||||||
assert.isTrue(observer.completed)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
describe("SubscribablePromise", () => {
|
describe("SubscribablePromise", () => {
|
||||||
|
|
||||||
it("should work", async () => {
|
it("should work", async () => {
|
||||||
@ -91,19 +21,19 @@ describe("SubscribablePromise", () => {
|
|||||||
|
|
||||||
assert.isDefined(subscription)
|
assert.isDefined(subscription)
|
||||||
assert.isDefined(subscription.unsubscribe)
|
assert.isDefined(subscription.unsubscribe)
|
||||||
assert.typeOf(subscription.unsubscribe, 'function')
|
assert.typeOf(subscription.unsubscribe, "function")
|
||||||
})
|
})
|
||||||
|
|
||||||
it("should listen the next values", (done) => {
|
it("should listen the next values", (done) => {
|
||||||
const onNextSpy = spy()
|
const onNextSpy = spy()
|
||||||
const subscribible = new SubscribablePromise(observer => {
|
const subscribible = new SubscribablePromise((observer) => {
|
||||||
setTimeout(() => observer.next('test'), 10)
|
setTimeout(() => observer.next("test"), 10)
|
||||||
setTimeout(() => observer.next('test'), 20)
|
setTimeout(() => observer.next("test"), 20)
|
||||||
})
|
})
|
||||||
subscribible.subscribe(onNextSpy)
|
subscribible.subscribe(onNextSpy)
|
||||||
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
expect(onNextSpy).to.has.been.called.with('test')
|
expect(onNextSpy).to.has.been.called.with("test")
|
||||||
expect(onNextSpy).to.has.been.called.exactly(2)
|
expect(onNextSpy).to.has.been.called.exactly(2)
|
||||||
done()
|
done()
|
||||||
}, 100)
|
}, 100)
|
||||||
@ -115,9 +45,9 @@ describe("SubscribablePromise", () => {
|
|||||||
it("should resolve", (done) => {
|
it("should resolve", (done) => {
|
||||||
const onCompleteSpy = spy()
|
const onCompleteSpy = spy()
|
||||||
const onFinallySpy = spy()
|
const onFinallySpy = spy()
|
||||||
const subscribible = new SubscribablePromise(observer => {
|
const subscribible = new SubscribablePromise((observer) => {
|
||||||
setTimeout(() => observer.next('test'), 10)
|
setTimeout(() => observer.next("test"), 10)
|
||||||
setTimeout(() => observer.complete('test'), 20)
|
setTimeout(() => observer.complete("test"), 20)
|
||||||
})
|
})
|
||||||
|
|
||||||
subscribible
|
subscribible
|
||||||
@ -125,7 +55,7 @@ describe("SubscribablePromise", () => {
|
|||||||
.finally(onFinallySpy)
|
.finally(onFinallySpy)
|
||||||
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
expect(onCompleteSpy).to.has.been.called.with('test')
|
expect(onCompleteSpy).to.has.been.called.with("test")
|
||||||
expect(onCompleteSpy).to.has.been.called.exactly(1)
|
expect(onCompleteSpy).to.has.been.called.exactly(1)
|
||||||
expect(onFinallySpy).to.has.been.called.exactly(1)
|
expect(onFinallySpy).to.has.been.called.exactly(1)
|
||||||
done()
|
done()
|
||||||
@ -138,9 +68,9 @@ describe("SubscribablePromise", () => {
|
|||||||
it("should catch the error", (done) => {
|
it("should catch the error", (done) => {
|
||||||
const onErrorSpy = spy()
|
const onErrorSpy = spy()
|
||||||
const onFinallySpy = spy()
|
const onFinallySpy = spy()
|
||||||
const subscribible = new SubscribablePromise(observer => {
|
const subscribible = new SubscribablePromise((observer) => {
|
||||||
setTimeout(() => observer.next('test'), 10)
|
setTimeout(() => observer.next("test"), 10)
|
||||||
setTimeout(() => observer.error('test'), 20)
|
setTimeout(() => observer.error("test"), 20)
|
||||||
})
|
})
|
||||||
|
|
||||||
subscribible
|
subscribible
|
||||||
@ -148,7 +78,7 @@ describe("SubscribablePromise", () => {
|
|||||||
.finally(onFinallySpy)
|
.finally(onFinallySpy)
|
||||||
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
expect(onErrorSpy).to.has.been.called.with('test')
|
expect(onErrorSpy).to.has.been.called.with("test")
|
||||||
expect(onErrorSpy).to.has.been.called.exactly(1)
|
expect(onErrorSpy).to.has.been.called.exactly(1)
|
||||||
expect(onFinallySpy).to.has.been.called.exactly(1)
|
expect(onFinallySpy).to.has.been.called.exactly(1)
|
||||||
done()
|
done()
|
||||||
@ -158,38 +88,38 @@ describe("SubscribablePromise", () => {
|
|||||||
|
|
||||||
it("should be able to subscribe and wait for a promise", async () => {
|
it("should be able to subscribe and wait for a promise", async () => {
|
||||||
const onNextSpy = spy()
|
const onNextSpy = spy()
|
||||||
const subscribible = new SubscribablePromise(observer => {
|
const subscribible = new SubscribablePromise((observer) => {
|
||||||
setTimeout(() => observer.next('test'), 10)
|
setTimeout(() => observer.next("test"), 10)
|
||||||
setTimeout(() => observer.next('test'), 20)
|
setTimeout(() => observer.next("test"), 20)
|
||||||
setTimeout(() => observer.complete('completed'), 30)
|
setTimeout(() => observer.complete("completed"), 30)
|
||||||
})
|
})
|
||||||
|
|
||||||
const result = await subscribible
|
const result = await subscribible
|
||||||
.next(onNextSpy)
|
.next(onNextSpy)
|
||||||
|
|
||||||
expect(onNextSpy).to.has.been.called.with('test')
|
expect(onNextSpy).to.has.been.called.with("test")
|
||||||
expect(onNextSpy).to.has.been.called.exactly(2)
|
expect(onNextSpy).to.has.been.called.exactly(2)
|
||||||
|
|
||||||
assert.equal(result, 'completed')
|
assert.equal(result, "completed")
|
||||||
})
|
})
|
||||||
|
|
||||||
it("should use the result of a the promise as executor to complete the observer", async () => {
|
it("should use the result of a the promise as executor to complete the observer", async () => {
|
||||||
const onNextSpy = spy()
|
const onNextSpy = spy()
|
||||||
const subscribible = new SubscribablePromise(async observer => {
|
const subscribible = new SubscribablePromise(async (observer) => {
|
||||||
await new Promise(resolve => setTimeout(resolve, 10))
|
await new Promise((resolve) => setTimeout(resolve, 10))
|
||||||
observer.next('test')
|
observer.next("test")
|
||||||
await new Promise(resolve => setTimeout(resolve, 10))
|
await new Promise((resolve) => setTimeout(resolve, 10))
|
||||||
observer.next('test')
|
observer.next("test")
|
||||||
await new Promise(resolve => setTimeout(resolve, 10))
|
await new Promise((resolve) => setTimeout(resolve, 10))
|
||||||
return 'completed'
|
return "completed"
|
||||||
})
|
})
|
||||||
|
|
||||||
const result = await subscribible
|
const result = await subscribible
|
||||||
.next(onNextSpy)
|
.next(onNextSpy)
|
||||||
|
|
||||||
expect(onNextSpy).to.has.been.called.with('test')
|
expect(onNextSpy).to.has.been.called.with("test")
|
||||||
expect(onNextSpy).to.has.been.called.exactly(2)
|
expect(onNextSpy).to.has.been.called.exactly(2)
|
||||||
|
|
||||||
assert.equal(result, 'completed')
|
assert.equal(result, "completed")
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -12,6 +12,7 @@
|
|||||||
true,
|
true,
|
||||||
"never"
|
"never"
|
||||||
],
|
],
|
||||||
|
"no-empty": [true, "allow-empty-catch", "allow-empty-functions"],
|
||||||
"ordered-imports": false
|
"ordered-imports": false
|
||||||
},
|
},
|
||||||
"rulesDirectory": []
|
"rulesDirectory": []
|
||||||
|
Loading…
Reference in New Issue
Block a user