Skip to content

Commit

Permalink
fix: minor performance improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
nishantwrp committed Nov 11, 2023
1 parent 0398499 commit 5cf7e8d
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 28 deletions.
63 changes: 37 additions & 26 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { TemplateAction, performAction } from "./actions";
import { loadLegacyTemplates } from "./legacyTemplates";
import * as open from "open";
import { Logger } from "./logger";
import { PromiseGroup } from "./utils/promises";

const DOCUMENTATION_URL = "https://github.com/joplin/plugin-templates#readme";

Expand Down Expand Up @@ -41,7 +42,7 @@ joplin.plugins.register({
description: "Apply tags using 'template_tags' variable while inserting template to notes/to-dos.",
section: "templatesPlugin"
},
"templatesSource":{
"templatesSource": {
public: true,
type: SettingItemType.String,
isEnum: true,
Expand All @@ -58,12 +59,18 @@ joplin.plugins.register({


// Global variables
const dialogViewHandle = await joplin.views.dialogs.create("dialog");
const joplinGlobalApis = new PromiseGroup();

const userLocale = await joplin.settings.globalValue("locale");
const userDateFormat = await joplin.settings.globalValue("dateFormat");
const userTimeFormat = await joplin.settings.globalValue("timeFormat");
const profileDir = await joplin.settings.globalValue("profileDir");
joplinGlobalApis.set("dialogViewHandle", joplin.views.dialogs.create("dialog"));
joplinGlobalApis.set("userLocale", joplin.settings.globalValue("locale"));
joplinGlobalApis.set("userDateFormat", joplin.settings.globalValue("dateFormat"));
joplinGlobalApis.set("userTimeFormat", joplin.settings.globalValue("timeFormat"));
joplinGlobalApis.set("profileDir", joplin.settings.globalValue("profileDir"));

const {
dialogViewHandle, userLocale, userDateFormat,
userTimeFormat, profileDir
} = await joplinGlobalApis.groupAll();

const dateAndTimeUtils = new DateAndTimeUtils(userLocale, userDateFormat, userTimeFormat);
const logger = new Logger(profileDir);
Expand All @@ -89,31 +96,33 @@ joplin.plugins.register({


// Register all commands
await joplin.commands.register({
const joplinCommands = new PromiseGroup();

joplinCommands.add(joplin.commands.register({
name: "createNoteFromTemplate",
label: "Create note from template",
execute: async () => {
await getTemplateAndPerformAction(TemplateAction.NewNote);
}
});
}));

await joplin.commands.register({
joplinCommands.add(joplin.commands.register({
name: "createTodoFromTemplate",
label: "Create to-do from template",
execute: async () => {
await getTemplateAndPerformAction(TemplateAction.NewTodo);
}
});
}));

await joplin.commands.register({
joplinCommands.add(joplin.commands.register({
name: "insertTemplate",
label: "Insert template",
execute: async () => {
await getTemplateAndPerformAction(TemplateAction.InsertText);
}
});
}));

await joplin.commands.register({
joplinCommands.add(joplin.commands.register({
name: "showDefaultTemplates",
label: "Show default templates",
execute: async () => {
Expand All @@ -126,9 +135,9 @@ joplin.plugins.register({
await setDefaultTemplatesView(dialogViewHandle, noteTemplateTitle, todoTemplateTitle);
await joplin.views.dialogs.open(dialogViewHandle);
}
});
}));

await joplin.commands.register({
joplinCommands.add(joplin.commands.register({
name: "setDefaultNoteTemplate",
label: "Set default note template",
execute: async () => {
Expand All @@ -138,9 +147,9 @@ joplin.plugins.register({
await joplin.views.dialogs.showMessageBox("Default note template set successfully!");
}
}
});
}));

await joplin.commands.register({
joplinCommands.add(joplin.commands.register({
name: "setDefaultTodoTemplate",
label: "Set default to-do template",
execute: async () => {
Expand All @@ -150,9 +159,9 @@ joplin.plugins.register({
await joplin.views.dialogs.showMessageBox("Default to-do template set successfully!");
}
}
});
}));

await joplin.commands.register({
joplinCommands.add(joplin.commands.register({
name: "createNoteFromDefaultTemplate",
label: "Create note from default template",
execute: async () => {
Expand All @@ -162,9 +171,9 @@ joplin.plugins.register({
}
await joplin.views.dialogs.showMessageBox("No default note template is set.");
}
});
}));

await joplin.commands.register({
joplinCommands.add(joplin.commands.register({
name: "createTodoFromDefaultTemplate",
label: "Create to-do from default template",
execute: async () => {
Expand All @@ -174,17 +183,17 @@ joplin.plugins.register({
}
await joplin.views.dialogs.showMessageBox("No default to-do template is set.");
}
});
}));

await joplin.commands.register({
joplinCommands.add(joplin.commands.register({
name: "showPluginDocumentation",
label: "Help",
execute: async () => {
open(DOCUMENTATION_URL);
}
});
}));

await joplin.commands.register({
joplinCommands.add(joplin.commands.register({
name: "copyFolderID",
label: "Copy notebook ID",
execute: async (folderId: string) => {
Expand All @@ -196,7 +205,7 @@ joplin.plugins.register({

await joplin.commands.execute("editor.focus");
}
});
}));


// Create templates menu
Expand Down Expand Up @@ -240,6 +249,8 @@ joplin.plugins.register({
}
]);

await joplinCommands.groupAll();


// Folder context menu
await joplin.views.menuItems.create("templates_folderid", "copyFolderID", MenuItemLocation.FolderContextMenu);
Expand Down
3 changes: 1 addition & 2 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export class Parser {
this.utils = dateAndTimeUtils;
this.dialog = dialogViewHandle;
this.logger = logger;
HelperFactory.registerHelpers(this.utils);
}

private getDefaultContext() {
Expand Down Expand Up @@ -228,8 +229,6 @@ export class Parser {
template.body = this.preProcessTemplateBody(template.body);

try {
HelperFactory.registerHelpers(this.utils);

const processedTemplate = frontmatter(template.body);
const templateVariables = processedTemplate.attributes;

Expand Down
31 changes: 31 additions & 0 deletions src/utils/promises.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
export class PromiseGroup {
private promises: { [key: string]: Promise<any> } = {};
private unnamedPromises: Promise<any>[] = [];

public add(promise: Promise<any>): void {
this.unnamedPromises.push(promise);
}

public set(key: string, promise: Promise<any>): void {
if (key in this.promises) {
throw new Error(`key: ${key} already in use`);
}

this.promises[key] = promise;
}

public async groupAll(): Promise<{[key: string]: any}> {
const namedPromises = Object.entries(this.promises);
const allPromises = [...this.unnamedPromises, ...namedPromises.map(np => np[1])];

const resolvedPromises = await Promise.all(allPromises);

const res = {};
for (let i = 0; i < namedPromises.length; i++) {
res[namedPromises[i][0]] = resolvedPromises[this.unnamedPromises.length + i];
}

return res;
}
}
5 changes: 5 additions & 0 deletions tests/parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ describe("Template parser", () => {

beforeEach(() => {
jest.clearAllMocks();

// Parser sets handlebars helpers only once in constructor block
// and it outlives the scope of the parser as the helpers are set on
// Handlebars level. So, just reset to default before running each test.
new Parser(dateAndTimeUtils, "variable-dialog", logger);
});

test("should parse built-in variables correctly", async () => {
Expand Down

0 comments on commit 5cf7e8d

Please sign in to comment.