@streamlayer/sdk-web-interfaces 0.20.6 → 0.21.0

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.
@@ -0,0 +1,61 @@
1
+ import { InvitationFrom, QuestionType } from '@streamlayer/sdk-web-types';
2
+ declare const EventBusActions: {
3
+ notification: {
4
+ received: string;
5
+ opened: string;
6
+ rendered: string;
7
+ closed: string;
8
+ };
9
+ invitation: {
10
+ accepted: string;
11
+ sent: string;
12
+ };
13
+ interactions: {
14
+ tap: string;
15
+ scroll: string;
16
+ };
17
+ poll: {
18
+ voted: string;
19
+ navigated: string;
20
+ opened: string;
21
+ received: string;
22
+ onboardingPassed: string;
23
+ };
24
+ };
25
+ export type EventBusActionPayload = {
26
+ notification: {
27
+ eventId?: string;
28
+ questionId?: string;
29
+ questionType?: QuestionType;
30
+ };
31
+ invitation: {
32
+ from?: InvitationFrom;
33
+ };
34
+ interactions: unknown;
35
+ poll: {
36
+ questionId?: string;
37
+ questionType?: QuestionType;
38
+ questionOpenedFrom?: 'list' | 'notification';
39
+ };
40
+ };
41
+ type EventBusActionsKeys = keyof typeof EventBusActions;
42
+ type EventBusActions<T> = T extends EventBusActionsKeys ? keyof (typeof EventBusActions)[T] : never;
43
+ export type EventBusData<T extends EventBusActionsKeys = EventBusActionsKeys> = {
44
+ type: T;
45
+ action: EventBusActions<T>;
46
+ payload: EventBusActionPayload[T];
47
+ };
48
+ export declare class EventBusEvent<T extends EventBusActionsKeys = EventBusActionsKeys> extends Event {
49
+ slEventBus?: EventBusData<T>;
50
+ constructor(name: string, payload?: EventBusData<T>);
51
+ }
52
+ export declare class EventBus {
53
+ private name;
54
+ private listeners;
55
+ constructor(name?: string);
56
+ emit: <K extends "notification" | "invitation" | "interactions" | "poll" = "notification" | "invitation" | "interactions" | "poll">(type: K, payload: Omit<EventBusData<K>, "type">) => void;
57
+ listen<T extends EventBusActionsKeys>(listener: (event: EventBusEvent<T>) => void): () => void;
58
+ off<T extends EventBusActionsKeys>(listener: (event: EventBusEvent<T>) => void): void;
59
+ destroy: () => void;
60
+ }
61
+ export {};
@@ -0,0 +1,63 @@
1
+ import { createLogger } from '@streamlayer/sdk-web-logger';
2
+ const EventBusActions = {
3
+ notification: {
4
+ received: 'received',
5
+ opened: 'opened',
6
+ rendered: 'rendered',
7
+ closed: 'closed',
8
+ },
9
+ invitation: {
10
+ accepted: 'accepted',
11
+ sent: 'sent',
12
+ },
13
+ interactions: {
14
+ tap: 'tap',
15
+ scroll: 'scroll',
16
+ },
17
+ poll: {
18
+ voted: 'voted',
19
+ navigated: 'navigated',
20
+ opened: 'opened',
21
+ received: 'received',
22
+ onboardingPassed: 'onboardingPassed',
23
+ },
24
+ };
25
+ export class EventBusEvent extends Event {
26
+ slEventBus;
27
+ constructor(name, payload) {
28
+ super(name);
29
+ this.slEventBus = payload;
30
+ }
31
+ }
32
+ const logger = createLogger('event-bus');
33
+ export class EventBus {
34
+ name;
35
+ listeners;
36
+ constructor(name = 'sl-event-bus') {
37
+ this.name = name;
38
+ this.listeners = new Set();
39
+ }
40
+ emit = (type, payload) => {
41
+ const event = new EventBusEvent(this.name, { ...payload, type });
42
+ logger.debug(event, 'Emitting event');
43
+ window.dispatchEvent(event);
44
+ };
45
+ listen(listener) {
46
+ window.addEventListener(this.name, listener);
47
+ this.listeners.add(listener);
48
+ return () => {
49
+ this.off(listener);
50
+ };
51
+ }
52
+ off(listener) {
53
+ window.removeEventListener(this.name, listener);
54
+ this.listeners.delete(listener);
55
+ }
56
+ destroy = () => {
57
+ this.listeners.forEach((listener) => {
58
+ window.removeEventListener(this.name, listener);
59
+ this.listeners.delete(listener);
60
+ });
61
+ this.listeners.clear();
62
+ };
63
+ }
package/lib/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { FeatureType } from '@streamlayer/sdk-web-types';
2
+ import { EventBus, EventBusEvent, EventBusData, EventBusActionPayload } from './event-bus';
2
3
  export { AbstractAuthenticationProvider } from './auth';
3
4
  export { AbstractFeature, FeatureSource, type FeatureProps, FeatureStatus } from './feature';
4
5
  export { MapStore, createMapStore } from './store/map';
@@ -6,7 +7,10 @@ export type { MapStoreListeners } from './store/map';
6
7
  export { SingleStore, createSingleStore, createComputedStore } from './store/single';
