cx 26.0.4 → 26.0.6

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 (127) hide show
  1. package/build/data/ArrayElementView.spec.js +1 -1
  2. package/build/hooks/store.spec.js +1 -1
  3. package/build/hooks/useTrigger.spec.js +1 -1
  4. package/build/ui/Controller.spec.js +6 -2
  5. package/build/ui/app/startAppLoop.d.ts +3 -2
  6. package/build/ui/app/startHotAppLoop.d.ts +3 -2
  7. package/build/ui/layout/ContentPlaceholder.spec.js +12 -12
  8. package/build.js +129 -129
  9. package/dist/manifest.js +844 -844
  10. package/package.json +3 -2
  11. package/src/charts/Chart.ts +108 -108
  12. package/src/data/ArrayElementView.ts +90 -90
  13. package/src/data/AugmentedViewBase.ts +88 -88
  14. package/src/data/Binding.ts +104 -104
  15. package/src/data/ExposedRecordView.ts +95 -95
  16. package/src/data/ExposedValueView.ts +89 -89
  17. package/src/data/Expression.spec.ts +229 -229
  18. package/src/data/Expression.ts +233 -233
  19. package/src/data/Grouper.spec.ts +57 -57
  20. package/src/data/Grouper.ts +158 -158
  21. package/src/data/NestedDataView.ts +43 -43
  22. package/src/data/ReadOnlyDataView.ts +39 -39
  23. package/src/data/Ref.ts +104 -104
  24. package/src/data/Selector.ts +10 -10
  25. package/src/data/Store.ts +52 -52
  26. package/src/data/StoreProxy.ts +19 -19
  27. package/src/data/StoreRef.ts +66 -66
  28. package/src/data/StringTemplate.spec.ts +132 -132
  29. package/src/data/StringTemplate.ts +93 -93
  30. package/src/data/StructuredSelector.spec.ts +113 -113
  31. package/src/data/StructuredSelector.ts +146 -146
  32. package/src/data/SubscribableView.ts +63 -63
  33. package/src/data/ZoomIntoPropertyView.spec.ts +64 -64
  34. package/src/data/ZoomIntoPropertyView.ts +45 -45
  35. package/src/data/computable.spec.ts +62 -62
  36. package/src/data/createAccessorModelProxy.ts +60 -60
  37. package/src/data/createStructuredSelector.ts +62 -62
  38. package/src/data/getAccessor.spec.ts +11 -11
  39. package/src/data/getAccessor.ts +74 -74
  40. package/src/data/getSelector.spec.ts +43 -43
  41. package/src/data/getSelector.ts +66 -66
  42. package/src/data/ops/filter.spec.ts +35 -35
  43. package/src/data/ops/filter.ts +9 -9
  44. package/src/data/ops/merge.ts +13 -13
  45. package/src/data/ops/removeTreeNodes.spec.ts +37 -37
  46. package/src/data/ops/removeTreeNodes.ts +15 -15
  47. package/src/data/ops/updateArray.spec.ts +69 -69
  48. package/src/data/ops/updateArray.ts +31 -31
  49. package/src/data/ops/updateTree.ts +23 -23
  50. package/src/data/test-types.ts +7 -7
  51. package/src/hooks/useTrigger.ts +26 -26
  52. package/src/index.scss +6 -6
  53. package/src/jsx-runtime.ts +72 -72
  54. package/src/svg/BoundedObject.ts +101 -101
  55. package/src/ui/CSSHelper.ts +17 -17
  56. package/src/ui/ContentResolver.ts +124 -124
  57. package/src/ui/Controller.ts +189 -189
  58. package/src/ui/Culture.ts +159 -159
  59. package/src/ui/DataProxy.ts +55 -55
  60. package/src/ui/FocusManager.ts +171 -171
  61. package/src/ui/Instance.ts +868 -868
  62. package/src/ui/RenderingContext.ts +99 -99
  63. package/src/ui/Rescope.ts +49 -49
  64. package/src/ui/StructuredInstanceDataAccessor.ts +32 -32
  65. package/src/ui/VDOM.ts +34 -34
  66. package/src/ui/adapter/ArrayAdapter.spec.ts +55 -55
  67. package/src/ui/adapter/ArrayAdapter.ts +226 -226
  68. package/src/ui/adapter/TreeAdapter.spec.ts +76 -76
  69. package/src/ui/adapter/TreeAdapter.ts +185 -185
  70. package/src/ui/app/History.ts +133 -133
  71. package/src/ui/app/Url.spec.ts +50 -50
  72. package/src/ui/app/startAppLoop.tsx +2 -1
  73. package/src/ui/app/startHotAppLoop.ts +2 -1
  74. package/src/ui/createFunctionalComponent.ts +65 -65
  75. package/src/ui/index.ts +45 -45
  76. package/src/ui/layout/Content.ts +30 -30
  77. package/src/ui/layout/FirstVisibleChildLayout.ts +60 -60
  78. package/src/ui/selection/KeySelection.ts +165 -165
  79. package/src/ui/selection/PropertySelection.ts +87 -87
  80. package/src/ui/selection/Selection.ts +118 -118
  81. package/src/util/Format.ts +267 -267
  82. package/src/util/browserSupportsPassiveEventHandlers.ts +20 -20
  83. package/src/util/color/rgbToHsl.ts +35 -35
  84. package/src/util/getActiveElement.ts +4 -4
  85. package/src/util/hasKey.ts +18 -18
  86. package/src/util/index.ts +55 -55
  87. package/src/util/innerTextTrim.ts +10 -10
  88. package/src/util/isArray.ts +3 -3
  89. package/src/util/isDataRecord.ts +5 -5
  90. package/src/util/isDefined.ts +3 -3
  91. package/src/util/isString.ts +3 -3
  92. package/src/widgets/Sandbox.ts +103 -103
  93. package/src/widgets/autoFocus.ts +9 -9
  94. package/src/widgets/cx.ts +63 -63
  95. package/src/widgets/drag-drop/ops.tsx +1 -1
  96. package/src/widgets/grid/GridCell.ts +143 -143
  97. package/src/widgets/icons/calendar.tsx +17 -17
  98. package/src/widgets/icons/check.tsx +13 -13
  99. package/src/widgets/icons/clear.tsx +15 -15
  100. package/src/widgets/icons/close.tsx +20 -20
  101. package/src/widgets/icons/cx.tsx +38 -38
  102. package/src/widgets/icons/drop-down.tsx +15 -15
  103. package/src/widgets/icons/file.tsx +13 -13
  104. package/src/widgets/icons/folder-open.tsx +15 -15
  105. package/src/widgets/icons/folder.tsx +13 -13
  106. package/src/widgets/icons/forward.tsx +22 -22
  107. package/src/widgets/icons/loading.tsx +24 -24
  108. package/src/widgets/icons/menu.tsx +17 -17
  109. package/src/widgets/icons/pixel-picker.tsx +18 -18
  110. package/src/widgets/icons/search.tsx +13 -13
  111. package/src/widgets/icons/sort-asc.tsx +14 -14
  112. package/src/widgets/icons/square.tsx +18 -18
  113. package/src/widgets/nav/Route.ts +142 -142
  114. package/src/widgets/overlay/ContextMenu.ts +42 -42
  115. package/src/widgets/overlay/Dropdown.tsx +762 -762
  116. package/src/widgets/overlay/MsgBox.tsx +141 -141
  117. package/src/widgets/overlay/Toast.ts +111 -111
  118. package/src/widgets/overlay/Window.tsx +299 -299
  119. package/src/widgets/overlay/alerts.ts +46 -46
  120. package/src/widgets/overlay/captureMouse.ts +195 -195
  121. package/src/widgets/overlay/createHotPromiseWindowFactory.ts +72 -72
  122. package/src/widgets/overlay/index.d.ts +11 -11
  123. package/src/widgets/overlay/index.ts +11 -11
  124. package/src/widgets/overlay/tooltip-ops.ts +173 -173
  125. package/build/ui/PureContainer.spec.d.ts +0 -1
  126. package/build/ui/PureContainer.spec.js +0 -149
  127. package/dist/manifest.d.ts +0 -1443
