-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Upgrade bindings, add streaming to Browser SDK (#825)
* Upgrade bindings * Remove pinnedFrameUrl from Node SDK * Remove pinnedFrameUrl from Browser SDK * Remove pinnedFrameUrl from dev tool * Add streaming to Browser SDK * Create slow-rocks-marry.md * Increase test timeout * Refactor browser SDK streaming tests
- Loading branch information
Showing
24 changed files
with
829 additions
and
313 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@xmtp/browser-sdk": patch | ||
"@xmtp/node-sdk": patch | ||
--- | ||
|
||
Upgrade bindings, add streaming to Browser SDK |
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
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,80 @@ | ||
type ResolveValue<T> = { | ||
value: T | undefined; | ||
done: boolean; | ||
}; | ||
|
||
type ResolveNext<T> = (resolveValue: ResolveValue<T>) => void; | ||
|
||
export type StreamCallback<T> = ( | ||
err: Error | null, | ||
value: T | undefined, | ||
) => void | Promise<void>; | ||
|
||
export class AsyncStream<T> { | ||
#done = false; | ||
#resolveNext: ResolveNext<T> | null; | ||
#queue: (T | undefined)[]; | ||
|
||
onReturn: (() => void) | undefined = undefined; | ||
|
||
constructor() { | ||
this.#queue = []; | ||
this.#resolveNext = null; | ||
this.#done = false; | ||
} | ||
|
||
get isDone() { | ||
return this.#done; | ||
} | ||
|
||
callback: StreamCallback<T> = (error, value) => { | ||
if (error) { | ||
throw error; | ||
} | ||
|
||
if (this.#done) { | ||
return; | ||
} | ||
|
||
if (this.#resolveNext) { | ||
this.#resolveNext({ | ||
done: false, | ||
value, | ||
}); | ||
this.#resolveNext = null; | ||
} else { | ||
this.#queue.push(value); | ||
} | ||
}; | ||
|
||
next = (): Promise<ResolveValue<T>> => { | ||
if (this.#queue.length > 0) { | ||
return Promise.resolve({ | ||
done: false, | ||
value: this.#queue.shift(), | ||
}); | ||
} else if (this.#done) { | ||
return Promise.resolve({ | ||
done: true, | ||
value: undefined, | ||
}); | ||
} else { | ||
return new Promise((resolve) => { | ||
this.#resolveNext = resolve; | ||
}); | ||
} | ||
}; | ||
|
||
return = (value: T | undefined) => { | ||
this.#done = true; | ||
this.onReturn?.(); | ||
return Promise.resolve({ | ||
done: true, | ||
value, | ||
}); | ||
}; | ||
|
||
[Symbol.asyncIterator]() { | ||
return this; | ||
} | ||
} |
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
Oops, something went wrong.