Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rewrite links with rxjs #1592

Closed
wants to merge 17 commits into from
9 changes: 5 additions & 4 deletions examples/express-server/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@examples/express-server",
"version": "10.0.0-alpha.9",
"version": "10.0.0-alpha.10",
"private": true,
"scripts": {
"dev:server": "nodemon -e ts -w . -x ts-node ./src/server",
Expand All @@ -12,13 +12,14 @@
"test-start": "start-server-and-test 'node dist/server' 2021 'node dist/client'"
},
"dependencies": {
"@trpc/client": "^10.0.0-alpha.9",
"@trpc/react": "^10.0.0-alpha.9",
"@trpc/server": "^10.0.0-alpha.9",
"@trpc/client": "^10.0.0-alpha.10",
"@trpc/react": "^10.0.0-alpha.10",
"@trpc/server": "^10.0.0-alpha.10",
"@types/node-fetch": "^2.5.11",
"abort-controller": "^3.0.0",
"express": "^4.17.1",
"node-fetch": "^2.6.1",
"rxjs": "^7.0.0",
"typescript": "4.4.4",
"zod": "^3.0.0"
},
Expand Down
2 changes: 1 addition & 1 deletion examples/express-server/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { loggerLink } from '@trpc/client/links/loggerLink';
import AbortController from 'abort-controller';
import fetch from 'node-fetch';
import type { AppRouter } from './server';
import { tap } from '@trpc/client/rx/operators';
import { tap } from 'rxjs';

