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

SSE: Add abort and closed methods #21

Merged
merged 5 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions _examples/node-ts/server/server.gen.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/* eslint-disable */
// node-ts v1.0.0 6713366104e62b8479d628a193e2a7ca03f37edc
// --
// Code generated by [email protected].0-1-g96d144e with ../../../gen-typescript generator. DO NOT EDIT.
// Code generated by [email protected].1 with ../../../gen-typescript generator. DO NOT EDIT.
//
// webrpc-gen -schema=service.ridl -target=../../../gen-typescript -server -out=./server/server.gen.ts

export const WebrpcHeader = "Webrpc"

export const WebrpcHeaderValue = "[email protected].0-1-g96d144e;gen-typescript@unknown;[email protected]"
export const WebrpcHeaderValue = "[email protected].1;gen-typescript@unknown;[email protected]"

// WebRPC description and code-gen version
export const WebRPCVersion = "v1"
Expand Down
4 changes: 2 additions & 2 deletions _examples/node-ts/webapp/client.gen.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/* eslint-disable */
// node-ts v1.0.0 6713366104e62b8479d628a193e2a7ca03f37edc
// --
// Code generated by [email protected].0-1-g96d144e with ../../../gen-typescript generator. DO NOT EDIT.
// Code generated by [email protected].1 with ../../../gen-typescript generator. DO NOT EDIT.
//
// webrpc-gen -schema=service.ridl -target=../../../gen-typescript -client -out=./webapp/client.gen.ts

export const WebrpcHeader = "Webrpc"

export const WebrpcHeaderValue = "[email protected].0-1-g96d144e;gen-typescript@unknown;[email protected]"
export const WebrpcHeaderValue = "[email protected].1;gen-typescript@unknown;[email protected]"

// WebRPC description and code-gen version
export const WebRPCVersion = "v1"
Expand Down
52 changes: 43 additions & 9 deletions _examples/sse/webapp/client.gen.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/* eslint-disable */
// webrpc-sse-chat v1.0.0 a799dc63b082644f5d003c8881424546aee23a2c
// --
// Code generated by [email protected].0-1-g96d144e with ../../ generator. DO NOT EDIT.
// Code generated by [email protected].1 with ../../ generator. DO NOT EDIT.
//
// webrpc-gen -schema=service.ridl -target=../../ -client -out=./webapp/client.gen.ts

export const WebrpcHeader = "Webrpc"

export const WebrpcHeaderValue = "[email protected].0-1-g96d144e;@unknown;[email protected]"
export const WebrpcHeaderValue = "[email protected].1;@unknown;[email protected]"

// WebRPC description and code-gen version
export const WebRPCVersion = "v1"
Expand Down Expand Up @@ -80,8 +80,8 @@ export interface Message {

export interface Chat {
sendMessage(args: SendMessageArgs, headers?: object, signal?: AbortSignal): Promise<SendMessageReturn>
subscribeMessages(args: SubscribeMessagesArgs, options: WebrpcStreamOptions<SubscribeMessagesReturn>): Promise<void>
subscribeUsers(options: WebrpcStreamOptions<SubscribeUsersReturn>): Promise<void>
subscribeMessages(args: SubscribeMessagesArgs, options: WebrpcStreamOptions<SubscribeMessagesReturn>): WebrpcStreamController
subscribeUsers(options: WebrpcStreamOptions<SubscribeUsersReturn>): WebrpcStreamController
}

export interface SendMessageArgs {
Expand Down Expand Up @@ -136,23 +136,51 @@ export class Chat implements Chat {
})
}

