Skip to content

Commit

Permalink
fix: replace Window type with reduced WindowLike type
Browse files Browse the repository at this point in the history
If comlink is used with typescript, where only compilerOptions.lib "webworker" without "dom"
is configured, the error "Cannot find name 'Window'" is thrown. This PR replaces the Window
type with a custom type WindowLike that declares the minimal required functionality to work.
  • Loading branch information
kyubisation committed Jun 15, 2019
1 parent ce7cd97 commit 9508f29
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
8 changes: 6 additions & 2 deletions src/comlink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import {
Message,
MessageType,
WireValue,
WireValueType
WireValueType,
WindowLike
} from "./protocol.js";
export { Endpoint };

Expand Down Expand Up @@ -245,7 +246,10 @@ export function proxy<T>(obj: T): T & { [proxyMarker]: true } {
return Object.assign(obj, { [proxyMarker]: true }) as any;
}

export function windowEndpoint(w: Window, context = self): Endpoint {
export function windowEndpoint(
w: WindowLike,
context: WindowLike = self
): Endpoint {
return {
postMessage: (msg: any, transferables: Transferable[]) =>
w.postMessage(msg, "*", transferables),
Expand Down
15 changes: 13 additions & 2 deletions src/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
* limitations under the License.
*/

export interface Endpoint {
postMessage(message: any, transfer?: Transferable[]): void;
export interface EventSource {
addEventListener(
type: string,
listener: EventListenerOrEventListenerObject,
Expand All @@ -23,6 +22,18 @@ export interface Endpoint {
listener: EventListenerOrEventListenerObject,
options?: {}
): void;
}

export interface WindowLike extends EventSource {
postMessage(
message: any,
targetOrigin: string,
transfer?: Transferable[]
): void;
}

export interface Endpoint extends EventSource {
postMessage(message: any, transfer?: Transferable[]): void;
start?: () => void;
}

Expand Down

0 comments on commit 9508f29

Please sign in to comment.