MCPcopy Create free account
hub / github.com/Effect-TS/effect / notifyLocal

Function notifyLocal

packages/cluster/src/Sharding.ts:967–1021  ·  view source on GitHub ↗
(
    message: M,
    discard: boolean,
    options?: {
      readonly waitUntilRead?: boolean | undefined
    }
  )

Source from the content-addressed store, hash-verified

965 })
966
967 const notifyLocal = <M extends Message.Outgoing<any> | Message.Incoming<any>>(
968 message: M,
969 discard: boolean,
970 options?: {
971 readonly waitUntilRead?: boolean | undefined
972 }
973 ) =>
974 Effect.suspend(function loop(): Effect.Effect<
975 void,
976 | EntityNotAssignedToRunner
977 | AlreadyProcessingMessage
978 | (M extends Message.Incoming<any> ? never : PersistenceError)
979 > {
980 const address = message.envelope.address
981 const state = entityManagers.get(address.entityType)
982 if (!state) {
983 return Effect.flatMap(waitForEntityManager(address.entityType), loop)
984 } else if (state.status === "closed") {
985 return Effect.fail(new EntityNotAssignedToRunner({ address }))
986 }
987
988 const isLocal = isEntityOnLocalShards(address)
989 const notify = storageEnabled
990 ? openStorageReadLatch
991 : () => Effect.die("Sharding.notifyLocal: storage is disabled")
992
993 if (message._tag === "IncomingRequest" || message._tag === "IncomingEnvelope") {
994 if (!isLocal) {
995 return Effect.fail(new EntityNotAssignedToRunner({ address }))
996 } else if (
997 message._tag === "IncomingRequest" && state.manager.isProcessingFor(message, { excludeReplies: true })
998 ) {
999 return Effect.fail(new AlreadyProcessingMessage({ address, envelopeId: message.envelope.requestId }))
1000 } else if (message._tag === "IncomingRequest" && options?.waitUntilRead) {
1001 if (!storageEnabled) return notify()
1002 return Effect.async<void, EntityNotAssignedToRunner>((resume) => {
1003 let entry = pendingNotifications.get(message.envelope.requestId)
1004 if (entry) {
1005 const prevResume = entry.resume
1006 entry.resume = (effect) => {
1007 prevResume(effect)
1008 resume(effect)
1009 }
1010 return
1011 }
1012 entry = { resume, message }
1013 pendingNotifications.set(message.envelope.requestId, entry)
1014 storageReadLatch.unsafeOpen()
1015 })
1016 }
1017 return notify()
1018 }
1019
1020 return runnersService.notifyLocal({ message, notify, discard, storageOnly: !isLocal }) as any
1021 })
1022
1023 const sendToRunner = (runner: RunnerAddress, message: Message.Outgoing<any>) =>
1024 isLocalRunner(runner)

Callers 2

sendOutgoingFunction · 0.70
Sharding.tsFile · 0.70

Calls 9

waitForEntityManagerFunction · 0.85
isEntityOnLocalShardsFunction · 0.85
notifyFunction · 0.85
unsafeOpenMethod · 0.80
resumeFunction · 0.70
getMethod · 0.65
failMethod · 0.65
dieMethod · 0.65
setMethod · 0.65

Tested by

no test coverage detected