cx 24.3.9 → 24.3.10

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/src/ui/Restate.js CHANGED
@@ -1,160 +1,163 @@
1
- import { PureContainer } from "./PureContainer";
2
- import { Store } from "../data/Store";
3
- import { Cx } from "./Cx";
4
- import { isString } from "../util/isString";
5
- import { VDOM } from "./VDOM";
6
- import { isFunction } from "../util/isFunction";
7
- import { isObject } from "../util/isObject";
8
- import { isUndefined } from "../util/isUndefined";
9
- import { Binding } from "../data/Binding";
10
- import { StructuredSelector } from "../data/StructuredSelector";
11
-
12
- let persistenceCache = {};
13
-
14
- export class Restate extends PureContainer {
15
- declareData() {
16
- return super.declareData(...arguments, {
17
- deferredUntilIdle: undefined,
18
- idleTimeout: undefined,
19
- cacheKey: undefined,
20
- });
21
- }
22
-
23
- init() {
24
- this.container = PureContainer.create({
25
- type: PureContainer,
26
- items: this.children || this.items,
27
- layout: this.layout,
28
- controller: this.controller,
29
- outerLayout: this.outerLayout,
30
- useParentLayout: !this.detached,
31
- ws: this.ws,
32
- });
33
- this.privateDataSelector = new StructuredSelector({
34
- props: this.data || {},
35
- values: this.data,
36
- });
37
- delete this.items;
38
- delete this.children;
39
- delete this.controller;
40
- delete this.outerLayout;
41
- delete this.layout;
42
- if (this.useParentLayout == null) this.useParentLayout = !this.detached;
43
- super.init();
44
- }
45
-
46
- initSubStore(context, instance) {
47
- let { cacheKey } = instance.data;
48
- this.privateDataSelector.init(instance.store);
49
- instance.subStore = new RestateStore({
50
- store: instance.store,
51
- detached: this.detached,
52
- privateData: this.data || {},
53
- data: cacheKey ? persistenceCache[cacheKey] || {} : {},
54
- dataSelector: this.privateDataSelector.create(),
55
- onSet: (path, value) => instance.nestedDataSet(path, value, this.data),
56
- });
57
-
58
- instance.setStore = (store) => {
59
- instance.store = store;
60
- instance.subStore.setStore(store);
61
- };
62
-
63
- if (cacheKey) {
64
- instance.subscribeOnDestroy(() => {
65
- persistenceCache[cacheKey] = instance.subStore.getData();
66
- });
67
- }
68
- }
69
-
70
- explore(context, instance) {
71
- if (!instance.subStore) this.initSubStore(context, instance);
72
- if (instance.subStore.parentDataCheck()) instance.markShouldUpdate();
73
- super.explore(context, instance);
74
- }
75
-
76
- exploreItems(context, instance, items) {
77
- if (!this.detached) {
78
- instance.container = instance.getChild(context, this.container, "container", instance.subStore);
79
- instance.container.scheduleExploreIfVisible(context);
80
- instance.children = [instance.container];
81
- }
82
- }
83
-
84
- render(context, instance, key) {
85
- if (!this.detached) return instance.container.render(context);
86
-
87
- return (
88
- <Cx
89
- key={key}
90
- widget={this.container}
91
- parentInstance={instance}
92
- store={instance.subStore}
93
- subscribe
94
- options={this.options}
95
- onError={this.onError}
96
- deferredUntilIdle={instance.data.deferredUntilIdle}
97
- idleTimeout={instance.data.idleTimeout}
98
- immediate={this.immediate}
99
- />
100
- );
101
- }
102
- }
103
-
104
- Restate.prototype.detached = false;
105
- Restate.prototype.waitForIdle = false;
106
- Restate.prototype.immediate = false;
107
-
108
- export const PrivateStore = Restate;
109
-
110
- class RestateStore extends Store {
111
- constructor(config) {
112
- super(config);
113
- this.parentDataVersion = -1;
114
- }
115
-
116
- getData() {
117
- this.silently(() => {
118
- this.parentDataCheck();
119
- });
120
- return super.getData();
121
- }
122
-
123
- parentDataCheck() {
124
- if (this.parentDataVersion == this.store.meta.version) return false;
125
- this.parentDataVersion = this.store.meta.version;
126
- this.parentData = this.dataSelector(this.store.getData());
127
- return this.batch(() => {
128
- for (let key in this.parentData) {
129
- super.setItem(key, this.parentData[key]);
130
- }
131
- });
132
- }
133
-
134
- setItem(path, value) {
135
- let binding = Binding.get(path);
136
- let bindingRoot = binding.parts[0];
137
- if (!isObject(this.privateData) || !this.privateData.hasOwnProperty(bindingRoot)) {
138
- let changed = isUndefined(value) ? super.deleteItem(path) : super.setItem(path, value);
139
- return changed;
140
- }
141
-
142
- let newValue = value;
143
- if (binding.parts.length > 1) newValue = binding.set(this.getData(), value)[bindingRoot];
144
- this.onSet(bindingRoot, newValue);
145
- this.batch(() => {
146
- super.setItem(bindingRoot, newValue);
147
- this.parentDataCheck();
148
- });
149
- return true;
150
- }
151
-
152
- deleteItem(path) {
153
- return this.setItem(path, undefined);
154
- }
155
-
156
- doNotify() {
157
- if (!this.detached) this.store.notify();
158
- super.doNotify();
159
- }
160
- }
1
+ import { PureContainer } from "./PureContainer";
2
+ import { Store } from "../data/Store";
3
+ import { Cx } from "./Cx";
4
+ import { isString } from "../util/isString";
5
+ import { VDOM } from "./VDOM";
6
+ import { isFunction } from "../util/isFunction";
7
+ import { isObject } from "../util/isObject";
8
+ import { isUndefined } from "../util/isUndefined";
9
+ import { Binding } from "../data/Binding";
10
+ import { StructuredSelector } from "../data/StructuredSelector";
11
+
12
+ let persistenceCache = {};
13
+
14
+ export class Restate extends PureContainer {
15
+ declareData() {
16
+ return super.declareData(...arguments, {
17
+ deferredUntilIdle: undefined,
18
+ idleTimeout: undefined,
19
+ cacheKey: undefined,
20
+ });
21
+ }
22
+
23
+ init() {
24
+ this.container = PureContainer.create({
25
+ type: PureContainer,
26
+ items: this.children || this.items,
27
+ layout: this.layout,
28
+ controller: this.controller,
29
+ outerLayout: this.outerLayout,
30
+ useParentLayout: !this.detached,
31
+ ws: this.ws,
32
+ });
33
+ this.privateDataSelector = new StructuredSelector({
34
+ props: this.data || {},
35
+ values: this.data,
36
+ });
37
+ delete this.items;
38
+ delete this.children;
39
+ delete this.controller;
40
+ delete this.outerLayout;
41
+ delete this.layout;
42
+ if (this.useParentLayout == null) this.useParentLayout = !this.detached;
43
+ super.init();
44
+ }
45
+
46
+ initSubStore(context, instance) {
47
+ let { cacheKey } = instance.data;
48
+ this.privateDataSelector.init(instance.store);
49
+ instance.subStore = new RestateStore({
50
+ store: instance.store,
51
+ detached: this.detached,
52
+ privateData: this.data || {},
53
+ data: cacheKey ? persistenceCache[cacheKey] || {} : {},
54
+ dataSelector: this.privateDataSelector.create(),
55
+ onSet: (path, value) => instance.nestedDataSet(path, value, this.data),
56
+ });
57
+
58
+ instance.setStore = (store) => {
59
+ instance.store = store;
60
+ instance.subStore.setStore(store);
61
+ };
62
+
63
+ if (cacheKey) {
64
+ instance.subscribeOnDestroy(() => {
65
+ persistenceCache[cacheKey] = instance.subStore.getData();
66
+ });
67
+ }
68
+ }
69
+
70
+ explore(context, instance) {
71
+ if (!instance.subStore) this.initSubStore(context, instance);
72
+ if (instance.subStore.parentDataCheck()) instance.markShouldUpdate();
73
+ instance.cultureInfo = context.cultureInfo;
74
+ if (instance.cache("cultureInfo", instance.culture)) instance.markShouldUpdate();
75
+ super.explore(context, instance);
76
+ }
77
+
78
+ exploreItems(context, instance, items) {
79
+ if (!this.detached) {
80
+ instance.container = instance.getChild(context, this.container, "container", instance.subStore);
81
+ instance.container.scheduleExploreIfVisible(context);
82
+ instance.children = [instance.container];
83
+ }
84
+ }
85
+
86
+ render(context, instance, key) {
87
+ if (!this.detached) return instance.container.render(context);
88
+
89
+ return (
90
+ <Cx
91
+ key={key}
92
+ widget={this.container}
93
+ parentInstance={instance}
94
+ store={instance.subStore}
95
+ subscribe
96
+ options={this.options}
97
+ onError={this.onError}
98
+ deferredUntilIdle={instance.data.deferredUntilIdle}
99
+ idleTimeout={instance.data.idleTimeout}
100
+ immediate={this.immediate}
101
+ cultureInfo={instance.cultureInfo}
102
+ />
103
+ );
104
+ }
105
+ }
106
+
107
+ Restate.prototype.detached = false;
108
+ Restate.prototype.waitForIdle = false;
109
+ Restate.prototype.immediate = false;
110
+
111
+ export const PrivateStore = Restate;
112
+
113
+ class RestateStore extends Store {
114
+ constructor(config) {
115
+ super(config);
116
+ this.parentDataVersion = -1;
117
+ }
118
+
119
+ getData() {
120
+ this.silently(() => {
121
+ this.parentDataCheck();
122
+ });
123
+ return super.getData();
124
+ }
125
+
126
+ parentDataCheck() {
127
+ if (this.parentDataVersion == this.store.meta.version) return false;
128
+ this.parentDataVersion = this.store.meta.version;
129
+ this.parentData = this.dataSelector(this.store.getData());
130
+ return this.batch(() => {
131
+ for (let key in this.parentData) {
132
+ super.setItem(key, this.parentData[key]);
133
+ }
134
+ });
135
+ }
136
+
137
+ setItem(path, value) {
138
+ let binding = Binding.get(path);
139
+ let bindingRoot = binding.parts[0];
140
+ if (!isObject(this.privateData) || !this.privateData.hasOwnProperty(bindingRoot)) {
141
+ let changed = isUndefined(value) ? super.deleteItem(path) : super.setItem(path, value);
142
+ return changed;
143
+ }
144
+
145
+ let newValue = value;
146
+ if (binding.parts.length > 1) newValue = binding.set(this.getData(), value)[bindingRoot];
147
+ this.onSet(bindingRoot, newValue);
148
+ this.batch(() => {
149
+ super.setItem(bindingRoot, newValue);
150
+ this.parentDataCheck();
151
+ });
152
+ return true;
153
+ }
154
+
155
+ deleteItem(path) {
156
+ return this.setItem(path, undefined);
157
+ }
158
+
159
+ doNotify() {
160
+ if (!this.detached) this.store.notify();
161
+ super.doNotify();
162
+ }
163
+ }