@antglobal/create-sdk-loader 1.0.3 → 1.0.4

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/lib/main.d.ts CHANGED
@@ -7,6 +7,10 @@ interface InitType<ClassType> {
7
7
  init: () => Promise<ClassType>;
8
8
  }
9
9
  interface LoaderConfig<Config, ClassType> {
10
+ /**
11
+ * @@description whether to load the SDK only once, default true, it will return the same instance when calling load method multiple times. If false, otherwise it will create a new instance each time.
12
+ */
13
+ single?: boolean;
10
14
  /**
11
15
  * @description sdk cdn url
12
16
  */
@@ -35,14 +39,21 @@ interface LoaderConfig<Config, ClassType> {
35
39
  */
36
40
  onInitialized?: (instance: ClassType, url: string) => void;
37
41
  }
42
+ /**
43
+ * @description create singleton instance, it will return the same instance when calling load method multiple times. If you want to create a new instance each time, please use createInstance method.
44
+ */
45
+ export declare function createSingletonInstance<Config, ClassType extends InitType<ClassType>>(ClassConstructor: new (config: Config) => ClassType, config: Config): Promise<ClassType>;
46
+ /**
47
+ * @description create instance and auto call init method, it will create a new instance each time when calling load method.
48
+ */
38
49
  export declare function createInstance<Config, ClassType extends InitType<ClassType>>(ClassConstructor: new (config: Config) => ClassType, config: Config): Promise<ClassType>;
39
50
  export declare function createLoader<Config, ClassType extends InitType<ClassType>>(loadConfig: LoaderConfig<Config, ClassType>): (config: Config) => Promise<ClassType>;
40
51
  /**
41
- * 加载远程脚本并获取指定的类
52
+ * @description load remote script and get the class constructor
42
53
  */
43
54
  export declare function loadScriptClass<ClassType extends InitType<ClassType>, Params = any, SDKConstructor = new (config: Params) => ClassType>(url: string, globalName: string | (() => SDKConstructor)): Promise<SDKConstructor>;
44
55
  /**
45
- * 加载远程脚本并获取指定的全局变量
56
+ * @description load remote script and get the global variable. If you want to get the class constructor, please use loadScriptClass method.
46
57
  */
47
58
  export declare function loadScript<T>(url: string, globalName: string | (() => T)): Promise<T>;
48
59
  export {};
package/lib/main.js CHANGED
@@ -51,6 +51,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
51
51
  }
52
52
  };
53
53
  Object.defineProperty(exports, "__esModule", { value: true });
54
+ exports.createSingletonInstance = createSingletonInstance;
54
55
  exports.createInstance = createInstance;
55
56
  exports.createLoader = createLoader;
56
57
  exports.loadScriptClass = loadScriptClass;
@@ -71,55 +72,74 @@ var LoaderError = /** @class */ (function (_super) {
71
72
  }
72
73
  return LoaderError;
73
74
  }(Error));
75
+ var instancePromise = null;
76
+ /**
77
+ * @description create singleton instance, it will return the same instance when calling load method multiple times. If you want to create a new instance each time, please use createInstance method.
78
+ */
79
+ function createSingletonInstance(ClassConstructor, config) {
80
+ if (instancePromise) {
81
+ return instancePromise;
82
+ }
83
+ instancePromise = createInstance(ClassConstructor, config);
84
+ return instancePromise;
85
+ }
86
+ /**
87
+ * @description create instance and auto call init method, it will create a new instance each time when calling load method.
88
+ */
74
89
  function createInstance(ClassConstructor, config) {
75
90
  var instance = new ClassConstructor(config);
76
91
  return instance.init();
77
92
  }
