File size: 2,071 Bytes
31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | import * as Context from "../../Context.js";
import * as Effect from "../../Effect.js";
import * as Layer from "../../Layer.js";
import * as Schema from "../../Schema.js";
import * as Transferable from "../workers/Transferable.js";
/**
* Context service that supplies the initial RPC worker message as encoded data
* paired with any transferables that should be posted with it.
*
* @category initial message
* @since 4.0.0
*/
export class InitialMessage extends /*#__PURE__*/Context.Service()("effect/rpc/RpcWorker/InitialMessage") {}
const ProtocolTag = /*#__PURE__*/Context.Service("effect/rpc/RpcServer/Protocol");
/**
* Runs an effect, encodes its result with the schema's JSON codec, and returns
* the encoded value together with collected transferables.
*
* @category initial message
* @since 4.0.0
*/
export const makeInitialMessage = (schema, effect) => {
const schemaJson = Schema.toCodecJson(schema);
return Effect.flatMap(effect, value => {
const collector = Transferable.makeCollectorUnsafe();
return Schema.encodeEffect(schemaJson)(value).pipe(Effect.provideService(Transferable.Collector, collector), Effect.map(encoded => [encoded, collector.clearUnsafe()]));
});
};
/**
* Provides the `InitialMessage` service from a schema and build effect,
* capturing the layer context and dying if schema encoding fails.
*
* @category initial message
* @since 4.0.0
*/
export const layerInitialMessage = (schema, build) => Layer.effect(InitialMessage)(Effect.contextWith(context => Effect.succeed(Effect.provideContext(Effect.orDie(makeInitialMessage(schema, build)), context))));
/**
* Reads the protocol initial message and decodes it with the supplied schema,
* failing if no initial message is available or decoding fails.
*
* @category initial message
* @since 4.0.0
*/
export const initialMessage = schema => ProtocolTag.pipe(Effect.flatMap(protocol => protocol.initialMessage), Effect.flatMap(Effect.fromOption), Effect.flatMap(Schema.decodeUnknownEffect(Schema.toCodecJson(schema))));
//# sourceMappingURL=RpcWorker.js.map |