Skip to content

Commit

Permalink
Merge pull request #212 from takker99:tree-shakable
Browse files Browse the repository at this point in the history
perf: Make export identifiers tree-shakable
  • Loading branch information
takker99 authored Nov 12, 2024
2 parents 80a0a11 + 24f5870 commit 865e7ff
Show file tree
Hide file tree
Showing 13 changed files with 324 additions and 278 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: ci

env:
DENO_VERSION: 1.x
DENO_VERSION: 2.x

on: [push, pull_request]

Expand All @@ -10,7 +10,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: denoland/setup-deno@v1
- uses: denoland/setup-deno@v2
with:
deno-version: ${{ env.DENO_VERSION }}
- name: Check fmt & lint & type check & test
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name: publish

env:
DENO_VERSION: 1.x
DENO_VERSION: 2.x

on:
push:
Expand All @@ -18,7 +18,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: denoland/setup-deno@v1
- uses: denoland/setup-deno@v2
with:
deno-version: ${{ env.DENO_VERSION }}
- name: Publish on tag
Expand Down
12 changes: 8 additions & 4 deletions browser/dom/getCachedLines.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import type { Line, Scrapbox } from "@cosense/types/userscript";
declare const scrapbox: Scrapbox;

let isLatestData = false;
let lines: typeof scrapbox.Page.lines = null;
let isLatestData = /* @__PURE__ */ false;
let lines: Line[] | null = /* @__PURE__ */ null;

scrapbox.addListener("lines:changed", () => isLatestData = false);
scrapbox.addListener("layout:changed", () => isLatestData = false);
let initialize: (() => void) | undefined = () => {
scrapbox.addListener("lines:changed", () => isLatestData = false);
scrapbox.addListener("layout:changed", () => isLatestData = false);
initialize = undefined;
};

/** scrapbox.Page.linesをcacheして取得する
*
Expand All @@ -14,6 +17,7 @@ scrapbox.addListener("layout:changed", () => isLatestData = false);
* @return `scrapbox.Page.lines`と同じ。常に最新のものが返される
*/
export const getCachedLines = (): readonly Line[] | null => {
initialize?.();
if (!isLatestData) {
lines = scrapbox.Page.lines;
isLatestData = true;
Expand Down
46 changes: 27 additions & 19 deletions browser/dom/textInputEventListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,34 @@ declare const scrapbox: Scrapbox;
* - second key: listener
* - value: encoded options
*/
const listenerMap = new Map<
const listenerMap = /* @__PURE__ */ new Map<
keyof HTMLElementEventMap,
Map<EventListener, Set<number>>
>();
const onceListenerMap = new Map<EventListener, Map<number, EventListener>>();
const onceListenerMap = /* @__PURE__ */ new Map<
EventListener,
Map<number, EventListener>
>();

/** re-register event listeners when the layout changes */
let reRegister: (() => void) | undefined = () => {
scrapbox.on("layout:changed", () => {
const textinput = textInput();
if (!textinput) return;
for (const [name, argMap] of listenerMap) {
for (const [listener, encodedOptions] of argMap) {
for (const encoded of encodedOptions) {
textinput.addEventListener(
name,
listener as EventListener,
decode(encoded),
);
}
}
}
});
reRegister = undefined;
};

/** `#text-input`に対してイベントリスナーを追加する
*
Expand All @@ -30,6 +53,7 @@ export const addTextInputEventListener = <K extends keyof HTMLElementEventMap>(
) => unknown,
options?: boolean | AddEventListenerOptions,
): void => {
reRegister?.();
const argMap = listenerMap.get(name) ?? new Map<EventListener, Set<number>>();
const encodedOptions = argMap.get(listener as EventListener) ?? new Set();
if (encodedOptions.has(encode(options))) return;
Expand Down Expand Up @@ -62,30 +86,14 @@ export const addTextInputEventListener = <K extends keyof HTMLElementEventMap>(
textinput.addEventListener<K>(name, listener, options);
};

// re-register event listeners when the layout changes
scrapbox.on("layout:changed", () => {
const textinput = textInput();
if (!textinput) return;
for (const [name, argMap] of listenerMap) {
for (const [listener, encodedOptions] of argMap) {
for (const encoded of encodedOptions) {
textinput.addEventListener(
name,
listener as EventListener,
decode(encoded),
);
}
}
}
});

export const removeTextInputEventListener = <
K extends keyof HTMLElementEventMap,
>(
name: K,
listener: (event: HTMLElementEventMap[K]) => unknown,
options?: boolean | AddEventListenerOptions,
): void => {
reRegister?.();
const argMap = listenerMap.get(name);
if (!argMap) return;
const encodedOptions = argMap.get(listener as EventListener);
Expand Down
Loading

0 comments on commit 865e7ff

Please sign in to comment.