7
8
  export { AbstractStore, mergeStores } from './store/abstract';
8
9
  export { ApiStore } from './store/api';
10
+ export type CancelMountCb = () => void;
11
+ export type OnMountCb = () => CancelMountCb;
9
12
  export interface StreamLayerSDK {
13
+ onMount: (cb: OnMountCb) => void;
10
14
  closeFeature: (destroy?: boolean) => void;
11
15
  openFeature: (featureType: FeatureType) => void;
12
16
  }
@@ -15,3 +19,5 @@ export interface StreamLayerContext {
15
19
  }
16
20
  type DoneFn = Function;
17
21
  export type StreamLayerPlugin = (instance: StreamLayerContext, opts: unknown, done: DoneFn) => void;
22
+ export declare const eventBus: EventBus;
23
+ export type { EventBusEvent, EventBusData, EventBusActionPayload };
package/lib/index.js CHANGED
@@ -1,6 +1,8 @@
1
+ import { EventBus } from './event-bus';
1
2
  export { AbstractAuthenticationProvider } from './auth';
2
3
  export { AbstractFeature, FeatureSource, FeatureStatus } from './feature';
3
4
  export { MapStore, createMapStore } from './store/map';
4
5
  export { SingleStore, createSingleStore, createComputedStore } from './store/single';
5
6
  export { AbstractStore, mergeStores } from './store/abstract';
6
7
  export { ApiStore } from './store/api';
8
+ export const eventBus = new EventBus();
@@ -1,4 +1,4 @@
1
- import type { AnyStore } from 'nanostores';
1
+ import { type AnyStore } from 'nanostores';
2
2
  declare global {
3
3
  interface Window {
4
4
  slStore: any;
@@ -1,54 +1,5 @@
1
1
  import { computed } from 'nanostores';
2
- import { buildLogger } from '@nanostores/logger';
3
- import diff from 'microdiff';
4
2
  window.slStore = Object.create(null);
5
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
6
- const slStoreLogger = (store, name) => buildLogger(store, name, {
7
- mount: ({ storeName }) => {
8
- if (!window.slStore[storeName]) {
9
- window.slStore[storeName] = { mounted: true, history: [{ type: 'mount' }] };
10
- }
11
- window.slStore[storeName].store = store;
12
- },
13
- unmount: ({ storeName }) => {
14
- window.slStore[storeName].mounted = false;
15
- window.slStore[storeName]?.history.push({ type: 'unmount' });
16
- },
17
- change: ({ actionName, storeName, changed, newValue, oldValue, valueMessage }) => {
18
- window.slStore[storeName]?.history.push({
19
- type: 'change',
20
- changed,
21
- newValue,
22
- oldValue,
23
- diff: diff({ ...oldValue }, { ...newValue }, { cyclesFix: false }),
24
- actionName,
25
- valueMessage,
26
- });
27
- },
28
- action: {
29
- start: ({ actionName, args, storeName }) => {
30
- window.slStore[storeName]?.history.push({
31
- type: 'action:start',
32
- actionName,
33
- args,
34
- storeName,
35
- });
36
- },
37
- error: ({ actionName, error, storeName }) => {
38
- window.slStore[storeName]?.history.push({
39
- type: 'action:error',
40
- actionName,
41
- error,
42
- });
43
- },
44
- end: ({ actionName, storeName }) => {
45
- window.slStore[storeName]?.history.push({
46
- type: 'action:end',
47
- actionName,
48
- });
49
- },
50
- },
51
- });
52
3
  /**
53
4
  * An abstract store is a wrapper for third-party stores,
54
5
  * providing developers with a consistent interface inspired
@@ -1,4 +1,4 @@
1
- import type { MapStoreKeys, MapStore as NMapStore } from 'nanostores';
1
+ import { type MapStoreKeys, type MapStore as NMapStore } from 'nanostores';
2
2
  import { AbstractStore } from './abstract';
3
3
  /**
4
4
  * Wrapper for nanostores MapStore
@@ -1,4 +1,4 @@
1
- import type { StoreValue, WritableAtom } from 'nanostores';
1
+ import { type StoreValue, type WritableAtom } from 'nanostores';
2
2
  import { AbstractStore } from './abstract';
3
3
  /**
4
4
  * Wrapper for nanostores WritableAtom
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@streamlayer/sdk-web-interfaces",
3
- "version": "0.20.6",
3
+ "version": "0.21.0",
4
4
  "type": "module",
5
5
  "main": "./lib/index.js",
6
6
  "typings": "./lib/index.d.ts",
@@ -17,11 +17,10 @@
17
17
  },
18
18
  "peerDependencies": {
19
19
  "@bufbuild/protobuf": "^1.7.2",
20
- "@nanostores/logger": "^0.2.4",
21
- "@nanostores/query": "^0.2.8",
22
- "microdiff": "^1.3.2",
23
- "nanostores": "^0.9.5",
24
- "@streamlayer/sdk-web-types": "^0.22.4"
20
+ "@nanostores/query": "^0.2.10",
21
+ "nanostores": "^0.10.0",
22
+ "@streamlayer/sdk-web-logger": "^0.5.18",
23
+ "@streamlayer/sdk-web-types": "^0.23.0"
25
24
  },
26
25
  "devDependencies": {
27
26
  "tslib": "^2.6.2"