Skip to content

Commit

Permalink
feat: Add an LRU Map to keep track of most recently used 10 items and…
Browse files Browse the repository at this point in the history
… sort those first
  • Loading branch information
tomershvueli committed Jul 18, 2021
1 parent 17da451 commit abde428
Show file tree
Hide file tree
Showing 6 changed files with 11,559 additions and 11,605 deletions.
37 changes: 37 additions & 0 deletions app/macos/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const {
Menu,
ipcMain,
} = require("electron");
const { LRUMap } = require("lru_map");

const robot = require("robotjs");

Expand All @@ -16,9 +17,15 @@ const path = require("path");

const assetsDirectory = path.join(__dirname, "assets");

// JSON list of emojis
const emojis = require("./src/emojis");

let tray = undefined;
let window = undefined;

// Let's preset our LRU Map that we'll use to keep track of recent uses
let lruMap = new LRUMap(10);

// Hide the menu and dev tools (for production build)
Menu.setApplicationMenu(null);

Expand Down Expand Up @@ -105,6 +112,36 @@ ipcMain.on("typeEmoji", (_event, arg) => {
robot.typeString(arg);
});

// Return filtered and sorted emojis based on a search query
ipcMain.handle("getEmojisForSearchString", (_event, arg) => {
const recents = Array.from(lruMap.keys());

// For each emoji in the emojis.js file, this will search
// through emoji.keywords and emoji.name (from emojis.js) if it contains the word from the user input
return emojis
.filter((item) => item.keywords.includes(arg) || item.name.toLowerCase().includes(arg))
.sort((a, b) => {
if (lruMap.has(a.char) && !lruMap.has(b.char)) {
// A is in recently used and B is not
return -1;
} else if (!lruMap.has(a.char) && lruMap.has(b.char)) {
// B is in recently used and A is not
return 1;
} else if (!lruMap.has(a.char) && !lruMap.has(b.char)) {
// Neither A nor B is in recently used
return a.no - b.no;
} else {
// Both A and B are in recently used
return recents.indexOf(b.char) - recents.indexOf(a.char);
}
})
});

// When we get a signal to select an emoji, update our LRU Map
ipcMain.on("selectEmoji", (_event, arg) => {
lruMap.set(arg, "");
});

const toggleWindow = () => {
if (window.isVisible()) {
hideWindow();
Expand Down
5 changes: 5 additions & 0 deletions app/macos/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/macos/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"homepage": "https://github.com/virejdasani/Geniemoji#readme",
"dependencies": {
"electron": "^12.0.4",
"lru_map": "^0.4.1",
"open": "^8.0.9",
"robotjs": "^0.6.0"
},
Expand Down
Loading

0 comments on commit abde428

Please sign in to comment.