|
| 1 | +import type { Task } from "./Task.js"; |
| 2 | + |
1 | 3 | /** |
2 | 4 | * The problem with throwing an exception in JavaScript is that the caught error |
3 | 5 | * is always of an unknown type. The unknown type is a problem because we can't |
|
82 | 84 | * }; |
83 | 85 | * ``` |
84 | 86 | * |
| 87 | + * For lazy, cancellable async operations, see {@link Task}. |
| 88 | + * |
85 | 89 | * ### Naming convention |
86 | 90 | * |
87 | | - * - For values: `const user = getUser()` |
88 | | - * - For a single void operation: `const result = foo()` |
89 | | - * - For multiple void operations: use descriptive names for all |
| 91 | + * - **For values you need:** use a name without Result suffix (`user`, `config`) |
| 92 | + * - **For void operations:** use `result` (no value to name) |
| 93 | + * |
| 94 | + * For multiple void operations, use block scopes to avoid potentially long |
| 95 | + * names like `createBaseTablesResult`, `createRelayTablesResult`, or counters |
| 96 | + * like `result1`, `result2`: |
90 | 97 | * |
91 | 98 | * ```ts |
92 | 99 | * const processUser = () => { |
93 | | - * // we have a value |
94 | 100 | * const user = getUser(); |
95 | 101 | * if (!user.ok) return user; |
96 | 102 | * |
97 | | - * // single void operation |
98 | 103 | * const result = saveToDatabase(user.value); |
99 | 104 | * if (!result.ok) return result; |
100 | 105 | * |
101 | 106 | * return ok(); |
102 | 107 | * }; |
103 | 108 | * |
104 | 109 | * const setupDatabase = () => { |
105 | | - * // multiple void operations - use descriptive names |
106 | | - * const baseTables = createBaseTables(); |
107 | | - * if (!baseTables.ok) return baseTables; |
108 | | - * |
109 | | - * const relayTables = createRelayTables(); |
110 | | - * if (!relayTables.ok) return relayTables; |
| 110 | + * // Multiple void operations - use block scopes to avoid name clash |
| 111 | + * { |
| 112 | + * const result = createBaseTables(); |
| 113 | + * if (!result.ok) return result; |
| 114 | + * } |
| 115 | + * { |
| 116 | + * const result = createRelayTables(); |
| 117 | + * if (!result.ok) return result; |
| 118 | + * } |
111 | 119 | * |
112 | 120 | * return ok(); |
113 | 121 | * }; |
|
124 | 132 | * schema, and initializes the database, stopping on the first error: |
125 | 133 | * |
126 | 134 | * ```ts |
127 | | - * const resetResult = deps.sqlite.transaction(() => { |
128 | | - * const dropAllTablesResult = dropAllTables(deps); |
129 | | - * if (!dropAllTablesResult.ok) return dropAllTablesResult; |
| 135 | + * const result = deps.sqlite.transaction(() => { |
| 136 | + * const result = dropAllTables(deps); |
| 137 | + * if (!result.ok) return result; |
130 | 138 | * |
131 | 139 | * if (message.restore) { |
132 | 140 | * const dbSchema = getDbSchema(deps)(); |
133 | 141 | * if (!dbSchema.ok) return dbSchema; |
134 | 142 | * |
135 | | - * const ensureDbSchemaResult = ensureDbSchema(deps)( |
136 | | - * message.restore.dbSchema, |
137 | | - * dbSchema.value, |
138 | | - * ); |
139 | | - * if (!ensureDbSchemaResult.ok) return ensureDbSchemaResult; |
140 | | - * |
141 | | - * const initializeDbResult = initializeDb(deps)( |
142 | | - * message.restore.mnemonic, |
143 | | - * ); |
144 | | - * if (!initializeDbResult.ok) return initializeDbResult; |
| 143 | + * { |
| 144 | + * const result = ensureDbSchema(deps)( |
| 145 | + * message.restore.dbSchema, |
| 146 | + * dbSchema.value, |
| 147 | + * ); |
| 148 | + * if (!result.ok) return result; |
| 149 | + * } |
| 150 | + * { |
| 151 | + * const result = initializeDb(deps)(message.restore.mnemonic); |
| 152 | + * if (!result.ok) return result; |
| 153 | + * } |
145 | 154 | * } |
146 | 155 | * return ok(); |
147 | 156 | * }); |
148 | 157 | * |
149 | | - * if (!resetResult.ok) { |
150 | | - * deps.postMessage({ type: "onError", error: resetResult.error }); |
| 158 | + * if (!result.ok) { |
| 159 | + * deps.postMessage({ type: "onError", error: result.error }); |
151 | 160 | * return; |
152 | 161 | * } |
153 | 162 | * ``` |
|
254 | 263 | * ```ts |
255 | 264 | * // ✅ Safe to return void - unsafe code is wrapped and error is handled |
256 | 265 | * const processData = (data: string): void => { |
257 | | - * const parseResult = trySync( |
| 266 | + * const result = trySync( |
258 | 267 | * () => JSON.parse(data), |
259 | 268 | * (error) => ({ type: "ParseError", message: String(error) }), |
260 | 269 | * ); |
261 | 270 | * |
262 | | - * if (!parseResult.ok) { |
263 | | - * logError(parseResult.error); |
| 271 | + * if (!result.ok) { |
| 272 | + * logError(result.error); |
264 | 273 | * return; |
265 | 274 | * } |
266 | 275 | * |
|
0 commit comments