Skip to content

Commit 06201c9

Browse files
committed
use normal function syntax
1 parent bb4d2f6 commit 06201c9

File tree

5 files changed

+100
-107
lines changed

5 files changed

+100
-107
lines changed

sites/preview/src/routes/+page.svelte

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@
154154
NameConflictResolution
155155
>();
156156
157-
const onRenameError = ({ error, name }: RenameErrorArgs): void => {
157+
function onRenameError({ error, name }: RenameErrorArgs): void {
158158
switch (error) {
159159
case "empty": {
160160
toast.error("Name cannot be empty");
@@ -165,16 +165,16 @@
165165
break;
166166
}
167167
}
168-
};
168+
}
169169
170-
const onMoveError = ({ target }: MoveErrorArgs): void => {
170+
function onMoveError({ target }: MoveErrorArgs): void {
171171
toast.error(`Cannot move "${target.name}" into or next to itself`);
172-
};
172+
}
173173
174-
const onNameConflict = ({
174+
function onNameConflict({
175175
operation,
176176
target,
177-
}: NameConflictArgs): Promise<NameConflictResolution> => {
177+
}: NameConflictArgs): Promise<NameConflictResolution> {
178178
const description = `An item with the name "${target.name}" already exists`;
179179
switch (operation) {
180180
case "move": {
@@ -190,7 +190,7 @@
190190
});
191191
}
192192
}
193-
};
193+
}
194194
</script>
195195

196196
<main class="p-8">

sites/sveltekit-example/src/hooks.server.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { db } from "$lib/server/database/index.js";
22
import type { ServerInit } from "@sveltejs/kit";
33

4-
const seedDatabase = (): void => {
4+
function seedDatabase(): void {
55
db.insertFiles({
66
parentId: null,
77
start: 0,
@@ -108,7 +108,7 @@ const seedDatabase = (): void => {
108108
},
109109
],
110110
});
111-
};
111+
}
112112

