@jolibox/sdk 1.1.16-beta.1 → 1.1.16-beta.3

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,14 +1,14 @@
1
1
  {
2
2
  "name": "@jolibox/sdk",
3
3
  "description": "This project is common Jolibox JS-SDk interfere",
4
- "version": "1.1.16-beta.1",
4
+ "version": "1.1.16-beta.3",
5
5
  "main": "dist/index.cjs.js",
6
6
  "module": "dist/index.esm.js",
7
7
  "typings": "dist/index.d.ts",
8
8
  "license": "MIT",
9
9
  "dependencies": {
10
- "@jolibox/common": "1.1.16-beta.1",
11
- "@jolibox/types": "1.1.16-beta.1"
10
+ "@jolibox/common": "1.1.16-beta.3",
11
+ "@jolibox/types": "1.1.16-beta.3"
12
12
  },
13
13
  "devDependencies": {
14
14
  "typescript": "5.7.3",
package/sdk.build.log CHANGED
@@ -1,17 +1,17 @@
1
1
  Invoking: npm run clean && npm run build:cjs && npm run build:esm && npm run build:iife && tsc
2
2
 
3
- > @jolibox/sdk@1.1.16-beta.1 clean
3
+ > @jolibox/sdk@1.1.16-beta.3 clean
4
4
  > rimraf ./dist
5
5
 
6
6
 
7
- > @jolibox/sdk@1.1.16-beta.1 build:cjs
7
+ > @jolibox/sdk@1.1.16-beta.3 build:cjs
8
8
  > BUILD_VERSION=$(node -p "require('./package.json').version") node esbuild.config.js --format=cjs
9
9
 
10
10
 
11
- > @jolibox/sdk@1.1.16-beta.1 build:esm
11
+ > @jolibox/sdk@1.1.16-beta.3 build:esm
12
12
  > BUILD_VERSION=$(node -p "require('./package.json').version") node esbuild.config.js --format=esm
13
13
 
14
14
 
15
- > @jolibox/sdk@1.1.16-beta.1 build:iife
15
+ > @jolibox/sdk@1.1.16-beta.3 build:iife
16
16
  > BUILD_VERSION=$(node -p "require('./package.json').version") node esbuild.config.js --format=iife
17
17
 
@@ -0,0 +1,63 @@
1
+ import { JoliboxSDK } from '../index';
2
+
3
+ describe('JoliboxSDK Singleton Pattern', () => {
4
+ beforeEach(() => {
5
+ //eslint-disable-next-line @typescript-eslint/ban-ts-comment
6
+ //@ts-ignore
7
+ JoliboxSDK.instance = null;
8
+
9
+ if (window.joliboxsdk) {
10
+ //eslint-disable-next-line @typescript-eslint/ban-ts-comment
11
+ // @ts-ignore
12
+ delete window.joliboxsdk;
13
+ }
14
+ });
15
+
16
+ it('should return the same instance when using new constructor', () => {
17
+ // 创建第一个实例
18
+ const instance1 = new JoliboxSDK();
19
+
20
+ // 创建第二个实例
21
+ const instance2 = new JoliboxSDK();
22
+
23
+ // 验证两个实例是同一个对象
24
+ expect(instance1).toBe(instance2);
25
+ });
26
+
27
+ it('should return the same instance when using getInstance method', () => {
28
+ // 使用静态方法创建实例
29
+ const instance1 = JoliboxSDK.getInstance();
30
+
31
+ // 再次使用静态方法获取实例
32
+ const instance2 = JoliboxSDK.getInstance();
33
+
34
+ // 验证两个实例是同一个对象
35
+ expect(instance1).toBe(instance2);
36
+ });
37
+
38
+ it('should return the same instance when mixing constructor and getInstance', () => {
39
+ // 使用构造函数创建实例
40
+ const instance1 = new JoliboxSDK();
41
+
42
+ // 使用静态方法获取实例
43
+ const instance2 = JoliboxSDK.getInstance();
44
+
45
+ // 验证两个实例是同一个对象
46
+ expect(instance1).toBe(instance2);
47
+ });
48
+
49
+ it('should initialize window.joliboxsdk with SDK instances', () => {
50
+ // 创建实例
51
+ new JoliboxSDK();
52
+
53
+ // 验证全局对象上的引用
54
+ expect(window.joliboxsdk).toBeDefined();
55
+ expect(window.joliboxsdk.runtime).toBeDefined();
56
+ expect(window.joliboxsdk.ads).toBeDefined();
57
+ expect(window.joliboxsdk.lifecycle).toBeDefined();
58
+ expect(window.joliboxsdk.storage).toBeDefined();
59
+ expect(window.joliboxsdk.keyboard).toBeDefined();
60
+ expect(window.joliboxsdk.task).toBeDefined();
61
+ expect(window.joliboxsdk.router).toBeDefined();
62
+ });
63
+ });
package/src/api/index.ts CHANGED
@@ -2,3 +2,4 @@ export * from './get-system-info';
2
2
  export * from './can-i-use';
3
3
  export * from './login';
4
4
  export * from './request';
5
+ export * from './on-custom-event';
@@ -0,0 +1,9 @@
1
+ import { hostEmitter } from '@jolibox/common';
2
+ import { HostEventParamsMap } from '@jolibox/types';
3
+
4
+ // Define the custom events we want to support
5
+ type CustomEventType = 'onI18nChanged';
6
+
7
+ export const onCustomEvent = <T extends CustomEventType>(event: T, callback: HostEventParamsMap[T]) => {
8
+ hostEmitter.on(event, callback);
9
+ };
package/src/index.ts CHANGED
@@ -8,7 +8,7 @@ import './loader';
8
8
  /**
9
9
  * @public Jolibox JS SDK Entry
10
10
  */
11
- import { getSystemInfoSync, canIUse, login, checkSession, request } from './api';
11
+ import { getSystemInfoSync, canIUse, login, checkSession, request, onCustomEvent } from './api';
12
12
  import { RuntimeSDK } from './sdks/runtime';
13
13
  import { LifecycleSDK } from './sdks/lifecycle';
14
14
  import { StorageSDK } from './sdks/storage';
@@ -33,6 +33,8 @@ declare global {
33
33
  }
34
34
 
35
35
  export class JoliboxSDK {
36
+ private static instance: JoliboxSDK | null = null;
37
+
36
38
  readonly jssdkVersion = '__JOLIBOX_LOCAL_SDK_VERSION__';
37
39
  readonly runtime = new RuntimeSDK();
38
40
  readonly ads = new JoliboxAds();
@@ -48,8 +50,13 @@ export class JoliboxSDK {
48
50
  login = login.bind(this);
49
51
  checkSession = checkSession.bind(this);
50
52
  request = request.bind(this);
53
+ on = onCustomEvent.bind(this);
51
54
 
52
55
  constructor() {
56
+ if (JoliboxSDK.instance) {
57
+ return JoliboxSDK.instance;
58
+ }
59
+
53
60
  window.joliboxsdk = {
54
61
  runtime: this.runtime,
55
62
  ads: this.ads,
@@ -59,6 +66,17 @@ export class JoliboxSDK {
59
66
  task: this.task,
60
67
  router: this.router
61
68
  };
69
+
70
+ JoliboxSDK.instance = this;
71
+ console.log(`JoliboxSDK ${this.jssdkVersion} init`);
72
+ }
73
+
74
+ // 添加静态方法获取实例
75
+ public static getInstance(): JoliboxSDK {
76
+ if (!JoliboxSDK.instance) {
77
+ JoliboxSDK.instance = new JoliboxSDK();
78
+ }
79
+ return JoliboxSDK.instance;
62
80
  }
63
81
  }
64
82