@streamlayer/sdk-web-features 0.2.22 → 0.7.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.
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # core
2
2
 
3
- This library was generatedwith [Nx](https://nx.dev).
3
+ This library was generated with [Nx](https://nx.dev).
4
4
 
5
5
  ## Building
6
6
  Run `nx build core` to build the library.
package/lib/index.d.ts ADDED
@@ -0,0 +1,30 @@
1
+ import { FeatureType } from '@streamlayer/sdk-web-types';
2
+ import { AbstractFeature, FeatureSource, FeatureProps, StreamLayerContext, SingleStore } from '@streamlayer/sdk-web-interfaces';
3
+ import { Highlights, Gamification } from '@streamlayer/feature-gamification';
4
+ export type Features = Feature | Gamification | Highlights;
5
+ export declare const AvailableFeatures: {
6
+ 12: boolean;
7
+ 14: boolean;
8
+ };
9
+ export declare class Feature extends AbstractFeature<undefined> {
10
+ constructor(overlay: FeatureProps, source: FeatureSource);
11
+ }
12
+ export declare const initFeature: (overlay: FeatureProps, source: FeatureSource, instance: StreamLayerContext) => Feature | Gamification | Highlights;
13
+ export { FeatureSource } from '@streamlayer/sdk-web-interfaces';
14
+ declare module '@streamlayer/sdk-web-interfaces' {
15
+ interface StreamLayerContext {
16
+ features: Map<FeatureType, Features>;
17
+ initFeature: (overlay: FeatureProps, source: FeatureSource) => void;
18
+ updateFeature: (overlay: FeatureProps, source: FeatureSource) => void;
19
+ destroyFeature: (overlay: FeatureProps) => void;
20
+ reinitializeFeatures: () => Promise<void>;
21
+ activeFeature: SingleStore<FeatureType>;
22
+ }
23
+ interface StreamLayerSDK {
24
+ getFeatures: () => Map<FeatureType, Features>;
25
+ getFeature: <T extends FeatureType = FeatureType>(featureType: T) => (T extends FeatureType.GAMES ? Gamification : T extends FeatureType.HIGHLIGHTS ? Highlights : Feature) | undefined;
26
+ getActiveFeature: SingleStore<FeatureType>['getStore'];
27
+ closeFeature: () => void;
28
+ }
29
+ }
30
+ export declare const features: (instance: StreamLayerContext, opts: unknown, done: () => void) => void;
package/lib/index.js ADDED
@@ -0,0 +1,124 @@
1
+ import { FeatureType } from '@streamlayer/sdk-web-types';
2
+ import { AbstractFeature, FeatureSource, SingleStore, createSingleStore, } from '@streamlayer/sdk-web-interfaces';
3
+ import { Highlights, Gamification } from '@streamlayer/feature-gamification';
4
+ export const AvailableFeatures = {
5
+ [FeatureType.GAMES]: true,
6
+ [FeatureType.HIGHLIGHTS]: true,
7
+ };
8
+ export class Feature extends AbstractFeature {
9
+ constructor(overlay, source) {
10
+ super(overlay, source);
11
+ }
12
+ }
13
+ export const initFeature = (overlay, source, instance) => {
14
+ if (overlay.type === FeatureType.GAMES) {
15
+ return new Gamification(overlay, source, instance);
16
+ }
17
+ if (overlay.type === FeatureType.HIGHLIGHTS) {
18
+ return new Highlights(overlay, source, instance);
19
+ }
20
+ return new Feature(overlay, source);
21
+ };
22
+ export { FeatureSource } from '@streamlayer/sdk-web-interfaces';
23
+ export const features = (instance, opts, done) => {
24
+ instance.features = new Map();
25
+ instance.activeFeature = new SingleStore(createSingleStore(), 'activeFeature');
26
+ instance.sdk.getFeatures = () => instance.features;
27
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
28
+ // @ts-ignore
29
+ instance.sdk.getFeature = (featureType) => instance.features.get(featureType);
30
+ instance.sdk.getActiveFeature = () => instance.activeFeature.getStore();
31
+ instance.sdk.openFeature = (featureType) => {
32
+ instance.activeFeature.setValue(featureType);
33
+ };
34
+ instance.sdk.closeFeature = () => {
35
+ instance.activeFeature.setValue(undefined);
36
+ };
37
+ /**
38
+ * A distinct object instance is created and initialized for each overlay.
39
+ */
40
+ instance.initFeature = (overlay, source = FeatureSource.ORGANIZATION) => {
41
+ const feature = initFeature(overlay, source, instance);
42
+ instance.features.set(overlay.type, feature);
43
+ };
44
+ instance.updateFeature = (overlay, source) => {
45
+ instance.features.get(overlay.type)?.update(overlay, source);
46
+ };
47
+ instance.destroyFeature = (overlay) => {
48
+ instance.features.delete(overlay.type);
49
+ };
50
+ instance.reinitializeFeatures = async () => {
51
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
52
+ // @ts-ignore
53
+ const organizationSettings = await instance.stores.organizationSettings.getValue();
54
+ instance.features.clear();
55
+ for (const overlay of organizationSettings?.overlays || []) {
56
+ instance.initFeature(overlay, FeatureSource.ORGANIZATION);
57
+ }
58
+ };
59
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
60
+ // @ts-ignore
61
+ instance.stores.providerStreamId.listen((eventId) => {
62
+ if (!eventId) {
63
+ void instance.reinitializeFeatures();
64
+ }
65
+ });
66
+ /**
67
+ * Essentially, features and stores establish subscriptions to other stores
68
+ * based on their dependencies. In certain cases, specific logic is
69
+ * directly invoked in response to changes in the store.
70
+ */
71
+ instance.storeSubscribe = () => {
72
+ const processSettings = (source, settings) => {
73
+ if (!settings?.overlays)
74
+ return;
75
+ if (source === FeatureSource.STREAM) {
76
+ instance.features.clear();
77
+ }
78
+ for (const overlay of settings.overlays) {
79
+ if (overlay.sdkEnable !== true) {
80
+ continue;
81
+ }
82
+ if (!instance.features.has(overlay.type)) {
83
+ instance.initFeature(overlay, source);
84
+ }
85
+ else {
86
+ instance.updateFeature(overlay, source);
87
+ }
88
+ }
89
+ };
90
+ const subscribes = {
91
+ /**
92
+ * During the initial SDK initialization, it's imperative to initialize features first,
93
+ * followed by their direct subscriptions to store fields.
94
+ * This section is currently in development, and it's
95
+ * essential to implement checks to determine whether a feature
96
+ * has already been initialized. If it has, events related to
97
+ * that feature should be skipped. Conversely, if a feature is
98
+ * missing in the new settings, it should be initialized again.
99
+ */
100
+ organizationSettings: (orgSettings) => {
101
+ if (orgSettings.data) {
102
+ try {
103
+ processSettings(FeatureSource.ORGANIZATION, orgSettings.data);
104
+ }
105
+ catch (err) {
106
+ console.log(err);
107
+ }
108
+ }
109
+ },
110
+ streamSettings: (streamSettings) => {
111
+ if (streamSettings.data) {
112
+ try {
113
+ processSettings(FeatureSource.STREAM, streamSettings.data);
114
+ }
115
+ catch (err) {
116
+ console.log(err);
117
+ }
118
+ }
119
+ },
120
+ };
121
+ instance.store.subscribe(subscribes);
122
+ };
123
+ done();
124
+ };
package/package.json CHANGED
@@ -1,20 +1,20 @@
1
1
  {
2
2
  "name": "@streamlayer/sdk-web-features",
3
- "dependencies": {
4
- "@streamlayer/sdk-web-interfaces": "*",
5
- "@streamlayer/sdk-web-types": "*",
6
- "@streamlayer/feature-gamification": "*",
7
- "tslib": "^2.6.2"
3
+ "peerDependencies": {
4
+ "@streamlayer/sdk-web-interfaces": "^0.16.1",
5
+ "@streamlayer/sdk-web-types": "^0.1.0",
6
+ "@streamlayer/feature-gamification": "^0.13.1",
7
+ "@streamlayer/sdk-web-core": "^0.9.0"
8
8
  },
9
- "version": "0.2.22",
9
+ "devDependencies": {
10
+ "tslib": "*"
11
+ },
12
+ "version": "0.7.0",
10
13
  "type": "module",
11
- "main": "./src/index.js",
12
- "module": "./src/index.js",
13
- "typings": "./src/index.d.ts",
14
+ "main": "./lib/index.js",
15
+ "typings": "./lib/index.d.ts",
14
16
  "files": [
15
- "src/"
16
- ],
17
- "devDependencies": {
18
- "@swc/helpers": "^0.5.2"
19
- }
20
- }
17
+ "lib/",
18
+ "package.json"
19
+ ]
20
+ }
package/src/index.d.ts DELETED
@@ -1,20 +0,0 @@
1
- import { FeatureConfig } from '@streamlayer/sdk-web-types';
2
- import { AbstractFeature, FeatureSource, FeatureProps, StreamLayerContext } from '@streamlayer/sdk-web-interfaces';
3
- import { Gamification } from '@streamlayer/feature-gamification';
4
- export declare class Feature extends AbstractFeature<undefined> {
5
- }
6
- export declare const initFeature: (overlay: FeatureProps, source: FeatureSource, instance: StreamLayerContext) => Feature | Gamification;
7
- export { FeatureSource } from '@streamlayer/sdk-web-interfaces';
8
- declare module '@streamlayer/sdk-web-interfaces' {
9
- interface StreamLayerContext {
10
- features: Map<FeatureConfig['name'], Feature | Gamification>;
11
- initFeature: (overlay: FeatureProps, source: FeatureSource) => void;
12
- updateFeature: (overlay: FeatureProps, source: FeatureSource) => void;
13
- destroyFeature: (overlay: FeatureProps) => void;
14
- reinitializeFeatures: () => Promise<void>;
15
- }
16
- interface StreamLayerSDK {
17
- getFeatures: () => Map<string, Feature | Gamification>;
18
- }
19
- }
20
- export declare const features: (instance: StreamLayerContext, opts: unknown, done: () => void) => void;
package/src/index.js DELETED
@@ -1,47 +0,0 @@
1
- import { FeatureType } from '@streamlayer/sdk-web-types';
2
- import { AbstractFeature, FeatureSource } from '@streamlayer/sdk-web-interfaces';
3
- import { Gamification } from '@streamlayer/feature-gamification';
4
- export class Feature extends AbstractFeature {
5
- }
6
- export const initFeature = (overlay, source, instance) => {
7
- if (overlay.type === FeatureType.GAMES) {
8
- return new Gamification(overlay, source, instance);
9
- }
10
- return new Feature(overlay, source);
11
- };
12
- export { FeatureSource } from '@streamlayer/sdk-web-interfaces';
13
- export const features = (instance, opts, done) => {
14
- instance.features = new Map();
15
- instance.sdk.getFeatures = () => instance.features;
16
- /**
17
- * A distinct object instance is created and initialized for each overlay.
18
- */
19
- instance.initFeature = (overlay, source = FeatureSource.ORGANIZATION) => {
20
- const feature = initFeature(overlay, source, instance);
21
- instance.features.set(overlay.name, feature);
22
- };
23
- instance.updateFeature = (overlay, source) => {
24
- instance.features.get(overlay.name)?.update(overlay, source);
25
- };
26
- instance.destroyFeature = (overlay) => {
27
- instance.features.delete(overlay.name);
28
- };
29
- instance.reinitializeFeatures = async () => {
30
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
31
- // @ts-ignore
32
- const organizationSettings = await instance.stores.organizationSettings.getValue();
33
- instance.features.clear();
34
- for (const overlay of organizationSettings?.overlays || []) {
35
- instance.initFeature(overlay, FeatureSource.ORGANIZATION);
36
- }
37
- };
38
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
39
- // @ts-ignore
40
- instance.stores.providerStreamId.listen((eventId) => {
41
- if (!eventId) {
42
- void instance.reinitializeFeatures();
43
- }
44
- });
45
- done();
46
- };
47
- //# sourceMappingURL=index.js.map
package/src/index.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../packages/sdk-web-features/src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAiB,WAAW,EAAE,MAAM,4BAA4B,CAAA;AACvE,OAAO,EAAE,eAAe,EAAE,aAAa,EAAoC,MAAM,iCAAiC,CAAA;AAClH,OAAO,EAAE,YAAY,EAAE,MAAM,mCAAmC,CAAA;AAEhE,MAAM,OAAO,OAAQ,SAAQ,eAA0B;CAAG;AAE1D,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,OAAqB,EAAE,MAAqB,EAAE,QAA4B,EAAE,EAAE;IACxG,IAAI,OAAO,CAAC,IAAI,KAAK,WAAW,CAAC,KAAK,EAAE;QACtC,OAAO,IAAI,YAAY,CAAC,OAAO,EAAE,MAAM,EAAE,QAAQ,CAAC,CAAA;KACnD;IAED,OAAO,IAAI,OAAO,CAAC,OAAO,EAAE,MAAM,CAAC,CAAA;AACrC,CAAC,CAAA;AAED,OAAO,EAAE,aAAa,EAAE,MAAM,iCAAiC,CAAA;AAwB/D,MAAM,CAAC,MAAM,QAAQ,GAAG,CAAC,QAA4B,EAAE,IAAa,EAAE,IAAgB,EAAE,EAAE;IACxF,QAAQ,CAAC,QAAQ,GAAG,IAAI,GAAG,EAAmB,CAAA;IAE9C,QAAQ,CAAC,GAAG,CAAC,WAAW,GAAG,GAAG,EAAE,CAAC,QAAQ,CAAC,QAAQ,CAAA;IAElD;;OAEG;IACH,QAAQ,CAAC,WAAW,GAAG,CAAC,OAAqB,EAAE,SAAwB,aAAa,CAAC,YAAY,EAAE,EAAE;QACnG,MAAM,OAAO,GAAG,WAAW,CAAC,OAAO,EAAE,MAAM,EAAE,QAAQ,CAAC,CAAA;QAEtD,QAAQ,CAAC,QAAQ,CAAC,GAAG,CAAC,OAAO,CAAC,IAAI,EAAE,OAAO,CAAC,CAAA;IAC9C,CAAC,CAAA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