avatarJenn M. Wilson

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

5070

Abstract

do that:</p><div id="2e8b"><pre>eventHandler<span class="hljs-selector-class">.Handle</span>(event1, <span class="hljs-built_in">ExampleMiddlewareFoo</span>(<span class="hljs-built_in">ExampleMiddlewareBar</span>(handle)))</pre></div><h1 id="a8a5">Managing Events with Handlers and Middleware</h1><p id="f869">Most articles I read explain how to use this design pattern; this one deals with implementing the internal logic. So let's start coding.</p><p id="35c4">The full code for this article can be found <a href="https://github.com/xNok/slack-go-demo-socketmode/blob/main/examples/middleware/main.go">here</a> to help you follow along.</p><h2 id="0f88">Designing events</h2><p id="b622">First, we are going to enumerate the list of events that our system can handle. The way we create enumeration in Go is a bit different than in other programming languages. In Go, we are going to use a set of constants sharing the same <code>type</code>. Here I define a type <code>EventType</code>that represents a string with the event's name.</p><div id="b181"><pre>// <span class="hljs-keyword">type</span> <span class="hljs-type">used </span>to enumerate events <span class="hljs-keyword">type</span> <span class="hljs-type">EventType </span>string</pre></div><div id="4817"><pre>const ( event1 EventType <span class="hljs-operator">=</span> <span class="hljs-string">"event1"</span> event2 EventType <span class="hljs-operator">=</span> <span class="hljs-string">"event2"</span> )</pre></div><p id="e041">Next, we define the event itself. In our example, the<code>Event</code> as a type which can be selected among the list of <code>EventType</code> created above.</p><div id="2bc9"><pre><span class="hljs-keyword">type</span> <span class="hljs-type">Event</span> struct { <span class="hljs-type">Type</span> <span class="hljs-type">EventType</span> <span class="hljs-type">Data</span> interface{} }</pre></div><h2 id="df6c">Create an event sender (for test purpose)</h2><p id="60aa">To test our system, we will need to create a small function to send events every 2s. Each<code>Event</code> is transmitted via a <a href="https://tour.golang.org/concurrency/2">channel</a> and, the <code>eventSender</code> below sends a random <code>Event</code> of type <code>event1</code> or <code>event2</code> to a channel.</p><blockquote id="53f3"><p><i>Channels are a type which you can send and receive values, they are great for communication among goroutines. In other words, there are perfect for sending and receive event through your application.</i></p></blockquote><div id="ffd1"><pre><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">eventSender</span><span class="hljs-params">(c <span class="hljs-keyword">chan</span> EventType)</span></span> {</pre></div><div id="7f57"><pre> for { // Send <span class="hljs-selector-tag">a</span> random event <span class="hljs-selector-tag">to</span> the channel rand<span class="hljs-selector-class">.Seed</span>(<span class="hljs-selector-tag">time</span><span class="hljs-selector-class">.Now</span>()<span class="hljs-selector-class">.Unix</span>()) events := []EventType{ event1, event2, } n := rand.<span class="hljs-built_in">Int</span>() % <span class="hljs-built_in">len</span>(events)</pre></div><div id="6101"><pre> c <- events[n] <span class="hljs-comment">// send event to channel</span></pre></div><div id="5285"><pre> // <span class="hljs-keyword">wait</span> a <span class="hljs-built_in">bit</span> <span class="hljs-built_in">time</span>.Sleep(<span class="hljs-number">2</span> * <span class="hljs-built_in">time</span>.Second) } }</pre></div><h2 id="000c">Handler and dispatcher</h2><p id="e562">We first need a struct to hold the list of events we want to listen to and which function to call whenever that event is transmitted. This struct also contains the channel used for communicating events.</p><div id="280d"><pre><span class="hljs-comment">// Create a struct to hold config</span> <span class="hljs-comment">// And simplify dependency injections</span> <span class="hljs-keyword">type</span> EventHandler <span class="hljs-keyword">struct</span> { <span class="hljs-comment">// Event channel</span> Events <span class="hljs-keyword">chan</span> Event <span class="hljs-comment">// hold the registedred event functionss</span> EventMap <span class="hljs-keyword">map</span>[EventType][]<span class="hljs-function"><span class="hljs-keyword">func</span><span class="hljs-params">(Event)</span></span> }</pre></div><p id="aa6c">Next, we need to provide an initializing constructor for our <code>EventHandler</code>.</p><div id="a4ed"><pre><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">NewEventHandler</span><span class="hljs-params">()</span></span> *EventHandler { eventMap := <span class="hljs-built_in">make</span>(<span class="hljs-keyword">map</span>[EventType][]<span class="hljs-function"><span class="hljs-keyword">func</span><span class="hljs-params">(Event)</span></span>) event

Options

