@inappstory/game-center-api 1.1.0 → 1.2.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.
@@ -17,13 +17,11 @@ export declare class ResourceManager {
17
17
  private addResourceToCacheTree;
18
18
  private createCacheTree;
19
19
  cacheAllResources(): Promise<void>;
20
- cacheResource({ uri, originUri, resouceKeys, onSuccess, onFail, }: {
20
+ cacheResource({ uri, originUri, resouceKeys, }: {
21
21
  uri: string;
22
22
  originUri?: string;
23
23
  resouceKeys?: string[];
24
- onSuccess: (objectUrl: string | null) => void;
25
- onFail: () => void;
26
- }): Promise<void>;
24
+ }): Promise<string>;
27
25
  private createObjectUrlByUri;
28
26
  revokeCache(): void;
29
27
  }
@@ -47,24 +47,20 @@ class ResourceManager {
47
47
  const resouceKeys = relatedResources.map(resource => resource.key);
48
48
  const uri = resourceForFetch.getUri();
49
49
  const originUri = resourceForFetch.getOriginUri();
50
- await this.cacheResource({
51
- uri,
52
- originUri,
53
- resouceKeys,
54
- onSuccess: objectUrl => {
55
- if (objectUrl === null) {
56
- resolve();
57
- return;
58
- }
59
- for (const resource of relatedResources) {
60
- resource.setCacheUri(objectUrl);
61
- }
50
+ try {
51
+ const objectUrl = await this.cacheResource({ uri, originUri, resouceKeys });
52
+ if (objectUrl === originUri) {
62
53
  resolve();
63
- },
64
- onFail: () => {
65
- reject(`Can't load resource for [${resouceKeys.join(", ")}]`);
66
- },
67
- });
54
+ return;
55
+ }
56
+ for (const resource of relatedResources) {
57
+ resource.setCacheUri(objectUrl);
58
+ }
59
+ resolve();
60
+ }
61
+ catch (e) {
62
+ reject(`Can't load resource for [${resouceKeys.join(", ")}]`);
63
+ }
68
64
  });
69
65
  })(resourceForFetch, relatedResources);
70
66
  promises.push(promise);
@@ -73,18 +69,18 @@ class ResourceManager {
73
69
  for (const resList of this.resLists)
74
70
  resList["onCacheDone"]();
75
71
  }
