@feng3d/reactivity 0.0.1 → 1.0.2

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.
Files changed (45) hide show
  1. package/README.md +124 -26
  2. package/dist/index.js +1018 -58
  3. package/dist/index.js.map +1 -1
  4. package/dist/index.umd.cjs +1017 -57
  5. package/dist/index.umd.cjs.map +1 -1
  6. package/lib/Reactivity.d.ts +69 -0
  7. package/lib/Reactivity.d.ts.map +1 -0
  8. package/lib/arrayInstrumentations.d.ts +2 -0
  9. package/lib/arrayInstrumentations.d.ts.map +1 -0
  10. package/lib/baseHandlers.d.ts +5 -0
  11. package/lib/baseHandlers.d.ts.map +1 -0
  12. package/lib/batch.d.ts +23 -0
  13. package/lib/batch.d.ts.map +1 -0
  14. package/lib/collectionHandlers.d.ts +7 -0
  15. package/lib/collectionHandlers.d.ts.map +1 -0
  16. package/lib/computed.d.ts +94 -0
  17. package/lib/computed.d.ts.map +1 -0
  18. package/lib/effect.d.ts +52 -0
  19. package/lib/effect.d.ts.map +1 -0
  20. package/lib/index.d.ts +7 -18
  21. package/lib/index.d.ts.map +1 -1
  22. package/lib/property.d.ts +44 -0
  23. package/lib/property.d.ts.map +1 -0
  24. package/lib/reactive.d.ts +66 -0
  25. package/lib/reactive.d.ts.map +1 -0
  26. package/lib/ref.d.ts +41 -0
  27. package/lib/ref.d.ts.map +1 -0
  28. package/lib/shared/constants.d.ts +34 -0
  29. package/lib/shared/constants.d.ts.map +1 -0
  30. package/lib/shared/general.d.ts +36 -0
  31. package/lib/shared/general.d.ts.map +1 -0
  32. package/package.json +3 -2
  33. package/src/Reactivity.ts +126 -0
  34. package/src/arrayInstrumentations.ts +448 -0
  35. package/src/baseHandlers.ts +220 -0
  36. package/src/batch.ts +91 -0
  37. package/src/collectionHandlers.ts +298 -0
  38. package/src/computed.ts +201 -0
  39. package/src/effect.ts +108 -0
  40. package/src/index.ts +7 -200
  41. package/src/property.ts +223 -0
  42. package/src/reactive.ts +130 -0
  43. package/src/ref.ts +88 -0
  44. package/src/shared/constants.ts +41 -0
  45. package/src/shared/general.ts +109 -0
@@ -0,0 +1,109 @@
1
+ /* eslint-disable no-var */
2
+ import { ReactiveFlags, TargetType } from './constants';
3
+
4
+ export { };
5
+ declare global
6
+ {
7
+ /**
8
+ * 是否为开发模式。
9
+ */
10
+ var __DEV__: boolean;
11
+ }
12
+ globalThis.__DEV__ ??= true;
13
+
14
+ export const isObject = (val: unknown): val is Record<any, any> => val !== null && typeof val === 'object';
15
+ // 判断是否为数组
16
+ export const isArray: typeof Array.isArray = Array.isArray;
17
+ export const isSymbol = (val: unknown): val is symbol => typeof val === 'symbol';
18
+ export const isString = (val: unknown): val is string => typeof val === 'string';
19
+ export const isIntegerKey = (key: unknown): boolean =>
20
+ isString(key)
21
+ && key !== 'NaN'
22
+ && key[0] !== '-'
23
+ && `${parseInt(key as any, 10)}` === key;
24
+ export const isMap = (val: unknown): val is Map<any, any> =>
25
+ toTypeString(val) === '[object Map]';
26
+
27
+ // 判断对象是否拥有指定属性
28
+ export const hasOwn = (
29
+ val: object,
30
+ key: string | symbol,
31
+ ): key is keyof typeof val => Object.prototype.hasOwnProperty.call(val, key);
32
+
33
+ // 比较两个值是否发生变化,考虑 NaN 的情况
34
+ export const hasChanged = (value: any, oldValue: any): boolean =>
35
+ !Object.is(value, oldValue);
36
+
37
+ function targetTypeMap(rawType: string)
38
+ {
39
+ switch (rawType)
40
+ {
41
+ case 'Object':
42
+ case 'Array':
43
+ return TargetType.COMMON;
44
+ case 'Map':
45
+ case 'Set':
46
+ case 'WeakMap':
47
+ case 'WeakSet':
48
+ return TargetType.COLLECTION;
49
+ default:
50
+ return TargetType.COMMON;
51
+ }
52
+ }
53
+
54
+ export function getTargetType(value: Target)
55
+ {
56
+ if (!Object.isExtensible(value)) return TargetType.INVALID;
57
+
58
+ return targetTypeMap(toRawType(value));
59
+ }
60
+
61
+ const toTypeString = (value: unknown): string => Object.prototype.toString.call(value);
62
+
63
+ // 获取值的原始类型
64
+ export const toRawType = (value: unknown): string =>
65
+
66
+ // 从类似 "[object RawType]" 的字符串中提取 "RawType"
67
+ toTypeString(value).slice(8, -1);
68
+
69
+ export interface Target
70
+ {
71
+ [ReactiveFlags.IS_REACTIVE]?: boolean
72
+ [ReactiveFlags.RAW]?: any
73
+ }
74
+
75
+ /**
76
+ * 将一个响应式对象转换为原始对象。
77
+ * @param observed 响应式对象。
78
+ * @returns 原始对象。
79
+ */
80
+ export function toRaw<T>(observed: T): T
81
+ {
82
+ const raw = observed && (observed as Target)[ReactiveFlags.RAW];
83
+
84
+ return raw ? toRaw(raw) : observed;
85
+ }
86
+
87
+ export function warn(msg: string, ...args: any[]): void
88
+ {
89
+ console.warn(`[Vue warn] ${msg}`, ...args);
90
+ }
91
+
92
+ /**
93
+ * 创建一个映射,并返回一个用于检查键是否存在于该映射中的函数。
94
+ * 重要提示:所有调用此函数的地方都必须以 \/\*#\_\_PURE\_\_\*\/ 作为前缀,
95
+ * 以便在必要时 Rollup 可以进行 tree-shaking。
96
+ */
97
+
98
+ /* ! #__NO_SIDE_EFFECTS__ */
99
+ export function makeMap(str: string): (key: string) => boolean
100
+ {
101
+ // 创建一个空对象作为映射,使用 Object.create(null) 避免原型链上的属性干扰
102
+ const map = Object.create(null);
103
+
104
+ // 将输入的字符串按逗号分隔,遍历每个键并将其添加到映射中,值为 1
105
+ for (const key of str.split(',')) map[key] = 1;
106
+
107
+ // 返回一个函数,该函数接受一个键值,并检查该键是否存在于映射中
108
+ return (val) => val in map;
109
+ }