-
Notifications
You must be signed in to change notification settings - Fork 6
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
Add injected isomorphic environment #4
Merged
Merged
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
1f8041c
isomorphic environment
Monkatraz a63cf42
add test environment stuff
Monkatraz f92fa3d
add declaration merging comment
Monkatraz 4b71bfc
remove unneeded helper
Monkatraz 91557b8
typo
Monkatraz 1c6c2bf
use a context map
Monkatraz d847417
use extended context concept instead
Monkatraz 50ac678
whoops omit state from being required
Monkatraz 5770520
whoops again
Monkatraz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export interface IsomorphicEnvironment { | ||
log: (...args: unknown[]) => void; | ||
error: (...args: unknown[]) => void; | ||
} |
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 |
---|---|---|
@@ -1,10 +1,16 @@ | ||
import { TObject } from '@sinclair/typebox'; | ||
import { Transport } from '../transport/types'; | ||
import { AnyService, Procedure, ValidProcType } from './builder'; | ||
import { | ||
AnyService, | ||
Procedure, | ||
ProcedureContext, | ||
ValidProcType, | ||
} from './builder'; | ||
import { Value } from '@sinclair/typebox/value'; | ||
import { pushable } from 'it-pushable'; | ||
import type { Pushable } from 'it-pushable'; | ||
import { OpaqueTransportMessage, TransportMessage } from '../transport/message'; | ||
import { IsomorphicEnvironment } from '../environment/types'; | ||
|
||
export interface Server<Services> { | ||
services: Services; | ||
|
@@ -18,12 +24,18 @@ interface ProcStream { | |
} | ||
|
||
export async function createServer<Services extends Record<string, AnyService>>( | ||
environment: IsomorphicEnvironment, | ||
transport: Transport, | ||
services: Services, | ||
): Promise<Server<Services>> { | ||
// create streams for every stream procedure | ||
const streamMap: Map<string, ProcStream> = new Map(); | ||
for (const [serviceName, service] of Object.entries(services)) { | ||
const context: ProcedureContext<object> = { | ||
environment, | ||
state: service.state, | ||
}; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. isn't this a |
||
for (const [procedureName, proc] of Object.entries(service.procedures)) { | ||
const procedure = proc as Procedure< | ||
object, | ||
|
@@ -39,7 +51,7 @@ export async function createServer<Services extends Record<string, AnyService>>( | |
outgoing, | ||
doneCtx: Promise.all([ | ||
// processing the actual procedure | ||
procedure.handler(service.state, incoming, outgoing), | ||
procedure.handler(context, incoming, outgoing), | ||
// sending outgoing messages back to client | ||
(async () => { | ||
for await (const response of outgoing) { | ||
|
@@ -78,7 +90,11 @@ export async function createServer<Services extends Record<string, AnyService>>( | |
Value.Check(procedure.input, inputMessage.payload) | ||
) { | ||
// synchronous rpc | ||
const response = await procedure.handler(service.state, inputMessage); | ||
const context: ProcedureContext<object> = { | ||
environment, | ||
state: service.state, | ||
}; | ||
const response = await procedure.handler(context, inputMessage); | ||
transport.send(response); | ||
return; | ||
} else if ( | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we add a comment here about declaration merging and then add a test that adds something to the environment?