Commit a5fc307
Incorporate timers into main Node.js lib defs and align with v24 (#55079)
Summary:
Pull Request resolved: #55079
This is an AI-assisted change to consolidate Metro's separate `node.js` file into the main Node.js library definitions and align with Node.js v24 (effectively v22) standards.
**Consolidation Changes:**
1. **Incorporated Separate File** - Removed duplication
- Moved definitions from `xplat/js/tools/metro/flow-typed/node.js` into main `node.js` files
- Deleted the separate node.js file (no longer needed)
- Both Metro and React Native now use unified definitions
- Positioned alphabetically between 'tls' and 'url' modules
**Timeout Class Enhancements:**
2. **Complete Timeout Class** - All timer management methods
- `close()` - Cancels the timeout (added in v0.9.1)
- `hasRef()` - Returns true if timer will keep Node.js running (added in v11.0.0)
- `ref()` - Request timer keep Node.js running (added in v0.9.1)
- `refresh()` - Refresh timer's start time (added in v10.2.0)
- `unref()` - Allow Node.js to exit if timer is only active thing (added in v0.9.1)
- `[Symbol.toPrimitive]` - Get numeric timer ID (added in v14.9.0) - Uses `[key: $SymbolToPrimitive]` syntax
- https://nodejs.org/api/timers.html#class-timeout
**Immediate Class Enhancements:**
3. **Complete Immediate Class** - Immediate timer management
- `hasRef()` - Returns true if immediate will keep Node.js running (added in v11.0.0)
- `ref()` - Request immediate keep Node.js running (added in v9.7.0, v8.12.0)
- `unref()` - Allow Node.js to exit if immediate is only active thing (added in v9.7.0, v8.12.0)
- https://nodejs.org/api/timers.html#class-immediate
**Core Timer Functions:**
4. **setTimeout/setInterval/setImmediate** - Complete signatures
- Generic `<TArgs: Iterable<mixed>>` for type-safe callback arguments
- `setTimeout(callback, delay?, ...args)` - Returns `Timeout`
- `setInterval(callback, delay?, ...args)` - Returns `Timeout`
- `setImmediate(callback, ...args)` - Returns `Immediate`
- All support passing additional arguments to callback
- https://nodejs.org/api/timers.html#settimeoutcallback-delay-args
5. **Clear Functions** - Flexible cancellation
- `clearTimeout(timeout?)` - Accepts `Timeout` or numeric ID
- `clearInterval(timeout?)` - Accepts `Timeout` or numeric ID
- `clearImmediate(immediate?)` - Accepts `Immediate` or numeric ID
- All parameters optional (no-op if not provided)
- https://nodejs.org/api/timers.html#cleartimeouttimeout
**Promises API (timers/promises):**
6. **Promise-based Timers** - Async/await compatible
- `setTimeout<T>(delay?, value?, options?)` - Returns `Promise<T>` (added in v15.0.0)
- `setImmediate<T>(value?, options?)` - Returns `Promise<T>` (added in v15.0.0)
- `setInterval<T>(delay?, value?, options?)` - Returns `AsyncIterator<T>` (added in v15.9.0)
- All support AbortSignal for cancellation
- https://nodejs.org/api/timers.html#timers-promises-api
7. **TimerOptions Type** - Modern options object
- `ref?: boolean` - Control if timer keeps Node.js running
- `signal?: AbortSignal` - Abort controller for cancellation
- Type uses `Readonly<{...}>` for input immutability
- https://nodejs.org/api/timers.html#timerspromisessettimeoutdelay-value-options
8. **setInterval as AsyncIterator** - Async iteration support (added in v15.9.0)
- Returns `AsyncIterator<T>` instead of `Promise<T>`
- Enables `for await (const value of setInterval(100, data))` patterns
- Useful for periodic async operations
- https://nodejs.org/api/timers.html#timerspromisessetintervaldelay-value-options
**Scheduler API:**
9. **scheduler object** - Task scheduling utilities (added in v16.0.0)
- `scheduler.wait(delay, options?)` - Promise-based delay (alternative to setTimeout)
- `scheduler.yield()` - Yield to event loop (microtask yield)
- Object marked as `Readonly<{...}>` for immutability
- https://nodejs.org/api/timers.html#timerspromisesschedulerwaitdelay-options
**Type Safety Improvements:**
10. **Readonly Input Types** - Applied consistently
- `TimerOptions`: `Readonly<{ref?, signal?}>`
- `scheduler` object: `Readonly<{wait, yield}>`
- Allows passing readonly types safely (inputs should be readonly)
11. **Generic Type Parameters** - Type-safe callbacks
- Timer functions: `<TArgs: Iterable<mixed>>` for variadic args
- Promises API: `<T>` for resolved value types
- Default generic: `<T = void>` for setInterval iterator
12. **Export Type Aliases** - Better developer experience
- `Timeout` and `Immediate` exported as types from module
- Consistent `timers$` prefix for internal types
- Clean module exports without duplication
**Symbol Support:**
13. **Symbol.toPrimitive** - Convert Timeout to number (added in v14.9.0)
- Allows implicit conversion: `Number(timeout)` or `+timeout`
- Returns the numeric timer ID
- Flow syntax: `[key: $SymbolToPrimitive]: () => number`
- https://nodejs.org/api/timers.html#timeoutsymboltoprimitive
**Flow Limitations:**
14. **Symbol.dispose not included** - Explicit resource management
- `[key: $SymbolDispose]: () => void` is commented out
- Node.js added Symbol.dispose in v18.18.0, v20.4.0
- Flow doesn't fully support this symbol yet or has issues with multiple symbol-keyed properties
- Will be enabled when Flow support improves
- https://nodejs.org/api/timers.html#timeoutsymboldispose
**References:**
- Node.js timers module docs: https://nodejs.org/api/timers.html
- TC39 Explicit Resource Management: https://github.com/tc39/proposal-explicit-resource-management
Changelog: [Internal]
---
> Generated by [Confucius Code Assist (CCA)](https://www.internalfb.com/wiki/Confucius/Analect/Shared_Analects/Confucius_Code_Assist_(CCA)/)
[Confucius Session](https://www.internalfb.com/confucius?host=devvm45708.cln0.facebook.com&port=8086&tab=Chat&session_id=1a3aa26e-e5a9-11f0-8d47-71a4a90f0494&entry_name=Code+Assist), [Trace](https://www.internalfb.com/confucius?session_id=1a3aa26e-e5a9-11f0-8d47-71a4a90f0494&tab=Trace)
Reviewed By: vzaidman
Differential Revision: D89944814
fbshipit-source-id: 38c1773d1a3591ef9f123681655720d17e7e53bb1 parent ac06f3b commit a5fc307
1 file changed
Lines changed: 77 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3702 | 3702 | | |
3703 | 3703 | | |
3704 | 3704 | | |
| 3705 | + | |
| 3706 | + | |
| 3707 | + | |
| 3708 | + | |
| 3709 | + | |
| 3710 | + | |
| 3711 | + | |
| 3712 | + | |
| 3713 | + | |
| 3714 | + | |
| 3715 | + | |
| 3716 | + | |
| 3717 | + | |
| 3718 | + | |
| 3719 | + | |
| 3720 | + | |
| 3721 | + | |
| 3722 | + | |
| 3723 | + | |
| 3724 | + | |
| 3725 | + | |
| 3726 | + | |
| 3727 | + | |
| 3728 | + | |
| 3729 | + | |
| 3730 | + | |
| 3731 | + | |
| 3732 | + | |
| 3733 | + | |
| 3734 | + | |
| 3735 | + | |
| 3736 | + | |
| 3737 | + | |
| 3738 | + | |
| 3739 | + | |
| 3740 | + | |
| 3741 | + | |
| 3742 | + | |
| 3743 | + | |
| 3744 | + | |
| 3745 | + | |
| 3746 | + | |
| 3747 | + | |
| 3748 | + | |
| 3749 | + | |
| 3750 | + | |
| 3751 | + | |
| 3752 | + | |
| 3753 | + | |
| 3754 | + | |
| 3755 | + | |
| 3756 | + | |
| 3757 | + | |
| 3758 | + | |
| 3759 | + | |
| 3760 | + | |
| 3761 | + | |
| 3762 | + | |
| 3763 | + | |
| 3764 | + | |
| 3765 | + | |
| 3766 | + | |
| 3767 | + | |
| 3768 | + | |
| 3769 | + | |
| 3770 | + | |
| 3771 | + | |
| 3772 | + | |
| 3773 | + | |
3705 | 3774 | | |
3706 | 3775 | | |
3707 | 3776 | | |
| |||
5057 | 5126 | | |
5058 | 5127 | | |
5059 | 5128 | | |
| 5129 | + | |
| 5130 | + | |
| 5131 | + | |
| 5132 | + | |
| 5133 | + | |
| 5134 | + | |
| 5135 | + | |
| 5136 | + | |
5060 | 5137 | | |
5061 | 5138 | | |
5062 | 5139 | | |
| |||
0 commit comments