// polyfill
global.AbortController = AbortController;
Expand Down
6 changes: 3 additions & 3 deletions examples/fastify-server/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@examples/fastify-server",
"version": "10.0.0-alpha.9",
"version": "10.0.0-alpha.10",
"private": true,
"scripts": {
"build": "tsc",
Expand All @@ -15,8 +15,8 @@
"test-start": "start-server-and-test 'node dist/server' http-get://localhost:2022 'node dist/client'"
},
"dependencies": {
"@trpc/client": "^10.0.0-alpha.9",
"@trpc/server": "^10.0.0-alpha.9",
"@trpc/client": "^10.0.0-alpha.10",
"@trpc/server": "^10.0.0-alpha.10",
"abort-controller": "^3.0.0",
"fastify": "^3.27.1",
"fastify-plugin": "^3.0.1",
Expand Down
4 changes: 2 additions & 2 deletions examples/fastify-server/src/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ async function start() {
let randomNumberCount = 0;

await new Promise<void>((resolve) => {
const unsub = anon.client.subscription('sub:randomNumber', null, {
const subscription = anon.client.subscription('sub:randomNumber', null, {
next(data) {
console.log('>>> anon:sub:randomNumber:received:', data);
randomNumberCount++;

if (randomNumberCount > 3) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've noticed this pattern in more than one spot. Consider using take(3) instead. It's the same basic idea.

unsub();
subscription.unsubscribe();
resolve();
}
},
Expand Down
10 changes: 5 additions & 5 deletions examples/next-prisma-starter-websockets/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@examples/next-starter-websockets",
"version": "10.0.0-alpha.9",
"version": "10.0.0-alpha.10",
"private": true,
"scripts": {
"build:1-generate": "prisma generate",
Expand Down Expand Up @@ -31,10 +31,10 @@
},
"dependencies": {
"@prisma/client": "^3.8.1",
"@trpc/client": "^10.0.0-alpha.9",
"@trpc/next": "^10.0.0-alpha.9",
"@trpc/react": "^10.0.0-alpha.9",
"@trpc/server": "^10.0.0-alpha.9",
"@trpc/client": "^10.0.0-alpha.10",
"@trpc/next": "^10.0.0-alpha.10",
"@trpc/react": "^10.0.0-alpha.10",
"@trpc/server": "^10.0.0-alpha.10",
"clsx": "^1.1.1",
"next": "^12.0.1",
"next-auth": "^3.27.3",
Expand Down
10 changes: 5 additions & 5 deletions examples/next-prisma-starter/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@examples/next-starter",
"version": "10.0.0-alpha.9",
"version": "10.0.0-alpha.10",
"private": true,
"scripts": {
"build:1-migrate": "prisma migrate deploy",
Expand Down Expand Up @@ -33,10 +33,10 @@
},
"dependencies": {
"@prisma/client": "^3.8.1",
"@trpc/client": "^10.0.0-alpha.9",
"@trpc/next": "^10.0.0-alpha.9",
"@trpc/react": "^10.0.0-alpha.9",
"@trpc/server": "^10.0.0-alpha.9",
"@trpc/client": "^10.0.0-alpha.10",
"@trpc/next": "^10.0.0-alpha.10",
"@trpc/react": "^10.0.0-alpha.10",
"@trpc/server": "^10.0.0-alpha.10",
"clsx": "^1.1.1",
"next": "^12.0.1",
"react": "^17.0.2",
Expand Down
1 change: 1 addition & 0 deletions examples/next-prisma-todomvc/next-env.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/// <reference types="next" />
/// <reference types="next/types/global" />
/// <reference types="next/image-types/global" />

// NOTE: This file should not be edited
Expand Down
10 changes: 5 additions & 5 deletions examples/next-prisma-todomvc/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@examples/todo-web",
"version": "10.0.0-alpha.9",
"version": "10.0.0-alpha.10",
"private": true,
"scripts": {
"migrate-sqlite": "npx prisma migrate dev --name init --schema=./prisma/_sqlite/schema.prisma",
Expand All @@ -24,10 +24,10 @@
},
"dependencies": {
"@prisma/client": "^3.8.1",
"@trpc/client": "^10.0.0-alpha.9",
"@trpc/next": "^10.0.0-alpha.9",
"@trpc/react": "^10.0.0-alpha.9",
"@trpc/server": "^10.0.0-alpha.9",
"@trpc/client": "^10.0.0-alpha.10",
"@trpc/next": "^10.0.0-alpha.10",
"@trpc/react": "^10.0.0-alpha.10",
"@trpc/server": "^10.0.0-alpha.10",
"clsx": "^1.1.1",
"jest": "^27.1.0",
"jest-playwright": "^0.0.1",
Expand Down
8 changes: 4 additions & 4 deletions examples/standalone-server/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@examples/standalone-server",
"version": "10.0.0-alpha.9",
"version": "10.0.0-alpha.10",
"private": true,
"scripts": {
"dev:server": "nodemon -e ts -w . -x ts-node ./src/server",
Expand All @@ -12,9 +12,9 @@
"test-start": "start-server-and-test 'node dist/server' 2022 'node dist/client'"
},
"dependencies": {
"@trpc/client": "^10.0.0-alpha.9",
"@trpc/react": "^10.0.0-alpha.9",
"@trpc/server": "^10.0.0-alpha.9",
"@trpc/client": "^10.0.0-alpha.10",
"@trpc/react": "^10.0.0-alpha.10",
"@trpc/server": "^10.0.0-alpha.10",
"@types/node-fetch": "^2.5.11",
"abort-controller": "^3.0.0",
"node-fetch": "^2.6.1",
Expand Down
11 changes: 5 additions & 6 deletions examples/standalone-server/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,22 @@ async function main() {
console.log('createPostResponse', createPostRes);

let count = 0;
const unsub = client.subscription('randomNumber', null, {
const subscription = client.subscription('randomNumber', null, {
next(data) {
// ^ note that `data` here is inferred
console.log('received', data);
count++;
if (count > 3) {
// stop after 3 pulls
unsub();
subscription.unsubscribe();

console.log('unsubscribing and closing websocket');
wsClient.close();
}
},
error(err) {
console.error('error', err);
},
complete() {
console.log('completed called - closing websocket');
wsClient.close();
},
});
}

Expand Down
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "10.0.0-alpha.9",
"version": "10.0.0-alpha.10",
"registry": "https://registry.npmjs.org/",
"publishConfig": {
"access": "public"
Expand Down
25 changes: 10 additions & 15 deletions packages/client/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@trpc/client",
"version": "10.0.0-alpha.9",
"version": "10.0.0-alpha.10",
"description": "tRPC Client lib",
"author": "KATT",
"license": "MIT",
Expand All @@ -19,6 +19,11 @@
"require": "./dist/index.js",
"default": "./dist/index.js"
},
"./rx": {
"import": "./dist/rx.mjs",
"require": "./dist/rx.js",
"default": "./dist/rx.js"
},
"./links/httpBatchLink": {
"import": "./dist/links/httpBatchLink.mjs",
"require": "./dist/links/httpBatchLink.js",
Expand Down Expand Up @@ -48,36 +53,26 @@
"import": "./dist/links/wsLink.mjs",
"require": "./dist/links/wsLink.js",
"default": "./dist/links/wsLink.js"
},
"./rx": {
"import": "./dist/rx/index.mjs",
"require": "./dist/rx/index.js",
"default": "./dist/rx/index.js"
},
"./rx/operators": {
"import": "./dist/rx/operators/index.mjs",
"require": "./dist/rx/operators/index.js",
"default": "./dist/rx/operators/index.js"
}
},
"files": [
"README.md",
"dist",
"links",
"src",
"rx"
"src"
],
"scripts": {
"build": "rollup -c"
},
"dependencies": {
"@babel/runtime": "^7.9.0"
"@babel/runtime": "^7.9.0",
"rxjs": "^7.0.0"
sachinraja marked this conversation as resolved.
Show resolved Hide resolved
},
"peerDependencies": {
"@trpc/server": "^9.12.0"
},
"devDependencies": {
"@trpc/server": "^10.0.0-alpha.9"
"@trpc/server": "^10.0.0-alpha.10"
},
"publishConfig": {
"access": "public"
Expand Down
3 changes: 1 addition & 2 deletions packages/client/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ import { getRollupConfig } from '../../scripts/rollup';
const config = getRollupConfig({
input: [
'src/index.ts',
'src/rx.ts',
'src/links/httpBatchLink.ts',
'src/links/httpLink.ts',
'src/links/loggerLink.ts',
'src/links/splitLink.ts',
'src/links/transformerLink.ts',
'src/links/wsLink.ts',
'src/rx/index.ts',
'src/rx/operators/index.ts',
],
});

Expand Down
2 changes: 1 addition & 1 deletion packages/client/rx/index.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = require('../dist/rx/index');
module.exports = require('../dist/rx');
1 change: 0 additions & 1 deletion packages/client/rx/operators/index.d.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/client/rx/operators/index.js

This file was deleted.

18 changes: 6 additions & 12 deletions packages/client/src/internals/TRPCClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ import {
OperationLink,
TRPCLink,
} from '../links/types';
import { UnsubscribeFn } from '../rx';
import { inferObservableValue } from '../rx/observable';
import { share } from '../rx/operators';
import { Observer } from '../rx/types';
import { observableToPromise } from '../rx/util/observableToPromise';
import { inferObservableValue, observableToPromise } from '../rx';
import { share } from 'rxjs/operators';
sachinraja marked this conversation as resolved.
Show resolved Hide resolved
import { Observer, Subscription } from 'rxjs';
import { TRPCClientError } from '../TRPCClientError';
import { getAbortController } from './fetchHelpers';

Expand Down Expand Up @@ -216,16 +214,15 @@ export class TRPCClient<TRouter extends AnyRouter> {
>(
path: TPath,
input: TInput,
opts: TRPCRequestOptions &
Partial<Observer<TRPCResult<TOutput>, TRPCClientError<TRouter>>>,
): UnsubscribeFn {
opts: TRPCRequestOptions & Partial<Observer<TRPCResult<TOutput>>>,
): Subscription {
const observable$ = this.$request<TInput, TOutput>({
type: 'subscription',
path,
input,
context: opts.context,
});
const observer = observable$.subscribe({
return observable$.subscribe({
next(result) {
if ('error' in result.data) {
const err = TRPCClientError.from(result.data, {
Expand All @@ -244,8 +241,5 @@ export class TRPCClient<TRouter extends AnyRouter> {
opts.complete?.();
},
});
return () => {
observer.unsubscribe();
};
}
}
6 changes: 3 additions & 3 deletions packages/client/src/links/dedupeLink.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { waitFor } from '@testing-library/dom';
import { AnyRouter } from '@trpc/server';
import { OperationLink } from '..';
import { observable } from '../rx/observable';
import { Observable } from 'rxjs';
import { dedupeLink } from './dedupeLink';
import { createChain } from './internals/createChain';

Expand All @@ -12,7 +12,7 @@ test('dedupeLink', async () => {
// "dedupe link"
dedupeLink()(null as any),
({ op }) => {
return observable((subscribe) => {
return new Observable((subscribe) => {
endingLinkTriggered();
const timer = setTimeout(() => {
timerTriggered();
Expand Down Expand Up @@ -75,7 +75,7 @@ test('dedupe - cancel one does not cancel the other', async () => {
// "dedupe link"
dedupeLink()(null as any),
({ op }) => {
return observable((subscribe) => {
return new Observable((subscribe) => {
endingLinkTriggered();
const timer = setTimeout(() => {
timerTriggered();
Expand Down