@prismicio/simulator 0.1.4 → 0.2.0-canary.a0bdcb9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -18
- package/dist/SimulatorAPI.cjs +36 -46
- package/dist/SimulatorAPI.cjs.map +1 -1
- package/dist/SimulatorAPI.d.cts +18 -0
- package/dist/SimulatorAPI.d.cts.map +1 -0
- package/dist/SimulatorAPI.d.ts +16 -10
- package/dist/SimulatorAPI.d.ts.map +1 -0
- package/dist/SimulatorAPI.js +35 -46
- package/dist/SimulatorAPI.js.map +1 -1
- package/dist/SimulatorClient.cjs +39 -51
- package/dist/SimulatorClient.cjs.map +1 -1
- package/dist/SimulatorClient.d.cts +16 -0
- package/dist/SimulatorClient.d.cts.map +1 -0
- package/dist/SimulatorClient.d.ts +15 -9
- package/dist/SimulatorClient.d.ts.map +1 -0
- package/dist/SimulatorClient.js +39 -52
- package/dist/SimulatorClient.js.map +1 -1
- package/dist/channel/ChannelEmitter.cjs +109 -143
- package/dist/channel/ChannelEmitter.cjs.map +1 -1
- package/dist/channel/ChannelEmitter.d.cts +36 -0
- package/dist/channel/ChannelEmitter.d.cts.map +1 -0
- package/dist/channel/ChannelEmitter.d.ts +34 -36
- package/dist/channel/ChannelEmitter.d.ts.map +1 -0
- package/dist/channel/ChannelEmitter.js +107 -141
- package/dist/channel/ChannelEmitter.js.map +1 -1
- package/dist/channel/ChannelNetwork.cjs +80 -109
- package/dist/channel/ChannelNetwork.cjs.map +1 -1
- package/dist/channel/ChannelNetwork.d.cts +28 -0
- package/dist/channel/ChannelNetwork.d.cts.map +1 -0
- package/dist/channel/ChannelNetwork.d.ts +25 -21
- package/dist/channel/ChannelNetwork.d.ts.map +1 -0
- package/dist/channel/ChannelNetwork.js +80 -109
- package/dist/channel/ChannelNetwork.js.map +1 -1
- package/dist/channel/ChannelReceiver.cjs +68 -92
- package/dist/channel/ChannelReceiver.cjs.map +1 -1
- package/dist/channel/ChannelReceiver.d.cts +20 -0
- package/dist/channel/ChannelReceiver.d.cts.map +1 -0
- package/dist/channel/ChannelReceiver.d.ts +18 -18
- package/dist/channel/ChannelReceiver.d.ts.map +1 -0
- package/dist/channel/ChannelReceiver.js +66 -90
- package/dist/channel/ChannelReceiver.js.map +1 -1
- package/dist/channel/errors.cjs +40 -46
- package/dist/channel/errors.cjs.map +1 -1
- package/dist/channel/errors.d.cts +28 -0
- package/dist/channel/errors.d.cts.map +1 -0
- package/dist/channel/errors.d.ts +22 -19
- package/dist/channel/errors.d.ts.map +1 -0
- package/dist/channel/errors.js +40 -52
- package/dist/channel/errors.js.map +1 -1
- package/dist/channel/messages.cjs +47 -43
- package/dist/channel/messages.cjs.map +1 -1
- package/dist/channel/messages.d.cts +11 -0
- package/dist/channel/messages.d.cts.map +1 -0
- package/dist/channel/messages.d.ts +11 -10
- package/dist/channel/messages.d.ts.map +1 -0
- package/dist/channel/messages.js +50 -53
- package/dist/channel/messages.js.map +1 -1
- package/dist/channel/types.cjs +14 -11
- package/dist/channel/types.cjs.map +1 -1
- package/dist/channel/types.d.cts +53 -0
- package/dist/channel/types.d.cts.map +1 -0
- package/dist/channel/types.d.ts +42 -50
- package/dist/channel/types.d.ts.map +1 -0
- package/dist/channel/types.js +13 -13
- package/dist/channel/types.js.map +1 -1
- package/dist/globalExtensions.d.cts +15 -0
- package/dist/globalExtensions.d.cts.map +1 -0
- package/dist/globalExtensions.d.ts +14 -11
- package/dist/globalExtensions.d.ts.map +1 -0
- package/dist/index.cjs +23 -32
- package/dist/index.d.cts +9 -0
- package/dist/index.d.ts +9 -8
- package/dist/index.js +3 -24
- package/dist/kit/SimulatorManager.cjs +103 -137
- package/dist/kit/SimulatorManager.cjs.map +1 -1
- package/dist/kit/SimulatorManager.d.cts +19 -0
- package/dist/kit/SimulatorManager.d.cts.map +1 -0
- package/dist/kit/SimulatorManager.d.ts +16 -12
- package/dist/kit/SimulatorManager.d.ts.map +1 -0
- package/dist/kit/SimulatorManager.js +101 -135
- package/dist/kit/SimulatorManager.js.map +1 -1
- package/dist/kit/State.cjs +81 -80
- package/dist/kit/State.cjs.map +1 -1
- package/dist/kit/State.d.cts +29 -0
- package/dist/kit/State.d.cts.map +1 -0
- package/dist/kit/State.d.ts +26 -22
- package/dist/kit/State.d.ts.map +1 -0
- package/dist/kit/State.js +80 -79
- package/dist/kit/State.js.map +1 -1
- package/dist/kit/domHelpers.cjs +24 -37
- package/dist/kit/domHelpers.cjs.map +1 -1
- package/dist/kit/domHelpers.d.cts +11 -0
- package/dist/kit/domHelpers.d.cts.map +1 -0
- package/dist/kit/domHelpers.d.ts +10 -8
- package/dist/kit/domHelpers.d.ts.map +1 -0
- package/dist/kit/domHelpers.js +25 -43
- package/dist/kit/domHelpers.js.map +1 -1
- package/dist/kit/eventHandlers.cjs +10 -9
- package/dist/kit/eventHandlers.cjs.map +1 -1
- package/dist/kit/eventHandlers.d.cts +8 -0
- package/dist/kit/eventHandlers.d.cts.map +1 -0
- package/dist/kit/eventHandlers.d.ts +7 -3
- package/dist/kit/eventHandlers.d.ts.map +1 -0
- package/dist/kit/eventHandlers.js +11 -11
- package/dist/kit/eventHandlers.js.map +1 -1
- package/dist/kit/getDefault.cjs +8 -7
- package/dist/kit/getDefault.cjs.map +1 -1
- package/dist/kit/getDefault.d.cts +10 -0
- package/dist/kit/getDefault.d.cts.map +1 -0
- package/dist/kit/getDefault.d.ts +10 -8
- package/dist/kit/getDefault.d.ts.map +1 -0
- package/dist/kit/getDefault.js +9 -10
- package/dist/kit/getDefault.js.map +1 -1
- package/dist/kit/messages.cjs +7 -24
- package/dist/kit/messages.cjs.map +1 -1
- package/dist/kit/messages.d.cts +5 -0
- package/dist/kit/messages.d.cts.map +1 -0
- package/dist/kit/messages.d.ts +5 -1
- package/dist/kit/messages.d.ts.map +1 -0
- package/dist/kit/messages.js +8 -25
- package/dist/kit/messages.js.map +1 -1
- package/dist/kit/types.cjs +11 -9
- package/dist/kit/types.cjs.map +1 -1
- package/dist/kit/types.d.cts +28 -0
- package/dist/kit/types.d.cts.map +1 -0
- package/dist/kit/types.d.ts +24 -19
- package/dist/kit/types.d.ts.map +1 -0
- package/dist/kit/types.js +11 -10
- package/dist/kit/types.js.map +1 -1
- package/dist/kit.cjs +20 -25
- package/dist/kit.d.cts +8 -0
- package/dist/kit.d.ts +8 -0
- package/dist/kit.js +5 -20
- package/dist/lib/EventEmitter.cjs +15 -25
- package/dist/lib/EventEmitter.cjs.map +1 -1
- package/dist/lib/EventEmitter.d.cts +12 -0
- package/dist/lib/EventEmitter.d.cts.map +1 -0
- package/dist/lib/EventEmitter.d.ts +11 -9
- package/dist/lib/EventEmitter.d.ts.map +1 -0
- package/dist/lib/EventEmitter.js +16 -26
- package/dist/lib/EventEmitter.js.map +1 -1
- package/dist/lib/throttle.cjs +20 -21
- package/dist/lib/throttle.cjs.map +1 -1
- package/dist/lib/throttle.js +21 -22
- package/dist/lib/throttle.js.map +1 -1
- package/dist/types.cjs +17 -14
- package/dist/types.cjs.map +1 -1
- package/dist/types.d.cts +39 -0
- package/dist/types.d.cts.map +1 -0
- package/dist/types.d.ts +32 -27
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +16 -16
- package/dist/types.js.map +1 -1
- package/package.json +44 -87
- package/src/SimulatorAPI.ts +26 -40
- package/src/SimulatorClient.ts +25 -43
- package/src/channel/ChannelEmitter.ts +75 -110
- package/src/channel/ChannelNetwork.ts +71 -98
- package/src/channel/ChannelReceiver.ts +45 -78
- package/src/channel/errors.ts +16 -21
- package/src/channel/index.ts +10 -25
- package/src/channel/messages.ts +43 -44
- package/src/channel/types.ts +51 -67
- package/src/globalExtensions.ts +6 -6
- package/src/index.ts +8 -16
- package/src/kit/SimulatorManager.ts +66 -78
- package/src/kit/State.ts +45 -49
- package/src/kit/domHelpers.ts +25 -27
- package/src/kit/eventHandlers.ts +8 -13
- package/src/kit/getDefault.ts +8 -8
- package/src/kit/index.ts +8 -12
- package/src/kit/messages.ts +5 -6
- package/src/kit/types.ts +13 -14
- package/src/lib/EventEmitter.ts +10 -16
- package/src/lib/__PRODUCTION__.ts +3 -4
- package/src/lib/throttle.ts +12 -12
- package/src/types.ts +20 -20
- package/dist/channel/index.d.ts +0 -10
- package/dist/index.cjs.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/kit/index.d.ts +0 -8
- package/dist/kit.cjs.map +0 -1
- package/dist/kit.js.map +0 -1
- package/dist/lib/__PRODUCTION__.d.ts +0 -7
- package/dist/lib/throttle.d.ts +0 -1
package/src/SimulatorClient.ts
CHANGED
|
@@ -1,30 +1,20 @@
|
|
|
1
|
-
import {
|
|
2
|
-
APIRequestType,
|
|
3
|
-
APITransactions,
|
|
4
|
-
ClientRequestType,
|
|
5
|
-
ClientTransactions,
|
|
6
|
-
} from "./types";
|
|
7
|
-
|
|
8
|
-
import { SimulatorAPIOptions } from "./SimulatorAPI";
|
|
9
|
-
import {
|
|
1
|
+
import type {
|
|
10
2
|
AllChannelEmitterOptions,
|
|
11
|
-
ChannelEmitter,
|
|
12
3
|
TransactionMethod,
|
|
13
4
|
TransactionsHandlers,
|
|
14
5
|
TransactionsMethods,
|
|
15
|
-
} from "./channel"
|
|
6
|
+
} from "./channel"
|
|
7
|
+
import { ChannelEmitter } from "./channel"
|
|
8
|
+
import type { SimulatorAPIOptions } from "./SimulatorAPI"
|
|
9
|
+
import type { APITransactions, ClientTransactions } from "./types"
|
|
10
|
+
import { APIRequestType, ClientRequestType } from "./types"
|
|
16
11
|
|
|
17
|
-
export const simulatorClientDefaultOptions: Partial<AllChannelEmitterOptions> =
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
};
|
|
12
|
+
export const simulatorClientDefaultOptions: Partial<AllChannelEmitterOptions> = {
|
|
13
|
+
requestIDPrefix: "client-",
|
|
14
|
+
}
|
|
21
15
|
|
|
22
16
|
export class SimulatorClient
|
|
23
|
-
extends ChannelEmitter<
|
|
24
|
-
APITransactions,
|
|
25
|
-
Partial<AllChannelEmitterOptions>,
|
|
26
|
-
SimulatorAPIOptions
|
|
27
|
-
>
|
|
17
|
+
extends ChannelEmitter<APITransactions, Partial<AllChannelEmitterOptions>, SimulatorAPIOptions>
|
|
28
18
|
implements TransactionsMethods<ClientTransactions>
|
|
29
19
|
{
|
|
30
20
|
constructor(
|
|
@@ -34,17 +24,16 @@ export class SimulatorClient
|
|
|
34
24
|
) {
|
|
35
25
|
// True if `options.debug` is true or `debug=true` is among query parameters
|
|
36
26
|
const debug =
|
|
37
|
-
options?.debug ||
|
|
38
|
-
/[?&]debug=true/i.test(decodeURIComponent(window.location.search));
|
|
27
|
+
options?.debug || /[?&]debug=true/i.test(decodeURIComponent(window.location.search))
|
|
39
28
|
|
|
40
29
|
super(
|
|
41
30
|
target,
|
|
42
31
|
{
|
|
43
32
|
[APIRequestType.SetActiveSlice]: (_req, res) => {
|
|
44
|
-
return res.success()
|
|
33
|
+
return res.success()
|
|
45
34
|
},
|
|
46
35
|
[APIRequestType.SetSliceZoneSize]: (_req, res) => {
|
|
47
|
-
return res.success()
|
|
36
|
+
return res.success()
|
|
48
37
|
},
|
|
49
38
|
...requestHandlers,
|
|
50
39
|
},
|
|
@@ -53,38 +42,31 @@ export class SimulatorClient
|
|
|
53
42
|
...options,
|
|
54
43
|
debug,
|
|
55
44
|
},
|
|
56
|
-
)
|
|
45
|
+
)
|
|
57
46
|
|
|
58
47
|
// Append client to window object
|
|
59
48
|
if (debug) {
|
|
60
|
-
window.prismic ||= {}
|
|
61
|
-
window.prismic.sliceSimulator ||= {}
|
|
62
|
-
window.prismic.sliceSimulator.client ||= []
|
|
63
|
-
window.prismic.sliceSimulator.client.push(this)
|
|
49
|
+
window.prismic ||= {}
|
|
50
|
+
window.prismic.sliceSimulator ||= {}
|
|
51
|
+
window.prismic.sliceSimulator.client ||= []
|
|
52
|
+
window.prismic.sliceSimulator.client.push(this)
|
|
64
53
|
}
|
|
65
54
|
}
|
|
66
55
|
|
|
67
|
-
[ClientRequestType.Ping]: TransactionMethod<
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
};
|
|
56
|
+
[ClientRequestType.Ping]: TransactionMethod<ClientTransactions[ClientRequestType.Ping]> =
|
|
57
|
+
async () => {
|
|
58
|
+
return await this.postFormattedRequest(ClientRequestType.Ping)
|
|
59
|
+
};
|
|
72
60
|
|
|
73
61
|
[ClientRequestType.SetSliceZone]: TransactionMethod<
|
|
74
62
|
ClientTransactions[ClientRequestType.SetSliceZone]
|
|
75
63
|
> = async (data) => {
|
|
76
|
-
return await this.postFormattedRequest(
|
|
77
|
-
ClientRequestType.SetSliceZone,
|
|
78
|
-
data,
|
|
79
|
-
);
|
|
64
|
+
return await this.postFormattedRequest(ClientRequestType.SetSliceZone, data)
|
|
80
65
|
};
|
|
81
66
|
|
|
82
67
|
[ClientRequestType.ScrollToSlice]: TransactionMethod<
|
|
83
68
|
ClientTransactions[ClientRequestType.ScrollToSlice]
|
|
84
69
|
> = async (data) => {
|
|
85
|
-
return await this.postFormattedRequest(
|
|
86
|
-
|
|
87
|
-
data,
|
|
88
|
-
);
|
|
89
|
-
};
|
|
70
|
+
return await this.postFormattedRequest(ClientRequestType.ScrollToSlice, data)
|
|
71
|
+
}
|
|
90
72
|
}
|
|
@@ -1,82 +1,65 @@
|
|
|
1
|
+
import type { ChannelNetworkOptions, PostRequestOptions } from "./ChannelNetwork"
|
|
2
|
+
import { ChannelNetwork } from "./ChannelNetwork"
|
|
3
|
+
import type { AllChannelReceiverOptions } from "./ChannelReceiver"
|
|
4
|
+
import { ChannelNotSetError, ConnectionTimeoutError, NotReadyError } from "./errors"
|
|
1
5
|
import {
|
|
6
|
+
createErrorResponseMessage,
|
|
7
|
+
createSuccessResponseMessage,
|
|
8
|
+
isRequestMessage,
|
|
9
|
+
validateMessage,
|
|
10
|
+
} from "./messages"
|
|
11
|
+
import type {
|
|
2
12
|
ExtractSuccessResponseMessage,
|
|
3
|
-
InternalEmitterRequestType,
|
|
4
13
|
InternalEmitterTransactions,
|
|
5
|
-
InternalReceiverRequestType,
|
|
6
14
|
SuccessResponseMessage,
|
|
7
15
|
TransactionsHandlers,
|
|
8
16
|
UnknownRequestMessage,
|
|
9
17
|
UnknownResponseMessage,
|
|
10
18
|
UnknownTransaction,
|
|
11
|
-
} from "./types"
|
|
12
|
-
|
|
13
|
-
import {
|
|
14
|
-
ChannelNotSetError,
|
|
15
|
-
ConnectionTimeoutError,
|
|
16
|
-
NotReadyError,
|
|
17
|
-
} from "./errors";
|
|
18
|
-
|
|
19
|
-
import {
|
|
20
|
-
ChannelNetwork,
|
|
21
|
-
ChannelNetworkOptions,
|
|
22
|
-
PostRequestOptions,
|
|
23
|
-
} from "./ChannelNetwork";
|
|
24
|
-
import { AllChannelReceiverOptions } from "./ChannelReceiver";
|
|
25
|
-
import {
|
|
26
|
-
createErrorResponseMessage,
|
|
27
|
-
createSuccessResponseMessage,
|
|
28
|
-
isRequestMessage,
|
|
29
|
-
validateMessage,
|
|
30
|
-
} from "./messages";
|
|
19
|
+
} from "./types"
|
|
20
|
+
import { InternalEmitterRequestType, InternalReceiverRequestType } from "./types"
|
|
31
21
|
|
|
32
22
|
export type ChannelEmitterOptions = {
|
|
33
|
-
connectTimeout: number
|
|
34
|
-
}
|
|
23
|
+
connectTimeout: number
|
|
24
|
+
}
|
|
35
25
|
|
|
36
|
-
export const channelEmitterDefaultOptions: ChannelEmitterOptions &
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
}
|
|
26
|
+
export const channelEmitterDefaultOptions: ChannelEmitterOptions & Partial<ChannelNetworkOptions> =
|
|
27
|
+
{
|
|
28
|
+
connectTimeout: 20000,
|
|
29
|
+
requestIDPrefix: "emitter-",
|
|
30
|
+
}
|
|
41
31
|
|
|
42
|
-
export type AllChannelEmitterOptions = ChannelEmitterOptions &
|
|
43
|
-
ChannelNetworkOptions;
|
|
32
|
+
export type AllChannelEmitterOptions = ChannelEmitterOptions & ChannelNetworkOptions
|
|
44
33
|
|
|
45
34
|
export abstract class ChannelEmitter<
|
|
46
|
-
TReceiverTransactions extends Record<string, UnknownTransaction> = Record<
|
|
47
|
-
string,
|
|
48
|
-
never
|
|
49
|
-
>,
|
|
35
|
+
TReceiverTransactions extends Record<string, UnknownTransaction> = Record<string, never>,
|
|
50
36
|
TOptions extends Record<string, unknown> = Record<string, unknown>,
|
|
51
37
|
TReceiverOptions extends Record<string, unknown> = Record<string, unknown>,
|
|
52
|
-
> extends ChannelNetwork<
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
> {
|
|
56
|
-
private _target: HTMLIFrameElement;
|
|
57
|
-
private _channel: MessageChannel | null = null;
|
|
38
|
+
> extends ChannelNetwork<TReceiverTransactions, ChannelEmitterOptions & TOptions> {
|
|
39
|
+
private _target: HTMLIFrameElement
|
|
40
|
+
private _channel: MessageChannel | null = null
|
|
58
41
|
protected get channel(): MessageChannel {
|
|
59
42
|
if (!this._channel) {
|
|
60
|
-
throw new ChannelNotSetError()
|
|
43
|
+
throw new ChannelNotSetError()
|
|
61
44
|
}
|
|
62
45
|
|
|
63
|
-
return this._channel
|
|
46
|
+
return this._channel
|
|
64
47
|
}
|
|
65
48
|
protected set channel(channel: MessageChannel | null) {
|
|
66
|
-
this._channel = channel
|
|
49
|
+
this._channel = channel
|
|
67
50
|
|
|
68
51
|
// Update port automatically
|
|
69
52
|
if (this._channel) {
|
|
70
|
-
this.port = this._channel.port1
|
|
53
|
+
this.port = this._channel.port1
|
|
71
54
|
} else {
|
|
72
|
-
this.port = null
|
|
55
|
+
this.port = null
|
|
73
56
|
}
|
|
74
57
|
}
|
|
75
|
-
private _receiverReady = ""
|
|
76
|
-
private _receiverReadyCallback: (() => Promise<void>) | null = null
|
|
77
|
-
private _connected = false
|
|
58
|
+
private _receiverReady = ""
|
|
59
|
+
private _receiverReadyCallback: (() => Promise<void>) | null = null
|
|
60
|
+
private _connected = false
|
|
78
61
|
public get connected(): boolean {
|
|
79
|
-
return this._connected
|
|
62
|
+
return this._connected
|
|
80
63
|
}
|
|
81
64
|
|
|
82
65
|
constructor(
|
|
@@ -84,22 +67,20 @@ export abstract class ChannelEmitter<
|
|
|
84
67
|
requestHandlers: TransactionsHandlers<TReceiverTransactions>,
|
|
85
68
|
options: Partial<AllChannelEmitterOptions> & TOptions,
|
|
86
69
|
) {
|
|
87
|
-
super(requestHandlers, { ...channelEmitterDefaultOptions, ...options })
|
|
70
|
+
super(requestHandlers, { ...channelEmitterDefaultOptions, ...options })
|
|
88
71
|
|
|
89
|
-
this._target = target
|
|
72
|
+
this._target = target
|
|
90
73
|
|
|
91
74
|
window.addEventListener("message", (event) => {
|
|
92
|
-
this._onPublicMessage(event)
|
|
93
|
-
})
|
|
75
|
+
this._onPublicMessage(event)
|
|
76
|
+
})
|
|
94
77
|
}
|
|
95
78
|
|
|
96
79
|
/**
|
|
97
80
|
* Initiates connection to receiver
|
|
98
81
|
*
|
|
99
82
|
* @param receiverOptions - Options to configure the receiver with
|
|
100
|
-
* @param newOrigin - Indicates to the emitter that we're connecting to a new
|
|
101
|
-
* origin
|
|
102
|
-
*
|
|
83
|
+
* @param newOrigin - Indicates to the emitter that we're connecting to a new origin
|
|
103
84
|
* @returns Success connect message
|
|
104
85
|
*/
|
|
105
86
|
connect(
|
|
@@ -109,10 +90,10 @@ export abstract class ChannelEmitter<
|
|
|
109
90
|
newOrigin = false,
|
|
110
91
|
): Promise<SuccessResponseMessage> {
|
|
111
92
|
// Disconnect first
|
|
112
|
-
this.disconnect()
|
|
93
|
+
this.disconnect()
|
|
113
94
|
// If changing origin we'll need to wait for receiver to be ready again
|
|
114
95
|
if (newOrigin) {
|
|
115
|
-
this._receiverReady = ""
|
|
96
|
+
this._receiverReady = ""
|
|
116
97
|
}
|
|
117
98
|
|
|
118
99
|
// Handshake promise
|
|
@@ -123,28 +104,28 @@ export abstract class ChannelEmitter<
|
|
|
123
104
|
() => {
|
|
124
105
|
// Throw if target doesn't allow access to content window
|
|
125
106
|
if (!this._target.contentWindow) {
|
|
126
|
-
return reject(new Error("Target window is not available"))
|
|
107
|
+
return reject(new Error("Target window is not available"))
|
|
127
108
|
}
|
|
128
109
|
|
|
129
110
|
const receiverReadyTimeout = setTimeout(() => {
|
|
130
|
-
reject(new ConnectionTimeoutError())
|
|
131
|
-
}, this.options.connectTimeout)
|
|
111
|
+
reject(new ConnectionTimeoutError())
|
|
112
|
+
}, this.options.connectTimeout)
|
|
132
113
|
|
|
133
114
|
// Connect to target once ready
|
|
134
115
|
const receiverReadyCallback = async (): Promise<void> => {
|
|
135
116
|
// Clear receiver ready timeout
|
|
136
|
-
clearTimeout(receiverReadyTimeout)
|
|
117
|
+
clearTimeout(receiverReadyTimeout)
|
|
137
118
|
|
|
138
119
|
// Create new message channel (set up port automatically)
|
|
139
120
|
// This is done here to prevent transferable objects neutering
|
|
140
121
|
// when calling `connect()` multiple times
|
|
141
|
-
this.channel = new MessageChannel()
|
|
122
|
+
this.channel = new MessageChannel()
|
|
142
123
|
|
|
143
124
|
// Conclude handshake by sending message channel port to target
|
|
144
125
|
const request = this.createRequestMessage(
|
|
145
126
|
InternalEmitterRequestType.Connect,
|
|
146
127
|
receiverOptions,
|
|
147
|
-
)
|
|
128
|
+
)
|
|
148
129
|
const response = await this.postRequest<
|
|
149
130
|
InternalEmitterTransactions<
|
|
150
131
|
AllChannelReceiverOptions & TReceiverOptions
|
|
@@ -155,10 +136,8 @@ export abstract class ChannelEmitter<
|
|
|
155
136
|
>(request, (request) => {
|
|
156
137
|
// Target content window is checked in previous statement
|
|
157
138
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
158
|
-
this._target.contentWindow!.postMessage(request, "*", [
|
|
159
|
-
|
|
160
|
-
]);
|
|
161
|
-
});
|
|
139
|
+
this._target.contentWindow!.postMessage(request, "*", [this.channel.port2])
|
|
140
|
+
})
|
|
162
141
|
|
|
163
142
|
// Finish by aknowledging ready
|
|
164
143
|
this.postResponse(
|
|
@@ -166,82 +145,74 @@ export abstract class ChannelEmitter<
|
|
|
166
145
|
(response) => {
|
|
167
146
|
// Target content window is checked in previous statement
|
|
168
147
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
169
|
-
this._target.contentWindow!.postMessage(response, "*")
|
|
148
|
+
this._target.contentWindow!.postMessage(response, "*")
|
|
170
149
|
},
|
|
171
|
-
)
|
|
150
|
+
)
|
|
172
151
|
|
|
173
152
|
// If post request succeed, we're connected
|
|
174
|
-
this._connected = true
|
|
153
|
+
this._connected = true
|
|
175
154
|
|
|
176
|
-
resolve(response)
|
|
177
|
-
}
|
|
155
|
+
resolve(response)
|
|
156
|
+
}
|
|
178
157
|
|
|
179
158
|
if (this._receiverReady) {
|
|
180
159
|
// If receiver is already ready, send port immediately
|
|
181
|
-
receiverReadyCallback()
|
|
160
|
+
receiverReadyCallback()
|
|
182
161
|
} else {
|
|
183
162
|
// Else wait for receiver to be ready
|
|
184
|
-
this._receiverReadyCallback = receiverReadyCallback
|
|
163
|
+
this._receiverReadyCallback = receiverReadyCallback
|
|
185
164
|
}
|
|
186
165
|
},
|
|
187
166
|
{ once: true },
|
|
188
|
-
)
|
|
189
|
-
})
|
|
167
|
+
)
|
|
168
|
+
})
|
|
190
169
|
}
|
|
191
170
|
|
|
192
|
-
/**
|
|
193
|
-
* Destroys current connection to receiver if any
|
|
194
|
-
*/
|
|
171
|
+
/** Destroys current connection to receiver if any */
|
|
195
172
|
disconnect(): void {
|
|
196
|
-
this._connected = false
|
|
197
|
-
this.channel = null
|
|
173
|
+
this._connected = false
|
|
174
|
+
this.channel = null
|
|
198
175
|
}
|
|
199
176
|
|
|
200
|
-
/**
|
|
201
|
-
* Handles public messages
|
|
202
|
-
*/
|
|
177
|
+
/** Handles public messages */
|
|
203
178
|
private async _onPublicMessage(event: MessageEvent<unknown>): Promise<void> {
|
|
204
179
|
// Return is event is not from target
|
|
205
180
|
if (event.source !== this._target.contentWindow) {
|
|
206
|
-
return
|
|
181
|
+
return
|
|
207
182
|
}
|
|
208
183
|
|
|
209
184
|
try {
|
|
210
|
-
const message = validateMessage(event.data)
|
|
185
|
+
const message = validateMessage(event.data)
|
|
211
186
|
|
|
212
187
|
if (isRequestMessage(message)) {
|
|
213
188
|
if (this.options.debug) {
|
|
214
189
|
// eslint-disable-next-line no-console
|
|
215
|
-
console.debug(event.data)
|
|
190
|
+
console.debug(event.data)
|
|
216
191
|
}
|
|
217
192
|
|
|
218
193
|
switch (message.type) {
|
|
219
194
|
case InternalReceiverRequestType.Ready:
|
|
220
|
-
this._receiverReady = message.requestID
|
|
195
|
+
this._receiverReady = message.requestID
|
|
221
196
|
|
|
222
197
|
// If emitter is waiting for receiver to be ready
|
|
223
198
|
if (this._receiverReadyCallback) {
|
|
224
199
|
// We don't await the promise directly as we need to clear the callback first
|
|
225
|
-
const receiverReadyCallbackPromise =
|
|
226
|
-
this._receiverReadyCallback();
|
|
200
|
+
const receiverReadyCallbackPromise = this._receiverReadyCallback()
|
|
227
201
|
|
|
228
|
-
this._receiverReadyCallback = null
|
|
202
|
+
this._receiverReadyCallback = null
|
|
229
203
|
|
|
230
|
-
await receiverReadyCallbackPromise
|
|
204
|
+
await receiverReadyCallbackPromise
|
|
231
205
|
}
|
|
232
|
-
break
|
|
206
|
+
break
|
|
233
207
|
|
|
234
208
|
default:
|
|
235
209
|
this.postResponse(
|
|
236
210
|
createErrorResponseMessage(message.requestID, undefined),
|
|
237
211
|
(response) => {
|
|
238
|
-
(event.source as WindowProxy).postMessage(
|
|
239
|
-
response,
|
|
240
|
-
event.origin,
|
|
241
|
-
);
|
|
212
|
+
;(event.source as WindowProxy).postMessage(response, event.origin)
|
|
242
213
|
},
|
|
243
|
-
)
|
|
244
|
-
break
|
|
214
|
+
)
|
|
215
|
+
break
|
|
245
216
|
}
|
|
246
217
|
} else {
|
|
247
218
|
// No response messages are expected on public channel
|
|
@@ -250,7 +221,7 @@ export abstract class ChannelEmitter<
|
|
|
250
221
|
if (error instanceof TypeError) {
|
|
251
222
|
// Ignore unknown messages
|
|
252
223
|
} else {
|
|
253
|
-
throw error
|
|
224
|
+
throw error
|
|
254
225
|
}
|
|
255
226
|
}
|
|
256
227
|
}
|
|
@@ -264,15 +235,9 @@ export abstract class ChannelEmitter<
|
|
|
264
235
|
options: PostRequestOptions = {},
|
|
265
236
|
): Promise<ExtractSuccessResponseMessage<TResponse>> {
|
|
266
237
|
if (!this._connected) {
|
|
267
|
-
throw new NotReadyError(
|
|
268
|
-
"Emitter is not connected, use `ChannelEmitter.connect()` first",
|
|
269
|
-
);
|
|
238
|
+
throw new NotReadyError("Emitter is not connected, use `ChannelEmitter.connect()` first")
|
|
270
239
|
}
|
|
271
240
|
|
|
272
|
-
return this.postRequest(
|
|
273
|
-
this.createRequestMessage(type, data),
|
|
274
|
-
undefined,
|
|
275
|
-
options,
|
|
276
|
-
);
|
|
241
|
+
return this.postRequest(this.createRequestMessage(type, data), undefined, options)
|
|
277
242
|
}
|
|
278
243
|
}
|