| 64 | const constDone = [empty, true] as const |
| 65 | |
| 66 | class MailboxImpl<A, E> extends Effectable.Class<readonly [messages: Chunk.Chunk<A>, done: boolean], E> |
| 67 | implements Api.Mailbox<A, E> |
| 68 | { |
| 69 | readonly [TypeId]: Api.TypeId = TypeId |
| 70 | readonly [ReadonlyTypeId]: Api.ReadonlyTypeId = ReadonlyTypeId |
| 71 | private state: MailboxState<A, E> = { |
| 72 | _tag: "Open", |
| 73 | takers: new Set(), |
| 74 | offers: new Set(), |
| 75 | awaiters: new Set() |
| 76 | } |
| 77 | private messages: Array<A> = [] |
| 78 | private messagesChunk = Chunk.empty<A>() |
| 79 | constructor( |
| 80 | readonly scheduler: Scheduler, |
| 81 | private capacity: number, |
| 82 | readonly strategy: "suspend" | "dropping" | "sliding" |
| 83 | ) { |
| 84 | super() |
| 85 | } |
| 86 | |
| 87 | offer(message: A): Effect<boolean> { |
| 88 | return core.suspend(() => { |
| 89 | if (this.state._tag !== "Open") { |
| 90 | return exitFalse |
| 91 | } else if (this.messages.length + this.messagesChunk.length >= this.capacity) { |
| 92 | switch (this.strategy) { |
| 93 | case "dropping": |
| 94 | return exitFalse |
| 95 | case "suspend": |
| 96 | if (this.capacity <= 0 && this.state.takers.size > 0) { |
| 97 | this.messages.push(message) |
| 98 | this.releaseTaker() |
| 99 | return exitTrue |
| 100 | } |
| 101 | return this.offerRemainingSingle(message) |
| 102 | case "sliding": |
| 103 | this.unsafeTake() |
| 104 | this.messages.push(message) |
| 105 | return exitTrue |
| 106 | } |
| 107 | } |
| 108 | this.messages.push(message) |
| 109 | this.scheduleReleaseTaker() |
| 110 | return exitTrue |
| 111 | }) |
| 112 | } |
| 113 | unsafeOffer(message: A): boolean { |
| 114 | if (this.state._tag !== "Open") { |
| 115 | return false |
| 116 | } else if (this.messages.length + this.messagesChunk.length >= this.capacity) { |
| 117 | if (this.strategy === "sliding") { |
| 118 | this.unsafeTake() |
| 119 | this.messages.push(message) |
| 120 | return true |
| 121 | } else if (this.capacity <= 0 && this.state.takers.size > 0) { |
| 122 | this.messages.push(message) |
| 123 | this.releaseTaker() |
nothing calls this directly
no test coverage detected