@genesislcap/foundation-store 14.117.0 → 14.118.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (34) hide show
  1. package/README.md +94 -36
  2. package/dist/dts/__test__/elements.d.ts +701 -0
  3. package/dist/dts/__test__/elements.d.ts.map +1 -0
  4. package/dist/dts/__test__/index.d.ts +5 -0
  5. package/dist/dts/__test__/index.d.ts.map +1 -0
  6. package/dist/dts/__test__/services.d.ts +40 -0
  7. package/dist/dts/__test__/services.d.ts.map +1 -0
  8. package/dist/dts/__test__/store.d.ts +145 -0
  9. package/dist/dts/__test__/store.d.ts.map +1 -0
  10. package/dist/dts/__test__/types.d.ts +15 -0
  11. package/dist/dts/__test__/types.d.ts.map +1 -0
  12. package/dist/dts/store/errorMap.d.ts +3 -0
  13. package/dist/dts/store/errorMap.d.ts.map +1 -1
  14. package/dist/dts/store/foundationStore.d.ts +108 -12
  15. package/dist/dts/store/foundationStore.d.ts.map +1 -1
  16. package/dist/esm/__test__/elements.js +118 -0
  17. package/dist/esm/__test__/index.js +4 -0
  18. package/dist/esm/__test__/services.js +37 -0
  19. package/dist/esm/__test__/store.js +154 -0
  20. package/dist/esm/__test__/types.js +1 -0
  21. package/dist/esm/store/foundationStore.js +145 -40
  22. package/dist/foundation-store.api.json +174 -13
  23. package/dist/foundation-store.d.ts +113 -12
  24. package/docs/api/foundation-store.abstractstore.createasynclistener.md +35 -1
  25. package/docs/api/foundation-store.abstractstore.createerrorlistener.md +1 -1
  26. package/docs/api/foundation-store.abstractstore.createlistener.md +1 -1
  27. package/docs/api/foundation-store.abstractstore.invokeasyncapi.md +46 -0
  28. package/docs/api/foundation-store.abstractstore.md +4 -3
  29. package/docs/api/foundation-store.errordetailmap.md +4 -0
  30. package/docs/api/foundation-store.errormap.md +4 -0
  31. package/docs/api/foundation-store.errormaplogger.md +4 -0
  32. package/docs/api/foundation-store.registerstore.md +1 -1
  33. package/docs/api-report.md +27 -6
  34. package/package.json +11 -7
package/README.md CHANGED
@@ -169,14 +169,13 @@ class DefaultStore extends AbstractStoreRoot<Store, StoreEventDetailMap, Interna
169
169
  */
170
170
  onFooEvent = this.createListener('store-foo', detail => {...});
171
171
  onBarEvent = this.createListener<boolean>('store-bar', detail => this.commit.someToggle = detail); // < commit values to the store synchronously
172
- onZooEvent = this.createAsyncListener<StoreZooEventDetail>('store-zoo', async (detail) => {
173
- try {
174
- const response = await this.someAsyncTask(detail); // < likely an injected service
175
- this.emit('store-zoo-success', response); // < you could commit here if you really wanted to...
176
- } catch (e) {
177
- this.emit('store-zoo-error', e);
178
- }
179
- });
172
+ onZooEvent = this.createAsyncListener<StoreZooEventDetail>('store-zoo', async (detail) =>
173
+ this.invokeAsyncAPI(
174
+ async () => this.someAsyncTask(detail), // < likely an injected service,
175
+ 'store-zoo-error',
176
+ 'store-zoo-success'
177
+ )
178
+ );
180
179
 
181
180
  /**
182
181
  * 9: Create getters for common derived data needs, similar to selectors in the Redux sense. These however do not
@@ -197,7 +196,7 @@ class DefaultStore extends AbstractStoreRoot<Store, StoreEventDetailMap, Interna
197
196
  /**
198
197
  * 10: Register the store which defines the DI key using the interface
199
198
  */
200
- export const Store = registerStore(DefaultStore, 'RootStore');
199
+ export const Store = registerStore<Store>(DefaultStore, 'RootStore');
201
200
  ```
202
201
 
203
202
  Your root store is now ready to be injected into your application. Hopefully the above gives you a good idea of general
@@ -225,7 +224,7 @@ class DefaultTradeEntryUI extends AbstractStore<TradeEntryUI, TradeEntryUIEventD
225
224
  onOpen = this.createListener<boolean>('trade-entry-ui-open', detail => this.commit.isOpen = detail);
226
225
  }
227
226
 
228
- export const TradeEntryUI = registerStore(DefaultTradeEntryUI, 'TradeEntryUI');
227
+ export const TradeEntryUI = registerStore<TradeEntryUI>(DefaultTradeEntryUI, 'TradeEntryUI');
229
228
  ```
230
229
 
231
230
  ## Store events
@@ -235,36 +234,81 @@ export const TradeEntryUI = registerStore(DefaultTradeEntryUI, 'TradeEntryUI');
235
234
  In your main application class, likely `./src/main/main.ts`, you need to fire a `'store-connected'` event in-order to
236
235
  fully initialise the store.
237
236
 
