Skip to content

perf: Make export identifiers tree-shakable #212

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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