@sanity/comlink
Advanced tools
Comparing version 2.0.2-release.0 to 2.0.2
# Changelog | ||
## [2.0.2](https://github.com/sanity-io/visual-editing/compare/comlink-v2.0.1...comlink-v2.0.2) (2024-12-16) | ||
### Bug Fixes | ||
* **visual-editing:** execute fetches after node connect event ([#2308](https://github.com/sanity-io/visual-editing/issues/2308)) ([c29a4b3](https://github.com/sanity-io/visual-editing/commit/c29a4b376f952badf87159c60f5e95fb1d87da7c)) | ||
## [2.0.1](https://github.com/sanity-io/visual-editing/compare/comlink-v2.0.0...comlink-v2.0.1) (2024-12-05) | ||
@@ -4,0 +11,0 @@ |
import { v4 } from "uuid"; | ||
import { fromEventObservable, setup, sendTo, assign, fromCallback, enqueueActions, assertEvent, raise, stopChild, createActor, emit } from "xstate"; | ||
import { fromEventObservable, setup, sendTo, assign, fromCallback, enqueueActions, assertEvent, emit, raise, stopChild, createActor } from "xstate"; | ||
import { defer, fromEvent, map, pipe, filter, bufferCount, concatMap, take, EMPTY, takeUntil } from "rxjs"; | ||
@@ -262,2 +262,6 @@ const listenInputFromContext = (config) => ({ | ||
}), | ||
"emit status": emit((_, params) => ({ | ||
type: "_status", | ||
status: params.status | ||
})), | ||
"flush buffer": enqueueActions(({ enqueue }) => { | ||
@@ -342,2 +346,3 @@ enqueue.raise(({ context }) => ({ | ||
idle: { | ||
entry: [{ type: "emit status", params: { status: "idle" } }], | ||
on: { | ||
@@ -355,2 +360,3 @@ connect: { | ||
id: "handshaking", | ||
entry: [{ type: "emit status", params: { status: "handshaking" } }], | ||
invoke: [ | ||
@@ -402,3 +408,3 @@ { | ||
connected: { | ||
entry: "flush buffer", | ||
entry: ["flush buffer", { type: "emit status", params: { status: "connected" } }], | ||
invoke: { | ||
@@ -461,3 +467,3 @@ id: "listen for messages", | ||
id: "disconnected", | ||
entry: "send disconnect", | ||
entry: ["send disconnect", { type: "emit status", params: { status: "disconnected" } }], | ||
on: { | ||
@@ -494,9 +500,10 @@ request: { | ||
actor.send({ type: "disconnect" }); | ||
}, onStatus = (handler) => { | ||
const currentSnapshot = actor.getSnapshot(); | ||
let currentStatus = typeof currentSnapshot.value == "string" ? currentSnapshot.value : Object.keys(currentSnapshot.value)[0]; | ||
const { unsubscribe } = actor.subscribe((state) => { | ||
const status = typeof state.value == "string" ? state.value : Object.keys(state.value)[0]; | ||
currentStatus !== status && (currentStatus = status, handler(status)); | ||
}); | ||
}, onStatus = (handler, filter2) => { | ||
const { unsubscribe } = actor.on( | ||
// @ts-expect-error @todo ReceivedEmitEvent causes this | ||
"_status", | ||
(event) => { | ||
filter2 && event.status !== filter2 || handler(event.status); | ||
} | ||
); | ||
return unsubscribe; | ||
@@ -727,2 +734,6 @@ }, setTarget = (target) => { | ||
}), | ||
"emit status": emit((_, params) => ({ | ||
type: "_status", | ||
status: params.status | ||
})), | ||
"flush buffer": enqueueActions(({ enqueue }) => { | ||
@@ -827,2 +838,3 @@ enqueue.raise(({ context }) => ({ | ||
idle: { | ||
entry: [{ type: "emit status", params: { status: "idle" } }], | ||
on: { | ||
@@ -836,3 +848,3 @@ post: { | ||
guard: "hasSource", | ||
entry: "send handshake syn ack", | ||
entry: ["send handshake syn ack", { type: "emit status", params: { status: "handshaking" } }], | ||
invoke: [ | ||
@@ -891,3 +903,7 @@ { | ||
connected: { | ||
entry: ["flush handshake buffer", "flush buffer"], | ||
entry: [ | ||
"flush handshake buffer", | ||
"flush buffer", | ||
{ type: "emit status", params: { status: "connected" } } | ||
], | ||
invoke: [ | ||
@@ -949,3 +965,3 @@ { | ||
const { unsubscribe } = actor.on( | ||
// @ts-expect-error @todo `type` typing | ||
// @ts-expect-error @todo ReceivedEmitEvent causes this | ||
type, | ||
@@ -957,9 +973,10 @@ (event) => { | ||
return unsubscribe; | ||
}, onStatus = (handler) => { | ||
const snapshot = actor.getSnapshot(); | ||
let currentStatus = typeof snapshot.value == "string" ? snapshot.value : Object.keys(snapshot.value)[0]; | ||
const { unsubscribe } = actor.subscribe((state) => { | ||
const status = typeof state.value == "string" ? state.value : Object.keys(state.value)[0]; | ||
currentStatus !== status && (currentStatus = status, handler(status)); | ||
}); | ||
}, onStatus = (handler, filter2) => { | ||
const { unsubscribe } = actor.on( | ||
// @ts-expect-error @todo ReceivedEmitEvent causes this | ||
"_status", | ||
(event) => { | ||
filter2 && event.status !== filter2 || handler(event.status); | ||
} | ||
); | ||
return unsubscribe; | ||
@@ -966,0 +983,0 @@ }, post = (type, data) => { |
{ | ||
"name": "@sanity/comlink", | ||
"version": "2.0.2-release.0", | ||
"version": "2.0.2", | ||
"keywords": [ | ||
@@ -5,0 +5,0 @@ "sanity.io", |
@@ -6,2 +6,3 @@ import {v4 as uuid} from 'uuid' | ||
createActor, | ||
emit, | ||
enqueueActions, | ||
@@ -33,4 +34,6 @@ fromCallback, | ||
ProtocolMessage, | ||
ReceivedEmitEvent, | ||
RequestData, | ||
Status, | ||
StatusEmitEvent, | ||
WithoutResponse, | ||
@@ -66,3 +69,3 @@ } from './types' | ||
) => () => void | ||
onStatus: (handler: (status: Status) => void) => () => void | ||
onStatus: (handler: (status: Status) => void, filter?: Status) => () => void | ||
post: <T extends S['type'], U extends Extract<S, {type: T}>>( | ||
@@ -142,3 +145,4 @@ ...params: (U['data'] extends undefined ? [T] : never) | [T, U['data']] | ||
| MessageEmitEvent<R> | ||
| (R extends R ? {type: R['type']; message: ProtocolMessage<R>} : never) | ||
| ReceivedEmitEvent<R> | ||
| StatusEmitEvent | ||
events: | ||
@@ -227,2 +231,8 @@ | {type: 'connect'} | ||
}), | ||
'emit status': emit((_, params: {status: Status}) => { | ||
return { | ||
type: '_status', | ||
status: params.status, | ||
} satisfies StatusEmitEvent | ||
}), | ||
'flush buffer': enqueueActions(({enqueue}) => { | ||
@@ -324,2 +334,3 @@ enqueue.raise(({context}) => ({ | ||
idle: { | ||
entry: [{type: 'emit status', params: {status: 'idle'}}], | ||
on: { | ||
@@ -337,2 +348,3 @@ connect: { | ||
id: 'handshaking', | ||
entry: [{type: 'emit status', params: {status: 'handshaking'}}], | ||
invoke: [ | ||
@@ -385,3 +397,3 @@ { | ||
connected: { | ||
entry: 'flush buffer', | ||
entry: ['flush buffer', {type: 'emit status', params: {status: 'connected'}}], | ||
invoke: { | ||
@@ -444,3 +456,3 @@ id: 'listen for messages', | ||
id: 'disconnected', | ||
entry: 'send disconnect', | ||
entry: ['send disconnect', {type: 'emit status', params: {status: 'disconnected'}}], | ||
on: { | ||
@@ -502,17 +514,13 @@ request: { | ||
const onStatus = (handler: (status: Status) => void) => { | ||
const currentSnapshot = actor.getSnapshot() | ||
let currentStatus: Status = | ||
typeof currentSnapshot.value === 'string' | ||
? currentSnapshot.value | ||
: Object.keys(currentSnapshot.value)[0] | ||
const {unsubscribe} = actor.subscribe((state) => { | ||
const status: Status = | ||
typeof state.value === 'string' ? state.value : Object.keys(state.value)[0] | ||
if (currentStatus !== status) { | ||
currentStatus = status | ||
handler(status) | ||
} | ||
}) | ||
const onStatus = (handler: (status: Status) => void, filter?: Status) => { | ||
const {unsubscribe} = actor.on( | ||
// @ts-expect-error @todo ReceivedEmitEvent causes this | ||
'_status', | ||
(event: StatusEmitEvent & {status: Status}) => { | ||
if (filter && event.status !== filter) { | ||
return | ||
} | ||
handler(event.status) | ||
}, | ||
) | ||
return unsubscribe | ||
@@ -519,0 +527,0 @@ } |
@@ -41,7 +41,9 @@ export { | ||
ProtocolMessage, | ||
ReceivedEmitEvent, | ||
RequestData, | ||
ResponseMessage, | ||
Status, | ||
StatusEmitEvent, | ||
StatusEvent, | ||
WithoutResponse, | ||
} from './types' |
@@ -33,4 +33,6 @@ import {v4 as uuid} from 'uuid' | ||
ProtocolMessage, | ||
ReceivedEmitEvent, | ||
RequestData, | ||
Status, | ||
StatusEmitEvent, | ||
WithoutResponse, | ||
@@ -76,3 +78,6 @@ } from './types' | ||
) => () => void | ||
onStatus: (handler: (status: Status) => void) => () => void | ||
onStatus: ( | ||
handler: (status: Omit<Status, 'disconnected'>) => void, | ||
filter?: Omit<Status, 'disconnected'>, | ||
) => () => void | ||
post: <T extends S['type'], U extends Extract<S, {type: T}>>( | ||
@@ -133,3 +138,4 @@ ...params: (U['data'] extends undefined ? [T] : never) | [T, U['data']] | ||
| MessageEmitEvent<R> | ||
| (R extends R ? {type: R['type']; message: ProtocolMessage<R>} : never) | ||
| ReceivedEmitEvent<R> | ||
| (StatusEmitEvent & {status: Omit<Status, 'disconnected'>}) | ||
events: | ||
@@ -245,2 +251,8 @@ | {type: 'heartbeat.received'; message: MessageEvent<ProtocolMessage<HeartbeatMessage>>} | ||
}), | ||
'emit status': emit((_, params: {status: Exclude<Status, 'disconnected'>}) => { | ||
return { | ||
type: '_status', | ||
status: params.status, | ||
} satisfies StatusEmitEvent & {status: Omit<Status, 'disconnected'>} | ||
}), | ||
'flush buffer': enqueueActions(({enqueue}) => { | ||
@@ -367,2 +379,3 @@ enqueue.raise(({context}) => ({ | ||
idle: { | ||
entry: [{type: 'emit status', params: {status: 'idle'}}], | ||
on: { | ||
@@ -376,3 +389,3 @@ post: { | ||
guard: 'hasSource', | ||
entry: 'send handshake syn ack', | ||
entry: ['send handshake syn ack', {type: 'emit status', params: {status: 'handshaking'}}], | ||
invoke: [ | ||
@@ -431,3 +444,7 @@ { | ||
connected: { | ||
entry: ['flush handshake buffer', 'flush buffer'], | ||
entry: [ | ||
'flush handshake buffer', | ||
'flush buffer', | ||
{type: 'emit status', params: {status: 'connected'}}, | ||
], | ||
invoke: [ | ||
@@ -504,3 +521,3 @@ { | ||
const {unsubscribe} = actor.on( | ||
// @ts-expect-error @todo `type` typing | ||
// @ts-expect-error @todo ReceivedEmitEvent causes this | ||
type, | ||
@@ -514,15 +531,16 @@ (event: {type: T; message: ProtocolMessage<U>}) => { | ||
const onStatus = (handler: (status: Status) => void) => { | ||
const snapshot = actor.getSnapshot() | ||
let currentStatus: Status = | ||
typeof snapshot.value === 'string' ? snapshot.value : Object.keys(snapshot.value)[0] | ||
const {unsubscribe} = actor.subscribe((state) => { | ||
const status: Status = | ||
typeof state.value === 'string' ? state.value : Object.keys(state.value)[0] | ||
if (currentStatus !== status) { | ||
currentStatus = status | ||
handler(status) | ||
} | ||
}) | ||
const onStatus = ( | ||
handler: (status: Omit<Status, 'disconnected'>) => void, | ||
filter?: Omit<Status, 'disconnected'>, | ||
) => { | ||
const {unsubscribe} = actor.on( | ||
// @ts-expect-error @todo ReceivedEmitEvent causes this | ||
'_status', | ||
(event: StatusEmitEvent & {status: Omit<Status, 'disconnected'>}) => { | ||
if (filter && event.status !== filter) { | ||
return | ||
} | ||
handler(event.status) | ||
}, | ||
) | ||
return unsubscribe | ||
@@ -529,0 +547,0 @@ } |
@@ -13,3 +13,3 @@ import { | ||
*/ | ||
export type Status = string // @todo strongly type these | ||
export type Status = 'idle' | 'handshaking' | 'connected' | 'disconnected' | ||
@@ -109,2 +109,14 @@ /** | ||
*/ | ||
export interface StatusEmitEvent { | ||
type: '_status' | ||
status: Status | ||
} | ||
export type ReceivedEmitEvent<T extends Message> = T extends T | ||
? {type: T['type']; message: ProtocolMessage<T>} | ||
: never | ||
/** | ||
* @public | ||
*/ | ||
export type InternalEmitEvent<S extends Message, R extends Message> = | ||
@@ -114,2 +126,3 @@ | BufferAddedEmitEvent<S> | ||
| MessageEmitEvent<R> | ||
| StatusEmitEvent | ||
@@ -116,0 +129,0 @@ /** |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
784400
11009
0