113113
export const init: ServerInit = () => {
114114
seedDatabase();

sites/sveltekit-example/src/lib/server/database/utils.ts

Lines changed: 56 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,5 @@
11
import Database from "better-sqlite3";
22

3-
export type FileInsert =
4-
| {
5-
type: "file";
6-
name: string;
7-
}
8-
| {
9-
type: "folder";
10-
name: string;
11-
children: Array<FileInsert>;
12-
};
13-
143
export type FileSelect = {
154
id: number;
165
type: "file" | "folder";
@@ -22,7 +11,18 @@ export type FileSelect = {
2211
children: Array<FileSelect>;
2312
};
2413

25-
export const createDatabase = (filename: string) => {
14+
export type FileInsert =
15+
| {
16+
type: "file";
17+
name: string;
18+
}
19+
| {
20+
type: "folder";
21+
name: string;
22+
children: Array<FileInsert>;
23+
};
24+
25+
export function createDatabase(filename: string) {
2626
const db = new Database(filename);
2727
db.pragma("foreign_keys = ON");
2828
db.exec(`
@@ -98,7 +98,7 @@ export const createDatabase = (filename: string) => {
9898
`),
9999
};
100100

101-
const getFiles = (): Array<FileSelect> => {
101+
function getFiles(): Array<FileSelect> {
102102
const lookup = new Map<number, FileSelect>();
103103
for (const file of statements.getFiles.all()) {
104104
lookup.set(file.id, {
@@ -137,13 +137,9 @@ export const createDatabase = (filename: string) => {
137137
}
138138
}
139139
return files;
140-
};
140+
}
141141

142-
const _insertFiles = (
143-
parentId: number | null,
144-
start: number,
145-
inserted: Array<FileInsert>,
146-
): void => {
142+
function _insertFiles(parentId: number | null, start: number, inserted: Array<FileInsert>): void {
147143
for (let i = 0; i < inserted.length; i++) {
148144
const file = inserted[i];
149145
const result = statements.insertFile.run({
@@ -157,50 +153,47 @@ export const createDatabase = (filename: string) => {
157153
_insertFiles(result.lastInsertRowid as number, 0, file.children);
158154
}
159155
}
160-
};
161-
162-
const insertFiles = db.transaction(
163-
({
164-
parentId,
156+
}
157+
158+
function insertFiles({
159+
parentId,
160+
start,
161+
inserted,
162+
}: {
163+
parentId: number | null;
164+
start: number;
165+
inserted: Array<FileInsert>;
166+
}): void {
167+
statements.shiftParentChildren.run({
168+
by: inserted.length,
169+
parent_id: parentId,
165170
start,
166-
inserted,
167-
}: {
168-
parentId: number | null;
169-
start: number;
170-
inserted: Array<FileInsert>;
171-
}): void => {
172-
statements.shiftParentChildren.run({
173-
by: inserted.length,
174-
parent_id: parentId,
175-
start,
176-
});
177-
_insertFiles(parentId, start, inserted);
178-
},
179-
);
171+
});
172+
173+
_insertFiles(parentId, start, inserted);
174+
}
180175

181-
const renameFile = ({ id, name }: { id: number; name: string }): void => {
176+
function renameFile({ id, name }: { id: number; name: string }): void {
182177
statements.renameFile.run({ id, name });
183-
};
178+
}
184179

185-
const moveFiles = db.transaction(
186-
(
187-
moved: Array<{
188-
id: number;
189-
parentId: number | null;
190-
index: number;
191-
}>,
192-
): void => {
193-
for (const { id, parentId, index } of moved) {
194-
statements.moveFile.run({
195-
id,
196-
parent_id: parentId,
197-
index_in_parent: index,
198-
});
199-
}
200-
},
201-
);
180+
function moveFiles(
181+
moved: Array<{
182+
id: number;
183+
parentId: number | null;
184+
index: number;
185+
}>,
186+
): void {
187+
for (const { id, parentId, index } of moved) {
188+
statements.moveFile.run({
189+
id,
190+
parent_id: parentId,
191+
index_in_parent: index,
192+
});
193+
}
194+
}
202195

203-
const deleteFiles = db.transaction((ids: Array<number>): void => {
196+
function deleteFiles(ids: Array<number>): void {
204197
const parentIds = new Set<number | null>();
205198
for (const id of ids) {
206199
const file = statements.getFile.get({ id });
@@ -225,13 +218,13 @@ export const createDatabase = (filename: string) => {
225218
}
226219
}
227220
}
228-
});
221+
}
229222

230223
return {
231224
getFiles,
232-
insertFiles,
225+
insertFiles: db.transaction(insertFiles),
233226
renameFile,
234-
moveFiles,
235-
deleteFiles,
227+
moveFiles: db.transaction(moveFiles),
228+
deleteFiles: db.transaction(deleteFiles),
236229
};
237-
};
230+
}

sites/sveltekit-example/src/routes/+page.svelte

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,13 @@
5454
5555
let disabledIds = new SvelteSet<string>();
5656
57-
const mutate = async ({
57+
async function mutate({
5858
affected,
5959
mutation,
6060
}: {
6161
affected: Array<FileTreeNode>;
6262
mutation: () => Promise<unknown>;
63-
}): Promise<void> => {
63+
}): Promise<void> {
6464
for (const node of affected) {
6565
disabledIds.add(node.id);
6666
}
@@ -76,9 +76,9 @@
7676
}
7777
}
7878
}
79-
};
79+
}
8080
81-
const optimisticRenameItem = ({ target, name }: RenameItemArgs): Promise<void> => {
81+
function optimisticRenameItem({ target, name }: RenameItemArgs): Promise<void> {
8282
target.name = name;
8383
8484
return mutate({
@@ -89,18 +89,18 @@
8989
name,
9090
}),
9191
});
92-
};
92+
}
9393
94-
const onRenameItem = (args: RenameItemArgs): boolean => {
94+
function onRenameItem(args: RenameItemArgs): boolean {
9595
toast.promise(optimisticRenameItem(args), {
9696
loading: "Updating database",
9797
success: "Renamed item successfully",
9898
error: "Failed to rename item",
9999
});
100100
return true;
101-
};
101+
}
102102
103-
const onRenameError = ({ error, name }: RenameErrorArgs): void => {
103+
function onRenameError({ error, name }: RenameErrorArgs): void {
104104
switch (error) {
105105
case "empty": {
106106
toast.error("Name cannot be empty");
@@ -111,9 +111,9 @@
111111
break;
112112
}
113113
}
114-
};
114+
}
115115
116-
const optimisticMoveItems = ({ updates }: MoveItemsArgs): Promise<void> => {
116+
function optimisticMoveItems({ updates }: MoveItemsArgs): Promise<void> {
117117
const body: MoveFilesBody = [];
118118
for (const { target, children } of updates) {
119119
const targetId = target instanceof FolderNode ? Number(target.id) : null;
@@ -145,22 +145,22 @@
145145
affected,
146146
mutation: () => api.moveFiles(body),
147147
});
148-
};
148+
}
149149
150-
const onMoveItems = (args: MoveItemsArgs): boolean => {
150+
function onMoveItems(args: MoveItemsArgs): boolean {
151151
toast.promise(optimisticMoveItems(args), {
152152
loading: "Updating database",
153153
success: "Moved items successfully",
154154
error: "Failed to move items",
155155
});
156156
return true;
157-
};
157+
}
158158
159-
const onMoveError = ({ target }: MoveErrorArgs): void => {
159+
function onMoveError({ target }: MoveErrorArgs): void {
160160
toast.error(`Cannot move "${target.name}" into or next to itself`);
161-
};
161+
}
162162
163-
const optimisticInsertItems = ({ target, start, inserted }: InsertItemsArgs): Promise<void> => {
163+
function optimisticInsertItems({ target, start, inserted }: InsertItemsArgs): Promise<void> {
164164
target.children.splice(start, 0, ...inserted);
165165
166166
return mutate({
@@ -172,23 +172,23 @@
172172
inserted: inserted.map((node) => node.toJSON()),
173173
}),
174174
});
175-
};
175+
}
176176
177-
const onInsertItems = (args: InsertItemsArgs): boolean => {
177+
function onInsertItems(args: InsertItemsArgs): boolean {
178178
toast.promise(optimisticInsertItems(args), {
179179
loading: "Updating database",
180180
success: "Inserted items successfully",
181181
error: "Failed to insert items",
182182
});
183183
return true;
184-
};
184+
}
185185
186-
const onNameConflict = ({ target }: NameConflictArgs): NameConflictResolution => {
186+
function onNameConflict({ target }: NameConflictArgs): NameConflictResolution {
187187
toast.error(`An item with the name "${target.name}" already exists`);
188188
return "cancel";
189-
};
189+
}
190190
191-
const optimisticDeleteItems = ({ updates, deleted }: DeleteItemsArgs): Promise<void> => {
191+
function optimisticDeleteItems({ updates, deleted }: DeleteItemsArgs): Promise<void> {
192192
for (const { target, children } of updates) {
193193
target.children = children;
194194
}
@@ -208,16 +208,16 @@
208208
affected,
209209
mutation: () => api.deleteFiles(deletedIds),
210210
});
211-
};
211+
}
212212
213-
const onDeleteItems = (args: DeleteItemsArgs): boolean => {
213+
function onDeleteItems(args: DeleteItemsArgs): boolean {
214214
toast.promise(optimisticDeleteItems(args), {
215215
loading: "Updating database",
216216
success: "Deleted items successfully",
217217
error: "Failed to delete items",
218218
});
219219
return true;
220-
};
220+
}
221221
</script>
222222

223223
<main class="p-8">

0 commit comments

Comments
 (0)