@hairy/react-lib-composition 1.46.0 → 1.49.0

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/dist/index.mjs ADDED
@@ -0,0 +1,448 @@
1
+ import { isFunction, noop } from "@hairy/utils";
2
+ import { Fragment, createElement, createElement as h, useCallback, useEffect, useInsertionEffect, useReducer, useRef, useState } from "react";
3
+ import { computed as computed$1, customRef as customRef$1, effectScope as effectScope$1, getCurrentScope as getCurrentScope$1, isProxy, isReactive, isReadonly, isRef, isShallow, markRaw, reactive as reactive$1, readonly as readonly$1, ref as ref$1, shallowReactive as shallowReactive$1, shallowReadonly as shallowReadonly$1, shallowRef as shallowRef$1, toRaw, toReactive, toReadonly, toRef, toRefs, toValue, unref, watch as watch$1 } from "@vue/reactivity";
4
+
5
+ //#region ../lib-react/src/hooks/tryUseCallback.ts
6
+ const tryUseCallback = (callback, deps) => {
7
+ try {
8
+ return useCallback(callback, deps);
9
+ } catch {
10
+ return callback;
11
+ }
12
+ };
13
+
14
+ //#endregion
15
+ //#region ../lib-react/src/hooks/tryUseEffect.ts
16
+ const tryUseEffect = (effect, deps) => {
17
+ try {
18
+ useEffect(effect, deps);
19
+ } catch {}
20
+ };
21
+
22
+ //#endregion
23
+ //#region ../lib-react/src/hooks/tryUseInsertionEffect.ts
24
+ const tryUseInsertionEffect = (callback, deps) => {
25
+ try {
26
+ useInsertionEffect(callback, deps);
27
+ } catch {}
28
+ };
29
+
30
+ //#endregion
31
+ //#region ../lib-react/src/hooks/tryUseReducer.ts
32
+ const tryUseReducer = (reducer, initializerArg, initializer) => {
33
+ try {
34
+ return useReducer(reducer, initializerArg, initializer);
35
+ } catch {
36
+ return [initializerArg, () => {}];
37
+ }
38
+ };
39
+
40
+ //#endregion
41
+ //#region ../lib-react/src/hooks/tryUseRef.ts
42
+ const tryUseRef = (initialValue) => {
43
+ try {
44
+ return useRef(initialValue);
45
+ } catch {
46
+ return { current: initialValue };
47
+ }
48
+ };
49
+
50
+ //#endregion
51
+ //#region ../lib-react/src/hooks/tryUseState.ts
52
+ const tryUseState = (initialState) => {
53
+ try {
54
+ return useState(initialState);
55
+ } catch {
56
+ return [isFunction(initialState) ? initialState() : initialState, noop];
57
+ }
58
+ };
59
+
60
+ //#endregion
61
+ //#region ../lib-react/src/hooks/useUpdate.ts
62
+ const updateReducer = (num) => (num + 1) % 1e6;
63
+ function useUpdate() {
64
+ const [, update] = tryUseReducer(updateReducer, 0);
65
+ return update;
66
+ }
67
+
68
+ //#endregion
69
+ //#region ../lib-react/src/hooks/tryUseUpdate.ts
70
+ function tryUseUpdate() {
71
+ try {
72
+ return useUpdate();
73
+ } catch {
74
+ return () => {};
75
+ }
76
+ }
77
+
78
+ //#endregion
79
+ //#region src/lifecycle.ts
80
+ /**
81
+ * The function is called right after the component is mounted.
82
+ *
83
+ * @param fn
84
+ * @see {@link https://react.dev/reference/react/Component#componentdidmount React `componentDidMount()`}
85
+ */
86
+ function onMounted(fn) {
87
+ tryUseEffect(() => {
88
+ fn();
89
+ }, []);
90
+ }
91
+ function onBeforeMount(fn) {
92
+ const isMounted = tryUseRef(false);
93
+ if (!isMounted.current) {
94
+ fn();
95
+ isMounted.current = true;
96
+ }
97
+ }
98
+ /**
99
+ * The function is called right before the component is unmounted.
100
+ *
101
+ * @param fn
102
+ * @see {@link https://react.dev/reference/react/Component#componentwillunmount React `componentWillUnmount()`}
103
+ */
104
+ function onBeforeUnmount(fn) {
105
+ tryUseEffect(() => {
106
+ return () => {
107
+ fn();
108
+ };
109
+ }, []);
110
+ }
111
+ function onUnmounted(fn) {
112
+ onBeforeUnmount(() => setTimeout(fn, 0));
113
+ }
114
+ /**
115
+ * The function is called immediately after the component is re-rendered with updated props or state.
116
+ * This method is not invoked during the initial render.
117
+ *
118
+ * @param fn
119
+ * @see {@link https://react.dev/reference/react/Component#componentdidupdate React `componentDidUpdate()`}
120
+ */
121
+ function onUpdated(fn) {
122
+ const isMounted = tryUseRef(false);
123
+ tryUseEffect(() => {
124
+ isMounted.current ? fn() : isMounted.current = true;
125
+ });
126
+ }
127
+ function onBeforeUpdate(fn) {
128
+ tryUseInsertionEffect(fn);
129
+ }
130
+
131
+ //#endregion
132
+ //#region src/watch.ts
133
+ function watch(source, callback, options = {}) {
134
+ const handle = tryUseRef(void 0);
135
+ const cancel = tryUseCallback(() => {
136
+ if (!handle.current) return;
137
+ handle.current();
138
+ handle.current = void 0;
139
+ }, []);
140
+ const create = tryUseCallback(() => {
141
+ handle.current && cancel();
142
+ handle.current = watch$1(source, callback, {
143
+ ...options,
144
+ scheduler: (job) => job()
145
+ });
146
+ cancel.pause = handle.current.pause;
147
+ cancel.resume = handle.current.resume;
148
+ cancel.stop = handle.current.stop;
149
+ }, []);
150
+ !handle.current && create();
151
+ onMounted(create);
152
+ onBeforeUnmount(cancel);
153
+ return cancel;
154
+ }
155
+
156
+ //#endregion
157
+ //#region src/computed.ts
158
+ function computed(arg1, arg2) {
159
+ const [ref] = tryUseState(() => computed$1(arg1, arg2));
160
+ watch(ref, tryUseUpdate());
161
+ return ref;
162
+ }
163
+
164
+ //#endregion
165
+ //#region src/effectScope.ts
166
+ let activeEffectScope;
167
+ function createVueEffectScope(...args) {
168
+ const scope = effectScope$1(...args);
169
+ Reflect.set(scope, "parent", activeEffectScope);
170
+ if (!args[0] && activeEffectScope) {
171
+ const scopes = activeEffectScope.scopes || (activeEffectScope.scopes = []);
172
+ Reflect.set(scope, "index", scopes.push(scope) - 1);
173
+ }
174
+ return scope;
175
+ }
176
+ /**
177
+ * Creates an effect scope object which can capture the reactive effects (i.e.
178
+ * computed and watchers) created within it so that these effects can be
179
+ * disposed together. For detailed use cases of this API, please consult its
180
+ * corresponding {@link https://github.com/vuejs/rfcs/blob/master/active-rfcs/0041-reactivity-effect-scope.md | RFC}.
181
+ *
182
+ * @param detached - Can be used to create a "detached" effect scope.
183
+ * @see {@link https://vuejs.org/api/reactivity-advanced.html#effectscope Vue `effectScope()`}
184
+ */
185
+ function effectScope(...args) {
186
+ const hasRun = tryUseRef(false);
187
+ const [scope] = tryUseState(() => createVueEffectScope(...args));
188
+ const originalRunRef = tryUseRef(scope.run);
189
+ scope.run = tryUseCallback((fn) => {
190
+ if (!hasRun.current) {
191
+ hasRun.current = true;
192
+ return originalRunRef.current.bind(scope)(fn);
193
+ } else return;
194
+ }, []);
195
+ return scope;
196
+ }
197
+ function withEffectScope(fn, detached) {
198
+ let currentEffectScope;
199
+ return ((...props) => {
200
+ const scope = effectScope(detached);
201
+ const element = createElement(fn, ...props);
202
+ onBeforeMount(() => {
203
+ currentEffectScope = activeEffectScope;
204
+ activeEffectScope = scope;
205
+ });
206
+ onMounted(() => activeEffectScope = currentEffectScope);
207
+ onUnmounted(() => scope.stop());
208
+ return element;
209
+ });
210
+ }
211
+ /**
212
+ * Returns the current active effect scope if there is one.
213
+ *
214
+ * @see {@link https://vuejs.org/api/reactivity-advanced.html#getcurrentscope}
215
+ */
216
+ function getCurrentScope() {
217
+ const [effectScope] = tryUseState(() => activeEffectScope || getCurrentScope$1());
218
+ return effectScope;
219
+ }
220
+ /**
221
+ * Registers a dispose callback on the current active effect scope. The
222
+ * callback will be invoked when the associated effect scope is stopped.
223
+ *
224
+ * @param fn - The callback function to attach to the scope's cleanup.
225
+ * @see {@link https://vuejs.org/api/reactivity-advanced.html#onscopedispose}
226
+ */
227
+ function onScopeDispose(fn, _failSilently = false) {
228
+ if (activeEffectScope) Reflect.get(activeEffectScope, "cleanups")?.push(fn);
229
+ }
230
+
231
+ //#endregion
232
+ //#region src/reactive.ts
233
+ function reactive(target) {
234
+ const [value] = useState(() => reactive$1(target));
235
+ watch(value, tryUseUpdate());
236
+ return value;
237
+ }
238
+ /**
239
+ * Shallow version of {@link reactive()}.
240
+ *
241
+ * Unlike {@link reactive()}, there is no deep conversion: only root-level
242
+ * properties are reactive for a shallow reactive object. Property values are
243
+ * stored and exposed as-is - this also means properties with ref values will
244
+ * not be automatically unwrapped.
245
+ *
246
+ * @param target - The source object.
247
+ * @see {@link https://vuejs.org/api/reactivity-advanced.html#shallowreactive Vue `shallowReactive()`}
248
+ *
249
+ * @example
250
+ * ```js
251
+ * const state = shallowReactive({
252
+ * foo: 1,
253
+ * nested: {
254
+ * bar: 2
255
+ * }
256
+ * })
257
+ *
258
+ * // mutating state's own properties is reactive
259
+ * state.foo++
260
+ *
261
+ * // ...but does not convert nested objects
262
+ * isReactive(state.nested) // false
263
+ *
264
+ * // NOT reactive
265
+ * state.nested.bar++
266
+ * ```
267
+ */
268
+ function shallowReactive(target) {
269
+ const [value] = useState(() => shallowReactive$1(target));
270
+ watch(value, tryUseUpdate());
271
+ return value;
272
+ }
273
+
274
+ //#endregion
275
+ //#region src/reactivity.ts
276
+ /**
277
+ * Converts some of the 'raw Vue' data, which is not already wrapped in a hook,
278
+ * into reactive hook data to ensure proper reactivity within the component.
279
+ *
280
+ * @param getter - A function that returns the data to be deeply watched.
281
+ * @example
282
+ * ```tsx
283
+ * import React from 'react'
284
+ * import { ref, reactivity } from 'veact'
285
+ *
286
+ * const countRef = ref(0)
287
+ *
288
+ * export const Component: React.FC = () => {
289
+ * // Convert to a reactivity hook
290
+ * const count = reactivity(() => countRef)
291
+ * const increment = () => {
292
+ * count.value++
293
+ * }
294
+ *
295
+ * return (
296
+ * <div>
297
+ * <span>{count.value}</span>
298
+ * <button onClick={increment}>Increment</button>
299
+ * </div>
300
+ * )
301
+ * }
302
+ * ```
303
+ */
304
+ function reactivity(getter) {
305
+ watch(() => getter(), tryUseUpdate(), { deep: true });
306
+ return getter();
307
+ }
308
+
309
+ //#endregion
310
+ //#region src/readonly.ts
311
+ /**
312
+ * Takes an object (reactive or plain) or a ref and returns a readonly proxy to
313
+ * the original.
314
+ *
315
+ * A readonly proxy is deep: any nested property accessed will be readonly as
316
+ * well. It also has the same ref-unwrapping behavior as {@link reactive()},
317
+ * except the unwrapped values will also be made readonly.
318
+ *
319
+ * @param target - The source object.
320
+ * @see {@link https://vuejs.org/api/reactivity-core.html#readonly Vue `readonly()`}
321
+ *
322
+ * @example
323
+ * ```js
324
+ * const original = reactive({ count: 0 })
325
+ * const copy = readonly(original)
326
+ *
327
+ * useWatchEffect(() => {
328
+ * // works for reactivity tracking
329
+ * console.log(copy.count)
330
+ * })
331
+ *
332
+ * // mutating original will trigger watchers relying on the copy
333
+ * original.count++
334
+ *
335
+ * // mutating the copy will fail and result in a warning
336
+ * copy.count++ // warning!
337
+ * ```
338
+ */
339
+ function readonly(target) {
340
+ const [value] = tryUseState(() => readonly$1(target));
341
+ watch(value, tryUseUpdate());
342
+ return value;
343
+ }
344
+ /**
345
+ * Shallow version of {@link readonly()}.
346
+ *
347
+ * Unlike {@link readonly()}, there is no deep conversion: only root-level
348
+ * properties are made readonly. Property values are stored and exposed as-is -
349
+ * this also means properties with ref values will not be automatically
350
+ * unwrapped.
351
+ *
352
+ * @param target - The source object.
353
+ * @see {@link https://vuejs.org/api/reactivity-advanced.html#shallowreadonly Vue `shallowReadonly()`}
354
+ *
355
+ * @example
356
+ * ```js
357
+ * const state = shallowReadonly({
358
+ * foo: 1,
359
+ * nested: {
360
+ * bar: 2
361
+ * }
362
+ * })
363
+ *
364
+ * // mutating state's own properties will fail
365
+ * state.foo++
366
+ *
367
+ * // ...but works on nested objects
368
+ * isReadonly(state.nested) // false
369
+ *
370
+ * // works
371
+ * state.nested.bar++
372
+ * ```
373
+ */
374
+ function shallowReadonly(target) {
375
+ const [value] = tryUseState(() => shallowReadonly$1(target));
376
+ watch(value, tryUseUpdate());
377
+ return value;
378
+ }
379
+
380
+ //#endregion
381
+ //#region src/ref.ts
382
+ function ref(initValue) {
383
+ const [ref] = tryUseState(() => {
384
+ const r = ref$1(initValue);
385
+ Object.defineProperty(r, "current", { set: (value) => r.value = value });
386
+ return r;
387
+ });
388
+ watch(ref, tryUseUpdate(), { deep: true });
389
+ return ref;
390
+ }
391
+ /**
392
+ * Creates a customized ref with explicit control over its dependency tracking
393
+ * and updates triggering.
394
+ *
395
+ * @param factory - The function that receives the `track` and `trigger` callbacks.
396
+ * @see {@link https://vuejs.org/api/reactivity-advanced.html#customref Vue `customRef()`}
397
+ */
398
+ function customRef(factory) {
399
+ const [ref] = tryUseState(() => customRef$1(factory));
400
+ watch(ref, tryUseUpdate());
401
+ return ref;
402
+ }
403
+ function shallowRef(initValue) {
404
+ const [ref] = tryUseState(() => shallowRef$1(initValue));
405
+ watch(ref, tryUseUpdate());
406
+ return ref;
407
+ }
408
+
409
+ //#endregion
410
+ //#region src/watchEffect.ts
411
+ /**
412
+ * Runs a function immediately while reactively tracking its dependencies and re-runs it whenever the dependencies are changed.
413
+ *
414
+ * @param effect - The effect function to run.
415
+ * @param options - An optional options object that can be used to adjust the effect's flush timing or to debug the effect's dependencies; the `flush` option is not supported compared to Vue (3.5.0).
416
+ * @see {@link https://vuejs.org/api/reactivity-core.html#watcheffect Vue `watchEffect()`}
417
+ *
418
+ * @example
419
+ * ```js
420
+ * const count = useRef(0)
421
+ * watchEffect(() => console.log(count.value))
422
+ * // -> logs 0
423
+ *
424
+ * count.value++
425
+ * // -> logs 1
426
+ * ```
427
+ */
428
+ function watchEffect(effect, options = {}) {
429
+ const [watchHandle] = tryUseState(() => watch$1(effect, null, {
430
+ ...options,
431
+ scheduler: (job) => job()
432
+ }));
433
+ onBeforeUnmount(() => watchHandle.stop());
434
+ return watchHandle;
435
+ }
436
+
437
+ //#endregion
438
+ //#region src/index.ts
439
+ const getCurrentInstance = noop;
440
+ const hasInjectionContext = noop;
441
+ const inject = noop;
442
+ const provide = noop;
443
+ const nextTick = noop;
444
+ const defineComponent = noop;
445
+ const TransitionGroup = noop;
446
+
447
+ //#endregion
448
+ export { Fragment, TransitionGroup, computed, customRef, defineComponent, effectScope, getCurrentInstance, getCurrentScope, h, hasInjectionContext, inject, isProxy, isReactive, isReadonly, isRef, isShallow, markRaw, nextTick, onBeforeMount, onBeforeUnmount, onBeforeUpdate, onMounted, onScopeDispose, onUnmounted, onUpdated, provide, reactive, reactivity, readonly, ref, shallowReactive, shallowReadonly, shallowRef, toRaw, toReactive, toReadonly, toRef, toRefs, toValue, unref, watch, watchEffect, withEffectScope };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@hairy/react-lib-composition",
3
3
  "type": "module",