76
- async cacheResource({ uri, originUri = uri, resouceKeys = ["outerResource"], onSuccess, onFail, }) {
72
+ async cacheResource({ uri, originUri = uri, resouceKeys = ["outerResource"], }) {
77
73
  let objectUrl = await this.createObjectUrlByUri(uri, resouceKeys);
78
74
  if (objectUrl === null && uri !== originUri)
79
75
  objectUrl = await this.createObjectUrlByUri(originUri, resouceKeys);
80
- if (objectUrl === null) {
76
+ if (objectUrl !== null)
77
+ return objectUrl;
78
+ return new Promise((resolve, reject) => {
81
79
  const image = new Image();
82
- image.onload = () => onSuccess(null);
83
- image.onerror = () => onFail();
80
+ image.onload = () => resolve(originUri);
81
+ image.onerror = () => reject();
84
82
  image.src = originUri;
85
- }
86
- else
87
- onSuccess(objectUrl);
83
+ });
88
84
  }
89
85
  async createObjectUrlByUri(uri, resouceKeys) {
90
86
  if (!uri)
package/lib/index.d.ts CHANGED
@@ -4,7 +4,7 @@ import { getDynamicResourceAsset, getDynamicResourceFont, getProjectFontFamilySt
4
4
  import { sendIasApiRequest } from "./iasApi";
5
5
  import { gameLocalData } from "./localData";
6
6
  import { closeGameReader, createSdkApi, reloadGameReader } from "./sdkApi";
7
- import { gameLoadedSdkCallback, gameLoadFailedSdkCallback } from "./sdkApi/initGame";
7
+ import { gameLoadedSdkCallback, gameLoadFailedSdkCallback, gameShouldForegroundCallback, isSdkSupportGameShouldForegroundCallback, gameOnForeground } from "./sdkApi/initGame";
8
8
  import { openUrl } from "./sdkApi/openUrl";
9
9
  import { share } from "./sdkApi/share";
10
10
  import { vibrate } from "./sdkApi/vibrate";
@@ -23,9 +23,9 @@ export type { RequestInfo as APIRequestInfo, Response as APIResponse } from "./i
23
23
  export type { Placeholder, GameLaunchConfig };
24
24
  export type { ProjectFontFamily } from "./gameResources";
25
25
  export type { ResourceInterface } from "./ResourceManager";
26
- export { createSdkApi, closeGameReader, gameLoadedSdkCallback, gameLoadFailedSdkCallback, gameLaunchConfig, isIos, isWeb, isAndroid, isDev, getSdkVersion, getSemverSdkVersion, gameLocalData, sendIasApiRequest, openUrl, share, vibrate, getDynamicResourceAsset, getDynamicResourceFont, getProjectFontFamilyStylesheet, getIsDemoMode, getSessionId, getApiBaseUrl, ScreenOrientation, PlaceholderType, fetchLocalFile, openStory, ResourceManager, dynamicResourceAssets, dynamicResourceFonts, staticResourcesImagePlaceholders, StaticResourceList, eventGame, reloadGameReader, openFilePicker, FilePickerResultType, isFilePickerResultFileList, isFilePickerResultLocalFileList, isLocalFile, hasFilePickerApi, };
26
+ export { createSdkApi, closeGameReader, gameLoadedSdkCallback, gameLoadFailedSdkCallback, gameLaunchConfig, isIos, isWeb, isAndroid, isDev, getSdkVersion, getSemverSdkVersion, gameLocalData, sendIasApiRequest, openUrl, share, vibrate, getDynamicResourceAsset, getDynamicResourceFont, getProjectFontFamilyStylesheet, getIsDemoMode, getSessionId, getApiBaseUrl, ScreenOrientation, PlaceholderType, fetchLocalFile, openStory, ResourceManager, dynamicResourceAssets, dynamicResourceFonts, staticResourcesImagePlaceholders, StaticResourceList, eventGame, reloadGameReader, openFilePicker, FilePickerResultType, isFilePickerResultFileList, isFilePickerResultLocalFileList, isLocalFile, hasFilePickerApi, gameShouldForegroundCallback, isSdkSupportGameShouldForegroundCallback, gameOnForeground, };
27
27
  declare const GameCenterApi: {
28
- createSdkApi: ({ mounted, beforeUnmount: beforeUnmountCb, onSdkCloseGameReaderIntent, onPause, onResume, onBackGesture, onAudioFocusChange, filterPlaceholders, }: Partial<{
28
+ createSdkApi: ({ mounted, beforeUnmount: beforeUnmountCb, onSdkCloseGameReaderIntent, onPause, onResume, onBackGesture, onAudioFocusChange, filterPlaceholders, gameShouldForeground, }: Partial<{
29
29
  mounted: () => void;
30
30
  beforeUnmount: () => void;
31
31
  onSdkCloseGameReaderIntent: () => void;
@@ -34,6 +34,7 @@ declare const GameCenterApi: {
34
34
  onBackGesture: () => void;
35
35
  onAudioFocusChange: (focusChange: number) => void;
36
36
  filterPlaceholders: (placeholders: Placeholder[]) => Placeholder[];
37
+ gameShouldForeground: () => void;
37
38
  }>) => void;
38
39
  closeGameReader: (data?: import("./sdkApi").CloseGameReaderOptions | undefined) => void;
39
40
  gameLoadedSdkCallback: (config?: Partial<Partial<{
@@ -85,5 +86,11 @@ declare const GameCenterApi: {
85
86
  isFilePickerResultLocalFileList: typeof isFilePickerResultLocalFileList;
86
87
  isLocalFile: typeof isLocalFile;
87
88
  hasFilePickerApi: () => boolean;
89
+ gameShouldForegroundCallback: (config?: Partial<Partial<{
90
+ showClose: boolean;
91
+ backGesture: boolean;
92
+ }>> | undefined) => void;
93
+ isSdkSupportGameShouldForegroundCallback: () => boolean | undefined;
94
+ gameOnForeground: Promise<void>;
88
95
  };
89
96
  export { GameCenterApi as default };
package/lib/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.default = exports.hasFilePickerApi = exports.isLocalFile = exports.isFilePickerResultLocalFileList = exports.isFilePickerResultFileList = exports.FilePickerResultType = exports.openFilePicker = exports.reloadGameReader = exports.eventGame = exports.StaticResourceList = exports.staticResourcesImagePlaceholders = exports.dynamicResourceFonts = exports.dynamicResourceAssets = exports.ResourceManager = exports.openStory = exports.fetchLocalFile = exports.PlaceholderType = exports.ScreenOrientation = exports.getApiBaseUrl = exports.getSessionId = exports.getIsDemoMode = exports.getProjectFontFamilyStylesheet = exports.getDynamicResourceFont = exports.getDynamicResourceAsset = exports.vibrate = exports.share = exports.openUrl = exports.sendIasApiRequest = exports.gameLocalData = exports.getSemverSdkVersion = exports.getSdkVersion = exports.isDev = exports.isAndroid = exports.isWeb = exports.isIos = exports.gameLaunchConfig = exports.gameLoadFailedSdkCallback = exports.gameLoadedSdkCallback = exports.closeGameReader = exports.createSdkApi = void 0;
3
+ exports.default = exports.gameOnForeground = exports.isSdkSupportGameShouldForegroundCallback = exports.gameShouldForegroundCallback = exports.hasFilePickerApi = exports.isLocalFile = exports.isFilePickerResultLocalFileList = exports.isFilePickerResultFileList = exports.FilePickerResultType = exports.openFilePicker = exports.reloadGameReader = exports.eventGame = exports.StaticResourceList = exports.staticResourcesImagePlaceholders = exports.dynamicResourceFonts = exports.dynamicResourceAssets = exports.ResourceManager = exports.openStory = exports.fetchLocalFile = exports.PlaceholderType = exports.ScreenOrientation = exports.getApiBaseUrl = exports.getSessionId = exports.getIsDemoMode = exports.getProjectFontFamilyStylesheet = exports.getDynamicResourceFont = exports.getDynamicResourceAsset = exports.vibrate = exports.share = exports.openUrl = exports.sendIasApiRequest = exports.gameLocalData = exports.getSemverSdkVersion = exports.getSdkVersion = exports.isDev = exports.isAndroid = exports.isWeb = exports.isIos = exports.gameLaunchConfig = exports.gameLoadFailedSdkCallback = exports.gameLoadedSdkCallback = exports.closeGameReader = exports.createSdkApi = void 0;
4
4
  const env_1 = require("./env");
5
5
  Object.defineProperty(exports, "getSdkVersion", { enumerable: true, get: function () { return env_1.getSdkVersion; } });
6
6
  Object.defineProperty(exports, "getSemverSdkVersion", { enumerable: true, get: function () { return env_1.getSemverSdkVersion; } });
@@ -32,6 +32,9 @@ Object.defineProperty(exports, "reloadGameReader", { enumerable: true, get: func
32
32
  const initGame_1 = require("./sdkApi/initGame");
33
33
  Object.defineProperty(exports, "gameLoadedSdkCallback", { enumerable: true, get: function () { return initGame_1.gameLoadedSdkCallback; } });
34
34
  Object.defineProperty(exports, "gameLoadFailedSdkCallback", { enumerable: true, get: function () { return initGame_1.gameLoadFailedSdkCallback; } });
35
+ Object.defineProperty(exports, "gameShouldForegroundCallback", { enumerable: true, get: function () { return initGame_1.gameShouldForegroundCallback; } });
36
+ Object.defineProperty(exports, "isSdkSupportGameShouldForegroundCallback", { enumerable: true, get: function () { return initGame_1.isSdkSupportGameShouldForegroundCallback; } });
37
+ Object.defineProperty(exports, "gameOnForeground", { enumerable: true, get: function () { return initGame_1.gameOnForeground; } });
35
38
  const openUrl_1 = require("./sdkApi/openUrl");
36
39
  Object.defineProperty(exports, "openUrl", { enumerable: true, get: function () { return openUrl_1.openUrl; } });
37
40
  const share_1 = require("./sdkApi/share");
@@ -97,6 +100,9 @@ const GameCenterApi = {
97
100
  isFilePickerResultLocalFileList: filePicker_h_1.isFilePickerResultLocalFileList,
98
101
  isLocalFile: filePicker_h_1.isLocalFile,
99
102
  hasFilePickerApi: filePicker_1.hasFilePickerApi,
103
+ gameShouldForegroundCallback: initGame_1.gameShouldForegroundCallback,
104
+ isSdkSupportGameShouldForegroundCallback: initGame_1.isSdkSupportGameShouldForegroundCallback,
105
+ gameOnForeground: initGame_1.gameOnForeground,
100
106
  };
101
107
  exports.default = GameCenterApi;
102
108
  // export { Placeholder as default};
@@ -3,7 +3,7 @@
3
3
  */
4
4
  import { SdkApiCallbacks } from "./index.h";
5
5
  import { OpenStoryOptions } from "./openStory.h";
6
- export declare const createSdkApi: ({ mounted, beforeUnmount: beforeUnmountCb, onSdkCloseGameReaderIntent, onPause, onResume, onBackGesture, onAudioFocusChange, filterPlaceholders, }: SdkApiCallbacks) => void;
6
+ export declare const createSdkApi: ({ mounted, beforeUnmount: beforeUnmountCb, onSdkCloseGameReaderIntent, onPause, onResume, onBackGesture, onAudioFocusChange, filterPlaceholders, gameShouldForeground, }: SdkApiCallbacks) => void;
7
7
  export type CloseGameReaderOptions = {
8
8
  [key: string]: any;
9
9
  openUrl?: string;
@@ -8,4 +8,5 @@ export type SdkApiCallbacks = Partial<{
8
8
  onBackGesture: () => void;
9
9
  onAudioFocusChange: (focusChange: number) => void;
10
10
  filterPlaceholders: (placeholders: Placeholder[]) => Placeholder[];
11
+ gameShouldForeground: () => void;
11
12
  }>;
@@ -10,10 +10,11 @@ const isObject_1 = require("../helpers/isObject");
10
10
  const gameLaunchConfig_1 = require("../gameLaunchConfig");
11
11
  const asyncQueue_1 = require("../asyncQueue");
12
12
  let beforeUnmount;
13
- const createSdkApi = ({ mounted, beforeUnmount: beforeUnmountCb, onSdkCloseGameReaderIntent, onPause, onResume, onBackGesture, onAudioFocusChange, filterPlaceholders, }) => {
13
+ const createSdkApi = ({ mounted, beforeUnmount: beforeUnmountCb, onSdkCloseGameReaderIntent, onPause, onResume, onBackGesture, onAudioFocusChange, filterPlaceholders, gameShouldForeground, }) => {
14
14
  beforeUnmount = beforeUnmountCb;
15
15
  gameLaunchConfig_1.gameLaunchHandlers.filterPlaceholders = filterPlaceholders ?? gameLaunchConfig_1.gameLaunchHandlers.filterPlaceholders;
16
16
  (0, initGame_1.createInitGame)(async () => (0, localData_1.initLocalData)(), mounted);
17
+ (0, initGame_1.createGameShouldForeground)(gameShouldForeground ?? (() => { }));
17
18
  window.closeGameReader = () => {
18
19
  if (onSdkCloseGameReaderIntent) {
19
20
  onSdkCloseGameReaderIntent();
@@ -77,6 +78,10 @@ const createSdkApi = ({ mounted, beforeUnmount: beforeUnmountCb, onSdkCloseGameR
77
78
  (0, isFunction_1.isFunction)(window.initGame) && window.initGame(data[1]);
78
79
  }
79
80
  break;
81
+ case "gameShouldForeground": {
82
+ window.gameShouldForeground();
83
+ break;
84
+ }
80
85
  case "closeGameReader":
81
86
  {
82
87
  window.closeGameReader();
@@ -19,6 +19,7 @@ declare global {
19
19
  error: string;
20
20
  };
21
21
  gameLoadFailed: typeof gameLoadFailedSdkCallback;
22
+ gameShouldForeground: () => void;
22
23
  }
23
24
  }
24
25
  type GameReaderInit = {
@@ -35,4 +36,15 @@ type GameLoadedSdkConfig = Partial<{
35
36
  */
36
37
  export declare const gameLoadedSdkCallback: (config?: Partial<GameLoadedSdkConfig>) => void;
37
38
  export declare const gameLoadFailedSdkCallback: (reason: string, canTryReload: boolean) => void;
39
+ export declare const createGameShouldForeground: (gameShouldForeground: () => void) => void;
40
+ type GameShouldForegroundConfig = Partial<{
41
+ showClose: boolean;
42
+ backGesture: boolean;
43
+ }>;
44
+ /**
45
+ * API method for remove loader screen from Reader
46
+ */
47
+ export declare const gameShouldForegroundCallback: (config?: Partial<GameShouldForegroundConfig>) => void;
48
+ export declare const isSdkSupportGameShouldForegroundCallback: () => boolean | undefined;
49
+ export declare const gameOnForeground: Promise<void>;
38
50
  export {};
@@ -1,11 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.gameLoadFailedSdkCallback = exports.gameLoadedSdkCallback = exports.createInitGame = void 0;
3
+ exports.gameOnForeground = exports.isSdkSupportGameShouldForegroundCallback = exports.gameShouldForegroundCallback = exports.createGameShouldForeground = exports.gameLoadFailedSdkCallback = exports.gameLoadedSdkCallback = exports.createInitGame = void 0;
4
4
  const isObject_1 = require("../helpers/isObject");
5
5
  const gameLaunchConfig_1 = require("../gameLaunchConfig");
6
6
  const env_1 = require("../env");
7
7
  const Source_1 = require("./web/Source");
8
8
  const createNonce_1 = require("../createNonce");
9
+ const semver = require("semver");
9
10
  window.gameLoadingInfo = {
10
11
  loaded: false,
11
12
  state: "before gameReader API creation",
@@ -100,28 +101,49 @@ const gameLoadedSdkCallback = (config) => {
100
101
  }
101
102
  if (env_1.isAndroid) {
102
103
  if (window.Android.gameLoaded !== undefined) {
103
- window.Android.gameLoaded(JSON.stringify({ showClose, backGesture }));
104
+ if ((0, exports.isSdkSupportGameShouldForegroundCallback)()) {
105
+ window.Android.gameLoaded();
106
+ }
107
+ else {
108
+ // old SDK API style
109
+ window.Android.gameLoaded(JSON.stringify({ showClose, backGesture }));
110
+ }
104
111
  }
105
112
  }
106
113
  else if (env_1.isIos) {
107
114
  if (env_1.iosMh.gameLoaded !== undefined) {
108
- env_1.iosMh.gameLoaded.postMessage(JSON.stringify({ showClose, backGesture: false }));
115
+ if ((0, exports.isSdkSupportGameShouldForegroundCallback)()) {
116
+ env_1.iosMh.gameLoaded.postMessage("");
117
+ }
118
+ else {
119
+ // old SDK API style
120
+ env_1.iosMh.gameLoaded.postMessage(JSON.stringify({ showClose, backGesture: false }));
121
+ }
109
122
  }
110
123
  }
111
124
  else if (env_1.isWeb) {
112
125
  if (Source_1.webSource.sourceWindow && Source_1.webSource.sourceWindowOrigin) {
113
- Source_1.webSource.sourceWindow.postMessage([
114
- "gameLoaded",
115
- JSON.stringify({
116
- showClose,
117
- backGesture,
118
- }),
119
- ], Source_1.webSource.sourceWindowOrigin);
126
+ if ((0, exports.isSdkSupportGameShouldForegroundCallback)()) {
127
+ Source_1.webSource.sourceWindow.postMessage(["gameLoaded"], Source_1.webSource.sourceWindowOrigin);
128
+ }
129
+ else {
130
+ // old SDK API style
131
+ Source_1.webSource.sourceWindow.postMessage([
132
+ "gameLoaded",
133
+ JSON.stringify({
134
+ showClose,
135
+ backGesture,
136
+ }),
137
+ ], Source_1.webSource.sourceWindowOrigin);
138
+ }
120
139
  }
121
140
  }
122
141
  window.gameLoadingInfo.state = "after call gameLoadedSdkCallback";
123
142
  window.gameLoadingInfo.description = "";
124
143
  window.gameLoadingInfo.loaded = true;
144
+ if (!(0, exports.isSdkSupportGameShouldForegroundCallback)()) {
145
+ gameOnForegroundResolve();
146
+ }
125
147
  }
126
148
  catch (e) {
127
149
  window._sendErrorLog && window._sendErrorLog({ src: "gameLoadedSdkCallback", message: e.message, stack: e.stack });
@@ -154,7 +176,74 @@ const gameLoadFailedSdkCallback = (reason, canTryReload) => {
154
176
  Source_1.webSource.sourceWindow.postMessage(["gameLoadFailed", reason, canTryReload], Source_1.webSource.sourceWindowOrigin);
155
177
  }
156
178
  }
179
+ gameOnForegroundReject(reason);
157
180
  }
158
181
  };
159
182
  exports.gameLoadFailedSdkCallback = gameLoadFailedSdkCallback;
160
183
  window.gameLoadFailed = exports.gameLoadFailedSdkCallback;
184
+ const createGameShouldForeground = (gameShouldForeground) => {
185
+ window.gameShouldForeground = gameShouldForeground;
186
+ };
187
+ exports.createGameShouldForeground = createGameShouldForeground;
188
+ /**
189
+ * API method for remove loader screen from Reader
190
+ */
191
+ const gameShouldForegroundCallback = (config) => {
192
+ let showClose = config?.showClose;
193
+ if (showClose == null) {
194
+ showClose = false;
195
+ }
196
+ let backGesture = config?.backGesture;
197
+ if (backGesture == null) {
198
+ backGesture = true;
199
+ }
200
+ if (env_1.isAndroid) {
201
+ if (window.Android.gameShouldForegroundCallback) {
202
+ window.Android.gameShouldForegroundCallback(JSON.stringify({ showClose, backGesture }));
203
+ }
204
+ }
205
+ else if (env_1.isIos) {
206
+ if (env_1.iosMh.gameShouldForegroundCallback) {
207
+ env_1.iosMh.gameShouldForegroundCallback.postMessage(JSON.stringify({ showClose, backGesture: false }));
208
+ }
209
+ }
210
+ else if (env_1.isWeb) {
211
+ if (Source_1.webSource.sourceWindow && Source_1.webSource.sourceWindowOrigin) {
212
+ Source_1.webSource.sourceWindow.postMessage([
213
+ "gameShouldForegroundCallback",
214
+ JSON.stringify({
215
+ showClose,
216
+ backGesture,
217
+ }),
218
+ ], Source_1.webSource.sourceWindowOrigin);
219
+ }
220
+ }
221
+ gameOnForegroundResolve();
222
+ };
223
+ exports.gameShouldForegroundCallback = gameShouldForegroundCallback;
224
+ const isSdkSupportGameShouldForegroundCallback = () => {
225
+ if (env_1.isAndroid) {
226
+ return "gameShouldForegroundCallback" in window.Android;
227
+ }
228
+ else if (env_1.isIos) {
229
+ return "gameShouldForegroundCallback" in env_1.iosMh;
230
+ }
231
+ else if (env_1.isWeb) {
232
+ let support = false;
233
+ const semverVersion = (0, env_1.getSemverSdkVersion)();
234
+ if (semverVersion != null && semverVersion) {
235
+ // gte(v1, v2): v1 >= v2
236
+ if (semver.gte(semverVersion, "2.12.0-rc.11")) {
237
+ support = true;
238
+ }
239
+ }
240
+ return support;
241
+ }
242
+ };
243
+ exports.isSdkSupportGameShouldForegroundCallback = isSdkSupportGameShouldForegroundCallback;
244
+ let gameOnForegroundResolve = () => { };
245
+ let gameOnForegroundReject = () => { };
246
+ exports.gameOnForeground = new Promise((resolve, reject) => {
247
+ gameOnForegroundResolve = resolve;
248
+ gameOnForegroundReject = reject;
249
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inappstory/game-center-api",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "description": "",
5
5
  "dependencies": {
6
6
  "semver": "^7.3.8",