@livestore/adapter-web 0.0.0-snapshot-12c4570d047bcfaefc85bc6243c1b61f57580913 → 0.0.0-snapshot-b12fe44662a4a2f917d402ea2562edb3c404f26a
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/dist/.tsbuildinfo +1 -1
- package/dist/web-worker/client-session/client-session-devtools.d.ts +1 -1
- package/dist/web-worker/client-session/client-session-devtools.d.ts.map +1 -1
- package/dist/web-worker/client-session/client-session-devtools.js +7 -97
- package/dist/web-worker/client-session/client-session-devtools.js.map +1 -1
- package/dist/web-worker/client-session/index.d.ts +10 -0
- package/dist/web-worker/client-session/index.d.ts.map +1 -1
- package/dist/web-worker/client-session/index.js +51 -17
- package/dist/web-worker/client-session/index.js.map +1 -1
- package/dist/web-worker/common/worker-schema.d.ts.map +1 -1
- package/package.json +6 -13
- package/src/web-worker/client-session/client-session-devtools.ts +8 -155
- package/src/web-worker/client-session/index.ts +79 -16
- package/dist/devtools-bridge/background-browser-channel.d.ts +0 -9
- package/dist/devtools-bridge/background-browser-channel.d.ts.map +0 -1
- package/dist/devtools-bridge/background-browser-channel.js +0 -31
- package/dist/devtools-bridge/background-browser-channel.js.map +0 -1
- package/dist/devtools-bridge/background-message.d.ts +0 -93
- package/dist/devtools-bridge/background-message.d.ts.map +0 -1
- package/dist/devtools-bridge/background-message.js +0 -53
- package/dist/devtools-bridge/background-message.js.map +0 -1
- package/dist/devtools-bridge/bridge-shared.d.ts +0 -14
- package/dist/devtools-bridge/bridge-shared.d.ts.map +0 -1
- package/dist/devtools-bridge/bridge-shared.js +0 -67
- package/dist/devtools-bridge/bridge-shared.js.map +0 -1
- package/dist/devtools-bridge/browser-extension-bridge.d.ts +0 -3
- package/dist/devtools-bridge/browser-extension-bridge.d.ts.map +0 -1
- package/dist/devtools-bridge/browser-extension-bridge.js +0 -59
- package/dist/devtools-bridge/browser-extension-bridge.js.map +0 -1
- package/dist/devtools-bridge/iframe-message.d.ts +0 -16
- package/dist/devtools-bridge/iframe-message.d.ts.map +0 -1
- package/dist/devtools-bridge/iframe-message.js +0 -11
- package/dist/devtools-bridge/iframe-message.js.map +0 -1
- package/dist/devtools-bridge/index.d.ts +0 -6
- package/dist/devtools-bridge/index.d.ts.map +0 -1
- package/dist/devtools-bridge/index.js +0 -5
- package/dist/devtools-bridge/index.js.map +0 -1
- package/dist/devtools-bridge/web-bridge.d.ts +0 -31
- package/dist/devtools-bridge/web-bridge.d.ts.map +0 -1
- package/dist/devtools-bridge/web-bridge.js +0 -131
- package/dist/devtools-bridge/web-bridge.js.map +0 -1
- package/src/devtools-bridge/background-browser-channel.ts +0 -57
- package/src/devtools-bridge/background-message.ts +0 -42
- package/src/devtools-bridge/bridge-shared.ts +0 -97
- package/src/devtools-bridge/browser-extension-bridge.ts +0 -64
- package/src/devtools-bridge/iframe-message.ts +0 -9
- package/src/devtools-bridge/index.ts +0 -9
- package/src/devtools-bridge/web-bridge.ts +0 -169
|
@@ -1,131 +0,0 @@
|
|
|
1
|
-
import { Equal, Hash, Schema } from '@livestore/utils/effect';
|
|
2
|
-
export class WebBridgeInfo extends Schema.Class('WebBridgeChannelInfo')({
|
|
3
|
-
appHostId: Schema.String,
|
|
4
|
-
storeId: Schema.String,
|
|
5
|
-
webBridgeId: Schema.String,
|
|
6
|
-
isLeader: Schema.Boolean,
|
|
7
|
-
}) {
|
|
8
|
-
// eslint-disable-next-line prefer-arrow/prefer-arrow-functions
|
|
9
|
-
[Hash.symbol]() {
|
|
10
|
-
return Hash.string(this.appHostId);
|
|
11
|
-
}
|
|
12
|
-
// eslint-disable-next-line prefer-arrow/prefer-arrow-functions
|
|
13
|
-
[Equal.symbol](that) {
|
|
14
|
-
return this.appHostId === that.appHostId;
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
// const prepareWebDevtoolsBridge = (
|
|
18
|
-
// options: {
|
|
19
|
-
// sharedWorker: WebAdapterOptions['sharedWorker']
|
|
20
|
-
// appSchema: LiveStoreSchema
|
|
21
|
-
// } & WebBridgeOptions,
|
|
22
|
-
// ): Effect.Effect<Devtools.PrepareDevtoolsBridge, never, Scope.Scope> =>
|
|
23
|
-
// Effect.gen(function* () {
|
|
24
|
-
// const responsePubSub = yield* PubSub.unbounded<
|
|
25
|
-
// Devtools.MessageFromApp | Devtools.MessageFromApp
|
|
26
|
-
// >().pipe(Effect.acquireRelease(PubSub.shutdown))
|
|
27
|
-
// const devtoolsId = nanoid()
|
|
28
|
-
// const portForDevtoolsDeferred = yield* Deferred.make<MessagePort>()
|
|
29
|
-
// const webBridgeBroadcastChannel = yield* Devtools.WebBridge.makeBroadcastChannel()
|
|
30
|
-
// yield* webBridgeBroadcastChannel.listen.pipe(
|
|
31
|
-
// Stream.flatten(),
|
|
32
|
-
// Stream.filter(Schema.is(Devtools.WebBridge.AppHostReady)),
|
|
33
|
-
// Stream.tap(() => webBridgeBroadcastChannel.send(Devtools.WebBridge.DevtoolsReady.make({ devtoolsId }))),
|
|
34
|
-
// Stream.runDrain,
|
|
35
|
-
// Effect.withSpan(`@livestore/adapter-web:devtools:webBridgeChannel:listen`),
|
|
36
|
-
// Effect.ignoreLogged,
|
|
37
|
-
// Effect.forkScoped,
|
|
38
|
-
// )
|
|
39
|
-
// yield* webBridgeBroadcastChannel.send(Devtools.WebBridge.DevtoolsReady.make({ devtoolsId }))
|
|
40
|
-
// const connectionFiberSet = yield* FiberSet.make()
|
|
41
|
-
// yield* webBridgeBroadcastChannel.listen.pipe(
|
|
42
|
-
// Stream.flatten(),
|
|
43
|
-
// Stream.filter(Schema.is(Devtools.WebBridge.ConnectToDevtools)),
|
|
44
|
-
// Stream.tap((msg) =>
|
|
45
|
-
// Effect.gen(function* () {
|
|
46
|
-
// if (devtoolsId !== msg.devtoolsId) return
|
|
47
|
-
// const bridgeInfo = new WebBridgeInfo({
|
|
48
|
-
// appHostId: msg.appHostId,
|
|
49
|
-
// webBridgeId: msg.webBridgeId,
|
|
50
|
-
// isLeader: msg.isLeader,
|
|
51
|
-
// storeId: msg.storeId,
|
|
52
|
-
// })
|
|
53
|
-
// // Propagate disconnect event while connecting.
|
|
54
|
-
// // There's another disconnect handler below after the connection is established.
|
|
55
|
-
// yield* webBridgeBroadcastChannel.listen.pipe(
|
|
56
|
-
// Stream.flatten(),
|
|
57
|
-
// Stream.filter(Schema.is(Devtools.WebBridge.AppHostWillDisconnect)),
|
|
58
|
-
// Stream.filter((msg) => msg.appHostId === bridgeInfo.appHostId),
|
|
59
|
-
// Stream.tap(() => SubscriptionRef.getAndUpdate(options.bridgeInfos, HashSet.remove(bridgeInfo))),
|
|
60
|
-
// Stream.runDrain,
|
|
61
|
-
// Effect.withSpan(`@livestore/adapter-web:devtools:webBridgeChannel:listenForAppHostWillDisconnect`),
|
|
62
|
-
// Effect.tapCauseLogPretty,
|
|
63
|
-
// FiberSet.run(connectionFiberSet),
|
|
64
|
-
// )
|
|
65
|
-
// yield* SubscriptionRef.getAndUpdate(options.bridgeInfos, HashSet.add(bridgeInfo))
|
|
66
|
-
// }),
|
|
67
|
-
// ),
|
|
68
|
-
// Stream.runDrain,
|
|
69
|
-
// Effect.withSpan(`@livestore/adapter-web:devtools:webBridgeChannel:listen`),
|
|
70
|
-
// Effect.tapCauseLogPretty,
|
|
71
|
-
// FiberSet.run(connectionFiberSet),
|
|
72
|
-
// )
|
|
73
|
-
// const selectedChannelInfo = yield* Deferred.await(options.selectedChannelInfoDeferred)
|
|
74
|
-
// const sharedWorker = tryAsFunctionAndNew(options.sharedWorker, {
|
|
75
|
-
// name: `livestore-shared-worker-${selectedChannelInfo.storeId}`,
|
|
76
|
-
// })
|
|
77
|
-
// const sharedWorkerDeferred = yield* Worker.makeSerialized<typeof WorkerSchema.SharedWorker.Request.Type>({
|
|
78
|
-
// initialMessage: () => new WorkerSchema.SharedWorker.InitialMessage({ payload: { _tag: 'FromWebBridge' } }),
|
|
79
|
-
// }).pipe(
|
|
80
|
-
// Effect.provide(BrowserWorker.layer(() => sharedWorker)),
|
|
81
|
-
// Effect.tapCauseLogPretty,
|
|
82
|
-
// Effect.withSpan('@livestore/adapter-web:coordinator:setupSharedWorker'),
|
|
83
|
-
// Effect.toForkedDeferred,
|
|
84
|
-
// )
|
|
85
|
-
// yield* Effect.gen(function* () {
|
|
86
|
-
// const mc = new MessageChannel()
|
|
87
|
-
// const worker = yield* Deferred.await(sharedWorkerDeferred)
|
|
88
|
-
// yield* worker.executeEffect(
|
|
89
|
-
// new WorkerSchema.SharedWorker.DevtoolsWebBridgeOfferPort({
|
|
90
|
-
// port: mc.port1,
|
|
91
|
-
// webBridgeId: selectedChannelInfo.webBridgeId,
|
|
92
|
-
// }),
|
|
93
|
-
// )
|
|
94
|
-
// yield* Deferred.succeed(portForDevtoolsDeferred, mc.port2)
|
|
95
|
-
// // Stop listening for new connections and close `AppHostWillDisconnect` listeners
|
|
96
|
-
// yield* FiberSet.clear(connectionFiberSet)
|
|
97
|
-
// }).pipe(Effect.tapCauseLogPretty, Effect.forkScoped)
|
|
98
|
-
// const { sendToAppHost, appHostId, isLeader } = yield* makeShared({ portForDevtoolsDeferred, responsePubSub })
|
|
99
|
-
// // NOTE we need a second listener here since we depend on the `appHostId` to be set
|
|
100
|
-
// yield* webBridgeBroadcastChannel.listen.pipe(
|
|
101
|
-
// Stream.flatten(),
|
|
102
|
-
// Stream.filter(Schema.is(Devtools.WebBridge.AppHostWillDisconnect)),
|
|
103
|
-
// Stream.filter((msg) => msg.appHostId === appHostId),
|
|
104
|
-
// Stream.tap(() => SubscriptionRef.getAndUpdate(options.bridgeInfos, HashSet.remove(selectedChannelInfo))),
|
|
105
|
-
// Stream.tap(() => PubSub.publish(responsePubSub, Devtools.Disconnect.make({ appHostId, liveStoreVersion }))),
|
|
106
|
-
// Stream.runDrain,
|
|
107
|
-
// Effect.withSpan(`@livestore/adapter-web:devtools:webBridgeChannel:listenForAppHostWillDisconnect`),
|
|
108
|
-
// Effect.ignoreLogged,
|
|
109
|
-
// Effect.forkScoped,
|
|
110
|
-
// )
|
|
111
|
-
// // NOTE this is not guaranteed to "go through" to the app host but at least we try 🤷
|
|
112
|
-
// yield* Stream.fromEventListener(window, 'beforeunload').pipe(
|
|
113
|
-
// Stream.tap(() => sendToAppHost(Devtools.Disconnect.make({ appHostId, liveStoreVersion }))),
|
|
114
|
-
// Stream.runDrain,
|
|
115
|
-
// Effect.ignoreLogged,
|
|
116
|
-
// Effect.forkScoped,
|
|
117
|
-
// )
|
|
118
|
-
// const copyToClipboard = (text: string) =>
|
|
119
|
-
// Effect.sync(() => {
|
|
120
|
-
// navigator.clipboard.writeText(text)
|
|
121
|
-
// })
|
|
122
|
-
// return {
|
|
123
|
-
// responsePubSub,
|
|
124
|
-
// sendToAppHost,
|
|
125
|
-
// appHostId,
|
|
126
|
-
// copyToClipboard,
|
|
127
|
-
// sendEscapeKey: Effect.void,
|
|
128
|
-
// isLeader,
|
|
129
|
-
// } satisfies Devtools.PrepareDevtoolsBridge
|
|
130
|
-
// }).pipe(Effect.orDie)
|
|
131
|
-
//# sourceMappingURL=web-bridge.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"web-bridge.js","sourceRoot":"","sources":["../../src/devtools-bridge/web-bridge.ts"],"names":[],"mappings":"AAMA,OAAO,EAAyB,KAAK,EAAY,IAAI,EAAU,MAAM,EAAkB,MAAM,yBAAyB,CAAA;AAKtH,MAAM,OAAO,aAAc,SAAQ,MAAM,CAAC,KAAK,CAAgB,sBAAsB,CAAC,CAAC;IACrF,SAAS,EAAE,MAAM,CAAC,MAAM;IACxB,OAAO,EAAE,MAAM,CAAC,MAAM;IACtB,WAAW,EAAE,MAAM,CAAC,MAAM;IAC1B,QAAQ,EAAE,MAAM,CAAC,OAAO;CACzB,CAAC;IACA,+DAA+D;IAC/D,CAAC,IAAI,CAAC,MAAM,CAAC;QACX,OAAO,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,CAAA;IACpC,CAAC;IAED,+DAA+D;IAC/D,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,IAAiB;QAC9B,OAAO,IAAI,CAAC,SAAS,KAAM,IAAsB,CAAC,SAAS,CAAA;IAC7D,CAAC;CACF;AAOD,qCAAqC;AACrC,eAAe;AACf,sDAAsD;AACtD,iCAAiC;AACjC,0BAA0B;AAC1B,0EAA0E;AAC1E,8BAA8B;AAC9B,sDAAsD;AACtD,0DAA0D;AAC1D,uDAAuD;AAEvD,kCAAkC;AAElC,0EAA0E;AAE1E,yFAAyF;AAEzF,oDAAoD;AACpD,0BAA0B;AAC1B,mEAAmE;AACnE,iHAAiH;AACjH,yBAAyB;AACzB,oFAAoF;AACpF,6BAA6B;AAC7B,2BAA2B;AAC3B,QAAQ;AAER,mGAAmG;AAEnG,wDAAwD;AAExD,oDAAoD;AACpD,0BAA0B;AAC1B,wEAAwE;AACxE,4BAA4B;AAC5B,oCAAoC;AACpC,sDAAsD;AAEtD,mDAAmD;AACnD,wCAAwC;AACxC,4CAA4C;AAC5C,sCAAsC;AACtC,oCAAoC;AACpC,eAAe;AAEf,4DAA4D;AAC5D,6FAA6F;AAC7F,0DAA0D;AAC1D,gCAAgC;AAChC,kFAAkF;AAClF,8EAA8E;AAC9E,+GAA+G;AAC/G,+BAA+B;AAC/B,kHAAkH;AAClH,wCAAwC;AACxC,gDAAgD;AAChD,cAAc;AAEd,8FAA8F;AAC9F,cAAc;AACd,WAAW;AACX,yBAAyB;AACzB,oFAAoF;AACpF,kCAAkC;AAClC,0CAA0C;AAC1C,QAAQ;AAER,6FAA6F;AAE7F,uEAAuE;AACvE,wEAAwE;AACxE,SAAS;AAET,iHAAiH;AACjH,oHAAoH;AACpH,eAAe;AACf,iEAAiE;AACjE,kCAAkC;AAClC,iFAAiF;AACjF,iCAAiC;AACjC,QAAQ;AAER,uCAAuC;AACvC,wCAAwC;AAExC,mEAAmE;AACnE,qCAAqC;AACrC,qEAAqE;AACrE,4BAA4B;AAC5B,0DAA0D;AAC1D,cAAc;AACd,UAAU;AAEV,mEAAmE;AAEnE,0FAA0F;AAC1F,kDAAkD;AAClD,2DAA2D;AAE3D,oHAAoH;AAEpH,0FAA0F;AAC1F,oDAAoD;AACpD,0BAA0B;AAC1B,4EAA4E;AAC5E,6DAA6D;AAC7D,kHAAkH;AAClH,qHAAqH;AACrH,yBAAyB;AACzB,4GAA4G;AAC5G,6BAA6B;AAC7B,2BAA2B;AAC3B,QAAQ;AAER,4FAA4F;AAC5F,oEAAoE;AACpE,oGAAoG;AACpG,yBAAyB;AACzB,6BAA6B;AAC7B,2BAA2B;AAC3B,QAAQ;AAER,gDAAgD;AAChD,4BAA4B;AAC5B,8CAA8C;AAC9C,WAAW;AAEX,eAAe;AACf,wBAAwB;AACxB,uBAAuB;AACvB,mBAAmB;AACnB,yBAAyB;AACzB,oCAAoC;AACpC,kBAAkB;AAClB,iDAAiD;AACjD,0BAA0B"}
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
import type { Either, ParseResult } from '@livestore/utils/effect'
|
|
2
|
-
import { Chunk, Deferred, Effect, Exit, Runtime, Schema, Scope, Stream, WebChannel } from '@livestore/utils/effect'
|
|
3
|
-
|
|
4
|
-
export const backgroundChannel = <MsgIn, MsgOut, MsgInEncoded, MsgOutEncoded>({
|
|
5
|
-
schema: inputSchema,
|
|
6
|
-
port,
|
|
7
|
-
}: {
|
|
8
|
-
schema:
|
|
9
|
-
| Schema.Schema<MsgIn | MsgOut, MsgInEncoded | MsgOutEncoded>
|
|
10
|
-
| { listen: Schema.Schema<MsgIn, MsgInEncoded>; send: Schema.Schema<MsgOut, MsgOutEncoded> }
|
|
11
|
-
port: chrome.runtime.Port
|
|
12
|
-
}): Effect.Effect<WebChannel.WebChannel<MsgIn, MsgOut>, never, Scope.Scope> =>
|
|
13
|
-
Effect.scopeWithCloseable((scope) =>
|
|
14
|
-
Effect.gen(function* () {
|
|
15
|
-
const schema = WebChannel.mapSchema(inputSchema)
|
|
16
|
-
|
|
17
|
-
const send = (msg: MsgOut) =>
|
|
18
|
-
Effect.gen(function* () {
|
|
19
|
-
const encoded = yield* Schema.encode(schema.send)(msg)
|
|
20
|
-
port.postMessage(encoded)
|
|
21
|
-
})
|
|
22
|
-
|
|
23
|
-
const runtime = yield* Effect.runtime()
|
|
24
|
-
|
|
25
|
-
const listen = Stream.async<Either.Either<MsgIn, ParseResult.ParseError>>((emit) => {
|
|
26
|
-
const onMessage = (message: any) =>
|
|
27
|
-
Effect.gen(function* () {
|
|
28
|
-
const result = yield* Schema.decode(schema.listen)(message).pipe(Effect.either)
|
|
29
|
-
|
|
30
|
-
emit(Effect.succeed(Chunk.make(result)))
|
|
31
|
-
}).pipe(
|
|
32
|
-
Effect.withSpan(`WebChannel:backgroundChannel:listen`),
|
|
33
|
-
Effect.tapCauseLogPretty,
|
|
34
|
-
Runtime.runFork(runtime),
|
|
35
|
-
)
|
|
36
|
-
|
|
37
|
-
port.onMessage.addListener(onMessage)
|
|
38
|
-
|
|
39
|
-
return Effect.sync(() => {
|
|
40
|
-
port.onMessage.removeListener(onMessage)
|
|
41
|
-
})
|
|
42
|
-
})
|
|
43
|
-
|
|
44
|
-
const closedDeferred = yield* Deferred.make<void>().pipe(Effect.acquireRelease(Deferred.done(Exit.void)))
|
|
45
|
-
const supportsTransferables = false
|
|
46
|
-
|
|
47
|
-
return {
|
|
48
|
-
[WebChannel.WebChannelSymbol]: WebChannel.WebChannelSymbol,
|
|
49
|
-
listen,
|
|
50
|
-
send,
|
|
51
|
-
closedDeferred,
|
|
52
|
-
schema,
|
|
53
|
-
supportsTransferables,
|
|
54
|
-
shutdown: Scope.close(scope, Exit.void),
|
|
55
|
-
}
|
|
56
|
-
}).pipe(Effect.withSpan(`WebChannel:backgroundChannel`)),
|
|
57
|
-
)
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import { Devtools } from '@livestore/common'
|
|
2
|
-
import { Schema, Transferable } from '@livestore/utils/effect'
|
|
3
|
-
|
|
4
|
-
export namespace BackgroundMessage {
|
|
5
|
-
export class CopyToClipboard extends Schema.TaggedStruct('Background.CopyToClipboard', {
|
|
6
|
-
text: Schema.String,
|
|
7
|
-
}) {}
|
|
8
|
-
|
|
9
|
-
export class MessageFromAppHost extends Schema.TaggedStruct('Background.MessageFromAppHost', {
|
|
10
|
-
msg: Devtools.Leader.MessageFromApp,
|
|
11
|
-
}) {}
|
|
12
|
-
|
|
13
|
-
export class MessageToAppHost extends Schema.TaggedStruct('Background.MessageToAppHost', {
|
|
14
|
-
msg: Devtools.Leader.MessageToApp,
|
|
15
|
-
}) {}
|
|
16
|
-
|
|
17
|
-
export class RequestPortForDevtools extends Schema.TaggedStruct('Background.RequestPortForDevtools', {
|
|
18
|
-
tabId: Schema.Number,
|
|
19
|
-
}) {}
|
|
20
|
-
|
|
21
|
-
export class RequestOneTimePortFromDevtools extends Schema.TaggedStruct('Background.RequestOneTimePortFromDevtools', {
|
|
22
|
-
tabId: Schema.Number,
|
|
23
|
-
}) {}
|
|
24
|
-
|
|
25
|
-
export class Disconnect extends Schema.TaggedStruct('Background.Disconnect', { appHostId: Schema.String }) {}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export namespace MessagePortInit {
|
|
29
|
-
/**
|
|
30
|
-
* Message is first sent from contentscript-iframe to background and then posted to the devtools.
|
|
31
|
-
*/
|
|
32
|
-
export class PortForDevtools extends Schema.TaggedStruct('MessagePortInit.PortForDevtools', {
|
|
33
|
-
port: Transferable.MessagePort,
|
|
34
|
-
tabId: Schema.Number,
|
|
35
|
-
appHostId: Schema.String,
|
|
36
|
-
}) {}
|
|
37
|
-
|
|
38
|
-
export class OneTimePortFromDevtools extends Schema.TaggedStruct('MessagePortInit.OneTimePortFromDevtools', {
|
|
39
|
-
port: Transferable.MessagePort,
|
|
40
|
-
tabId: Schema.Number,
|
|
41
|
-
}) {}
|
|
42
|
-
}
|
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
// @ts-nocheck
|
|
2
|
-
import { Devtools, liveStoreVersion } from '@livestore/common'
|
|
3
|
-
import { Deferred, Effect, PubSub, Schema, Stream, WebChannel } from '@livestore/utils/effect'
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* This code is running in the devtools window where it's assumed that message ports
|
|
7
|
-
* can be transferred over the bridge.
|
|
8
|
-
*/
|
|
9
|
-
export const makeShared = ({
|
|
10
|
-
portForDevtoolsDeferred,
|
|
11
|
-
responsePubSub,
|
|
12
|
-
}: {
|
|
13
|
-
portForDevtoolsDeferred: Deferred.Deferred<MessagePort>
|
|
14
|
-
responsePubSub: PubSub.PubSub<Devtools.MessageFromApp | Devtools.MessageFromApp>
|
|
15
|
-
}) =>
|
|
16
|
-
Effect.gen(function* () {
|
|
17
|
-
const appHostInfoDeferred = yield* Deferred.make<{ appHostId: string; isLeader: boolean }>()
|
|
18
|
-
|
|
19
|
-
const appHostStoreChannelDeferred =
|
|
20
|
-
yield* Deferred.make<
|
|
21
|
-
WebChannel.WebChannel<typeof Devtools.MessageFromApp.Type, typeof Devtools.MessageToApp.Type>
|
|
22
|
-
>()
|
|
23
|
-
|
|
24
|
-
const portForDevtools = yield* Deferred.await(portForDevtoolsDeferred)
|
|
25
|
-
|
|
26
|
-
const appHostCoordinatorChannel = yield* WebChannel.messagePortChannel({
|
|
27
|
-
port: portForDevtools,
|
|
28
|
-
schema: { listen: Devtools.MessageFromApp, send: Devtools.MessageToApp },
|
|
29
|
-
})
|
|
30
|
-
|
|
31
|
-
yield* appHostCoordinatorChannel.listen.pipe(
|
|
32
|
-
Stream.flatten(),
|
|
33
|
-
// Stream.tapLogWithLabel('appHostCoordinatorChannel.listen'),
|
|
34
|
-
Stream.tap((msg) =>
|
|
35
|
-
Effect.gen(function* () {
|
|
36
|
-
// if (msg._tag === 'LSD.AppHostReady') {
|
|
37
|
-
// const { appHostId, isLeader } = msg
|
|
38
|
-
// yield* Deferred.succeed(appHostInfoDeferred, { appHostId, isLeader })
|
|
39
|
-
// } else if (msg._tag === 'LSD.MessagePortForStoreReq') {
|
|
40
|
-
// Here we're "duplicating" the message port since we need one for the coordinator
|
|
41
|
-
// and one for the store
|
|
42
|
-
const storeMessageChannel = new MessageChannel()
|
|
43
|
-
|
|
44
|
-
// yield* sendToAppHost(
|
|
45
|
-
// Devtools.MessagePortForStoreRes.make({
|
|
46
|
-
// // appHostId: msg.appHostId,
|
|
47
|
-
// liveStoreVersion: msg.liveStoreVersion,
|
|
48
|
-
// port: storeMessageChannel.port1,
|
|
49
|
-
// requestId: msg.requestId,
|
|
50
|
-
// }),
|
|
51
|
-
// )
|
|
52
|
-
|
|
53
|
-
// const portForAppHostStoreChannel = yield* WebChannel.messagePortChannel({
|
|
54
|
-
// port: storeMessageChannel.port2,
|
|
55
|
-
// schema: { listen: Devtools.MessageFromApp, send: Devtools.MessageToApp },
|
|
56
|
-
// })
|
|
57
|
-
|
|
58
|
-
// yield* portForAppHostStoreChannel.listen.pipe(
|
|
59
|
-
// Stream.flatten(),
|
|
60
|
-
// Stream.tap((msg) => PubSub.publish(responsePubSub, msg)),
|
|
61
|
-
// Stream.runDrain,
|
|
62
|
-
// Effect.withSpan('portForStoreChannel.listen'),
|
|
63
|
-
// Effect.tapCauseLogPretty,
|
|
64
|
-
// Effect.forkScoped,
|
|
65
|
-
// )
|
|
66
|
-
|
|
67
|
-
// yield* Deferred.succeed(appHostStoreChannelDeferred, portForAppHostStoreChannel)
|
|
68
|
-
// } else {
|
|
69
|
-
yield* PubSub.publish(responsePubSub, msg)
|
|
70
|
-
// }
|
|
71
|
-
}),
|
|
72
|
-
),
|
|
73
|
-
Stream.runDrain,
|
|
74
|
-
Effect.withSpan('portForDevtoolsChannel.listen'),
|
|
75
|
-
Effect.tapCauseLogPretty,
|
|
76
|
-
Effect.forkScoped,
|
|
77
|
-
)
|
|
78
|
-
|
|
79
|
-
// Sends the message to the app host (i.e. contentscript) via the devtools panel window and the background script
|
|
80
|
-
// const sendToAppHost: Devtools.PrepareDevtoolsBridge['sendToAppHost'] = (msg) =>
|
|
81
|
-
// Effect.gen(function* () {
|
|
82
|
-
// // console.log('bridge-shared: sendToAppHost', msg)
|
|
83
|
-
// if (Schema.is(Devtools.MessageToApp)(msg)) {
|
|
84
|
-
// yield* appHostCoordinatorChannel.send(msg)
|
|
85
|
-
// } else {
|
|
86
|
-
// // console.log('bridge-shared: sendToAppHostStore', msg)
|
|
87
|
-
// const appHostStoreChannel = yield* Deferred.await(appHostStoreChannelDeferred)
|
|
88
|
-
// yield* appHostStoreChannel.send(msg)
|
|
89
|
-
// }
|
|
90
|
-
// }).pipe(Effect.withSpan('sendToAppHost'), Effect.orDie)
|
|
91
|
-
|
|
92
|
-
// yield* sendToAppHost(Devtools.DevtoolsReady.make({ liveStoreVersion }))
|
|
93
|
-
|
|
94
|
-
const { appHostId, isLeader } = yield* Deferred.await(appHostInfoDeferred)
|
|
95
|
-
|
|
96
|
-
return { appHostId, isLeader }
|
|
97
|
-
})
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
import { Devtools, liveStoreVersion } from '@livestore/common'
|
|
2
|
-
import { Deferred, Effect, PubSub, Schema, Stream, WebChannel } from '@livestore/utils/effect'
|
|
3
|
-
|
|
4
|
-
import { BackgroundMessage, MessagePortInit } from './background-message.js'
|
|
5
|
-
import { makeShared } from './bridge-shared.js'
|
|
6
|
-
import { EscapeKey, IframeReady, MessageToPanel } from './iframe-message.js'
|
|
7
|
-
|
|
8
|
-
// NOTE this code is running inside the devtools iframe, so will be re-running from scratch if the iframe is reloaded
|
|
9
|
-
// TODO make sure this also works reliably for HMR
|
|
10
|
-
export const prepareBrowserExtensionDevtoolsBridge = Effect.gen(function* () {
|
|
11
|
-
// const iframeWindow = window
|
|
12
|
-
// const iframeChannel = yield* WebChannel.windowChannel({
|
|
13
|
-
// window: iframeWindow,
|
|
14
|
-
// schema: {
|
|
15
|
-
// listen: Schema.Union(MessagePortInit.PortForDevtools, BackgroundMessage.Disconnect),
|
|
16
|
-
// send: Schema.Never,
|
|
17
|
-
// },
|
|
18
|
-
// })
|
|
19
|
-
// const panelChannel = yield* WebChannel.windowChannel({
|
|
20
|
-
// window: iframeWindow.parent,
|
|
21
|
-
// schema: { listen: Schema.Never, send: MessageToPanel },
|
|
22
|
-
// })
|
|
23
|
-
// const responsePubSub = yield* PubSub.unbounded<
|
|
24
|
-
// Devtools.MessageFromApp | Devtools.MessageFromApp
|
|
25
|
-
// >().pipe(Effect.acquireRelease(PubSub.shutdown))
|
|
26
|
-
// const portForDevtoolsDeferred = yield* Deferred.make<MessagePort>()
|
|
27
|
-
// // Messages coming from the app host (i.e. contentscript) via the background script and the devtools panel window
|
|
28
|
-
// yield* iframeChannel.listen.pipe(
|
|
29
|
-
// Stream.flatten(),
|
|
30
|
-
// Stream.tap((msg) =>
|
|
31
|
-
// Effect.gen(function* () {
|
|
32
|
-
// if (msg._tag === 'MessagePortInit.PortForDevtools') {
|
|
33
|
-
// yield* Deferred.succeed(portForDevtoolsDeferred, msg.port)
|
|
34
|
-
// } else {
|
|
35
|
-
// // yield* PubSub.publish(
|
|
36
|
-
// // responsePubSub,
|
|
37
|
-
// // Devtools.Disconnect.make({ clientId: msg.clientId, sessionId: msg.sessionId, liveStoreVersion }),
|
|
38
|
-
// // )
|
|
39
|
-
// }
|
|
40
|
-
// }),
|
|
41
|
-
// ),
|
|
42
|
-
// Stream.runDrain,
|
|
43
|
-
// Effect.withSpan('iframeChannel.listen'),
|
|
44
|
-
// Effect.tapCauseLogPretty,
|
|
45
|
-
// Effect.forkScoped,
|
|
46
|
-
// )
|
|
47
|
-
// yield* panelChannel.send(IframeReady.make({})).pipe(Effect.ignoreLogged)
|
|
48
|
-
// // NOTE When using the web bridge and browser extension bridge at the same time, both will show `isLeader: true`
|
|
49
|
-
// // even though the page origin is the same, given the browser extension app is running in an iframe
|
|
50
|
-
// // this will cause the origin to be "sandboxed" and thus the locks be isolated
|
|
51
|
-
// const { sendToAppHost, appHostId, isLeader } = yield* makeShared({ portForDevtoolsDeferred, responsePubSub })
|
|
52
|
-
// const copyToClipboard = (text: string) =>
|
|
53
|
-
// panelChannel.send(BackgroundMessage.CopyToClipboard.make({ text })).pipe(Effect.ignoreLogged)
|
|
54
|
-
// const sendEscapeKey = panelChannel.send(EscapeKey.make({})).pipe(Effect.ignoreLogged)
|
|
55
|
-
// return {
|
|
56
|
-
// responsePubSub,
|
|
57
|
-
// sendToAppHost,
|
|
58
|
-
// clientId,
|
|
59
|
-
// sessionId,
|
|
60
|
-
// copyToClipboard,
|
|
61
|
-
// sendEscapeKey,
|
|
62
|
-
// isLeader,
|
|
63
|
-
// } satisfies Devtools.PrepareDevtoolsBridge
|
|
64
|
-
})
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { Schema } from '@livestore/utils/effect'
|
|
2
|
-
|
|
3
|
-
import { BackgroundMessage } from './background-message.js'
|
|
4
|
-
|
|
5
|
-
export class IframeReady extends Schema.TaggedStruct('IframeReady', {}) {}
|
|
6
|
-
export class EscapeKey extends Schema.TaggedStruct('EscapeKey', {}) {}
|
|
7
|
-
|
|
8
|
-
export class MessageToIframeWindow extends Schema.Union() {}
|
|
9
|
-
export class MessageToPanel extends Schema.Union(IframeReady, EscapeKey, BackgroundMessage.CopyToClipboard) {}
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
export { prepareBrowserExtensionDevtoolsBridge } from './browser-extension-bridge.js'
|
|
2
|
-
export {
|
|
3
|
-
// prepareWebDevtoolsBridge,
|
|
4
|
-
type WebBridgeOptions,
|
|
5
|
-
type WebBridgeInfo as WebBridgeChannelInfo,
|
|
6
|
-
} from './web-bridge.js'
|
|
7
|
-
export { BackgroundMessage, MessagePortInit } from './background-message.js'
|
|
8
|
-
export { backgroundChannel } from './background-browser-channel.js'
|
|
9
|
-
export * as IframeMessage from './iframe-message.js'
|
|
@@ -1,169 +0,0 @@
|
|
|
1
|
-
import type { WebAdapterOptions } from '@livestore/adapter-web'
|
|
2
|
-
import { WorkerSchema } from '@livestore/adapter-web'
|
|
3
|
-
import { Devtools, liveStoreVersion } from '@livestore/common'
|
|
4
|
-
import type { LiveStoreSchema } from '@livestore/common/schema'
|
|
5
|
-
import { tryAsFunctionAndNew } from '@livestore/utils'
|
|
6
|
-
import type { Deferred, HashSet, Scope, SubscriptionRef } from '@livestore/utils/effect'
|
|
7
|
-
import { BrowserWorker, Effect, Equal, FiberSet, Hash, PubSub, Schema, Stream, Worker } from '@livestore/utils/effect'
|
|
8
|
-
import { nanoid } from '@livestore/utils/nanoid'
|
|
9
|
-
|
|
10
|
-
import { makeShared } from './bridge-shared.js'
|
|
11
|
-
|
|
12
|
-
export class WebBridgeInfo extends Schema.Class<WebBridgeInfo>('WebBridgeChannelInfo')({
|
|
13
|
-
appHostId: Schema.String,
|
|
14
|
-
storeId: Schema.String,
|
|
15
|
-
webBridgeId: Schema.String,
|
|
16
|
-
isLeader: Schema.Boolean,
|
|
17
|
-
}) {
|
|
18
|
-
// eslint-disable-next-line prefer-arrow/prefer-arrow-functions
|
|
19
|
-
[Hash.symbol](): number {
|
|
20
|
-
return Hash.string(this.appHostId)
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
// eslint-disable-next-line prefer-arrow/prefer-arrow-functions
|
|
24
|
-
[Equal.symbol](that: Equal.Equal): boolean {
|
|
25
|
-
return this.appHostId === (that as WebBridgeInfo).appHostId
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export type WebBridgeOptions = {
|
|
30
|
-
selectedChannelInfoDeferred: Deferred.Deferred<WebBridgeInfo>
|
|
31
|
-
bridgeInfos: SubscriptionRef.SubscriptionRef<HashSet.HashSet<WebBridgeInfo>>
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
// const prepareWebDevtoolsBridge = (
|
|
35
|
-
// options: {
|
|
36
|
-
// sharedWorker: WebAdapterOptions['sharedWorker']
|
|
37
|
-
// appSchema: LiveStoreSchema
|
|
38
|
-
// } & WebBridgeOptions,
|
|
39
|
-
// ): Effect.Effect<Devtools.PrepareDevtoolsBridge, never, Scope.Scope> =>
|
|
40
|
-
// Effect.gen(function* () {
|
|
41
|
-
// const responsePubSub = yield* PubSub.unbounded<
|
|
42
|
-
// Devtools.MessageFromApp | Devtools.MessageFromApp
|
|
43
|
-
// >().pipe(Effect.acquireRelease(PubSub.shutdown))
|
|
44
|
-
|
|
45
|
-
// const devtoolsId = nanoid()
|
|
46
|
-
|
|
47
|
-
// const portForDevtoolsDeferred = yield* Deferred.make<MessagePort>()
|
|
48
|
-
|
|
49
|
-
// const webBridgeBroadcastChannel = yield* Devtools.WebBridge.makeBroadcastChannel()
|
|
50
|
-
|
|
51
|
-
// yield* webBridgeBroadcastChannel.listen.pipe(
|
|
52
|
-
// Stream.flatten(),
|
|
53
|
-
// Stream.filter(Schema.is(Devtools.WebBridge.AppHostReady)),
|
|
54
|
-
// Stream.tap(() => webBridgeBroadcastChannel.send(Devtools.WebBridge.DevtoolsReady.make({ devtoolsId }))),
|
|
55
|
-
// Stream.runDrain,
|
|
56
|
-
// Effect.withSpan(`@livestore/adapter-web:devtools:webBridgeChannel:listen`),
|
|
57
|
-
// Effect.ignoreLogged,
|
|
58
|
-
// Effect.forkScoped,
|
|
59
|
-
// )
|
|
60
|
-
|
|
61
|
-
// yield* webBridgeBroadcastChannel.send(Devtools.WebBridge.DevtoolsReady.make({ devtoolsId }))
|
|
62
|
-
|
|
63
|
-
// const connectionFiberSet = yield* FiberSet.make()
|
|
64
|
-
|
|
65
|
-
// yield* webBridgeBroadcastChannel.listen.pipe(
|
|
66
|
-
// Stream.flatten(),
|
|
67
|
-
// Stream.filter(Schema.is(Devtools.WebBridge.ConnectToDevtools)),
|
|
68
|
-
// Stream.tap((msg) =>
|
|
69
|
-
// Effect.gen(function* () {
|
|
70
|
-
// if (devtoolsId !== msg.devtoolsId) return
|
|
71
|
-
|
|
72
|
-
// const bridgeInfo = new WebBridgeInfo({
|
|
73
|
-
// appHostId: msg.appHostId,
|
|
74
|
-
// webBridgeId: msg.webBridgeId,
|
|
75
|
-
// isLeader: msg.isLeader,
|
|
76
|
-
// storeId: msg.storeId,
|
|
77
|
-
// })
|
|
78
|
-
|
|
79
|
-
// // Propagate disconnect event while connecting.
|
|
80
|
-
// // There's another disconnect handler below after the connection is established.
|
|
81
|
-
// yield* webBridgeBroadcastChannel.listen.pipe(
|
|
82
|
-
// Stream.flatten(),
|
|
83
|
-
// Stream.filter(Schema.is(Devtools.WebBridge.AppHostWillDisconnect)),
|
|
84
|
-
// Stream.filter((msg) => msg.appHostId === bridgeInfo.appHostId),
|
|
85
|
-
// Stream.tap(() => SubscriptionRef.getAndUpdate(options.bridgeInfos, HashSet.remove(bridgeInfo))),
|
|
86
|
-
// Stream.runDrain,
|
|
87
|
-
// Effect.withSpan(`@livestore/adapter-web:devtools:webBridgeChannel:listenForAppHostWillDisconnect`),
|
|
88
|
-
// Effect.tapCauseLogPretty,
|
|
89
|
-
// FiberSet.run(connectionFiberSet),
|
|
90
|
-
// )
|
|
91
|
-
|
|
92
|
-
// yield* SubscriptionRef.getAndUpdate(options.bridgeInfos, HashSet.add(bridgeInfo))
|
|
93
|
-
// }),
|
|
94
|
-
// ),
|
|
95
|
-
// Stream.runDrain,
|
|
96
|
-
// Effect.withSpan(`@livestore/adapter-web:devtools:webBridgeChannel:listen`),
|
|
97
|
-
// Effect.tapCauseLogPretty,
|
|
98
|
-
// FiberSet.run(connectionFiberSet),
|
|
99
|
-
// )
|
|
100
|
-
|
|
101
|
-
// const selectedChannelInfo = yield* Deferred.await(options.selectedChannelInfoDeferred)
|
|
102
|
-
|
|
103
|
-
// const sharedWorker = tryAsFunctionAndNew(options.sharedWorker, {
|
|
104
|
-
// name: `livestore-shared-worker-${selectedChannelInfo.storeId}`,
|
|
105
|
-
// })
|
|
106
|
-
|
|
107
|
-
// const sharedWorkerDeferred = yield* Worker.makeSerialized<typeof WorkerSchema.SharedWorker.Request.Type>({
|
|
108
|
-
// initialMessage: () => new WorkerSchema.SharedWorker.InitialMessage({ payload: { _tag: 'FromWebBridge' } }),
|
|
109
|
-
// }).pipe(
|
|
110
|
-
// Effect.provide(BrowserWorker.layer(() => sharedWorker)),
|
|
111
|
-
// Effect.tapCauseLogPretty,
|
|
112
|
-
// Effect.withSpan('@livestore/adapter-web:coordinator:setupSharedWorker'),
|
|
113
|
-
// Effect.toForkedDeferred,
|
|
114
|
-
// )
|
|
115
|
-
|
|
116
|
-
// yield* Effect.gen(function* () {
|
|
117
|
-
// const mc = new MessageChannel()
|
|
118
|
-
|
|
119
|
-
// const worker = yield* Deferred.await(sharedWorkerDeferred)
|
|
120
|
-
// yield* worker.executeEffect(
|
|
121
|
-
// new WorkerSchema.SharedWorker.DevtoolsWebBridgeOfferPort({
|
|
122
|
-
// port: mc.port1,
|
|
123
|
-
// webBridgeId: selectedChannelInfo.webBridgeId,
|
|
124
|
-
// }),
|
|
125
|
-
// )
|
|
126
|
-
|
|
127
|
-
// yield* Deferred.succeed(portForDevtoolsDeferred, mc.port2)
|
|
128
|
-
|
|
129
|
-
// // Stop listening for new connections and close `AppHostWillDisconnect` listeners
|
|
130
|
-
// yield* FiberSet.clear(connectionFiberSet)
|
|
131
|
-
// }).pipe(Effect.tapCauseLogPretty, Effect.forkScoped)
|
|
132
|
-
|
|
133
|
-
// const { sendToAppHost, appHostId, isLeader } = yield* makeShared({ portForDevtoolsDeferred, responsePubSub })
|
|
134
|
-
|
|
135
|
-
// // NOTE we need a second listener here since we depend on the `appHostId` to be set
|
|
136
|
-
// yield* webBridgeBroadcastChannel.listen.pipe(
|
|
137
|
-
// Stream.flatten(),
|
|
138
|
-
// Stream.filter(Schema.is(Devtools.WebBridge.AppHostWillDisconnect)),
|
|
139
|
-
// Stream.filter((msg) => msg.appHostId === appHostId),
|
|
140
|
-
// Stream.tap(() => SubscriptionRef.getAndUpdate(options.bridgeInfos, HashSet.remove(selectedChannelInfo))),
|
|
141
|
-
// Stream.tap(() => PubSub.publish(responsePubSub, Devtools.Disconnect.make({ appHostId, liveStoreVersion }))),
|
|
142
|
-
// Stream.runDrain,
|
|
143
|
-
// Effect.withSpan(`@livestore/adapter-web:devtools:webBridgeChannel:listenForAppHostWillDisconnect`),
|
|
144
|
-
// Effect.ignoreLogged,
|
|
145
|
-
// Effect.forkScoped,
|
|
146
|
-
// )
|
|
147
|
-
|
|
148
|
-
// // NOTE this is not guaranteed to "go through" to the app host but at least we try 🤷
|
|
149
|
-
// yield* Stream.fromEventListener(window, 'beforeunload').pipe(
|
|
150
|
-
// Stream.tap(() => sendToAppHost(Devtools.Disconnect.make({ appHostId, liveStoreVersion }))),
|
|
151
|
-
// Stream.runDrain,
|
|
152
|
-
// Effect.ignoreLogged,
|
|
153
|
-
// Effect.forkScoped,
|
|
154
|
-
// )
|
|
155
|
-
|
|
156
|
-
// const copyToClipboard = (text: string) =>
|
|
157
|
-
// Effect.sync(() => {
|
|
158
|
-
// navigator.clipboard.writeText(text)
|
|
159
|
-
// })
|
|
160
|
-
|
|
161
|
-
// return {
|
|
162
|
-
// responsePubSub,
|
|
163
|
-
// sendToAppHost,
|
|
164
|
-
// appHostId,
|
|
165
|
-
// copyToClipboard,
|
|
166
|
-
// sendEscapeKey: Effect.void,
|
|
167
|
-
// isLeader,
|
|
168
|
-
// } satisfies Devtools.PrepareDevtoolsBridge
|
|
169
|
-
// }).pipe(Effect.orDie)
|