4
- "version": "1.46.0",
4
+ "version": "1.49.0",
5
5
  "description": "Library for react reactivity",
6
6
  "author": "Hairyf <wwu710632@gmail.com>",
7
7
  "license": "MIT",
@@ -14,13 +14,22 @@
14
14
  "bugs": "https://github.com/hairyf/hairylib/issues",
15
15
  "keywords": [],
16
16
  "sideEffects": false,
17
- "main": "./dist/index.js",
18
- "publishConfig": {
19
- "jsdelivr": "./dist/index.global.js"
17
+ "exports": {
18
+ ".": {
19
+ "import": "./dist/index.mjs",
20
+ "require": "./dist/index.cjs"
21
+ },
22
+ "./package.json": "./package.json"
20
23
  },
24
+ "main": "./dist/index.mjs",
25
+ "module": "./dist/index.mjs",
26
+ "types": "./dist/index.d.mts",
21
27
  "files": [
22
28
  "dist"
23
29
  ],
30
+ "publishConfig": {
31
+ "jsdelivr": "./dist/index.iife.js"
32
+ },
24
33
  "peerDependencies": {
25
34
  "react": "^18.2.0",
26
35
  "react-dom": "^18.2.0",
@@ -30,7 +39,8 @@
30
39
  "@vue/reactivity": "^3.5.13",
31
40
  "html-parse-stringify": "^3.0.1",
32
41
  "mitt": "^3.0.1",
33
- "valtio": "^2"
42
+ "valtio": "^2",
43
+ "@hairy/utils": "1.49.0"
34
44
  },
35
45
  "devDependencies": {
36
46
  "@types/react": "^19.1.3",
@@ -39,23 +49,12 @@
39
49
  "react-dom": "^19.1.0",
40
50
  "react-i18next": "^14.1.2",
41
51
  "react-use": "^17.6.0",
42
- "@hairy/utils": "1.46.0",
43
- "@hairy/react-lib": "1.46.0"
52
+ "@hairy/react-lib": "1.49.0"
44
53
  },
45
54
  "scripts": {
46
- "build": "tsup",
47
- "dev": "tsup --watch",
55
+ "build": "tsdown",
56
+ "dev": "tsdown --watch",
48
57
  "start": "tsx src/index.ts"
49
58
  },
50
- "module": "./dist/index.js",
51
- "types": "./dist/index.d.ts",
52
- "unpkg": "./dist/index.global.js",
53
- "exports": {
54
- ".": {
55
- "import": "./dist/index.js",
56
- "require": "./dist/index.cjs",
57
- "types": "./dist/index.d.ts"
58
- },
59
- "./*": "./*"
60
- }
59
+ "unpkg": "./dist/index.iife.js"
61
60
  }