graphql-pub-sub-with-initial-value 2.0.0 → 2.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +10 -10
- package/.github/FUNDING.yml +0 -3
- package/.github/workflows/push.yml +0 -24
- package/.parcel-cache/13f2d5707e7af45c-RequestGraph +0 -0
- package/.parcel-cache/4550cd402a6bc1d6-RequestGraph +0 -0
- package/.parcel-cache/5ae0570a78c0dba3-AssetGraph +0 -0
- package/.parcel-cache/9ac092379278e465-BundleGraph +0 -0
- package/.parcel-cache/c23923128c744515-AssetGraph +0 -0
- package/.parcel-cache/data.mdb +0 -0
- package/.parcel-cache/e1c7b57f833f142e-AssetGraph +0 -0
- package/.parcel-cache/f8091f1d3b644a38-BundleGraph +0 -0
- package/.parcel-cache/ff30a583416c3463-AssetGraph +0 -0
- package/.parcel-cache/lock.mdb +0 -0
- package/.parcel-cache/snapshot-13f2d5707e7af45c.txt +0 -12561
- package/.parcel-cache/snapshot-4550cd402a6bc1d6.txt +0 -12561
- package/example/index.js +0 -22
- package/src/index.ts +0 -62
package/example/index.js
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { PubSubWithIntialValue, withCancel } from '../lib/graphql-pub-sub-with-initial-value.mjs'
|
|
2
|
-
|
|
3
|
-
const asyncInititalValueFn = () => Promise.resolve(['test'])
|
|
4
|
-
|
|
5
|
-
const main = async () => {
|
|
6
|
-
const pubSubInit = new PubSubWithIntialValue()
|
|
7
|
-
const iterator = pubSubInit.asyncIteratorWithInitialValue('TOPIC', asyncInititalValueFn)
|
|
8
|
-
const data = await iterator.next()
|
|
9
|
-
|
|
10
|
-
console.log(data)
|
|
11
|
-
|
|
12
|
-
// cb is executed, if client unsubscribes
|
|
13
|
-
pubSubInit.withCancel(iterator, () => console.log('disconnected'))
|
|
14
|
-
// or
|
|
15
|
-
withCancel(iterator, () => console.log('disconnected'))
|
|
16
|
-
// done!
|
|
17
|
-
iterator.return()
|
|
18
|
-
|
|
19
|
-
process.exit(0)
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
main()
|
package/src/index.ts
DELETED
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
import { PubSub } from 'graphql-subscriptions'
|
|
2
|
-
|
|
3
|
-
export interface AsyncIterableIteratorWithInitialState<T> extends AsyncIterableIterator<T> {
|
|
4
|
-
initialValuePushed: boolean
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
export const withCancel = <T, P>(
|
|
8
|
-
asyncIterator: AsyncIterator<T | undefined>,
|
|
9
|
-
onCancel: (args?: P) => void,
|
|
10
|
-
args?: P
|
|
11
|
-
): AsyncIterator<T | undefined> => {
|
|
12
|
-
if (!asyncIterator.return) {
|
|
13
|
-
asyncIterator.return = () => Promise.resolve({
|
|
14
|
-
done: true,
|
|
15
|
-
value: undefined
|
|
16
|
-
})
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
const savedReturn = asyncIterator.return.bind(asyncIterator)
|
|
20
|
-
asyncIterator.return = () => {
|
|
21
|
-
onCancel(args)
|
|
22
|
-
return savedReturn()
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
return asyncIterator
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export class PubSubWithIntialValue extends PubSub {
|
|
29
|
-
private withInitialValue<T>(iterator: AsyncIterator<T>, getInitialValue: () => Promise<T>): AsyncIterableIteratorWithInitialState<T> {
|
|
30
|
-
return {
|
|
31
|
-
[Symbol.asyncIterator](): AsyncIterableIterator<T> { return this },
|
|
32
|
-
initialValuePushed: false,
|
|
33
|
-
next(value?: any): Promise<IteratorResult<T>> {
|
|
34
|
-
if (this.initialValuePushed) {
|
|
35
|
-
return iterator.next(value).then(({ value, done }: IteratorResult<T>): IteratorResult<T> => ({
|
|
36
|
-
done,
|
|
37
|
-
value
|
|
38
|
-
}))
|
|
39
|
-
} else {
|
|
40
|
-
return getInitialValue().then((value: T): IteratorResult<T> => {
|
|
41
|
-
this.initialValuePushed = true
|
|
42
|
-
return { done: false, value }
|
|
43
|
-
})
|
|
44
|
-
}
|
|
45
|
-
},
|
|
46
|
-
return(value?: any): Promise<IteratorResult<T>> {
|
|
47
|
-
return iterator.return!(value)
|
|
48
|
-
},
|
|
49
|
-
throw(value?: any): Promise<IteratorResult<T>> {
|
|
50
|
-
return iterator.throw!(value)
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
public asyncIteratorWithInitialValue<T>(topic: string | string[], initialValueFn: () => Promise<T>): AsyncIterableIteratorWithInitialState<T> {
|
|
56
|
-
return this.withInitialValue(this.asyncIterableIterator<T>(topic), (): Promise<T> => initialValueFn())
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
public withCancel<T, P>(asyncIterator: AsyncIterator<T | undefined>, onCancel: (args?: P) => void, args?: P): AsyncIterator<T, any, undefined> {
|
|
60
|
-
return withCancel<T, P>(asyncIterator, onCancel, args)
|
|
61
|
-
}
|
|
62
|
-
}
|