-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[breadboard] Start teaching Breadboard about streams.
- Loading branch information
Showing
5 changed files
with
97 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/** | ||
* @license | ||
* Copyright 2023 Google LLC | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import type { Capability, NodeValue } from "./types.js"; | ||
|
||
const STREAM_KIND = "stream" as const; | ||
|
||
export interface StreamCapabilityType<ChunkType = object> extends Capability { | ||
kind: typeof STREAM_KIND; | ||
stream: ReadableStream<ChunkType>; | ||
} | ||
|
||
export class StreamCapability<ChunkType> | ||
implements StreamCapabilityType<ChunkType> | ||
{ | ||
kind = STREAM_KIND; | ||
stream: ReadableStream<ChunkType>; | ||
|
||
constructor(stream: ReadableStream<ChunkType>) { | ||
this.stream = stream; | ||
} | ||
} | ||
|
||
const findStreams = (value: NodeValue, foundStreams: ReadableStream[]) => { | ||
if (Array.isArray(value)) { | ||
value.forEach((item: NodeValue) => { | ||
findStreams(item, foundStreams); | ||
}); | ||
} else if (typeof value === "object") { | ||
const maybeCapability = value as StreamCapabilityType; | ||
if (maybeCapability.kind && maybeCapability.kind === STREAM_KIND) { | ||
foundStreams.push(maybeCapability.stream); | ||
} else { | ||
Object.values(value as object).forEach((item) => { | ||
findStreams(item, foundStreams); | ||
}); | ||
} | ||
} | ||
}; | ||
|
||
export const getStreams = (value: NodeValue) => { | ||
const foundStreams: ReadableStream[] = []; | ||
findStreams(value, foundStreams); | ||
return foundStreams; | ||
}; |
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