s := <span class="hljs-built_in">make</span>(<span class="hljs-keyword">chan</span> Event)</pre></div><div id="78bb"><pre> return <span class="hljs-variable">&</span>EventHandler<span class="hljs-punctuation">{</span> <span class="hljs-symbol"> Events:</span> events, <span class="hljs-symbol"> EventMap:</span> eventMap, <span class="hljs-punctuation">}</span> <span class="hljs-punctuation">}</span></pre></div><p id="98fe">Then, we can create our <code>Handle</code> function that associates the event with a callback function.</p><div id="308c"><pre><span class="hljs-comment">// register the handler function to handle an event type</span> func (h *EventHandler) <span class="hljs-built_in">Handle</span>(e EventType, f <span class="hljs-built_in">func</span>(Event)) { h<span class="hljs-selector-class">.EventMap</span><span class="hljs-selector-attr">[e]</span> = <span class="hljs-built_in">append</span>(h<span class="hljs-selector-class">.EventMap</span><span class="hljs-selector-attr">[e]</span>, f) }</pre></div><p id="e42a">Finally, we create the <code>EventDispatcher</code> function, the core of this mechanism. The <code>EventDispatcher</code>process any event sent to a channel, check its type, and if any function has been registering for that type, we call all registered functions.</p><div id="6597"><pre><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-params">(h *EventHandler)</span></span> EventDispatcher() { <span class="hljs-keyword">for</span> evt := <span class="hljs-keyword">range</span> h.Events { log.Printf(<span class="hljs-string">"event recieved: %v"</span>, evt) <span class="hljs-keyword">if</span> handlers, ok := h.EventMap[evt.Type]; ok { <span class="hljs-comment">// If we registered an event</span> <span class="hljs-keyword">for</span> _, f := <span class="hljs-keyword">range</span> handlers { <span class="hljs-comment">// exacute function as goroutine</span> <span class="hljs-keyword">go</span> f(evt) } } } }</pre></div><h1 id="13e4">Using our system</h1><p id="4d4e">Everything is ready; we can start using our event handling system.</p><ol><li>Instantiate our event Handler</li><li>Register which event to listen to and what function to callback</li><li>Start the event sender</li><li>Start the event dispatcher</li></ol><div id="62ab"><pre><span class="hljs-keyword">func</span> <span class="hljs-title function_">main</span><span class="hljs-params">()</span> {</pre></div><div id="5b09"><pre> <span class="hljs-variable">eventHandler</span> := <span class="hljs-function"><span class="hljs-title">NewEventHandler</span>()</span></pre></div><div id="d244"><pre> eventHandler.Handle(event1, <span class="hljs-keyword">func</span><span class="hljs-params">()</span> { <span class="hljs-built_in">log</span>.Printf(<span class="hljs-string">"event Handled: %v"</span>, event1) })</pre></div><div id="5c56"><pre> <span class="hljs-variable">go</span> <span class="hljs-function"><span class="hljs-title">eventSender</span>(<span class="hljs-variable">eventHandler.Events</span>)</span></pre></div><div id="30f2"><pre> eventHandler<span class="hljs-selector-class">.EventDispatcher</span>()</pre></div><div id="9bf2"><pre>}</pre></div><p id="e590">The result should be along those lines:</p><figure id="5ee7"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*_D-GCRSu3K8CgTBXewJjJQ.gif"><figcaption></figcaption></figure><p id="19d6">Since we only handle an event of type <code>event1</code>, only <code>event1</code> shows as <code>Hansled</code>. All is good!</p><p id="d48a">The full code for this article can be found <a href="https://github.com/xNok/slack-go-demo-socketmode/blob/main/examples/middleware/main.go">here</a>.</p><h1 id="6f5e">Interesting Articles tackling the same topic</h1><ul><li><a href="https://drstearns.github.io/tutorials/gomiddleware/">Middleware Patterns in Go</a></li><li><a href="https://sathishvj.medium.com/web-handlers-and-middleware-in-golang-2706c2ecfb75">Web Handlers and Middleware in GoLang</a></li><li><a href="https://readmedium.com/lightweight-event-management-implemented-by-go-a654d59ac65">Lightweight event management implemented by Go</a></li><li><a href="https://readmedium.com/the-7-most-important-software-design-patterns-d60e546afb0e">The 7 Most Important Software Design Patterns</a></li></ul><p id="13e7"><i>Do you want <b>unlimited access</b> to all my content and many other writers’ content on Medium? Consider using my affiliate link to <a href="https://couedeloalexandre.medium.com/membership">become a Medium member today</a></i></p><h2 id="853a">Further reads</h2> <figure id="81d2"> <div> <div> <img class="ratio" src="http://placehold.it/16x9"> <iframe class="" src="https://couedeloalexandre.medium.com/embed/list/3f9d03f2cb8f" allowfullscreen="" frameborder="0" height="184" width="undefined"> </div> </div> </figure></iframe></div></div></figure></article></body>

Is Divorce PTSD Real?

What if you’re the one who wanted it?

Photo by Loren Cutler on Unsplash

It’s been 4.5 months since I moved out.

It doesn’t feel like I transitioned into a new life. It feels like I threw a bomb on my old life and jumped into a portal to a new land.

Divorce was my porn for years. I imagined a fantasy life where I didn’t have a man-child to handle. Where I could hang out in the living room instead of always retreating to my bedroom. Fun fact: I still do that. It’s a bright sunny day but I’m typing this on my bed instead of being downstairs with a room full of light and a comfy couch.

