react-native-nitro-input 0.4.1 β 0.4.4
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/CHANGELOG.md +41 -0
- package/NitroInput.podspec +2 -2
- package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroNumberView.kt +24 -1
- package/android/src/main/java/com/margelo/nitro/nitroinput/NitroInputView.kt +60 -0
- package/android/src/main/java/com/margelo/nitro/nitroinput/NitroNumberView.kt +290 -137
- package/ios/CodePointOffsets.swift +35 -0
- package/ios/NitroInputView.swift +2 -10
- package/ios/__tests__/CodePointOffsetsTest.swift +77 -0
- package/lib/commonjs/src/NitroNumber.js +94 -29
- package/lib/module/src/NitroNumber.js +94 -29
- package/package.json +2 -1
- package/src/NitroNumber.tsx +121 -54
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
|
|
3
|
+
// Host test for ios/CodePointOffsets.swift: `bun run test:swift`.
|
|
4
|
+
// UITextField's text is a bridged NSString, so every case runs on a native
|
|
5
|
+
// Swift string and on a bridged one.
|
|
6
|
+
|
|
7
|
+
@main
|
|
8
|
+
enum CodePointOffsetsTest {
|
|
9
|
+
static var failures = 0
|
|
10
|
+
static var checks = 0
|
|
11
|
+
|
|
12
|
+
static func expect(_ got: Int, _ want: Int, _ what: @autoclosure () -> String) {
|
|
13
|
+
checks += 1
|
|
14
|
+
if got != want {
|
|
15
|
+
failures += 1
|
|
16
|
+
print("FAIL \(what()): got \(got), want \(want)")
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
static func variants(_ s: String) -> [String] { [s, NSMutableString(string: s) as String] }
|
|
21
|
+
|
|
22
|
+
static func main() {
|
|
23
|
+
// The production crash: a UTF-16 offset inside the first code point's
|
|
24
|
+
// surrogate pair. This trapped (EXC_BREAKPOINT) before the fix.
|
|
25
|
+
for s in variants("πab") {
|
|
26
|
+
expect(CodePointOffsets.codePoint(in: s, utf16: 0), 0, "πab @0")
|
|
27
|
+
expect(CodePointOffsets.codePoint(in: s, utf16: 1), 0, "πab @1 (mid-surrogate)")
|
|
28
|
+
expect(CodePointOffsets.codePoint(in: s, utf16: 2), 1, "πab @2")
|
|
29
|
+
expect(CodePointOffsets.codePoint(in: s, utf16: 4), 3, "πab @4")
|
|
30
|
+
}
|
|
31
|
+
// Inside a later pair it used to count one too few (0 here).
|
|
32
|
+
for s in variants("aπbπ") {
|
|
33
|
+
expect(CodePointOffsets.codePoint(in: s, utf16: 2), 1, "aπbπ @2 (mid-surrogate)")
|
|
34
|
+
expect(CodePointOffsets.codePoint(in: s, utf16: 5), 3, "aπbπ @5 (mid-surrogate)")
|
|
35
|
+
expect(CodePointOffsets.codePoint(in: s, utf16: 6), 4, "aπbπ @6")
|
|
36
|
+
}
|
|
37
|
+
// Out of range clamps.
|
|
38
|
+
for s in variants("ab") {
|
|
39
|
+
expect(CodePointOffsets.codePoint(in: s, utf16: -3), 0, "ab @-3")
|
|
40
|
+
expect(CodePointOffsets.codePoint(in: s, utf16: 9), 2, "ab @9")
|
|
41
|
+
}
|
|
42
|
+
// A lone surrogate (a bridged NSString can hold one mid-edit) is one unit.
|
|
43
|
+
let lone = NSString(characters: [0xD83D, 0x61], length: 2) as String
|
|
44
|
+
expect(CodePointOffsets.codePoint(in: lone, utf16: 1), 1, "lone @1")
|
|
45
|
+
expect(CodePointOffsets.codePoint(in: lone, utf16: 2), 2, "lone @2")
|
|
46
|
+
|
|
47
|
+
// Every offset of strings UIKit edits around: skin tones, ZWJ families,
|
|
48
|
+
// flags, combining marks, CJK. Monotonic, rounds down inside a pair, and
|
|
49
|
+
// agrees with utf16(in:codePoint:) on every scalar boundary.
|
|
50
|
+
let samples = ["", "a", "π", "ππ½x", "π¨βπ©βπ§βπ¦", "πΏπ¦πΊπΈ", "Γ©", "e\u{301}", "ζ₯ζ¬θͺπ", "aπππz"]
|
|
51
|
+
for sample in samples {
|
|
52
|
+
for s in variants(sample) {
|
|
53
|
+
let n = s.utf16.count
|
|
54
|
+
var boundaries = Set<Int>()
|
|
55
|
+
var units = 0
|
|
56
|
+
boundaries.insert(0)
|
|
57
|
+
for scalar in s.unicodeScalars { units += scalar.utf16.count; boundaries.insert(units) }
|
|
58
|
+
var previous = 0
|
|
59
|
+
for offset in 0...n {
|
|
60
|
+
let got = CodePointOffsets.codePoint(in: s, utf16: offset)
|
|
61
|
+
let back = CodePointOffsets.utf16(in: s, codePoint: got)
|
|
62
|
+
checks += 1
|
|
63
|
+
if got < previous || back > offset || (boundaries.contains(offset) && back != offset) {
|
|
64
|
+
failures += 1
|
|
65
|
+
print("FAIL \(sample.debugDescription) @\(offset): \(got) -> \(back)")
|
|
66
|
+
}
|
|
67
|
+
previous = got
|
|
68
|
+
}
|
|
69
|
+
expect(CodePointOffsets.codePoint(in: s, utf16: n), s.unicodeScalars.count, "\(sample.debugDescription) end")
|
|
70
|
+
expect(CodePointOffsets.utf16(in: s, codePoint: 99), n, "\(sample.debugDescription) utf16 clamp")
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
print("CodePointOffsetsTest: \(checks) checks, \(failures) failures")
|
|
75
|
+
if failures > 0 { exit(1) }
|
|
76
|
+
}
|
|
77
|
+
}
|
|
@@ -50,6 +50,73 @@ const formatProps_1 = require("./formatProps");
|
|
|
50
50
|
exports.NativeNitroNumberView = (0, react_native_nitro_modules_1.getHostComponent)('NitroNumberView', () => NitroNumberViewConfig_json_1.default);
|
|
51
51
|
const EMPTY = [];
|
|
52
52
|
const EMPTY_STRINGS = [];
|
|
53
|
+
/**
|
|
54
|
+
* A weak reference where the engine has them (Hermes, JSC, V8), else a strong
|
|
55
|
+
* one. Two functions on purpose: an engine keeps one scope per function for
|
|
56
|
+
* everything its closures capture, so a `() => target` fallback written beside
|
|
57
|
+
* the weak closure would put `target` in the weak closure's scope too.
|
|
58
|
+
*/
|
|
59
|
+
function weakly(target) {
|
|
60
|
+
return typeof WeakRef === 'function' ? weakDeref(new WeakRef(target)) : strongDeref(target);
|
|
61
|
+
}
|
|
62
|
+
function weakDeref(ref) {
|
|
63
|
+
return () => ref.deref();
|
|
64
|
+
}
|
|
65
|
+
function strongDeref(target) {
|
|
66
|
+
return () => target;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* A Nitro callback that calls `handler(self, ...args)` for as long as the
|
|
70
|
+
* component behind `self` is alive, holding `self` only weakly.
|
|
71
|
+
*
|
|
72
|
+
* Why. Nitro keeps every callback prop of a view as a JS function held from
|
|
73
|
+
* C++ (in the Fabric props of its shadow node), which Hermes treats as a GC
|
|
74
|
+
* root. A closure written inside the component shares the render's scope,
|
|
75
|
+
* so it reaches the fiber (through `setSize`) and the hybrid view's handle
|
|
76
|
+
* (through `nativeRef`), and the fiber holds the shadow node that holds the
|
|
77
|
+
* props that hold the closure: a loop through native code that Hermes can
|
|
78
|
+
* never collect. Every unmounted NitroNumber stayed, its fiber subtree, its
|
|
79
|
+
* hybrid view and Nitro's C++ part included: over 4,000 after a minute of
|
|
80
|
+
* a scrolling list, and the app's memory climbed with them. Built here, in
|
|
81
|
+
* module scope, the closure holds only the weak reference and `handler`
|
|
82
|
+
* (itself module-level); the fiber is what keeps `self` alive.
|
|
83
|
+
*/
|
|
84
|
+
function relay(self, handler) {
|
|
85
|
+
return relayTo(weakly(self), handler);
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* The closure itself, made where `self` is not in scope at all: Hermes (in
|
|
89
|
+
* debuggable builds, at least) keeps every variable of a function that makes
|
|
90
|
+
* a closure in that closure's scope, used or not, and a `self` there would
|
|
91
|
+
* hold the component strongly again.
|
|
92
|
+
*/
|
|
93
|
+
function relayTo(live, handler) {
|
|
94
|
+
return (0, react_native_nitro_modules_1.callback)((...args) => {
|
|
95
|
+
const target = live();
|
|
96
|
+
if (target !== undefined)
|
|
97
|
+
handler(target, ...args);
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
function attachNative(self, instance) {
|
|
101
|
+
self.nativeRef.current = instance;
|
|
102
|
+
self.latestOnNativeRef.current?.(instance);
|
|
103
|
+
}
|
|
104
|
+
function applySize(self, width, height) {
|
|
105
|
+
self.setSize((previous) => previous !== null && previous.width === width && previous.height === height ? previous : { width, height });
|
|
106
|
+
}
|
|
107
|
+
function fireRevealEnd(self) {
|
|
108
|
+
self.latestOnRevealEnd.current?.();
|
|
109
|
+
}
|
|
110
|
+
function fireRevealMilestone(self, index, milestone) {
|
|
111
|
+
self.latestOnRevealMilestone.current?.(index, milestone);
|
|
112
|
+
}
|
|
113
|
+
function fireAnimationStart(self) {
|
|
114
|
+
self.latestOnAnimationStart.current?.();
|
|
115
|
+
}
|
|
116
|
+
/** The figure a compact format rolls is not the value (1.2 for 1,234): report the value. */
|
|
117
|
+
function fireAnimationEnd(self, shown) {
|
|
118
|
+
self.latestOnAnimationEnd.current?.(self.compactRef.current ? (0, formatProps_1.toNumber)(self.latestValue.current) : shown);
|
|
119
|
+
}
|
|
53
120
|
/** Each transition's own timing, used when the props leave it unsaid. */
|
|
54
121
|
const TRANSITION_DEFAULTS = {
|
|
55
122
|
roll: { duration: 500, easing: 'easeInOut', stagger: 0 },
|
|
@@ -68,6 +135,8 @@ const TRANSITION_DEFAULTS = {
|
|
|
68
135
|
*/
|
|
69
136
|
exports.NitroNumber = (0, react_1.forwardRef)(function NitroNumber({ value, minorDigits, fractionDigits, minimumIntegerDigits, groupingSeparator, decimalSeparator, prefix, suffix, transition, flashUpColor, flashDownColor, flashDuration, popOnChange, duration, easing, bounce, stagger, direction, reveal, revealStyle, revealDuration, revealBounce, revealGrow, revealStagger, revealMilestones, revealMilestoneHold, onRevealEnd, onRevealMilestone, loading, shimmerColor, shimmerDuration, shimmerAngle, shimmerWidth, shimmerBaseColor, shimmerDirection, shimmerDelay, fontSize, prefixFontSize, suffixFontSize, affixAlign, prefixAlign, suffixAlign, letterSpacing, prefixSpacing, suffixSpacing, prefixOffset, suffixOffset, tabularNums, format, adjustsFontSizeToFit, minimumFontScale, allowFontScaling, maxFontSizeMultiplier, fontWeight, fontFamily, color, textAlign, signDisplay, plusSign, minusSign, prefixColor, suffixColor, fractionColor, fractionFontSize, fractionAlign, digitGlyphs, groupingSizes, digits, continuous, animated, respectReduceMotion, onAnimationStart, onAnimationEnd, onNativeRef, style, ...viewProps }, ref) {
|
|
70
137
|
const nativeRef = (0, react_1.useRef)(null);
|
|
138
|
+
// Whether a compact format is rolling (its figure is not the value); set below.
|
|
139
|
+
const compactRef = (0, react_1.useRef)(false);
|
|
71
140
|
const latestOnNativeRef = (0, react_1.useRef)(onNativeRef);
|
|
72
141
|
latestOnNativeRef.current = onNativeRef;
|
|
73
142
|
const latestOnRevealEnd = (0, react_1.useRef)(onRevealEnd);
|
|
@@ -83,34 +152,31 @@ exports.NitroNumber = (0, react_1.forwardRef)(function NitroNumber({ value, mino
|
|
|
83
152
|
const latestOnAnimationEnd = (0, react_1.useRef)(onAnimationEnd);
|
|
84
153
|
latestOnAnimationEnd.current = onAnimationEnd;
|
|
85
154
|
const [size, setSize] = (0, react_1.useState)(null);
|
|
86
|
-
//
|
|
87
|
-
//
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
//
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
const
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
const
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
const onAnimationEndCallback = (0, react_1.useMemo)(() => (0, react_native_nitro_modules_1.callback)((shown) => {
|
|
112
|
-
latestOnAnimationEnd.current?.(compactRef.current ? (0, formatProps_1.toNumber)(latestValue.current) : shown);
|
|
113
|
-
}), []);
|
|
155
|
+
// The native callbacks: wrapped with `callback()` and referentially stable
|
|
156
|
+
// (a new wrapper re-sets the prop every render), and built in module scope
|
|
157
|
+
// around a weak reference to this component's state (see `relay`).
|
|
158
|
+
const self = (0, react_1.useMemo)(() => ({
|
|
159
|
+
nativeRef,
|
|
160
|
+
latestOnNativeRef,
|
|
161
|
+
setSize,
|
|
162
|
+
latestOnRevealEnd,
|
|
163
|
+
latestOnRevealMilestone,
|
|
164
|
+
latestOnAnimationStart,
|
|
165
|
+
latestOnAnimationEnd,
|
|
166
|
+
compactRef,
|
|
167
|
+
latestValue,
|
|
168
|
+
}),
|
|
169
|
+
// Every member is a ref or a state setter, stable for the component's life.
|
|
170
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
171
|
+
[]);
|
|
172
|
+
const hybridRef = (0, react_1.useMemo)(() => relay(self, attachNative), [self]);
|
|
173
|
+
const onSizeChange = (0, react_1.useMemo)(() => relay(self, applySize), [self]);
|
|
174
|
+
// The latest handlers are read through refs, so a caller passing an inline
|
|
175
|
+
// arrow doesn't re-set the native prop each render.
|
|
176
|
+
const onRevealEndCallback = (0, react_1.useMemo)(() => relay(self, fireRevealEnd), [self]);
|
|
177
|
+
const onRevealMilestoneCallback = (0, react_1.useMemo)(() => relay(self, fireRevealMilestone), [self]);
|
|
178
|
+
const onAnimationStartCallback = (0, react_1.useMemo)(() => relay(self, fireAnimationStart), [self]);
|
|
179
|
+
const onAnimationEndCallback = (0, react_1.useMemo)(() => relay(self, fireAnimationEnd), [self]);
|
|
114
180
|
// A new array literal each render must not re-set the native prop. Always an
|
|
115
181
|
// array (empty = no tiers): removing the prop would reach native as `null`,
|
|
116
182
|
// which Nitro's array parser rejects ("Value is null, expected an Object").
|
|
@@ -150,7 +216,6 @@ exports.NitroNumber = (0, react_1.forwardRef)(function NitroNumber({ value, mino
|
|
|
150
216
|
// rolls the figure 1.2.
|
|
151
217
|
const figure = (0, react_1.useMemo)(() => (format ? (0, formatProps_1.figureOf)(format, input) : undefined), [format, input]);
|
|
152
218
|
const compact = derived?.compact ? figure : undefined;
|
|
153
|
-
const compactRef = (0, react_1.useRef)(false);
|
|
154
219
|
compactRef.current = compact !== undefined;
|
|
155
220
|
// Which way the value moved, for a compact figure that fell while the value grew (999 β 1K).
|
|
156
221
|
const numericValue = (0, formatProps_1.toNumber)(input);
|
|
@@ -11,6 +11,73 @@ import { figureOf, formatProps, toNumber, withMinorDigits } from './formatProps'
|
|
|
11
11
|
export const NativeNitroNumberView = getHostComponent('NitroNumberView', () => NitroNumberViewConfig);
|
|
12
12
|
const EMPTY = [];
|
|
13
13
|
const EMPTY_STRINGS = [];
|
|
14
|
+
/**
|
|
15
|
+
* A weak reference where the engine has them (Hermes, JSC, V8), else a strong
|
|
16
|
+
* one. Two functions on purpose: an engine keeps one scope per function for
|
|
17
|
+
* everything its closures capture, so a `() => target` fallback written beside
|
|
18
|
+
* the weak closure would put `target` in the weak closure's scope too.
|
|
19
|
+
*/
|
|
20
|
+
function weakly(target) {
|
|
21
|
+
return typeof WeakRef === 'function' ? weakDeref(new WeakRef(target)) : strongDeref(target);
|
|
22
|
+
}
|
|
23
|
+
function weakDeref(ref) {
|
|
24
|
+
return () => ref.deref();
|
|
25
|
+
}
|
|
26
|
+
function strongDeref(target) {
|
|
27
|
+
return () => target;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* A Nitro callback that calls `handler(self, ...args)` for as long as the
|
|
31
|
+
* component behind `self` is alive, holding `self` only weakly.
|
|
32
|
+
*
|
|
33
|
+
* Why. Nitro keeps every callback prop of a view as a JS function held from
|
|
34
|
+
* C++ (in the Fabric props of its shadow node), which Hermes treats as a GC
|
|
35
|
+
* root. A closure written inside the component shares the render's scope,
|
|
36
|
+
* so it reaches the fiber (through `setSize`) and the hybrid view's handle
|
|
37
|
+
* (through `nativeRef`), and the fiber holds the shadow node that holds the
|
|
38
|
+
* props that hold the closure: a loop through native code that Hermes can
|
|
39
|
+
* never collect. Every unmounted NitroNumber stayed, its fiber subtree, its
|
|
40
|
+
* hybrid view and Nitro's C++ part included: over 4,000 after a minute of
|
|
41
|
+
* a scrolling list, and the app's memory climbed with them. Built here, in
|
|
42
|
+
* module scope, the closure holds only the weak reference and `handler`
|
|
43
|
+
* (itself module-level); the fiber is what keeps `self` alive.
|
|
44
|
+
*/
|
|
45
|
+
function relay(self, handler) {
|
|
46
|
+
return relayTo(weakly(self), handler);
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* The closure itself, made where `self` is not in scope at all: Hermes (in
|
|
50
|
+
* debuggable builds, at least) keeps every variable of a function that makes
|
|
51
|
+
* a closure in that closure's scope, used or not, and a `self` there would
|
|
52
|
+
* hold the component strongly again.
|
|
53
|
+
*/
|
|
54
|
+
function relayTo(live, handler) {
|
|
55
|
+
return callback((...args) => {
|
|
56
|
+
const target = live();
|
|
57
|
+
if (target !== undefined)
|
|
58
|
+
handler(target, ...args);
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
function attachNative(self, instance) {
|
|
62
|
+
self.nativeRef.current = instance;
|
|
63
|
+
self.latestOnNativeRef.current?.(instance);
|
|
64
|
+
}
|
|
65
|
+
function applySize(self, width, height) {
|
|
66
|
+
self.setSize((previous) => previous !== null && previous.width === width && previous.height === height ? previous : { width, height });
|
|
67
|
+
}
|
|
68
|
+
function fireRevealEnd(self) {
|
|
69
|
+
self.latestOnRevealEnd.current?.();
|
|
70
|
+
}
|
|
71
|
+
function fireRevealMilestone(self, index, milestone) {
|
|
72
|
+
self.latestOnRevealMilestone.current?.(index, milestone);
|
|
73
|
+
}
|
|
74
|
+
function fireAnimationStart(self) {
|
|
75
|
+
self.latestOnAnimationStart.current?.();
|
|
76
|
+
}
|
|
77
|
+
/** The figure a compact format rolls is not the value (1.2 for 1,234): report the value. */
|
|
78
|
+
function fireAnimationEnd(self, shown) {
|
|
79
|
+
self.latestOnAnimationEnd.current?.(self.compactRef.current ? toNumber(self.latestValue.current) : shown);
|
|
80
|
+
}
|
|
14
81
|
/** Each transition's own timing, used when the props leave it unsaid. */
|
|
15
82
|
const TRANSITION_DEFAULTS = {
|
|
16
83
|
roll: { duration: 500, easing: 'easeInOut', stagger: 0 },
|
|
@@ -29,6 +96,8 @@ const TRANSITION_DEFAULTS = {
|
|
|
29
96
|
*/
|
|
30
97
|
export const NitroNumber = forwardRef(function NitroNumber({ value, minorDigits, fractionDigits, minimumIntegerDigits, groupingSeparator, decimalSeparator, prefix, suffix, transition, flashUpColor, flashDownColor, flashDuration, popOnChange, duration, easing, bounce, stagger, direction, reveal, revealStyle, revealDuration, revealBounce, revealGrow, revealStagger, revealMilestones, revealMilestoneHold, onRevealEnd, onRevealMilestone, loading, shimmerColor, shimmerDuration, shimmerAngle, shimmerWidth, shimmerBaseColor, shimmerDirection, shimmerDelay, fontSize, prefixFontSize, suffixFontSize, affixAlign, prefixAlign, suffixAlign, letterSpacing, prefixSpacing, suffixSpacing, prefixOffset, suffixOffset, tabularNums, format, adjustsFontSizeToFit, minimumFontScale, allowFontScaling, maxFontSizeMultiplier, fontWeight, fontFamily, color, textAlign, signDisplay, plusSign, minusSign, prefixColor, suffixColor, fractionColor, fractionFontSize, fractionAlign, digitGlyphs, groupingSizes, digits, continuous, animated, respectReduceMotion, onAnimationStart, onAnimationEnd, onNativeRef, style, ...viewProps }, ref) {
|
|
31
98
|
const nativeRef = useRef(null);
|
|
99
|
+
// Whether a compact format is rolling (its figure is not the value); set below.
|
|
100
|
+
const compactRef = useRef(false);
|
|
32
101
|
const latestOnNativeRef = useRef(onNativeRef);
|
|
33
102
|
latestOnNativeRef.current = onNativeRef;
|
|
34
103
|
const latestOnRevealEnd = useRef(onRevealEnd);
|
|
@@ -44,34 +113,31 @@ export const NitroNumber = forwardRef(function NitroNumber({ value, minorDigits,
|
|
|
44
113
|
const latestOnAnimationEnd = useRef(onAnimationEnd);
|
|
45
114
|
latestOnAnimationEnd.current = onAnimationEnd;
|
|
46
115
|
const [size, setSize] = useState(null);
|
|
47
|
-
//
|
|
48
|
-
//
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
//
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
const
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
const
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
const onAnimationEndCallback = useMemo(() => callback((shown) => {
|
|
73
|
-
latestOnAnimationEnd.current?.(compactRef.current ? toNumber(latestValue.current) : shown);
|
|
74
|
-
}), []);
|
|
116
|
+
// The native callbacks: wrapped with `callback()` and referentially stable
|
|
117
|
+
// (a new wrapper re-sets the prop every render), and built in module scope
|
|
118
|
+
// around a weak reference to this component's state (see `relay`).
|
|
119
|
+
const self = useMemo(() => ({
|
|
120
|
+
nativeRef,
|
|
121
|
+
latestOnNativeRef,
|
|
122
|
+
setSize,
|
|
123
|
+
latestOnRevealEnd,
|
|
124
|
+
latestOnRevealMilestone,
|
|
125
|
+
latestOnAnimationStart,
|
|
126
|
+
latestOnAnimationEnd,
|
|
127
|
+
compactRef,
|
|
128
|
+
latestValue,
|
|
129
|
+
}),
|
|
130
|
+
// Every member is a ref or a state setter, stable for the component's life.
|
|
131
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
132
|
+
[]);
|
|
133
|
+
const hybridRef = useMemo(() => relay(self, attachNative), [self]);
|
|
134
|
+
const onSizeChange = useMemo(() => relay(self, applySize), [self]);
|
|
135
|
+
// The latest handlers are read through refs, so a caller passing an inline
|
|
136
|
+
// arrow doesn't re-set the native prop each render.
|
|
137
|
+
const onRevealEndCallback = useMemo(() => relay(self, fireRevealEnd), [self]);
|
|
138
|
+
const onRevealMilestoneCallback = useMemo(() => relay(self, fireRevealMilestone), [self]);
|
|
139
|
+
const onAnimationStartCallback = useMemo(() => relay(self, fireAnimationStart), [self]);
|
|
140
|
+
const onAnimationEndCallback = useMemo(() => relay(self, fireAnimationEnd), [self]);
|
|
75
141
|
// A new array literal each render must not re-set the native prop. Always an
|
|
76
142
|
// array (empty = no tiers): removing the prop would reach native as `null`,
|
|
77
143
|
// which Nitro's array parser rejects ("Value is null, expected an Object").
|
|
@@ -111,7 +177,6 @@ export const NitroNumber = forwardRef(function NitroNumber({ value, minorDigits,
|
|
|
111
177
|
// rolls the figure 1.2.
|
|
112
178
|
const figure = useMemo(() => (format ? figureOf(format, input) : undefined), [format, input]);
|
|
113
179
|
const compact = derived?.compact ? figure : undefined;
|
|
114
|
-
const compactRef = useRef(false);
|
|
115
180
|
compactRef.current = compact !== undefined;
|
|
116
181
|
// Which way the value moved, for a compact figure that fell while the value grew (999 β 1K).
|
|
117
182
|
const numericValue = toNumber(input);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-native-nitro-input",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.4",
|
|
4
4
|
"description": "Native text for React Native, built with Nitro: a text and amount input with native formatting, masks and a Torph-style reflow, and an animated number (odometer, SwiftUI-style numeric text, ticker) whose digits roll or swap natively, with currency layouts and animated currency switches, shrink-to-fit, a loading shimmer and a jackpot reveal.",
|
|
5
5
|
"main": "lib/commonjs/src/index.js",
|
|
6
6
|
"module": "lib/module/src/index.js",
|
|
@@ -40,6 +40,7 @@
|
|
|
40
40
|
"prepack": "bun run build",
|
|
41
41
|
"test": "jest",
|
|
42
42
|
"test:cpp": "mkdir -p .cache && clang++ -std=c++20 -O1 -Wall -Wextra -Icpp cpp/ReflowEngine.cpp cpp/AmountFormatter.cpp cpp/__tests__/ReflowEngineTest.cpp -o .cache/reflow-engine-test && .cache/reflow-engine-test && clang++ -std=c++20 -O1 -Wall -Wextra -Icpp cpp/AmountFormatter.cpp cpp/__tests__/AmountFormatterTest.cpp -o .cache/amount-formatter-test && .cache/amount-formatter-test && clang++ -std=c++20 -O1 -Wall -Wextra -Icpp cpp/AmountFormatter.cpp cpp/MaskEngine.cpp cpp/__tests__/MaskEngineTest.cpp -o .cache/mask-engine-test && .cache/mask-engine-test && clang++ -std=c++20 -O1 -Wall -Wextra -Icpp cpp/AmountFormatter.cpp cpp/MaskEngine.cpp cpp/__tests__/MaskEngineCorpusTest.cpp -o .cache/mask-engine-corpus-test && .cache/mask-engine-corpus-test && clang++ -std=c++20 -O1 -Wall -Wextra -Icpp cpp/OutlineGeometry.cpp cpp/__tests__/OutlineGeometryTest.cpp -o .cache/outline-geometry-test && .cache/outline-geometry-test && clang++ -std=c++20 -O1 -Wall -Wextra -Icpp cpp/RollingEngine.cpp cpp/__tests__/RollingEngineTest.cpp -o .cache/rolling-engine-test && .cache/rolling-engine-test && clang++ -std=c++20 -O1 -Wall -Wextra -Icpp cpp/numberformat/NumberFormatCore.cpp cpp/numberformat/FormatFigure.cpp cpp/__tests__/NumberFormatCoreTest.cpp -o .cache/number-format-core-test && .cache/number-format-core-test && clang++ -std=c++20 -O1 -Wall -Wextra -Icpp cpp/numberformat/NumberFormatCore.cpp cpp/numberformat/NumberFormatProbe.cpp cpp/__tests__/NumberFormatProbeTest.cpp -o .cache/number-format-probe-test && .cache/number-format-probe-test",
|
|
43
|
+
"test:swift": "mkdir -p .cache && swiftc -parse-as-library ios/CodePointOffsets.swift ios/__tests__/CodePointOffsetsTest.swift -o .cache/code-point-offsets-test && .cache/code-point-offsets-test",
|
|
43
44
|
"test:cpp:sanitize": "mkdir -p .cache && clang++ -std=c++20 -O1 -g -fsanitize=address,undefined -fno-omit-frame-pointer -Icpp cpp/RollingEngine.cpp cpp/__tests__/RollingEngineTest.cpp -o .cache/rolling-engine-sanitize && .cache/rolling-engine-sanitize",
|
|
44
45
|
"release": "release-it"
|
|
45
46
|
},
|
package/src/NitroNumber.tsx
CHANGED
|
@@ -376,6 +376,100 @@ interface Size {
|
|
|
376
376
|
const EMPTY: number[] = []
|
|
377
377
|
const EMPTY_STRINGS: string[] = []
|
|
378
378
|
|
|
379
|
+
/** What the native callbacks reach: one NitroNumber's refs and state setter. */
|
|
380
|
+
interface NumberSelf {
|
|
381
|
+
nativeRef: React.MutableRefObject<NitroNumberRef | null>
|
|
382
|
+
latestOnNativeRef: React.MutableRefObject<((native: NitroNumberRef) => void) | undefined>
|
|
383
|
+
setSize: React.Dispatch<React.SetStateAction<Size | null>>
|
|
384
|
+
latestOnRevealEnd: React.MutableRefObject<(() => void) | undefined>
|
|
385
|
+
latestOnRevealMilestone: React.MutableRefObject<((index: number, milestone: number) => void) | undefined>
|
|
386
|
+
latestOnAnimationStart: React.MutableRefObject<(() => void) | undefined>
|
|
387
|
+
latestOnAnimationEnd: React.MutableRefObject<((value: number) => void) | undefined>
|
|
388
|
+
compactRef: React.MutableRefObject<boolean>
|
|
389
|
+
latestValue: React.MutableRefObject<ReturnType<typeof withMinorDigits>>
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
type Deref<T extends object> = () => T | undefined
|
|
393
|
+
|
|
394
|
+
/**
|
|
395
|
+
* A weak reference where the engine has them (Hermes, JSC, V8), else a strong
|
|
396
|
+
* one. Two functions on purpose: an engine keeps one scope per function for
|
|
397
|
+
* everything its closures capture, so a `() => target` fallback written beside
|
|
398
|
+
* the weak closure would put `target` in the weak closure's scope too.
|
|
399
|
+
*/
|
|
400
|
+
function weakly<T extends object>(target: T): Deref<T> {
|
|
401
|
+
return typeof WeakRef === 'function' ? weakDeref(new WeakRef(target)) : strongDeref(target)
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
function weakDeref<T extends object>(ref: WeakRef<T>): Deref<T> {
|
|
405
|
+
return () => ref.deref()
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
function strongDeref<T extends object>(target: T): Deref<T> {
|
|
409
|
+
return () => target
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
/**
|
|
413
|
+
* A Nitro callback that calls `handler(self, ...args)` for as long as the
|
|
414
|
+
* component behind `self` is alive, holding `self` only weakly.
|
|
415
|
+
*
|
|
416
|
+
* Why. Nitro keeps every callback prop of a view as a JS function held from
|
|
417
|
+
* C++ (in the Fabric props of its shadow node), which Hermes treats as a GC
|
|
418
|
+
* root. A closure written inside the component shares the render's scope,
|
|
419
|
+
* so it reaches the fiber (through `setSize`) and the hybrid view's handle
|
|
420
|
+
* (through `nativeRef`), and the fiber holds the shadow node that holds the
|
|
421
|
+
* props that hold the closure: a loop through native code that Hermes can
|
|
422
|
+
* never collect. Every unmounted NitroNumber stayed, its fiber subtree, its
|
|
423
|
+
* hybrid view and Nitro's C++ part included: over 4,000 after a minute of
|
|
424
|
+
* a scrolling list, and the app's memory climbed with them. Built here, in
|
|
425
|
+
* module scope, the closure holds only the weak reference and `handler`
|
|
426
|
+
* (itself module-level); the fiber is what keeps `self` alive.
|
|
427
|
+
*/
|
|
428
|
+
function relay<A extends unknown[]>(self: NumberSelf, handler: (self: NumberSelf, ...args: A) => void) {
|
|
429
|
+
return relayTo(weakly(self), handler)
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
/**
|
|
433
|
+
* The closure itself, made where `self` is not in scope at all: Hermes (in
|
|
434
|
+
* debuggable builds, at least) keeps every variable of a function that makes
|
|
435
|
+
* a closure in that closure's scope, used or not, and a `self` there would
|
|
436
|
+
* hold the component strongly again.
|
|
437
|
+
*/
|
|
438
|
+
function relayTo<A extends unknown[]>(live: Deref<NumberSelf>, handler: (self: NumberSelf, ...args: A) => void) {
|
|
439
|
+
return callback((...args: A) => {
|
|
440
|
+
const target = live()
|
|
441
|
+
if (target !== undefined) handler(target, ...args)
|
|
442
|
+
})
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
function attachNative(self: NumberSelf, instance: NitroNumberRef) {
|
|
446
|
+
self.nativeRef.current = instance
|
|
447
|
+
self.latestOnNativeRef.current?.(instance)
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
function applySize(self: NumberSelf, width: number, height: number) {
|
|
451
|
+
self.setSize((previous) =>
|
|
452
|
+
previous !== null && previous.width === width && previous.height === height ? previous : { width, height }
|
|
453
|
+
)
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
function fireRevealEnd(self: NumberSelf) {
|
|
457
|
+
self.latestOnRevealEnd.current?.()
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
function fireRevealMilestone(self: NumberSelf, index: number, milestone: number) {
|
|
461
|
+
self.latestOnRevealMilestone.current?.(index, milestone)
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
function fireAnimationStart(self: NumberSelf) {
|
|
465
|
+
self.latestOnAnimationStart.current?.()
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
/** The figure a compact format rolls is not the value (1.2 for 1,234): report the value. */
|
|
469
|
+
function fireAnimationEnd(self: NumberSelf, shown: number) {
|
|
470
|
+
self.latestOnAnimationEnd.current?.(self.compactRef.current ? toNumber(self.latestValue.current) : shown)
|
|
471
|
+
}
|
|
472
|
+
|
|
379
473
|
/** Each transition's own timing, used when the props leave it unsaid. */
|
|
380
474
|
const TRANSITION_DEFAULTS: Record<NitroNumberTransition, { duration: number; easing: NitroNumberEasing; stagger: number }> = {
|
|
381
475
|
roll: { duration: 500, easing: 'easeInOut', stagger: 0 },
|
|
@@ -476,6 +570,8 @@ export const NitroNumber = forwardRef<NitroNumberHandle, NitroNumberProps>(
|
|
|
476
570
|
ref
|
|
477
571
|
) {
|
|
478
572
|
const nativeRef = useRef<NitroNumberRef | null>(null)
|
|
573
|
+
// Whether a compact format is rolling (its figure is not the value); set below.
|
|
574
|
+
const compactRef = useRef(false)
|
|
479
575
|
const latestOnNativeRef = useRef(onNativeRef)
|
|
480
576
|
latestOnNativeRef.current = onNativeRef
|
|
481
577
|
const latestOnRevealEnd = useRef(onRevealEnd)
|
|
@@ -493,61 +589,33 @@ export const NitroNumber = forwardRef<NitroNumberHandle, NitroNumberProps>(
|
|
|
493
589
|
|
|
494
590
|
const [size, setSize] = useState<Size | null>(null)
|
|
495
591
|
|
|
496
|
-
//
|
|
497
|
-
//
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
? previous
|
|
514
|
-
: { width, height }
|
|
515
|
-
)
|
|
516
|
-
}),
|
|
517
|
-
[]
|
|
518
|
-
)
|
|
519
|
-
|
|
520
|
-
// Stable like the others; the latest handler is read through a ref so a
|
|
521
|
-
// caller passing an inline arrow doesn't re-set the native prop each render.
|
|
522
|
-
const onRevealEndCallback = useMemo(
|
|
523
|
-
() =>
|
|
524
|
-
callback(() => {
|
|
525
|
-
latestOnRevealEnd.current?.()
|
|
526
|
-
}),
|
|
527
|
-
[]
|
|
528
|
-
)
|
|
529
|
-
const onRevealMilestoneCallback = useMemo(
|
|
530
|
-
() =>
|
|
531
|
-
callback((index: number, milestone: number) => {
|
|
532
|
-
latestOnRevealMilestone.current?.(index, milestone)
|
|
533
|
-
}),
|
|
534
|
-
[]
|
|
535
|
-
)
|
|
536
|
-
const onAnimationStartCallback = useMemo(
|
|
537
|
-
() =>
|
|
538
|
-
callback(() => {
|
|
539
|
-
latestOnAnimationStart.current?.()
|
|
540
|
-
}),
|
|
541
|
-
[]
|
|
542
|
-
)
|
|
543
|
-
// The figure a compact format rolls is not the value (1.2 for 1,234): report the value.
|
|
544
|
-
const onAnimationEndCallback = useMemo(
|
|
545
|
-
() =>
|
|
546
|
-
callback((shown: number) => {
|
|
547
|
-
latestOnAnimationEnd.current?.(compactRef.current ? toNumber(latestValue.current) : shown)
|
|
548
|
-
}),
|
|
592
|
+
// The native callbacks: wrapped with `callback()` and referentially stable
|
|
593
|
+
// (a new wrapper re-sets the prop every render), and built in module scope
|
|
594
|
+
// around a weak reference to this component's state (see `relay`).
|
|
595
|
+
const self = useMemo<NumberSelf>(
|
|
596
|
+
() => ({
|
|
597
|
+
nativeRef,
|
|
598
|
+
latestOnNativeRef,
|
|
599
|
+
setSize,
|
|
600
|
+
latestOnRevealEnd,
|
|
601
|
+
latestOnRevealMilestone,
|
|
602
|
+
latestOnAnimationStart,
|
|
603
|
+
latestOnAnimationEnd,
|
|
604
|
+
compactRef,
|
|
605
|
+
latestValue,
|
|
606
|
+
}),
|
|
607
|
+
// Every member is a ref or a state setter, stable for the component's life.
|
|
608
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
549
609
|
[]
|
|
550
610
|
)
|
|
611
|
+
const hybridRef = useMemo(() => relay(self, attachNative), [self])
|
|
612
|
+
const onSizeChange = useMemo(() => relay(self, applySize), [self])
|
|
613
|
+
// The latest handlers are read through refs, so a caller passing an inline
|
|
614
|
+
// arrow doesn't re-set the native prop each render.
|
|
615
|
+
const onRevealEndCallback = useMemo(() => relay(self, fireRevealEnd), [self])
|
|
616
|
+
const onRevealMilestoneCallback = useMemo(() => relay(self, fireRevealMilestone), [self])
|
|
617
|
+
const onAnimationStartCallback = useMemo(() => relay(self, fireAnimationStart), [self])
|
|
618
|
+
const onAnimationEndCallback = useMemo(() => relay(self, fireAnimationEnd), [self])
|
|
551
619
|
// A new array literal each render must not re-set the native prop. Always an
|
|
552
620
|
// array (empty = no tiers): removing the prop would reach native as `null`,
|
|
553
621
|
// which Nitro's array parser rejects ("Value is null, expected an Object").
|
|
@@ -599,7 +667,6 @@ export const NitroNumber = forwardRef<NitroNumberHandle, NitroNumberProps>(
|
|
|
599
667
|
// rolls the figure 1.2.
|
|
600
668
|
const figure = useMemo(() => (format ? figureOf(format, input) : undefined), [format, input])
|
|
601
669
|
const compact = derived?.compact ? figure : undefined
|
|
602
|
-
const compactRef = useRef(false)
|
|
603
670
|
compactRef.current = compact !== undefined
|
|
604
671
|
// Which way the value moved, for a compact figure that fell while the value grew (999 β 1K).
|
|
605
672
|
const numericValue = toNumber(input)
|