piral-core 0.15.0-alpha.4332 → 0.15.0-alpha.4345
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/esm/actions/state.js +6 -7
- package/esm/actions/state.js.map +1 -1
- package/esm/debugger.js +1 -2
- package/esm/debugger.js.map +1 -1
- package/esm/hooks/globalState.js +2 -9
- package/esm/hooks/globalState.js.map +1 -1
- package/esm/state/createActions.d.ts +2 -2
- package/esm/state/createActions.js.map +1 -1
- package/esm/state/createGlobalState.d.ts +1 -2
- package/esm/state/createGlobalState.js +2 -2
- package/esm/state/createGlobalState.js.map +1 -1
- package/esm/types/state.d.ts +2 -3
- package/lib/actions/state.js +6 -7
- package/lib/actions/state.js.map +1 -1
- package/lib/debugger.js +1 -2
- package/lib/debugger.js.map +1 -1
- package/lib/hooks/globalState.js +2 -9
- package/lib/hooks/globalState.js.map +1 -1
- package/lib/state/createActions.d.ts +2 -2
- package/lib/state/createActions.js.map +1 -1
- package/lib/state/createGlobalState.d.ts +1 -2
- package/lib/state/createGlobalState.js +2 -2
- package/lib/state/createGlobalState.js.map +1 -1
- package/lib/types/state.d.ts +2 -3
- package/package.json +7 -13
- package/src/actions/app.test.ts +32 -32
- package/src/actions/components.test.ts +10 -10
- package/src/actions/data.test.ts +49 -49
- package/src/actions/define.test.ts +2 -2
- package/src/actions/portal.test.ts +17 -17
- package/src/actions/state.test.ts +3 -3
- package/src/actions/state.ts +7 -8
- package/src/components/Mediator.test.tsx +11 -10
- package/src/debugger.ts +1 -2
- package/src/helpers.test.tsx +9 -8
- package/src/hooks/globalState.ts +2 -11
- package/src/modules/element-server.test.ts +4 -4
- package/src/modules/element.test.ts +6 -6
- package/src/setters/SetComponent.test.tsx +7 -6
- package/src/setters/SetError.test.tsx +7 -6
- package/src/setters/SetErrors.test.tsx +7 -6
- package/src/setters/SetLayout.test.tsx +7 -6
- package/src/setters/SetProvider.test.tsx +7 -6
- package/src/setters/SetRedirect.test.tsx +7 -6
- package/src/setters/SetRoute.test.tsx +7 -6
- package/src/state/createActions.ts +3 -3
- package/src/state/createGlobalState.test.ts +9 -11
- package/src/state/createGlobalState.ts +2 -2
- package/src/state/withApi.test.tsx +5 -5
- package/src/types/state.ts +2 -3
- package/src/hooks/globalState-server.test.ts +0 -41
- package/src/hooks/globalState.test.ts +0 -47
package/src/actions/data.test.ts
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
|
-
import
|
|
1
|
+
import create from 'zustand';
|
|
2
2
|
import { createListener } from 'piral-base';
|
|
3
3
|
import { readDataItem, readDataValue, resetData, tryWriteDataItem, writeDataItem } from './data';
|
|
4
4
|
|
|
5
5
|
describe('Data Actions Module', () => {
|
|
6
6
|
it('readDataItem reads the current item', () => {
|
|
7
|
-
const state =
|
|
7
|
+
const state = create(() => ({
|
|
8
8
|
foo: 5,
|
|
9
9
|
data: {
|
|
10
10
|
foo: 10,
|
|
11
11
|
},
|
|
12
|
-
});
|
|
12
|
+
}));
|
|
13
13
|
const ctx: any = {
|
|
14
14
|
state,
|
|
15
15
|
dispatch(update) {
|
|
16
|
-
|
|
16
|
+
state.setState(update(state.getState()));
|
|
17
17
|
},
|
|
18
18
|
readState(select) {
|
|
19
|
-
return select(
|
|
19
|
+
return select(state.getState());
|
|
20
20
|
},
|
|
21
21
|
};
|
|
22
22
|
const value = readDataItem(ctx, 'foo');
|
|
@@ -24,21 +24,21 @@ describe('Data Actions Module', () => {
|
|
|
24
24
|
});
|
|
25
25
|
|
|
26
26
|
it('readDataValue reads the current value', () => {
|
|
27
|
-
const state =
|
|
27
|
+
const state = create(() => ({
|
|
28
28
|
foo: 5,
|
|
29
29
|
data: {
|
|
30
30
|
foo: {
|
|
31
31
|
value: 15,
|
|
32
32
|
},
|
|
33
33
|
},
|
|
34
|
-
});
|
|
34
|
+
}));
|
|
35
35
|
const ctx: any = {
|
|
36
36
|
state,
|
|
37
37
|
dispatch(update) {
|
|
38
|
-
|
|
38
|
+
state.setState(update(state.getState()));
|
|
39
39
|
},
|
|
40
40
|
readState(select) {
|
|
41
|
-
return select(
|
|
41
|
+
return select(state.getState());
|
|
42
42
|
},
|
|
43
43
|
};
|
|
44
44
|
const value = readDataValue(ctx, 'foo');
|
|
@@ -46,48 +46,48 @@ describe('Data Actions Module', () => {
|
|
|
46
46
|
});
|
|
47
47
|
|
|
48
48
|
it('resetData clears all items', () => {
|
|
49
|
-
const state =
|
|
49
|
+
const state = create(() => ({
|
|
50
50
|
foo: 5,
|
|
51
51
|
data: {
|
|
52
52
|
foo: 10,
|
|
53
53
|
bar: [5],
|
|
54
54
|
},
|
|
55
|
-
});
|
|
55
|
+
}));
|
|
56
56
|
const ctx: any = {
|
|
57
57
|
state,
|
|
58
58
|
dispatch(update) {
|
|
59
|
-
|
|
59
|
+
state.setState(update(state.getState()));
|
|
60
60
|
},
|
|
61
61
|
readState(select) {
|
|
62
|
-
return select(
|
|
62
|
+
return select(state.getState());
|
|
63
63
|
},
|
|
64
64
|
};
|
|
65
65
|
resetData(ctx);
|
|
66
|
-
expect(
|
|
66
|
+
expect(state.getState()).toEqual({
|
|
67
67
|
foo: 5,
|
|
68
68
|
data: {},
|
|
69
69
|
});
|
|
70
70
|
});
|
|
71
71
|
|
|
72
72
|
it('writeDataItem adds a new data item', () => {
|
|
73
|
-
const state =
|
|
73
|
+
const state = create(() => ({
|
|
74
74
|
foo: 5,
|
|
75
75
|
data: {
|
|
76
76
|
foo: 10,
|
|
77
77
|
bar: [5],
|
|
78
78
|
},
|
|
79
|
-
});
|
|
79
|
+
}));
|
|
80
80
|
const ctx: any = Object.assign(createListener(undefined), {
|
|
81
81
|
state,
|
|
82
82
|
dispatch(update) {
|
|
83
|
-
|
|
83
|
+
state.setState(update(state.getState()));
|
|
84
84
|
},
|
|
85
85
|
readState(select) {
|
|
86
|
-
return select(
|
|
86
|
+
return select(state.getState());
|
|
87
87
|
},
|
|
88
88
|
});
|
|
89
|
-
writeDataItem(ctx, 'fi', 0);
|
|
90
|
-
expect(
|
|
89
|
+
writeDataItem(ctx, 'fi', 0, undefined, undefined, undefined);
|
|
90
|
+
expect(state.getState()).toEqual({
|
|
91
91
|
foo: 5,
|
|
92
92
|
data: {
|
|
93
93
|
foo: 10,
|
|
@@ -103,24 +103,24 @@ describe('Data Actions Module', () => {
|
|
|
103
103
|
});
|
|
104
104
|
|
|
105
105
|
it('writeDataItem overwrites an existing data item', () => {
|
|
106
|
-
const state =
|
|
106
|
+
const state = create(() => ({
|
|
107
107
|
foo: 5,
|
|
108
108
|
data: {
|
|
109
109
|
foo: 10,
|
|
110
110
|
bar: [5],
|
|
111
111
|
},
|
|
112
|
-
});
|
|
112
|
+
}));
|
|
113
113
|
const ctx: any = Object.assign(createListener(undefined), {
|
|
114
114
|
state,
|
|
115
115
|
dispatch(update) {
|
|
116
|
-
|
|
116
|
+
state.setState(update(state.getState()));
|
|
117
117
|
},
|
|
118
118
|
readState(select) {
|
|
119
|
-
return select(
|
|
119
|
+
return select(state.getState());
|
|
120
120
|
},
|
|
121
121
|
});
|
|
122
|
-
writeDataItem(ctx, 'bar', 0);
|
|
123
|
-
expect(
|
|
122
|
+
writeDataItem(ctx, 'bar', 0, undefined, undefined, undefined);
|
|
123
|
+
expect(state.getState()).toEqual({
|
|
124
124
|
foo: 5,
|
|
125
125
|
data: {
|
|
126
126
|
foo: 10,
|
|
@@ -135,24 +135,24 @@ describe('Data Actions Module', () => {
|
|
|
135
135
|
});
|
|
136
136
|
|
|
137
137
|
it('writeDataItem removes an existing data item', () => {
|
|
138
|
-
const state =
|
|
138
|
+
const state = create(() => ({
|
|
139
139
|
foo: 5,
|
|
140
140
|
data: {
|
|
141
141
|
foo: 10,
|
|
142
142
|
bar: [5],
|
|
143
143
|
},
|
|
144
|
-
});
|
|
144
|
+
}));
|
|
145
145
|
const ctx: any = Object.assign(createListener(undefined), {
|
|
146
146
|
state,
|
|
147
147
|
dispatch(update) {
|
|
148
|
-
|
|
148
|
+
state.setState(update(state.getState()));
|
|
149
149
|
},
|
|
150
150
|
readState(select) {
|
|
151
|
-
return select(
|
|
151
|
+
return select(state.getState());
|
|
152
152
|
},
|
|
153
153
|
});
|
|
154
|
-
writeDataItem(ctx, 'bar', null);
|
|
155
|
-
expect(
|
|
154
|
+
writeDataItem(ctx, 'bar', null, undefined, undefined, undefined);
|
|
155
|
+
expect(state.getState()).toEqual({
|
|
156
156
|
foo: 5,
|
|
157
157
|
data: {
|
|
158
158
|
foo: 10,
|
|
@@ -161,27 +161,27 @@ describe('Data Actions Module', () => {
|
|
|
161
161
|
});
|
|
162
162
|
|
|
163
163
|
it('tryWriteDataItem can write new item', () => {
|
|
164
|
-
const state =
|
|
164
|
+
const state = create(() => ({
|
|
165
165
|
foo: 5,
|
|
166
166
|
data: {
|
|
167
167
|
foo: 10,
|
|
168
168
|
},
|
|
169
|
-
});
|
|
169
|
+
}));
|
|
170
170
|
const ctx: any = Object.assign(createListener(undefined), {
|
|
171
171
|
state,
|
|
172
172
|
dispatch(update) {
|
|
173
|
-
|
|
173
|
+
state.setState(update(state.getState()));
|
|
174
174
|
},
|
|
175
175
|
readState(select) {
|
|
176
|
-
return select(
|
|
176
|
+
return select(state.getState());
|
|
177
177
|
},
|
|
178
178
|
});
|
|
179
|
-
const success = tryWriteDataItem(ctx, 'bar', 10, 'me');
|
|
179
|
+
const success = tryWriteDataItem(ctx, 'bar', 10, 'me', undefined, undefined);
|
|
180
180
|
expect(success).toBe(true);
|
|
181
181
|
});
|
|
182
182
|
|
|
183
183
|
it('tryWriteDataItem can overwrite item if owner', () => {
|
|
184
|
-
const state =
|
|
184
|
+
const state = create(() => ({
|
|
185
185
|
foo: 5,
|
|
186
186
|
data: {
|
|
187
187
|
foo: 10,
|
|
@@ -190,23 +190,23 @@ describe('Data Actions Module', () => {
|
|
|
190
190
|
value: 5,
|
|
191
191
|
},
|
|
192
192
|
},
|
|
193
|
-
});
|
|
193
|
+
}));
|
|
194
194
|
const ctx: any = Object.assign(createListener(undefined), {
|
|
195
195
|
state,
|
|
196
196
|
dispatch(update) {
|
|
197
|
-
|
|
197
|
+
state.setState(update(state.getState()));
|
|
198
198
|
},
|
|
199
199
|
readState(select) {
|
|
200
|
-
return select(
|
|
200
|
+
return select(state.getState());
|
|
201
201
|
},
|
|
202
202
|
});
|
|
203
|
-
const success = tryWriteDataItem(ctx, 'bar', 10, 'me');
|
|
203
|
+
const success = tryWriteDataItem(ctx, 'bar', 10, 'me', undefined, undefined);
|
|
204
204
|
expect(success).toBe(true);
|
|
205
|
-
expect(
|
|
205
|
+
expect(state.getState().data.bar.value).toBe(10);
|
|
206
206
|
});
|
|
207
207
|
|
|
208
208
|
it('tryWriteDataItem can not overwrite item if not owner', () => {
|
|
209
|
-
const state =
|
|
209
|
+
const state = create(() => ({
|
|
210
210
|
foo: 5,
|
|
211
211
|
data: {
|
|
212
212
|
foo: 10,
|
|
@@ -215,18 +215,18 @@ describe('Data Actions Module', () => {
|
|
|
215
215
|
value: 5,
|
|
216
216
|
},
|
|
217
217
|
},
|
|
218
|
-
});
|
|
218
|
+
}));
|
|
219
219
|
const ctx: any = Object.assign(createListener(undefined), {
|
|
220
220
|
state,
|
|
221
221
|
dispatch(update) {
|
|
222
|
-
|
|
222
|
+
state.setState(update(state.getState()));
|
|
223
223
|
},
|
|
224
224
|
readState(select) {
|
|
225
|
-
return select(
|
|
225
|
+
return select(state.getState());
|
|
226
226
|
},
|
|
227
227
|
});
|
|
228
|
-
const success = tryWriteDataItem(ctx, 'bar', 10, 'me');
|
|
228
|
+
const success = tryWriteDataItem(ctx, 'bar', 10, 'me', undefined, undefined);
|
|
229
229
|
expect(success).toBe(false);
|
|
230
|
-
expect(
|
|
230
|
+
expect(state.getState().data.bar.value).toBe(5);
|
|
231
231
|
});
|
|
232
232
|
});
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import
|
|
1
|
+
import create from 'zustand';
|
|
2
2
|
import { defineAction, defineActions } from './define';
|
|
3
3
|
|
|
4
4
|
function createMockContainer(initialState = {}) {
|
|
5
|
-
const state =
|
|
5
|
+
const state = create(() => initialState);
|
|
6
6
|
return {
|
|
7
7
|
context: {
|
|
8
8
|
on: jest.fn(),
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
|
-
import
|
|
2
|
+
import create from 'zustand';
|
|
3
3
|
import { showPortal, destroyPortal, hidePortal, updatePortal } from './portal';
|
|
4
4
|
|
|
5
5
|
describe('Piral-Core portal actions', () => {
|
|
@@ -7,19 +7,19 @@ describe('Piral-Core portal actions', () => {
|
|
|
7
7
|
const children = React.createElement('div');
|
|
8
8
|
const portal: React.ReactPortal = { key: 'test', children: { children }, type: 'div', props: null };
|
|
9
9
|
|
|
10
|
-
const state =
|
|
10
|
+
const state = create(() => ({
|
|
11
11
|
portals: { test: { portal } },
|
|
12
|
-
});
|
|
12
|
+
}));
|
|
13
13
|
|
|
14
14
|
const ctx: any = {
|
|
15
15
|
state,
|
|
16
16
|
dispatch(update) {
|
|
17
|
-
|
|
17
|
+
state.setState(update(state.getState()));
|
|
18
18
|
},
|
|
19
19
|
};
|
|
20
20
|
|
|
21
21
|
destroyPortal(ctx, 'test');
|
|
22
|
-
const { portals } =
|
|
22
|
+
const { portals } = ctx.state.getState();
|
|
23
23
|
expect(portals).not.toBeNull();
|
|
24
24
|
expect(portals.test).toBeUndefined();
|
|
25
25
|
});
|
|
@@ -29,19 +29,19 @@ describe('Piral-Core portal actions', () => {
|
|
|
29
29
|
const newPortal: React.ReactPortal = { key: 'test', children: { children }, type: 'div', props: null };
|
|
30
30
|
const portal: React.ReactPortal = { key: 'toast', children: { children }, type: 'div', props: null };
|
|
31
31
|
|
|
32
|
-
const state =
|
|
32
|
+
const state = create(() => ({
|
|
33
33
|
portals: { p1: { portal } },
|
|
34
|
-
});
|
|
34
|
+
}));
|
|
35
35
|
|
|
36
36
|
const ctx: any = {
|
|
37
37
|
state,
|
|
38
38
|
dispatch(update) {
|
|
39
|
-
|
|
39
|
+
state.setState(update(state.getState()));
|
|
40
40
|
},
|
|
41
41
|
};
|
|
42
42
|
|
|
43
43
|
hidePortal(ctx, 'test', newPortal);
|
|
44
|
-
const { portals } =
|
|
44
|
+
const { portals } = ctx.state.getState();
|
|
45
45
|
expect(portals).not.toBeNull();
|
|
46
46
|
expect(portals.test).not.toBeNull();
|
|
47
47
|
});
|
|
@@ -52,19 +52,19 @@ describe('Piral-Core portal actions', () => {
|
|
|
52
52
|
const next: React.ReactPortal = { key: 'next', children: { children }, type: 'div', props: null };
|
|
53
53
|
const portal: React.ReactPortal = { key: 'toast', children: { children }, type: 'div', props: null };
|
|
54
54
|
|
|
55
|
-
const state =
|
|
55
|
+
const state = create(() => ({
|
|
56
56
|
portals: { p1: { portal } },
|
|
57
|
-
});
|
|
57
|
+
}));
|
|
58
58
|
|
|
59
59
|
const ctx: any = {
|
|
60
60
|
state,
|
|
61
61
|
dispatch(update) {
|
|
62
|
-
|
|
62
|
+
state.setState(update(state.getState()));
|
|
63
63
|
},
|
|
64
64
|
};
|
|
65
65
|
|
|
66
66
|
updatePortal(ctx, 'test', current, next);
|
|
67
|
-
const { portals } =
|
|
67
|
+
const { portals } = ctx.state.getState();
|
|
68
68
|
expect(portals).not.toBeNull();
|
|
69
69
|
expect(portals.test).not.toBeNull();
|
|
70
70
|
});
|
|
@@ -74,19 +74,19 @@ describe('Piral-Core portal actions', () => {
|
|
|
74
74
|
const newPortal: React.ReactPortal = { key: 'test', children: { children }, type: 'div', props: null };
|
|
75
75
|
const portal: React.ReactPortal = { key: 'toast', children: { children }, type: 'div', props: null };
|
|
76
76
|
|
|
77
|
-
const state =
|
|
77
|
+
const state = create(() => ({
|
|
78
78
|
portals: { p1: { portal } },
|
|
79
|
-
});
|
|
79
|
+
}));
|
|
80
80
|
|
|
81
81
|
const ctx: any = {
|
|
82
82
|
state,
|
|
83
83
|
dispatch(update) {
|
|
84
|
-
|
|
84
|
+
state.setState(update(state.getState()));
|
|
85
85
|
},
|
|
86
86
|
};
|
|
87
87
|
|
|
88
88
|
showPortal(ctx, 'test', newPortal);
|
|
89
|
-
const { portals } =
|
|
89
|
+
const { portals } = ctx.state.getState();
|
|
90
90
|
expect(portals).not.toBeNull();
|
|
91
91
|
expect(portals.test).not.toBeNull();
|
|
92
92
|
});
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import
|
|
1
|
+
import create from 'zustand';
|
|
2
2
|
import { readState, dispatch } from './state';
|
|
3
3
|
|
|
4
4
|
function createMockContainer(initialState = {}) {
|
|
5
|
-
const state =
|
|
5
|
+
const state = create(() => initialState);
|
|
6
6
|
return {
|
|
7
7
|
context: {
|
|
8
8
|
on: jest.fn(),
|
|
@@ -11,7 +11,7 @@ function createMockContainer(initialState = {}) {
|
|
|
11
11
|
defineActions() {},
|
|
12
12
|
state,
|
|
13
13
|
read() {
|
|
14
|
-
return
|
|
14
|
+
return state.getState();
|
|
15
15
|
},
|
|
16
16
|
} as any,
|
|
17
17
|
};
|
package/src/actions/state.ts
CHANGED
|
@@ -1,16 +1,15 @@
|
|
|
1
|
-
import { swap, deref } from '@dbeining/react-atom';
|
|
2
1
|
import { isSame } from '../utils';
|
|
3
2
|
import { GlobalState, GlobalStateContext } from '../types';
|
|
4
3
|
|
|
5
|
-
function onlyChangedState(oldState: GlobalState, newState: GlobalState) {
|
|
6
|
-
return isSame(oldState, newState) ? oldState : newState;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
4
|
export function dispatch(ctx: GlobalStateContext, update: (state: GlobalState) => GlobalState) {
|
|
10
|
-
|
|
5
|
+
const oldState = ctx.state.getState();
|
|
6
|
+
const newState = update(oldState);
|
|
7
|
+
|
|
8
|
+
if (!isSame(oldState, newState)) {
|
|
9
|
+
ctx.state.setState(newState);
|
|
10
|
+
}
|
|
11
11
|
}
|
|
12
12
|
|
|
13
13
|
export function readState<S>(ctx: GlobalStateContext, read: (state: GlobalState) => S) {
|
|
14
|
-
|
|
15
|
-
return read(state);
|
|
14
|
+
return read(ctx.state.getState());
|
|
16
15
|
}
|
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
|
+
import create from 'zustand';
|
|
2
3
|
import { Mediator } from './Mediator';
|
|
3
|
-
import { Atom, swap, deref } from '@dbeining/react-atom';
|
|
4
4
|
import { mount } from 'enzyme';
|
|
5
5
|
import { StateContext } from '../state';
|
|
6
6
|
import { PiletMetadata } from '../types';
|
|
7
7
|
|
|
8
8
|
function createMockContainer() {
|
|
9
|
-
const state =
|
|
9
|
+
const state = create(() => ({
|
|
10
10
|
app: {
|
|
11
11
|
layout: 'tablet',
|
|
12
12
|
loading: false,
|
|
13
13
|
error: undefined,
|
|
14
14
|
},
|
|
15
|
-
});
|
|
15
|
+
}));
|
|
16
16
|
return {
|
|
17
17
|
context: {
|
|
18
18
|
on: jest.fn(),
|
|
@@ -20,21 +20,22 @@ function createMockContainer() {
|
|
|
20
20
|
emit: jest.fn(),
|
|
21
21
|
state,
|
|
22
22
|
dispatch(update) {
|
|
23
|
-
|
|
23
|
+
state.setState(update(state.getState()));
|
|
24
24
|
},
|
|
25
25
|
readState(select) {
|
|
26
|
-
return select(
|
|
26
|
+
return select(state.getState());
|
|
27
27
|
},
|
|
28
28
|
initialize(loading, error, modules) {
|
|
29
|
-
|
|
30
|
-
|
|
29
|
+
const s = state.getState();
|
|
30
|
+
state.setState({
|
|
31
|
+
...s,
|
|
31
32
|
app: {
|
|
32
|
-
...
|
|
33
|
+
...s.app,
|
|
33
34
|
error,
|
|
34
35
|
loading,
|
|
35
36
|
},
|
|
36
37
|
modules,
|
|
37
|
-
})
|
|
38
|
+
} as any);
|
|
38
39
|
},
|
|
39
40
|
} as any,
|
|
40
41
|
};
|
|
@@ -54,6 +55,6 @@ describe('Component Mediator', () => {
|
|
|
54
55
|
<Mediator options={options} />
|
|
55
56
|
</StateContext.Provider>,
|
|
56
57
|
);
|
|
57
|
-
expect(
|
|
58
|
+
expect(context.state.getState().app.layout).toEqual('tablet');
|
|
58
59
|
});
|
|
59
60
|
});
|
package/src/debugger.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { addChangeHandler } from '@dbeining/react-atom';
|
|
2
1
|
import { LoadPiletsOptions } from 'piral-base';
|
|
3
2
|
import { installPiralDebug, DebuggerExtensionOptions } from 'piral-debug-utils';
|
|
4
3
|
import { GlobalStateContext } from './types';
|
|
@@ -68,7 +67,7 @@ export function integrateDebugger(
|
|
|
68
67
|
},
|
|
69
68
|
}));
|
|
70
69
|
|
|
71
|
-
|
|
70
|
+
context.state.subscribe((current, previous) => {
|
|
72
71
|
const pilets = current.modules !== previous.modules;
|
|
73
72
|
const pages = current.registry.pages !== previous.registry.pages || current.routes !== previous.routes;
|
|
74
73
|
const extensions = current.registry.extensions !== previous.registry.extensions;
|
package/src/helpers.test.tsx
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
+
import create from 'zustand';
|
|
1
2
|
import { createPiletOptions, PiletOptionsConfig } from './helpers';
|
|
2
3
|
import { globalDependencies } from './modules';
|
|
3
4
|
import { PiletMetadata } from 'piral-base';
|
|
4
|
-
import { Atom, swap, deref } from '@dbeining/react-atom';
|
|
5
5
|
|
|
6
6
|
function createMockApi(meta: PiletMetadata) {
|
|
7
7
|
return {
|
|
@@ -13,7 +13,7 @@ function createMockApi(meta: PiletMetadata) {
|
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
function createMockContainer() {
|
|
16
|
-
const state =
|
|
16
|
+
const state = create(() => ({
|
|
17
17
|
registry: {
|
|
18
18
|
pages: {},
|
|
19
19
|
extensions: {},
|
|
@@ -22,7 +22,7 @@ function createMockContainer() {
|
|
|
22
22
|
routes: {},
|
|
23
23
|
components: {},
|
|
24
24
|
modules: [],
|
|
25
|
-
});
|
|
25
|
+
}));
|
|
26
26
|
return {
|
|
27
27
|
context: {
|
|
28
28
|
on: jest.fn(),
|
|
@@ -30,19 +30,20 @@ function createMockContainer() {
|
|
|
30
30
|
emit: jest.fn(),
|
|
31
31
|
state,
|
|
32
32
|
dispatch(cb) {
|
|
33
|
-
|
|
33
|
+
state.setState(cb(state.getState()));
|
|
34
34
|
},
|
|
35
35
|
readState(cb) {
|
|
36
|
-
return cb(
|
|
36
|
+
return cb(state.getState());
|
|
37
37
|
},
|
|
38
38
|
setComponent(name, comp) {
|
|
39
|
-
|
|
39
|
+
const s = state.getState();
|
|
40
|
+
state.setState({
|
|
40
41
|
...s,
|
|
41
42
|
components: {
|
|
42
43
|
...s.components,
|
|
43
44
|
[name]: comp,
|
|
44
45
|
},
|
|
45
|
-
})
|
|
46
|
+
});
|
|
46
47
|
},
|
|
47
48
|
} as any,
|
|
48
49
|
};
|
|
@@ -85,7 +86,7 @@ describe('Piral-Core helpers module', () => {
|
|
|
85
86
|
const options = createPiletOptions(optionsConfig);
|
|
86
87
|
|
|
87
88
|
// Assert
|
|
88
|
-
expect(options.pilets
|
|
89
|
+
expect(options.pilets?.length).toEqual(providedPilets.length);
|
|
89
90
|
|
|
90
91
|
if (wasUndefined) {
|
|
91
92
|
process.env.DEBUG_PIRAL = undefined;
|
package/src/hooks/globalState.ts
CHANGED
|
@@ -1,16 +1,7 @@
|
|
|
1
1
|
import { useContext } from 'react';
|
|
2
|
-
import { useAtom, deref } from '@dbeining/react-atom';
|
|
3
2
|
import { StateContext } from '../state/stateContext';
|
|
4
3
|
import { GlobalState } from '../types';
|
|
5
4
|
|
|
6
|
-
const useGlobalAtom = typeof window !== 'undefined' ? useAtom : useDirectAtom;
|
|
7
|
-
|
|
8
|
-
function useDirectAtom(atom: any, opts: any) {
|
|
9
|
-
const state = deref(atom);
|
|
10
|
-
const select = opts && opts.select;
|
|
11
|
-
return typeof select === 'function' ? select(state) : state;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
5
|
/**
|
|
15
6
|
* Hook to obtain the global state context, which gives you directly
|
|
16
7
|
* all actions, state, and more of the Piral instance.
|
|
@@ -35,6 +26,6 @@ export function useGlobalState(): GlobalState;
|
|
|
35
26
|
export function useGlobalState<R>(select: (state: GlobalState) => R): R;
|
|
36
27
|
|
|
37
28
|
export function useGlobalState<R>(select?: (state: GlobalState) => R) {
|
|
38
|
-
const { state } = useGlobalStateContext();
|
|
39
|
-
return
|
|
29
|
+
const { state: useState } = useGlobalStateContext();
|
|
30
|
+
return useState(select);
|
|
40
31
|
}
|
|
@@ -2,17 +2,17 @@
|
|
|
2
2
|
* @jest-environment node
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
|
-
import
|
|
5
|
+
import create from 'zustand';
|
|
6
6
|
import { createListener } from 'piral-base';
|
|
7
7
|
import { createActions } from '../state';
|
|
8
8
|
import { renderElement } from './element';
|
|
9
9
|
|
|
10
10
|
function createMockContext(): [any, any] {
|
|
11
|
-
const state =
|
|
11
|
+
const state: any = create(() => ({
|
|
12
12
|
portals: {
|
|
13
13
|
root: [],
|
|
14
14
|
},
|
|
15
|
-
});
|
|
15
|
+
}));
|
|
16
16
|
const context = createActions(state, createListener({}));
|
|
17
17
|
return [context, state];
|
|
18
18
|
}
|
|
@@ -24,6 +24,6 @@ describe('Elements Module from SSR', () => {
|
|
|
24
24
|
addEventListener() {},
|
|
25
25
|
};
|
|
26
26
|
renderElement(context, element, {});
|
|
27
|
-
expect(
|
|
27
|
+
expect(state.getState().portals.root.length).toBe(0);
|
|
28
28
|
});
|
|
29
29
|
});
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import create from 'zustand';
|
|
2
2
|
import { createListener } from 'piral-base';
|
|
3
3
|
import { createActions } from '../state';
|
|
4
4
|
import { renderElement } from './element';
|
|
@@ -14,9 +14,9 @@ declare global {
|
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
function createMockContext(): [any, any] {
|
|
17
|
-
const state =
|
|
17
|
+
const state: any = create(() => ({
|
|
18
18
|
portals: {},
|
|
19
|
-
});
|
|
19
|
+
}));
|
|
20
20
|
const context = createActions(state, createListener({}));
|
|
21
21
|
return [context, state];
|
|
22
22
|
}
|
|
@@ -31,7 +31,7 @@ describe('Elements Module', () => {
|
|
|
31
31
|
element.dispatchEvent(event);
|
|
32
32
|
document.body.removeChild(element);
|
|
33
33
|
(element as any).connectedCallback();
|
|
34
|
-
expect(
|
|
34
|
+
expect(state.getState().portals.root.length).toBe(1);
|
|
35
35
|
});
|
|
36
36
|
|
|
37
37
|
it('disposing piral-extension web component', () => {
|
|
@@ -39,9 +39,9 @@ describe('Elements Module', () => {
|
|
|
39
39
|
const element = document.createElement('piral-extension');
|
|
40
40
|
document.body.appendChild(element);
|
|
41
41
|
const [dispose] = renderElement(context, element, {});
|
|
42
|
-
expect(
|
|
42
|
+
expect(state.getState().portals.root.length).toBe(1);
|
|
43
43
|
dispose();
|
|
44
|
-
expect(
|
|
44
|
+
expect(state.getState().portals.root.length).toBe(0);
|
|
45
45
|
});
|
|
46
46
|
|
|
47
47
|
it('testing setters and getters in piral-extension web component', () => {
|