I was quasi-single anyway. Joseph worked so far away that I only saw him around 9 pm each night. Our weekends were busy focusing on the kids. He was a crappy roommate and an even worse coworker. There was no partnership.

Is it worse to be alone after divorce or feel alone when married? It’s a pendulum of emotions that I’m still navigating.

On the outside, I’m doing fucking amazing. In a short amount of time, I:

  • got pre-approved for a house
  • got accepted for my first offer for a detached, three-bedroom house in one of the hardest real estate markets ever
  • packed half of a massive house and scheduled the move
  • gutted the kitchen and bathrooms while juggling a new world with kids who still needed food, lunches, and care
  • did dozens of home improvement projects on my own
  • unpacked every single box because I refused to leave unpacked cardboard boxes containing unknown junk like I did when married
  • restocked almost everything that I split in the divorce

All while still maintaining my job, parenting, dating, and relationships with my friends.

Speaking of friends, they’re in awe of me. I don’t fuck around when it’s time for me to take action. One minute we’re having lunch and I mention divorce, next minute we hang out I’m buying rugs for my new home.

I look happy. I should feel happy. I should feel like the weight of an unhealthy marriage is off my shoulders. This should feel like zen and relaxation.

Uh yeah…no.

With the dust settling (literally and figuratively, as the construction dust still lingers), the emotions I pushed down to help me plow through this New Life Portal are surfacing.

The strangest things trigger me. Grabbing a large bin from the top rack in the garage makes me think how I asked Joseph to handle things like that. While he was awful at being an equal-level partner, he was great for grunt labor. I’ll struggle to move something heavy and suddenly start crying because I miss the 35% part of my marriage that was good.

I was at his house (getting used to saying “his house” instead of “my old house”) picking up things and Joseph started crying. He told me how he missed me. Like an emotionless robot, I got up and hugged him. My “there there” hug and pat on the back is so epic, I should host a Ted Talk about it.

But driving home, I’m filled with rage. “You made this happen,” I yell to him mentally. This wasn’t an easy choice. I feel pushed into it and now I’m the bad guy for pulling the plug. Why am I an asshole because I wanted to be treated better and left when it was clear it would never happen? Am I in the wrong because it was the only emotional self-preservation tactic short of suicide?

Today I sat in bed, too tired to do real work. Or really…I’m just too tired to care. My brain is on overdrive trying to process the reality of an unintended life. It’s like I erased twenty years of my life but I can’t go back to being twenty-three. I might as well have been in a coma.

I’m angry. I’m so fucking angry. I’m mourning something that shouldn’t ever have happened. I’m mourning something so awful, I began to think all marriages were that way until I opened my eyes. I thought all couples who seemed happy also fought nonstop behind closed doors.

Until I learned, that’s not true. It’s not normal or healthy to fight even once a week. It’s not normal for a spouse to regularly threaten divorce during arguments, let alone melodramatically pack bags and walk out the door. Stonewalling isn’t the same as giving someone a moment to regroup their thoughts and calm down. Having to do everything for him since “it’s just quicker if I do it” is a sign of his weaponized incompetence, not because I was a control freak. A fucked up sex life, or lack of one, isn’t standard practice.

I’m angry that I used to fantasize about being trapped on an island after a plane crash. Just for a year. I’d emerge tanned and super buff from building my island house with fellow passengers (in this fantasy, my teeth remain intact and brilliantly white against my tanned skin). Meanwhile, Joseph would crumble from the weight of having to watch the children and run a household like I had been doing. Then, upon return, he’d tell me about how he never realized all the work that I did until I was gone.

Sometimes, that fantasy was during a coma. Same outcome.

But there was no chance of those happening and I would never get the appreciation I craved for the level of effort I put in. It wasn’t an effort going above and beyond. I’m talking about the minimum effort I could do as a faux-single working mother of two small children, one of them with autism.

The enormity of everything I did without help or recognition is crashing down on me. Sometimes it hits me so hard, I can barely breathe between the sobs and mental re-living of it all.

When friends would put anniversary posts on social media, they’d always regurgitate the same “our life has been full of ups and downs but I wouldn’t have it any other way.”

Fuck. That. My brain immediately thinks, “uh yes…yes I would very much have liked any other way than this.”

What’s strange to me is how much I cry. I used to time my crying to the drive between when I left work to when I picked up my kids, as well as when I showered (raining water drowns out the sobbing sounds).

I think it’s because I can cry now. I can cry for hours and there’s no reason to stop. With work from home and not having my kids full-time (a whole other heartbreak), I find myself crying longer than I have in years.

It’s an odd feeling. Being allowed to cry without boundaries.

Experiencing this extreme life transition feels like I have PTSD combined with mourning a great loss. But it’s a loss that I chose, and with that adds the burden of guilt on top of the Wah-Wah-Woe-Is-Me pile

I don’t know if this is healthy or normal.

I need to stop writing and find a therapist.

Psychology
Sex
Love
Marriage
Divorce
Recommended from ReadMedium