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/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
- }