subscribeMessages = (args: SubscribeMessagesArgs, options: WebrpcStreamOptions<SubscribeMessagesReturn>): Promise<void> => {
const _fetch = () => this.fetch(this.url('SubscribeMessages'),createHTTPRequest(args, options.headers, options.signal)
subscribeMessages = (args: SubscribeMessagesArgs, options: WebrpcStreamOptions<SubscribeMessagesReturn>): WebrpcStreamController => {
const abortController = new AbortController();
const abortSignal = abortController.signal

if (options.signal) {
abortSignal.addEventListener("abort", () => abortController.abort(options.signal?.reason), {
signal: options.signal,
});
}

const _fetch = () => this.fetch(this.url('SubscribeMessages'),createHTTPRequest(args, options.headers, abortSignal)
).then(async (res) => {
await sseResponse(res, options, _fetch);
}, (error) => {
options.onError(error, _fetch);
});
return _fetch();

const resp = _fetch();
return {
abort: abortController.abort.bind(abortController),
closed: resp
};
}
subscribeUsers = (options: WebrpcStreamOptions<SubscribeUsersReturn>): Promise<void> => {
subscribeUsers = (options: WebrpcStreamOptions<SubscribeUsersReturn>): WebrpcStreamController => {
const abortController = new AbortController();
const abortSignal = abortController.signal

if (options.signal) {
abortSignal.addEventListener("abort", () => abortController.abort(options.signal?.reason), {
signal: options.signal,
});
}

const _fetch = () => this.fetch(this.url('SubscribeUsers'),createHTTPRequest({}, options.headers, options.signal)
).then(async (res) => {
await sseResponse(res, options, _fetch);
}, (error) => {
options.onError(error, _fetch);
});
return _fetch();

const resp = _fetch();
return {
abort: abortController.abort.bind(abortController),
closed: resp
};
}
}

Expand Down Expand Up @@ -549,8 +577,14 @@ export interface WebrpcStreamOptions<T> extends WebrpcOptions {
onOpen?: () => void;
onClose?: () => void;
}

export interface WebrpcOptions {
headers?: HeadersInit;
signal?: AbortSignal;
}

export interface WebrpcStreamController {
abort: (reason?: any) => void;
VojtechVitek marked this conversation as resolved.
Show resolved Hide resolved
closed: Promise<void>;
}

20 changes: 17 additions & 3 deletions client.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,30 @@ export class {{.Name}} implements {{.Name}} {
return this.hostname + this.path + name
}
{{range $_, $method := .Methods}}
{{firstLetterToLower .Name}} = ({{template "methodInputs" dict "Method" . "TypeMap" $typeMap}}): Promise<{{if $method.StreamOutput}}void{{else}}{{$method.Name}}Return{{end}}> => {
{{firstLetterToLower .Name}} = ({{template "methodInputs" dict "Method" . "TypeMap" $typeMap}}): {{if $method.StreamOutput}}WebrpcStreamController{{else}}Promise<{{$method.Name}}Return>{{end}} => {
{{- if $method.StreamOutput }}
const abortController = new AbortController();
const abortSignal = abortController.signal

if (options.signal) {
abortSignal.addEventListener("abort", () => abortController.abort(options.signal?.reason), {
signal: options.signal,
});
}

const _fetch = () => this.fetch(this.url('{{.Name}}'),
{{- if .Inputs | len }}createHTTPRequest(args, options.headers, options.signal){{- else}}createHTTPRequest({}, options.headers, options.signal){{end }}
{{- if .Inputs | len }}createHTTPRequest(args, options.headers, abortSignal){{- else}}createHTTPRequest({}, options.headers, options.signal){{end }}
).then(async (res) => {
await sseResponse(res, options, _fetch);
}, (error) => {
options.onError(error, _fetch);
});
return _fetch();

const resp = _fetch();
return {
abort: abortController.abort.bind(abortController),
closed: resp
};
}
{{- else }}
return this.fetch(
Expand Down
6 changes: 6 additions & 0 deletions clientHelpers.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,15 @@ export interface WebrpcStreamOptions<T> extends WebrpcOptions {
onOpen?: () => void;
onClose?: () => void;
}

export interface WebrpcOptions {
headers?: HeadersInit;
signal?: AbortSignal;
}

export interface WebrpcStreamController {
abort: (reason?: any) => void;
closed: Promise<void>;
}
{{end}}
{{end}}
2 changes: 1 addition & 1 deletion types.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export interface {{$type.Name}} {
{{- range $_, $service := $services}}
export interface {{$service.Name}} {
{{- range $_, $method := $service.Methods}}
{{firstLetterToLower $method.Name}}({{template "methodInputs" dict "Method" $method "TypeMap" $typeMap}}): Promise<{{if $method.StreamOutput}}void{{else}}{{$method.Name}}Return{{end}}>
{{firstLetterToLower $method.Name}}({{template "methodInputs" dict "Method" $method "TypeMap" $typeMap}}): {{if $method.StreamOutput}}WebrpcStreamController{{else}}Promise<{{$method.Name}}Return>{{end}}
{{- end}}
}

Expand Down
Loading