@fictjs/runtime 0.0.12 → 0.0.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/signal.ts CHANGED
@@ -2,6 +2,11 @@ import { beginFlushGuard, beforeEffectRunGuard, endFlushGuard } from './cycle-gu
2
2
  import { getDevtoolsHook } from './devtools'
3
3
  import { registerRootCleanup } from './lifecycle'
4
4
 
5
+ const isDev =
6
+ typeof __DEV__ !== 'undefined'
7
+ ? __DEV__
8
+ : typeof process === 'undefined' || process.env?.NODE_ENV !== 'production'
9
+
5
10
  // ============================================================================
6
11
  // Type Definitions
7
12
  // ============================================================================
@@ -1177,21 +1182,28 @@ export function endBatch(): void {
1177
1182
  */
1178
1183
  export function batch<T>(fn: () => T): T {
1179
1184
  ++batchDepth
1180
- let _error: unknown
1181
- let hasError = false
1185
+ let result!: T
1186
+ let error: unknown
1182
1187
  try {
1183
- return fn()
1188
+ result = fn()
1184
1189
  } catch (e) {
1185
- _error = e
1186
- hasError = true
1187
- throw e
1190
+ error = e
1188
1191
  } finally {
1189
1192
  --batchDepth
1190
- // Only flush if no error occurred to avoid interfering with error propagation
1191
- if (!hasError && batchDepth === 0) {
1192
- flush()
1193
+ if (batchDepth === 0) {
1194
+ try {
1195
+ flush()
1196
+ } catch (flushErr) {
1197
+ if (error === undefined) {
1198
+ error = flushErr
1199
+ }
1200
+ }
1193
1201
  }
1194
1202
  }
1203
+ if (error !== undefined) {
1204
+ throw error
1205
+ }
1206
+ return result
1195
1207
  }
1196
1208
  /**
1197
1209
  * Get the current active subscriber
@@ -1335,43 +1347,51 @@ export default {
1335
1347
  }
1336
1348
  export const $state = signal as <T>(value: T) => T
1337
1349
 
1338
- let devtoolsSignalId = 0
1339
- let devtoolsEffectId = 0
1340
-
1341
1350
  interface DevtoolsIdentifiable {
1342
1351
  __id?: number
1343
1352
  }
1344
1353
 
1345
- function registerSignalDevtools(value: unknown, node: SignalNode): number | undefined {
1346
- const hook = getDevtoolsHook()
1347
- if (!hook) return undefined
1348
- const id = ++devtoolsSignalId
1349
- hook.registerSignal(id, value)
1350
- ;(node as SignalNode & DevtoolsIdentifiable).__id = id
1351
- return id
1352
- }
1354
+ let registerSignalDevtools: (value: unknown, node: SignalNode) => number | undefined = () =>
1355
+ undefined
1356
+ let updateSignalDevtools: (node: SignalNode, value: unknown) => void = () => {}
1357
+ let registerEffectDevtools: (node: EffectNode) => number | undefined = () => undefined
1358
+ let effectRunDevtools: (node: EffectNode) => void = () => {}
1359
+
1360
+ if (isDev) {
1361
+ let devtoolsSignalId = 0
1362
+ let devtoolsEffectId = 0
1363
+
1364
+ registerSignalDevtools = (value, node) => {
1365
+ const hook = getDevtoolsHook()
1366
+ if (!hook) return undefined
1367
+ const id = ++devtoolsSignalId
1368
+ hook.registerSignal(id, value)
1369
+ ;(node as SignalNode & DevtoolsIdentifiable).__id = id
1370
+ return id
1371
+ }
1353
1372
 
1354
- function updateSignalDevtools(node: SignalNode, value: unknown): void {
1355
- const hook = getDevtoolsHook()
1356
- if (!hook) return
1357
- const id = (node as SignalNode & DevtoolsIdentifiable).__id
1358
- if (id) hook.updateSignal(id, value)
1359
- }
1373
+ updateSignalDevtools = (node, value) => {
1374
+ const hook = getDevtoolsHook()
1375
+ if (!hook) return
1376
+ const id = (node as SignalNode & DevtoolsIdentifiable).__id
1377
+ if (id) hook.updateSignal(id, value)
1378
+ }
1360
1379
 
1361
- function registerEffectDevtools(node: EffectNode): number | undefined {
1362
- const hook = getDevtoolsHook()
1363
- if (!hook) return undefined
1364
- const id = ++devtoolsEffectId
1365
- hook.registerEffect(id)
1366
- ;(node as EffectNode & DevtoolsIdentifiable).__id = id
1367
- return id
1368
- }
1380
+ registerEffectDevtools = node => {
1381
+ const hook = getDevtoolsHook()
1382
+ if (!hook) return undefined
1383
+ const id = ++devtoolsEffectId
1384
+ hook.registerEffect(id)
1385
+ ;(node as EffectNode & DevtoolsIdentifiable).__id = id
1386
+ return id
1387
+ }
1369
1388
 
1370
- function effectRunDevtools(node: EffectNode): void {
1371
- const hook = getDevtoolsHook()
1372
- if (!hook) return
1373
- const id = (node as EffectNode & DevtoolsIdentifiable).__id
1374
- if (id) hook.effectRun(id)
1389
+ effectRunDevtools = node => {
1390
+ const hook = getDevtoolsHook()
1391
+ if (!hook) return
1392
+ const id = (node as EffectNode & DevtoolsIdentifiable).__id
1393
+ if (id) hook.effectRun(id)
1394
+ }
1375
1395
  }
1376
1396
 
1377
1397
  // ============================================================================