cx 24.3.9 → 24.3.11

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.
@@ -1,18 +1,21 @@
1
- import { Instance } from "./Instance";
2
- import * as Cx from "../core";
3
-
4
- interface RestateProps extends Cx.PureContainerProps {
5
- data?: Cx.StructuredProp;
6
- detached?: boolean;
7
- deferredUntilIdle?: Cx.BooleanProp;
8
- idleTimeout?: Cx.NumberProp;
9
- cacheKey?: Cx.StringProp;
10
-
11
- /* Set to true to disable batching of updates. */
12
- immediate?: boolean;
13
-
14
- onError?: (err: Error, instance: Instance) => void;
15
- }
16
-
17
- export class Restate extends Cx.Widget<RestateProps> {}
18
- export class PrivateStore extends Cx.Widget<RestateProps> {}
1
+ import { Instance } from "./Instance";
2
+ import * as Cx from "../core";
3
+ import { CultureInfo } from "./Culture";
4
+
5
+ interface RestateProps extends Cx.PureContainerProps {
6
+ data?: Cx.StructuredProp;
7
+ detached?: boolean;
8
+ deferredUntilIdle?: Cx.BooleanProp;
9
+ idleTimeout?: Cx.NumberProp;
10
+ cacheKey?: Cx.StringProp;
11
+
12
+ /* Set to true to disable batching of updates. */
13
+ immediate?: boolean;
14
+
15
+ onError?: (err: Error, instance: Instance) => void;
16
+
17
+ culture?: CultureInfo;
18
+ }
19
+
20
+ export class Restate extends Cx.Widget<RestateProps> {}
21
+ export class PrivateStore extends Cx.Widget<RestateProps> {}
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 { VDOM } from "./VDOM";
5
+ import { isObject } from "../util/isObject";
6
+ import { isUndefined } from "../util/isUndefined";
7
+ import { Binding } from "../data/Binding";
8
+ import { StructuredSelector } from "../data/StructuredSelector";
9
+ import { getCurrentCulture } from "./Culture";
10
+
11
+ let persistenceCache = {};
12
+
13
+ export class Restate extends PureContainer {
14
+ declareData() {
15
+ return super.declareData(...arguments, {
16
+ deferredUntilIdle: undefined,
17
+ idleTimeout: undefined,
18
+ cacheKey: undefined,
19
+ });
20
+ }
21
+
22
+ init() {
23
+ this.container = PureContainer.create({
24
+ type: PureContainer,
25
+ items: this.children || this.items,
26
+ layout: this.layout,
27
+ controller: this.controller,
28
+ outerLayout: this.outerLayout,
29
+ useParentLayout: !this.detached,
30
+ ws: this.ws,
31
+ });
32
+ this.privateDataSelector = new StructuredSelector({
33
+ props: this.data || {},
34
+ values: this.data,
35
+ });
36
+ delete this.items;
37
+ delete this.children;
38
+ delete this.controller;
39
+ delete this.outerLayout;
40
+ delete this.layout;
41
+ if (this.useParentLayout == null) this.useParentLayout = !this.detached;
42
+ super.init();
43
+ }
44
+
45
+ initSubStore(context, instance) {
46
+ let { cacheKey } = instance.data;
47
+ this.privateDataSelector.init(instance.store);
48
+ instance.subStore = new RestateStore({
49
+ store: instance.store,
50
+ detached: this.detached,
51
+ privateData: this.data || {},
52
+ data: cacheKey ? persistenceCache[cacheKey] || {} : {},
53
+ dataSelector: this.privateDataSelector.create(),
54
+ onSet: (path, value) => instance.nestedDataSet(path, value, this.data),
55
+ });
56
+
57
+ instance.setStore = (store) => {
58
+ instance.store = store;
59
+ instance.subStore.setStore(store);
60
+ };
61
+
62
+ if (cacheKey) {
63
+ instance.subscribeOnDestroy(() => {
64
+ persistenceCache[cacheKey] = instance.subStore.getData();
65
+ });
66
+ }
67
+ }
68
+
69
+ explore(context, instance) {
70
+ if (!instance.subStore) this.initSubStore(context, instance);
71
+ if (instance.subStore.parentDataCheck()) instance.markShouldUpdate();
72
+ instance.cultureInfo = this.culture ?? getCurrentCulture();
73
+ if (instance.cache("cultureInfo", instance.culture)) instance.markShouldUpdate();
74
+ super.explore(context, instance);
75
+ }
76
+
77
+ exploreItems(context, instance, items) {
78
+ if (!this.detached) {
79
+ instance.container = instance.getChild(context, this.container, "container", instance.subStore);
80
+ instance.container.scheduleExploreIfVisible(context);
81
+ instance.children = [instance.container];
82
+ }
83
+ }
84
+
85
+ render(context, instance, key) {
86
+ if (!this.detached) return instance.container.render(context);
87
+
88
+ return (
89
+ <Cx
90
+ key={key}
91
+ widget={this.container}
92
+ parentInstance={instance}
93
+ store={instance.subStore}
94
+ subscribe
95
+ options={this.options}
96
+ onError={this.onError}
97
+ deferredUntilIdle={instance.data.deferredUntilIdle}
98
+ idleTimeout={instance.data.idleTimeout}
99
+ immediate={this.immediate}
100
+ cultureInfo={instance.cultureInfo}
101
+ />
102
+ );
103
+ }
104
+ }
105
+
106
+ Restate.prototype.detached = false;
107
+ Restate.prototype.waitForIdle = false;
108
+ Restate.prototype.immediate = false;
109
+ Restate.prototype.culture = null;
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
+ }