@cascateer/core 2.4.16 → 2.4.18

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cascateer/core",
3
- "version": "2.4.16",
3
+ "version": "2.4.18",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/cascateer/core.git"
@@ -31,7 +31,7 @@ interface MulticastActions<Data> {
31
31
  };
32
32
  }
33
33
 
34
- type MulticastBaseActionMessage<
34
+ export type MulticastBaseActionMessage<
35
35
  Data,
36
36
  Type extends keyof MulticastActions<Data>,
37
37
  > = MulticastBaseMessage<Type, MulticastActions<Data>[Type]["data"]>;
package/src/store.ts CHANGED
@@ -1,8 +1,7 @@
1
1
  import { EndoFunction, ExtendableDictionary } from "@cascateer/lib";
2
2
  import { chain, flatMap } from "@cascateer/lib/observables";
3
- import { constant, Dictionary, mapValues, tap, thru } from "lodash";
3
+ import { constant, Dictionary, mapValues, tap } from "lodash";
4
4
  import {
5
- identity,
6
5
  merge,
7
6
  mergeMap,
8
7
  Observable,
@@ -12,8 +11,8 @@ import {
12
11
  } from "rxjs";
13
12
  import { MulticastAction, MulticastSubject } from "./operators";
14
13
  import {
14
+ MulticastBaseActionMessage,
15
15
  MulticastClientMessage,
16
- MulticastHostMessage,
17
16
  MulticastMessageConstructor,
18
17
  } from "./operators/multicast";
19
18
  import { Serializable } from "./serializable";
@@ -59,23 +58,17 @@ export class ExtendableStoreAdapter<
59
58
  }
60
59
 
61
60
  constructor(
62
- public context: {
63
- transform: UnaryFunction<
64
- Promise<string>,
65
- {
66
- share: UnaryFunction<
67
- MulticastMessageConstructor<MulticastClientMessage>,
68
- void
69
- >;
70
- register: UnaryFunction<
71
- UnaryFunction<
72
- MulticastHostMessage,
73
- Promise<MulticastAction<any, "transformAction"> | undefined>
74
- >,
75
- void
76
- >;
77
- }
61
+ public transform: {
62
+ share: UnaryFunction<
63
+ MulticastMessageConstructor<MulticastClientMessage>,
64
+ void
78
65
  >;
66
+ parse: (
67
+ key: Promise<string>,
68
+ handler: (
69
+ action: MulticastBaseActionMessage<any, "transformAction">,
70
+ ) => Promise<MulticastAction<any, "transformAction"> | undefined>,
71
+ ) => void;
79
72
  },
80
73
  private extendableSignals: ExtendableDictionary<
81
74
  ComputedSignal<any>,
@@ -95,7 +88,7 @@ export class ExtendableStoreAdapter<
95
88
  >,
96
89
  ) {
97
90
  return new ExtendableStoreAdapter(
98
- this.context,
91
+ this.transform,
99
92
  this.extendableSignals.extend(
100
93
  (currentSignals) => () =>
101
94
  signals({
@@ -131,7 +124,7 @@ export class ExtendableStoreAdapter<
131
124
  >,
132
125
  ) {
133
126
  return new ExtendableStoreAdapter(
134
- this.context,
127
+ this.transform,
135
128
  this.extendableSignals,
136
129
  this.extendableActions.extend(
137
130
  () =>
@@ -143,50 +136,40 @@ export class ExtendableStoreAdapter<
143
136
  mapValues(
144
137
  this.extendableSignals.currentValue,
145
138
  (signal) => ({
146
- update: (predicate, config = {}) =>
147
- thru(this.context.transform(key), (transform) => {
148
- const callbacks = new Map<
149
- string,
150
- UnaryFunction<unknown, void>
151
- >();
152
-
153
- transform.register(async (event) => {
154
- if (
155
- event.type === "transformAction" &&
156
- event.data.key === (await key)
157
- ) {
158
- return {
159
- ...event,
160
- predicate: signal.pull(
161
- predicate(
162
- Serializable.parse(
163
- event.data.args ?? null,
164
- ),
165
- ),
166
- ),
167
- callback: callbacks.get(event.id),
168
- };
169
- }
170
- });
171
-
172
- return (args) =>
173
- new Promise<unknown>((callback) =>
174
- transform.share(
175
- async ({ id }) => (
176
- callbacks.set(id, callback),
177
- {
178
- id,
179
- type: "transformAction",
180
- data: {
181
- key: await key,
182
- args: JSON.stringify(args),
183
- },
184
- sameOrigin: config.sameOrigin,
185
- }
186
- ),
139
+ update: (predicate, config = {}) => {
140
+ const callbacks = new Map<
141
+ string,
142
+ UnaryFunction<unknown, void>
143
+ >();
144
+
145
+ this.transform.parse(key, async (event) => ({
146
+ ...event,
147
+ predicate: signal.pull(
148
+ predicate(
149
+ Serializable.parse(event.data.args ?? null),
150
+ ),
151
+ ),
152
+ callback: callbacks.get(event.id),
153
+ }));
154
+
155
+ return (args) =>
156
+ new Promise<unknown>((callback) =>
157
+ this.transform.share(
158
+ async ({ id }) => (
159
+ callbacks.set(id, callback),
160
+ {
161
+ id,
162
+ type: "transformAction",
163
+ data: {
164
+ key: await key,
165
+ args: JSON.stringify(args),
166
+ },
167
+ sameOrigin: config.sameOrigin,
168
+ }
187
169
  ),
188
- );
189
- }),
170
+ ),
171
+ );
172
+ },
190
173
  }),
191
174
  ),
192
175
  ),
@@ -217,22 +200,23 @@ export class StoreProvider<Data> extends ExtendableStoreAdapter<
217
200
  ),
218
201
  );
219
202
 
220
- const callbacks = new Map<string, UnaryFunction<unknown, void>>();
221
-
222
203
  super(
223
204
  {
224
- transform: (key) => ({
225
- share: (messageConstructor) => actions.next(messageConstructor),
226
- register: (project) =>
227
- actions
228
- .pipe(
229
- mergeMap((event) =>
230
- project(event).then((action) => action ?? []),
231
- ),
232
- flatMap(identity),
233
- )
234
- .subscribe(transformActions),
235
- }),
205
+ share: (action) => actions.next(action),
206
+ parse: (key, handler) =>
207
+ actions
208
+ .pipe(
209
+ mergeMap(async (event) => {
210
+ if (
211
+ event.type === "transformAction" &&
212
+ event.data.key === (await key)
213
+ ) {
214
+ return handler(event);
215
+ }
216
+ }),
217
+ flatMap((action) => action ?? []),
218
+ )
219
+ .subscribe(transformActions),
236
220
  },
237
221
  new ExtendableDictionary({
238
222
  data: new ComputedSignal({