-
-
Notifications
You must be signed in to change notification settings - Fork 822
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use in-house pubsub to refactor receiver
- Loading branch information
Showing
8 changed files
with
224 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{ | ||
"name": "@graphql-tools/pubsub", | ||
"version": "7.0.0", | ||
"description": "A set of utils for faster development of GraphQL tools", | ||
"repository": { | ||
"type": "git", | ||
"url": "ardatan/graphql-tools", | ||
"directory": "packages/batch-delegate" | ||
}, | ||
"license": "MIT", | ||
"sideEffects": false, | ||
"main": "dist/index.cjs.js", | ||
"module": "dist/index.esm.js", | ||
"typings": "dist/index.d.ts", | ||
"typescript": { | ||
"definition": "dist/index.d.ts" | ||
}, | ||
"peerDependencies": { | ||
"graphql": "^14.0.0 || ^15.0.0" | ||
}, | ||
"buildOptions": { | ||
"input": "./src/index.ts" | ||
}, | ||
"dependencies": { | ||
"@repeaterjs/repeater": "^3.0.4" | ||
}, | ||
"publishConfig": { | ||
"access": "public", | ||
"directory": "dist" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// adapted from https://github.com/repeaterjs/repeater/blob/7b294acfa7e2c21721ff77018cf77c452c51dad9/packages/pubsub/src/pubsub.ts | ||
// adapted rather than importing @repeaterjs/pubsub | ||
// because of https://github.com/repeaterjs/repeater/issues/67 in which pubsub will be killed! | ||
|
||
import { Repeater, RepeaterBuffer } from '@repeaterjs/repeater'; | ||
|
||
import { Channel } from './types'; | ||
|
||
interface Hooks<T> { | ||
push(value: T): Promise<unknown>; | ||
stop(reason?: any): unknown; | ||
} | ||
|
||
export class InMemoryChannel<T> implements Channel<T> { | ||
protected hooks: Set<Hooks<T>> = new Set(); | ||
|
||
publish(value: T): void { | ||
const hooks = this.hooks; | ||
|
||
for (const { push, stop } of hooks) { | ||
try { | ||
push(value).catch(stop); | ||
} catch (err) { | ||
// push queue is full | ||
stop(err); | ||
} | ||
} | ||
} | ||
|
||
unpublish(reason?: any): void { | ||
const hooks = this.hooks; | ||
|
||
for (const { stop } of hooks) { | ||
stop(reason); | ||
} | ||
|
||
hooks.clear(); | ||
} | ||
|
||
subscribe(buffer?: RepeaterBuffer): Repeater<T> { | ||
return new Repeater<T>(async (push, stop) => { | ||
const publisher = { push, stop }; | ||
this.hooks.add(publisher); | ||
await stop; | ||
this.hooks.delete(publisher); | ||
}, buffer); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// adapted from https://github.com/repeaterjs/repeater/blob/7b294acfa7e2c21721ff77018cf77c452c51dad9/packages/pubsub/src/pubsub.ts | ||
// adapted rather than importing @repeaterjs/pubsub | ||
// because of https://github.com/repeaterjs/repeater/issues/67 in which pubsub will be killed! | ||
|
||
import { Repeater, RepeaterBuffer } from '@repeaterjs/repeater'; | ||
import { InMemoryChannel } from './in-memory-channel'; | ||
|
||
import { PubSub } from './types'; | ||
|
||
export class InMemoryPubSub<T> implements PubSub<T> { | ||
protected channels: Record<string, InMemoryChannel<T>> = Object.create(null); | ||
|
||
publish(topic: string, value: T): void { | ||
let channel = this.channels[topic]; | ||
|
||
if (channel == null) { | ||
channel = this.channels[topic] = new InMemoryChannel(); | ||
} | ||
|
||
channel.publish(value); | ||
} | ||
|
||
unpublish(topic: string, reason?: any): void { | ||
const channel = this.channels[topic]; | ||
|
||
if (channel == null) { | ||
return; | ||
} | ||
|
||
channel.unpublish(reason); | ||
|
||
delete this.channels[topic]; | ||
} | ||
|
||
subscribe(topic: string, buffer?: RepeaterBuffer): Repeater<T> { | ||
let channel = this.channels[topic]; | ||
|
||
if (this.channels[topic] == null) { | ||
channel = this.channels[topic] = new InMemoryChannel(); | ||
} | ||
|
||
return channel.subscribe(buffer); | ||
} | ||
|
||
close(reason?: any): void { | ||
for (const channel of Object.values(this.channels)) { | ||
channel.unpublish(reason); | ||
} | ||
|
||
this.channels = Object.create(null); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export * from './in-memory-channel'; | ||
export * from './in-memory-pubsub'; | ||
|
||
export * from './types'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { RepeaterBuffer } from '@repeaterjs/repeater'; | ||
|
||
export interface Channel<T> { | ||
publish(value: T): Promise<unknown> | unknown; | ||
unpublish(reason?: any): Promise<unknown> | unknown; | ||
subscribe(buffer?: RepeaterBuffer): AsyncIterableIterator<T>; | ||
} | ||
|
||
export interface PubSub<T> { | ||
publish(topic: string, value: T): Promise<unknown> | unknown; | ||
unpublish(topic: string, reason?: any): Promise<unknown> | unknown; | ||
subscribe(topic: string, buffer?: RepeaterBuffer): AsyncIterableIterator<T>; | ||
close(reason?: any): Promise<unknown> | unknown; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2781,6 +2781,11 @@ | |
resolved "https://registry.yarnpkg.com/@polka/url/-/url-1.0.0-next.11.tgz#aeb16f50649a91af79dbe36574b66d0f9e4d9f71" | ||
integrity sha512-3NsZsJIA/22P3QUyrEDNA2D133H4j224twJrdipXN38dpnIOzAbUDtOwkcJ5pXmn75w7LSQDjA4tO9dm1XlqlA== | ||
|
||
"@repeaterjs/repeater@^3.0.4": | ||
version "3.0.4" | ||
resolved "https://registry.yarnpkg.com/@repeaterjs/repeater/-/repeater-3.0.4.tgz#a04d63f4d1bf5540a41b01a921c9a7fddc3bd1ca" | ||
integrity sha512-AW8PKd6iX3vAZ0vA43nOUOnbq/X5ihgU+mSXXqunMkeQADGiqw/PY0JNeYtD5sr0PAy51YPgAPbDoeapv9r8WA== | ||
|
||
"@rollup/[email protected]": | ||
version "7.1.1" | ||
resolved "https://registry.yarnpkg.com/@rollup/plugin-node-resolve/-/plugin-node-resolve-7.1.1.tgz#8c6e59c4b28baf9d223028d0e450e06a485bb2b7" | ||
|
@@ -7806,7 +7811,7 @@ [email protected]: | |
dependencies: | ||
tslib "~2.0.3" | ||
|
||
[email protected], graphql-subscriptions@^1.1.0: | ||
[email protected]: | ||
version "1.2.0" | ||
resolved "https://registry.yarnpkg.com/graphql-subscriptions/-/graphql-subscriptions-1.2.0.tgz#d82ff76e7504ac91acbbea15f36cd3904043937b" | ||
integrity sha512-uXvp729fztqwa7HFUFaAqKwNMwwOfsvu4HwOu7/35Cd44bNrMPCn97mNGN0ybuuZE36CPXBTaW/4U/xyOS4D9w== | ||
|