cx 23.4.3 → 23.4.5

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/hooks.js ADDED
@@ -0,0 +1,146 @@
1
+ import { getCurrentInstance } from "cx/ui";
2
+ import { expression, Ref, stringTemplate, getSelector, computable } from "cx/data";
3
+ import { isObject, isArray, isFunction, isString } from "cx/util";
4
+
5
+ function useStore() {
6
+ return getCurrentInstance().store;
7
+ }
8
+ function useStoreMethods() {
9
+ return getCurrentInstance().store.getMethods();
10
+ }
11
+ function ref(info) {
12
+ if (isObject(info)) {
13
+ if (info.bind) return useStore().ref(info.bind, info.defaultValue);
14
+ if (info.get) return info;
15
+ if (info.expr) {
16
+ var store = useStore();
17
+ var selector = expression(info.expr).memoize();
18
+ return new Ref({
19
+ get: function get() {
20
+ return selector(store.getData());
21
+ },
22
+ set: info.set,
23
+ });
24
+ }
25
+ if (info.tpl) {
26
+ var _store = useStore();
27
+ var _selector = stringTemplate(info.tpl).memoize();
28
+ return new Ref({
29
+ get: function get() {
30
+ return _selector(_store.getData());
31
+ },
32
+ set: info.set,
33
+ });
34
+ }
35
+ }
36
+ return getSelector(info);
37
+ }
38
+
39
+ function useEffect(callback) {
40
+ var destroyCallback = callback();
41
+ if (destroyCallback) getCurrentInstance().subscribeOnDestroy(destroyCallback);
42
+ }
43
+ function useCleanup(cleanupCallback) {
44
+ var unsubscribe = getCurrentInstance().subscribeOnDestroy(cleanupCallback);
45
+ return function () {
46
+ unsubscribe();
47
+ cleanupCallback();
48
+ };
49
+ }
50
+
51
+ function useInterval(callback, timeout) {
52
+ var timer = setInterval(callback, timeout);
53
+ return useCleanup(function () {
54
+ clearInterval(timer);
55
+ });
56
+ }
57
+
58
+ function addExploreCallback(callback) {
59
+ var instance = getCurrentInstance();
60
+ if (!instance.computables) instance.computables = [];
61
+ instance.computables.push(callback);
62
+ return function () {
63
+ instance.computables = instance.computables.filter(function (cb) {
64
+ return cb !== callback;
65
+ });
66
+ };
67
+ }
68
+ function useTrigger(args, callback, autoRun) {
69
+ if (!isArray(args)) throw new Error("First argument to addTrigger should be an array.");
70
+ var store = useStore();
71
+ var selector = computable.apply(void 0, args.concat([callback])).memoize(!autoRun && store.getData());
72
+ return addExploreCallback(selector);
73
+ }
74
+
75
+ var key = 0;
76
+ function useState(defaultValue) {
77
+ var _instance$setState;
78
+ var instance = getCurrentInstance();
79
+ var storeKey = "_state" + ++key;
80
+ instance.setState(((_instance$setState = {}), (_instance$setState[storeKey] = defaultValue), _instance$setState));
81
+ return new Ref({
82
+ get: function get() {
83
+ return instance.state[storeKey];
84
+ },
85
+ set: function set(value) {
86
+ var _instance$setState2;
87
+ return instance.setState(
88
+ ((_instance$setState2 = {}), (_instance$setState2[storeKey] = value), _instance$setState2)
89
+ );
90
+ },
91
+ });
92
+ }
93
+
94
+ function createLoacalStorageRef(key) {
95
+ var store = useStore();
96
+ return new Ref({
97
+ get: function get() {
98
+ var json = localStorage.getItem(key);
99
+ if (!json) return localStorage.hasOwnProperty(key) ? null : undefined;
100
+ return JSON.parse(json);
101
+ },
102
+ set: function set(value) {
103
+ if (value === undefined) localStorage.removeItem(key);
104
+ else localStorage.setItem(key, JSON.stringify(value));
105
+ store.meta.version++;
106
+ store.notify();
107
+ },
108
+ });
109
+ }
110
+
111
+ function resolveCallback(callback, instance) {
112
+ if (isFunction(callback)) return callback;
113
+ if (isString(callback)) {
114
+ if (!instance) instance = getCurrentInstance();
115
+ return function () {
116
+ var _instance;
117
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
118
+ args[_key] = arguments[_key];
119
+ }
120
+ return (_instance = instance).invokeControllerMethod.apply(_instance, [callback].concat(args));
121
+ };
122
+ }
123
+ }
124
+
125
+ function invokeCallback(instance, callback) {
126
+ for (var _len = arguments.length, args = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
127
+ args[_key - 2] = arguments[_key];
128
+ }
129
+ if (isString(callback)) return instance.invokeControllerMethod.apply(instance, [callback].concat(args));
130
+ if (isFunction(callback)) return callback.apply(void 0, args);
131
+ }
132
+
133
+ export {
134
+ addExploreCallback,
135
+ createLoacalStorageRef,
136
+ invokeCallback,
137
+ ref,
138
+ resolveCallback,
139
+ useCleanup,
140
+ useEffect,
141
+ useInterval,
142
+ useState,
143
+ useStore,
144
+ useStoreMethods,
145
+ useTrigger,
146
+ };