238
- For example:
239
-
240
237
  ```typescript
241
238
  // ./main/main.ts
242
239
 
243
240
  type EventMap = StoreEventDetailMap & {...}; // < whatever other events you allow your custom element to emit
244
241
 
245
- @customElement({name, template, styles})
242
+ /**
243
+ * MainApplication
244
+ *
245
+ * @fires store-connected - Fires a custom 'store-connected' event after connection to connect the store.
246
+ * @fires store-ready - Fires a custom 'store-ready' event after connection to ready the store.
247
+ * @fires store-disconnected - Fires a custom 'store-disconnected' event after disconnection to unbind the store.
248
+ *
249
+ * @public
250
+ */
251
+ @customElement({ name, template, styles })
246
252
  export class MainApplication extends EventEmitter<EventMap>(FASTElement) {
247
- @Store store!: Store; // < injected root store
248
- @observable rootElement!: HTMLElement; // < rootElement provided via a 'ref' directive in the tempate
253
+ /**
254
+ * @public
255
+ */
256
+ @Store store: Store; // < injected root store
257
+
258
+ /**
259
+ * @public
260
+ */
249
261
  connectedCallback() {
250
262
  super.connectedCallback();
251
- this.$emit('store-connected', this.rootElement); // < emit the strongly typed event when we're connected to the dom
263
+ this.addEventListeners();
264
+ this.readyStore();
252
265
  }
253
- }
254
- ```
255
266
 
256
- This `'store-connected'` event handler needs to be explicitly bound as an event handler in the main template.
267
+ /**
268
+ * @public
269
+ */
270
+ disconnectedCallback() {
271
+ super.disconnectedCallback();
272
+ this.removeEventListeners();
273
+ this.disconnectStore();
274
+ }
257
275
 
258
- ```typescript jsx
259
- // ./main/main.template.ts
276
+ /**
277
+ * @internal
278
+ */
279
+ protected addEventListeners() {
280
+ this.addEventListener('store-connected', this.store.onConnected);
281
+ }
260
282
 
261
- <template
262
- ${ref('rootElement')}
263
- @store-connected=${(x, c) => x.store.onConnected(customEvent(c))}
264
- >
283
+ /**
284
+ * @internal
285
+ */
286
+ protected removeEventListeners() {
287
+ this.removeEventListener('store-connected', this.store.onConnected);
288
+ }
289
+
290
+ /**
291
+ * @internal
292
+ */
293
+ protected readyStore() {
294
+ this.$emit('store-connected', this);
295
+ /**
296
+ * Do some other work if needed.
297
+ */
298
+ this.$emit('store-ready', true);
299
+ }
300
+
301
+ /**
302
+ * @internal
303
+ */
304
+ protected disconnectStore() {
305
+ this.$emit('store-disconnected');
306
+ }
307
+ }
265
308
  ```
266
309
 
267
- When the root store handles `'store-connected'`, it auto binds all the store event listeners to the rootElement.
310
+ The `'store-connected'` event handler needs to be explicitly bound. When the root store handles `'store-connected'`,
311
+ it auto binds all the store event listeners to the rootElement.
268
312
 
269
313
  At this point you can start emitting strongly typed store events, and they will be handled by their corresponding store.
270
314
  See [EventEmitter](src/.foundation/events/eventEmitter/README.md) for more information.
@@ -278,11 +322,26 @@ do, you can just emit this right after `'store-connected'`.
278
322
  ```typescript
279
323
  // ./main/main.ts
280
324
 
281
- this.$emit('store-connected', this.rootElement);
282
- // possible other work done in-between...
325
+ this.$emit('store-connected', this);
326
+ /**
327
+ * Do some other work if needed.
328
+ */
283
329
  this.$emit('store-ready', true);
284
330
  ```
285
331
 
332
+ ### `'store-disconnected'`
333
+
334
+ Emitting `'store-disconnected'` will remove all the previously bound event listeners.
335
+
336
+ ```typescript
337
+ // ./main/main.ts
338
+
339
+ disconnectedCallback() {
340
+ super.disconnectedCallback();
341
+ this.$emit('store-disconnected');
342
+ }
343
+ ```
344
+
286
345
  ## Using store fragments
287
346
 
288
347
  To use a store fragment in your custom element simply inject it using its `interface` as the DI key.
@@ -425,14 +484,13 @@ constructor(
425
484
  this.createErrorListener('trades-load-error', () => this.commit.trades = undefined);
426
485
  }
427
486
 
428
- onTradesLoad = this.createAsyncListener<string>('trades-load', async (positionId) => {
429
- try {
430
- const trades = await this.service.getTrades(positionId);
431
- this.emit('trades-load-success', trades);
432
- } catch (e) {
433
- this.emit('trades-load-error', e);
434
- }
435
- });
487
+ onTradesLoad = this.createAsyncListener<string>('trades-load', async (positionId) =>
488
+ this.invokeAsyncAPI(
489
+ async () => this.service.getTrades(positionId),
490
+ 'trades-load-error',
491
+ 'trades-load-success'
492
+ )
493
+ );
436
494
  ```
437
495
 
438
496
  ### Errors