JSFiddle - React, Tailwind, and code Playground

by gianlucaguarini

JavaScript

const map = new Map()
const set = new Set()


// public
export function subscribe(id, logic) {
	map.set(id, logic)
  
  // set is active
  return function() {
  	set.add(id)
  }
}

export function unsubscribe(id) {
  set.remove(id)
  map.delete(id)
}


// private
window.addEventListener('keydown', handleKey)

function handleKey(e) {
  switch (e.key)
}

function moveLeft() {
  if (set.size) {
    // last move Left
  }
}

function moveRight() {
  if (set.size) {
    // last move Left
  }
}