78
93
  function createLoader(loadConfig) {
79
94
  var _this = this;
80
- var sdkURL = loadConfig.sdkURL, globalName = loadConfig.globalName, _a = loadConfig.timeout, timeout = _a === void 0 ? 20000 : _a, onLoadComplete = loadConfig.onLoadComplete, onError = loadConfig.onError, onLoadStart = loadConfig.onLoadStart, onInitialized = loadConfig.onInitialized;
81
- return function (config) {
82
- onLoadStart === null || onLoadStart === void 0 ? void 0 : onLoadStart();
83
- var _loadScriptClass = createTimeout(function () { return loadScriptClass(sdkURL, globalName); }, timeout);
84
- return _loadScriptClass()
85
- .then(function (RemoteClass) { return __awaiter(_this, void 0, void 0, function () {
86
- var instance, e_1;
87
- return __generator(this, function (_a) {
88
- switch (_a.label) {
89
- case 0:
90
- onLoadComplete === null || onLoadComplete === void 0 ? void 0 : onLoadComplete();
91
- if (!RemoteClass) {
92
- throw new LoaderError('NO_REMOTE_CLASS', "No variables ".concat(globalName, " were found. Cannot be initialized the Class ").concat(globalName));
95
+ var sdkURL = loadConfig.sdkURL, globalName = loadConfig.globalName, _a = loadConfig.timeout, timeout = _a === void 0 ? 20000 : _a, onLoadComplete = loadConfig.onLoadComplete, onError = loadConfig.onError, onLoadStart = loadConfig.onLoadStart, onInitialized = loadConfig.onInitialized, _b = loadConfig.single, single = _b === void 0 ? true : _b;
96
+ return function (config) { return __awaiter(_this, void 0, void 0, function () {
97
+ var _loadScriptClass;
98
+ var _this = this;
99
+ return __generator(this, function (_a) {
100
+ onLoadStart === null || onLoadStart === void 0 ? void 0 : onLoadStart();
101
+ _loadScriptClass = createTimeout(function () { return loadScriptClass(sdkURL, globalName); }, timeout);
102
+ return [2 /*return*/, _loadScriptClass()
103
+ .then(function (RemoteClass) { return __awaiter(_this, void 0, void 0, function () {
104
+ var createMethod, instance, e_1;
105
+ return __generator(this, function (_a) {
106
+ switch (_a.label) {
107
+ case 0:
108
+ onLoadComplete === null || onLoadComplete === void 0 ? void 0 : onLoadComplete();
109
+ if (!RemoteClass) {
110
+ throw new LoaderError('NO_REMOTE_CLASS', "No variables ".concat(globalName, " were found. Cannot be initialized the Class ").concat(globalName));
111
+ }
112
+ _a.label = 1;
113
+ case 1:
114
+ _a.trys.push([1, 3, , 4]);
115
+ createMethod = single ? createSingletonInstance : createInstance;
116
+ return [4 /*yield*/, createMethod(RemoteClass, config)];
117
+ case 2:
118
+ instance = _a.sent();
119
+ onInitialized === null || onInitialized === void 0 ? void 0 : onInitialized(instance, window.location.href);
120
+ return [2 /*return*/, instance];
121
+ case 3:
122
+ e_1 = _a.sent();
123
+ throw new LoaderError('INIT_ERROR', "Initialize Class ".concat(globalName, " failed: ").concat(e_1.message));
124
+ case 4: return [2 /*return*/];
93
125
  }
94
- _a.label = 1;
95
- case 1:
96
- _a.trys.push([1, 3, , 4]);
97
- return [4 /*yield*/, createInstance(RemoteClass, config)];
98
- case 2:
99
- instance = _a.sent();
100
- onInitialized === null || onInitialized === void 0 ? void 0 : onInitialized(instance, window.location.href);
101
- return [2 /*return*/, instance];
102
- case 3:
103
- e_1 = _a.sent();
104
- throw new LoaderError('INIT_ERROR', "Initialize Class ".concat(globalName, " failed: ").concat(e_1.message));
105
- case 4: return [2 /*return*/];
106
- }
107
- });
108
- }); })
109
- .catch(function (e) {
110
- onError === null || onError === void 0 ? void 0 : onError(e);
111
- return Promise.reject(e);
126
+ });
127
+ }); })
128
+ .catch(function (e) {
129
+ onError === null || onError === void 0 ? void 0 : onError(e);
130
+ return Promise.reject(e);
131
+ })];
112
132
  });
113
- };
133
+ }); };
114
134
  }
115
135
  /**
116
- * 加载远程脚本并获取指定的类
136
+ * @description load remote script and get the class constructor
117
137
  */
118
138
  function loadScriptClass(url, globalName) {
119
139
  return loadScript(url, globalName);
120
140
  }
121
141
  /**
122
- * 加载远程脚本并获取指定的全局变量
142
+ * @description load remote script and get the global variable. If you want to get the class constructor, please use loadScriptClass method.
123
143
  */
124
144
  function loadScript(url, globalName) {
125
145
  var getRemoteClass = function () {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@antglobal/create-sdk-loader",
3
- "version": "1.0.3",
3
+ "version": "1.0.4",
4
4
  "main": "./lib/main.js",
5
5
  "module": "./lib/main.js",
6
6
  "types": "./lib/main.d.ts",