@@ -1,62 +1,62 @@
1
- import { computable } from "./computable";
2
- import assert from "assert";
3
- import { createAccessorModelProxy } from "./createAccessorModelProxy";
4
-
5
- describe("computable", function () {
6
- it("creates a selector", function () {
7
- let state = { person: { name: "Joe" } };
8
- let nameLength = computable("person.name", (name: string) => name.length);
9
- assert.equal(nameLength(state), 3);
10
- });
11
-
12
- it("fires every time if not memoized", function () {
13
- let state = { person: { name: "Joe" } };
14
- let fired = 0;
15
- let nameLength = computable("person.name", (name: string) => {
16
- fired++;
17
- return name.length;
18
- });
19
- assert.equal(nameLength(state), 3);
20
- assert.equal(nameLength(state), 3);
21
- assert.equal(fired, 2);
22
- });
23
-
24
- it("fires once if memoized and data has not changed", function () {
25
- let state = { person: { name: "Joe" } };
26
- let fired = 0;
27
- let nameLength = computable("person.name", (name: string) => {
28
- fired++;
29
- return name.length;
30
- }).memoize();
31
- assert.equal(nameLength(state), 3);
32
- assert.equal(nameLength(state), 3);
33
- assert.equal(fired, 1);
34
- });
35
-
36
- //weird but that's how triggers work and
37
- it("memoize with warmup data will not call compute", function () {
38
- let state = { person: { name: "Joe" } };
39
- let fired = 0;
40
- let nameLength = computable("person.name", (name: string) => {
41
- fired++;
42
- return name.length;
43
- }).memoize(state);
44
- assert.equal(nameLength(state), undefined);
45
- assert.equal(nameLength(state), undefined);
46
- assert.equal(fired, 0);
47
- });
48
-
49
- it("works with accessors", function () {
50
- var m = createAccessorModelProxy<{ person: { name: string } }>();
51
- let state = { person: { name: "Joe" } };
52
- let nameLength = computable(m.person.name, (name) => name.length);
53
- assert.equal(nameLength(state), 3);
54
- });
55
-
56
- it("works with array length accessor", function () {
57
- var m = createAccessorModelProxy<{ items: string[] }>();
58
- let state = { items: ["a", "b", "c"] };
59
- let itemCount = computable(m.items.length, (length) => length);
60
- assert.equal(itemCount(state), 3);
61
- });
62
- });
1
+ import { computable } from "./computable";
2
+ import assert from "assert";
3
+ import { createAccessorModelProxy } from "./createAccessorModelProxy";
4
+
5
+ describe("computable", function () {
6
+ it("creates a selector", function () {
7
+ let state = { person: { name: "Joe" } };
8
+ let nameLength = computable("person.name", (name: string) => name.length);
9
+ assert.equal(nameLength(state), 3);
10
+ });
11
+
12
+ it("fires every time if not memoized", function () {
13
+ let state = { person: { name: "Joe" } };
14
+ let fired = 0;
15
+ let nameLength = computable("person.name", (name: string) => {
16
+ fired++;
17
+ return name.length;
18
+ });
19
+ assert.equal(nameLength(state), 3);
20
+ assert.equal(nameLength(state), 3);
21
+ assert.equal(fired, 2);
22
+ });
23
+
24
+ it("fires once if memoized and data has not changed", function () {
25
+ let state = { person: { name: "Joe" } };
26
+ let fired = 0;
27
+ let nameLength = computable("person.name", (name: string) => {
28
+ fired++;
29
+ return name.length;
30
+ }).memoize();
31
+ assert.equal(nameLength(state), 3);
32
+ assert.equal(nameLength(state), 3);
33
+ assert.equal(fired, 1);
34
+ });
35
+
36
+ //weird but that's how triggers work and
37
+ it("memoize with warmup data will not call compute", function () {
38
+ let state = { person: { name: "Joe" } };
39
+ let fired = 0;
40
+ let nameLength = computable("person.name", (name: string) => {
41
+ fired++;
42
+ return name.length;
43
+ }).memoize(state);
44
+ assert.equal(nameLength(state), undefined);
45
+ assert.equal(nameLength(state), undefined);
46
+ assert.equal(fired, 0);
47
+ });
48
+
49
+ it("works with accessors", function () {
50
+ var m = createAccessorModelProxy<{ person: { name: string } }>();
51
+ let state = { person: { name: "Joe" } };
52
+ let nameLength = computable(m.person.name, (name) => name.length);
53
+ assert.equal(nameLength(state), 3);
54
+ });
55
+
56
+ it("works with array length accessor", function () {
57
+ var m = createAccessorModelProxy<{ items: string[] }>();
58
+ let state = { items: ["a", "b", "c"] };
59
+ let itemCount = computable(m.items.length, (length) => length);
60
+ assert.equal(itemCount(state), 3);
61
+ });
62
+ });
@@ -1,60 +1,60 @@
1
- interface AccessorChainMethods {
2
- toString(): string;
3
- valueOf(): unknown;
4
- nameOf(): string;
5
- }
6
-
7
- type AccessorChainMap<M extends object> = {
8
- [prop in Exclude<keyof M, keyof AccessorChainMethods>]: AccessorChain<M[prop]>;
9
- };
10
-
11
- export type AccessorChain<M> = {
12
- toString(): string;
13
- valueOf(): string;
14
- nameOf(): string;
15
- __accessorChainType?: M; // Type-only marker for inference
16
- } & (M extends object ? AccessorChainMap<M> : {});
17
-
18
- const emptyFn = () => {};
19
-
20
- export function createAccessorModelProxy<M>(chain: string = ""): AccessorChain<M> {
21
- let lastOp: string | null = null;
22
-
23
- const proxy = new Proxy(emptyFn, {
24
- get: (_, name: string | symbol) => {
25
- if (typeof name !== "string") return proxy;
26
-
27
- switch (name) {
28
- case "isAccessorChain":
29
- return true;
30
-
31
- case "toString":
32
- case "valueOf":
33
- case "nameOf":
34
- lastOp = name;
35
- return proxy;
36
- }
37
-
38
- let newChain = chain;
39
- if (newChain.length > 0) newChain += ".";
40
- newChain += name;
41
- return createAccessorModelProxy(newChain);
42
- },
43
-
44
- apply(): string {
45
- switch (lastOp) {
46
- case "nameOf":
47
- const lastDotIndex = chain.lastIndexOf(".");
48
- return lastDotIndex > 0 ? chain.substring(lastDotIndex + 1) : chain;
49
-
50
- default:
51
- return chain;
52
- }
53
- },
54
- });
55
- return proxy as unknown as AccessorChain<M>;
56
- }
57
-
58
- export function isAccessorChain<M>(value: unknown): value is AccessorChain<M> {
59
- return value != null && !!(value as any).isAccessorChain;
60
- }
1
+ interface AccessorChainMethods {
2
+ toString(): string;
3
+ valueOf(): unknown;
4
+ nameOf(): string;
5
+ }
6
+
7
+ type AccessorChainMap<M extends object> = {
8
+ [prop in Exclude<keyof M, keyof AccessorChainMethods>]: AccessorChain<M[prop]>;
9
+ };
10
+
11
+ export type AccessorChain<M> = {
12
+ toString(): string;
13
+ valueOf(): string;
14
+ nameOf(): string;
15
+ __accessorChainType?: M; // Type-only marker for inference
16
+ } & (M extends object ? AccessorChainMap<M> : {});
17
+
18
+ const emptyFn = () => {};
19
+
20
+ export function createAccessorModelProxy<M>(chain: string = ""): AccessorChain<M> {
21
+ let lastOp: string | null = null;
22
+
23
+ const proxy = new Proxy(emptyFn, {
24
+ get: (_, name: string | symbol) => {
25
+ if (typeof name !== "string") return proxy;
26
+
27
+ switch (name) {
28
+ case "isAccessorChain":
29
+ return true;
30
+
31
+ case "toString":
32
+ case "valueOf":
33
+ case "nameOf":
34
+ lastOp = name;
35
+ return proxy;
36
+ }
37
+
38
+ let newChain = chain;
39
+ if (newChain.length > 0) newChain += ".";
40
+ newChain += name;
41
+ return createAccessorModelProxy(newChain);
42
+ },
43
+
44
+ apply(): string {
45
+ switch (lastOp) {
46
+ case "nameOf":
47
+ const lastDotIndex = chain.lastIndexOf(".");
48
+ return lastDotIndex > 0 ? chain.substring(lastDotIndex + 1) : chain;
49
+
50
+ default:
51
+ return chain;
52
+ }
53
+ },
54
+ });
55
+ return proxy as unknown as AccessorChain<M>;
56
+ }
57
+
58
+ export function isAccessorChain<M>(value: unknown): value is AccessorChain<M> {
59
+ return value != null && !!(value as any).isAccessorChain;
60
+ }
@@ -1,62 +1,62 @@
1
- import { MemoSelector, Selector } from "./Selector";
2
-
3
- export interface StructuredSelectorConfig {
4
- [prop: string]: Selector;
5
- }
6
-
7
- // Helper type to infer result type from selector config
8
- type InferStructuredSelectorResult<T extends StructuredSelectorConfig, C = {}> = {
9
- [K in keyof T]: T[K] extends Selector<infer R> ? R : any;
10
- } & C;
11
-
12
- export function createStructuredSelector<S extends StructuredSelectorConfig, C extends Record<string, any> = {}>(
13
- selector: S,
14
- constants?: C,
15
- ): MemoSelector<InferStructuredSelectorResult<S, C>> {
16
- let keys = Object.keys(selector);
17
- constants = constants ?? ({} as C);
18
- if (keys.length == 0) {
19
- let result: Selector = () => constants;
20
- result.memoize = () => result;
21
- return result as MemoSelector<InferStructuredSelectorResult<S, C>>;
22
- }
23
-
24
- function memoize() {
25
- let lastResult: Record<string, any> = Object.assign({}, constants);
26
-
27
- let memoizedSelectors: Record<string, Selector> = {};
28
-
29
- keys.forEach((key) => {
30
- memoizedSelectors[key] = selector[key].memoize ? selector[key].memoize() : selector[key];
31
- lastResult[key] = undefined;
32
- });
33
-
34
- return function (data: any) {
35
- let result = lastResult,
36
- k,
37
- v,
38
- i;
39
- for (i = 0; i < keys.length; i++) {
40
- k = keys[i];
41
- v = memoizedSelectors[k](data);
42
- if (result === lastResult) {
43
- if (v === lastResult[k]) continue;
44
- result = Object.assign({}, lastResult);
45
- }
46
- result[k] = v;
47
- }
48
- return (lastResult = result);
49
- };
50
- }
51
-
52
- let result: Selector = function evaluate(data: any) {
53
- let result: Record<string, any> = Object.assign({}, constants);
54
- for (let i = 0; i < keys.length; i++) {
55
- result[keys[i]] = selector[keys[i]](data);
56
- }
57
- return result;
58
- };
59
-
60
- result.memoize = memoize;
61
- return result as MemoSelector<InferStructuredSelectorResult<S, C>>;
62
- }
1
+ import { MemoSelector, Selector } from "./Selector";
2
+
3
+ export interface StructuredSelectorConfig {
4
+ [prop: string]: Selector;
5
+ }
6
+
7
+ // Helper type to infer result type from selector config
8
+ type InferStructuredSelectorResult<T extends StructuredSelectorConfig, C = {}> = {
9
+ [K in keyof T]: T[K] extends Selector<infer R> ? R : any;
10
+ } & C;
11
+
12
+ export function createStructuredSelector<S extends StructuredSelectorConfig, C extends Record<string, any> = {}>(
13
+ selector: S,
14
+ constants?: C,
15
+ ): MemoSelector<InferStructuredSelectorResult<S, C>> {
16
+ let keys = Object.keys(selector);
17
+ constants = constants ?? ({} as C);
18
+ if (keys.length == 0) {
19
+ let result: Selector = () => constants;
20
+ result.memoize = () => result;
21
+ return result as MemoSelector<InferStructuredSelectorResult<S, C>>;
22
+ }
23
+
24
+ function memoize() {
25
+ let lastResult: Record<string, any> = Object.assign({}, constants);
26
+
27
+ let memoizedSelectors: Record<string, Selector> = {};
28
+
29
+ keys.forEach((key) => {
30
+ memoizedSelectors[key] = selector[key].memoize ? selector[key].memoize() : selector[key];
31
+ lastResult[key] = undefined;
32
+ });
33
+
34
+ return function (data: any) {
35
+ let result = lastResult,
36
+ k,
37
+ v,
38
+ i;
39
+ for (i = 0; i < keys.length; i++) {
40
+ k = keys[i];
41
+ v = memoizedSelectors[k](data);
42
+ if (result === lastResult) {
43
+ if (v === lastResult[k]) continue;
44
+ result = Object.assign({}, lastResult);
45
+ }
46
+ result[k] = v;
47
+ }
48
+ return (lastResult = result);
49
+ };
50
+ }
51
+
52
+ let result: Selector = function evaluate(data: any) {
53
+ let result: Record<string, any> = Object.assign({}, constants);
54
+ for (let i = 0; i < keys.length; i++) {
55
+ result[keys[i]] = selector[keys[i]](data);
56
+ }
57
+ return result;
58
+ };
59
+
60
+ result.memoize = memoize;
61
+ return result as MemoSelector<InferStructuredSelectorResult<S, C>>;
62
+ }
@@ -1,11 +1,11 @@
1
- import assert from "assert";
2
- import { createAccessorModelProxy } from "./createAccessorModelProxy";
3
- import { getAccessor } from "./getAccessor";
4
-
5
- describe("getAccessor", function () {
6
- it("works with accessor chains", function () {
7
- let m = createAccessorModelProxy<{ a: { b: any } }>();
8
- let accessor = getAccessor(m.a.b);
9
- assert(typeof accessor.set == "function");
10
- });
11
- });
1
+ import assert from "assert";
2
+ import { createAccessorModelProxy } from "./createAccessorModelProxy";
3
+ import { getAccessor } from "./getAccessor";
4
+
5
+ describe("getAccessor", function () {
6
+ it("works with accessor chains", function () {
7
+ let m = createAccessorModelProxy<{ a: { b: any } }>();
8
+ let accessor = getAccessor(m.a.b);
9
+ assert(typeof accessor.set == "function");
10
+ });
11
+ });
@@ -1,74 +1,74 @@
1
- import { Binding, isBinding } from "./Binding";
2
- import { isSelector } from "./isSelector";
3
- import { getSelector } from "./getSelector";
4
- import { isObject } from "../util/isObject";
5
- import { AccessorChain, isAccessorChain } from "./createAccessorModelProxy";
6
- import { Prop } from "../ui/Prop";
7
- import { View } from "./View";
8
-
9
- /*
10
- Accessor provides a common ground between refs and bindings.
11
- Refs offer simplicity, bindings have better performance with more arguments.
12
- Accessor works as a common interface which works with both patterns.
13
- */
14
-
15
- export interface Accessor<T = any> {
16
- get: (data: any) => T;
17
- set?: (value: T, store: View) => boolean;
18
- bindInstance?(instance: any): Accessor;
19
- isAccessor?: boolean;
20
- isRef?: boolean;
21
- }
22
-
23
- export function getAccessor<T = any>(accessor: Prop<T>): Accessor;
24
- export function getAccessor(accessor: Accessor): Accessor;
25
-
26
- export function getAccessor(accessor: any): Accessor | undefined {
27
- if (accessor == null) return undefined;
28
-
29
- if (isObject(accessor)) {
30
- if ("isAccessor" in accessor || "isRef" in accessor) return accessor as Accessor;
31
- if (isBinding(accessor)) {
32
- let binding = Binding.get(accessor);
33
- return {
34
- get: binding.value,
35
- set: (v, store) => store.set(binding.path, v),
36
- isAccessor: true,
37
- };
38
- }
39
- }
40
-
41
- if (isAccessorChain(accessor)) {
42
- let binding = Binding.get(accessor);
43
- return {
44
- get: binding.value,
45
- set: (v, store) => store.set(binding.path, v),
46
- isAccessor: true,
47
- };
48
- }
49
-
50
- if (isSelector(accessor)) {
51
- let selector = getSelector(accessor);
52
- if (accessor && accessor.set)
53
- return {
54
- get: selector,
55
- isAccessor: true,
56
- bindInstance(instance) {
57
- return {
58
- get: selector,
59
- set: (value) => accessor.set(value, instance),
60
- isAccessor: true,
61
- };
62
- },
63
- };
64
-
65
- return {
66
- get: selector,
67
- isAccessor: true,
68
- };
69
- }
70
-
71
- return {
72
- get: () => accessor,
73
- };
74
- }
1
+ import { Binding, isBinding } from "./Binding";
2
+ import { isSelector } from "./isSelector";
3
+ import { getSelector } from "./getSelector";
4
+ import { isObject } from "../util/isObject";
5
+ import { AccessorChain, isAccessorChain } from "./createAccessorModelProxy";
6
+ import { Prop } from "../ui/Prop";
7
+ import { View } from "./View";
8
+
9
+ /*
10
+ Accessor provides a common ground between refs and bindings.
11
+ Refs offer simplicity, bindings have better performance with more arguments.
12
+ Accessor works as a common interface which works with both patterns.
13
+ */
14
+
15
+ export interface Accessor<T = any> {
16
+ get: (data: any) => T;
17
+ set?: (value: T, store: View) => boolean;
18
+ bindInstance?(instance: any): Accessor;
19
+ isAccessor?: boolean;
20
+ isRef?: boolean;
21
+ }
22
+
23
+ export function getAccessor<T = any>(accessor: Prop<T>): Accessor;
24
+ export function getAccessor(accessor: Accessor): Accessor;
25
+
26
+ export function getAccessor(accessor: any): Accessor | undefined {
27
+ if (accessor == null) return undefined;
28
+
29
+ if (isObject(accessor)) {
30
+ if ("isAccessor" in accessor || "isRef" in accessor) return accessor as Accessor;
31
+ if (isBinding(accessor)) {
32
+ let binding = Binding.get(accessor);
33
+ return {
34
+ get: binding.value,
35
+ set: (v, store) => store.set(binding.path, v),
36
+ isAccessor: true,
37
+ };
38
+ }
39
+ }
40
+
41
+ if (isAccessorChain(accessor)) {
42
+ let binding = Binding.get(accessor);
43
+ return {
44
+ get: binding.value,
45
+ set: (v, store) => store.set(binding.path, v),
46
+ isAccessor: true,
47
+ };
48
+ }
49
+
50
+ if (isSelector(accessor)) {
51
+ let selector = getSelector(accessor);
52
+ if (accessor && accessor.set)
53
+ return {
54
+ get: selector,
55
+ isAccessor: true,
56
+ bindInstance(instance) {
57
+ return {
58
+ get: selector,
59
+ set: (value) => accessor.set(value, instance),
60
+ isAccessor: true,
61
+ };
62
+ },
63
+ };
64
+
65
+ return {
66
+ get: selector,
67
+ isAccessor: true,
68
+ };
69
+ }
70
+
71
+ return {
72
+ get: () => accessor,
73
+ };
74
+ }
@@ -1,43 +1,43 @@
1
- import { getSelector } from "./getSelector";
2
- import assert from "assert";
3
- import { createAccessorModelProxy } from "./createAccessorModelProxy";
4
-
5
- describe("getSelector", function () {
6
- it("array of selectors converts each element into a selector", function () {
7
- let arraySelector = [{ bind: "name" }, { expr: "{name}" }];
8
- let state = { name: "Joe" };
9
- let selector = getSelector(arraySelector);
10
- assert.deepEqual(selector(state), ["Joe", "Joe"]);
11
- });
12
-
13
- it("get can be used for selectors that have set defined too ", function () {
14
- let selector = getSelector({ get: (data: any) => data.name, set: () => {} });
15
- assert.deepEqual(selector({ name: "Jack" }), "Jack");
16
- });
17
-
18
- it("0 is a valid selector", function () {
19
- let selector = getSelector(0);
20
- assert.deepEqual(selector({}), 0);
21
- });
22
-
23
- it("false is a valid selector", function () {
24
- let selector = getSelector(false);
25
- assert.deepEqual(selector({}), false);
26
- });
27
-
28
- it("undefined is a valid selector", function () {
29
- let selector = getSelector(undefined);
30
- assert(selector({}) === undefined);
31
- });
32
-
33
- it("null is a valid selector", function () {
34
- let selector = getSelector(null);
35
- assert(selector({}) === null);
36
- });
37
-
38
- it("works with accessor chains", function () {
39
- let m = createAccessorModelProxy<{ a: { b: any } }>();
40
- let selector = getSelector(m.a.b);
41
- assert(selector({ a: { b: 1 } }) === 1);
42
- });
43
- });
1
+ import { getSelector } from "./getSelector";
2
+ import assert from "assert";
3
+ import { createAccessorModelProxy } from "./createAccessorModelProxy";
4
+
5
+ describe("getSelector", function () {
6
+ it("array of selectors converts each element into a selector", function () {
7
+ let arraySelector = [{ bind: "name" }, { expr: "{name}" }];
8
+ let state = { name: "Joe" };
9
+ let selector = getSelector(arraySelector);
10
+ assert.deepEqual(selector(state), ["Joe", "Joe"]);
11
+ });
12
+
13
+ it("get can be used for selectors that have set defined too ", function () {
14
+ let selector = getSelector({ get: (data: any) => data.name, set: () => {} });
15
+ assert.deepEqual(selector({ name: "Jack" }), "Jack");
16
+ });
17
+
18
+ it("0 is a valid selector", function () {
19
+ let selector = getSelector(0);
20
+ assert.deepEqual(selector({}), 0);
21
+ });
22
+
23
+ it("false is a valid selector", function () {
24
+ let selector = getSelector(false);
25
+ assert.deepEqual(selector({}), false);
26
+ });
27
+
28
+ it("undefined is a valid selector", function () {
29
+ let selector = getSelector(undefined);
30
+ assert(selector({}) === undefined);
31
+ });
32
+
33
+ it("null is a valid selector", function () {
34
+ let selector = getSelector(null);
35
+ assert(selector({}) === null);
36
+ });
37
+
38
+ it("works with accessor chains", function () {
39
+ let m = createAccessorModelProxy<{ a: { b: any } }>();
40
+ let selector = getSelector(m.a.b);
41
+ assert(selector({ a: { b: 1 } }) === 1);
42
+ });
43
+ });