@tarojs/plugin-platform-harmony-ets 4.0.0-beta.30 → 4.0.0-beta.31

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.
@@ -22,7 +22,7 @@ let preferences: any
22
22
 
23
23
  function getPreferences () {
24
24
  try {
25
- if (!preferences) {
25
+ if (!preferences && context) {
26
26
  const data = bundleManager.getBundleInfoForSelfSync(bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION)
27
27
  preferences = dataPreferences.getPreferencesSync(context, { name: `${data.appInfo.uid}Store` })
28
28
  }
@@ -37,13 +37,42 @@ const storageSchema = {
37
37
  key: 'String'
38
38
  }
39
39
 
40
+ function checkContextExist (api: string, isAsync = false) {
41
+ if (!context) {
42
+ const message = `${api} 调用失败,Taro 不支持过早地调用 ${api},请确保页面已经渲染完成再调用此 API`
43
+ if (isAsync) {
44
+ return {
45
+ isExist: false,
46
+ error: Promise.reject(new Error(message))
47
+ }
48
+ } else {
49
+ console.warn(message)
50
+
51
+ return {
52
+ isExist: false,
53
+ }
54
+ }
55
+ }
56
+
57
+ return {
58
+ isExist: true,
59
+ }
60
+ }
61
+
40
62
  export function getStorage<T = any> (options: Taro.getStorage.Option<T>) {
63
+ const name = 'getStorage'
64
+ const { isExist, error } = checkContextExist(name, true)
65
+
66
+ if (!isExist) {
67
+ return error
68
+ }
69
+
41
70
  const { key, success, fail, complete } = options || {}
42
- const handle = new MethodHandler<{data: any}>({ name: 'getStorage', success, fail, complete })
71
+ const handle = new MethodHandler<{data: any}>({ name, success, fail, complete })
43
72
 
44
73
  return new Promise((resolve, reject) => {
45
74
  try {
46
- validateParams('getStorage', options, storageSchema)
75
+ validateParams(name, options, storageSchema)
47
76
  } catch (error) {
48
77
  const res = { errMsg: error.message }
49
78
  return handle.fail(res, { resolve, reject })
@@ -63,14 +92,21 @@ export function getStorage<T = any> (options: Taro.getStorage.Option<T>) {
63
92
  }
64
93
 
65
94
  export function getStorageSync (key: string) {
95
+ const name = 'getStorageSync'
96
+ const { isExist, error } = checkContextExist(name, false)
97
+
98
+ if (!isExist) {
99
+ return error
100
+ }
101
+
66
102
  if (!key) {
67
- throw new Error('getStorageSync:fail parameter error: parameter should be String')
103
+ throw new Error(`${name}:fail parameter error: parameter should be String`)
68
104
  }
69
105
 
70
106
  const preferences = getPreferences()
71
107
 
72
108
  if (!preferences) {
73
- throw new Error('getStorageSync:fail:preferences is null')
109
+ throw new Error(`${name}:fail:preferences is null`)
74
110
  }
75
111
 
76
112
  const data = preferences.getSync(key, null)
@@ -82,12 +118,19 @@ export function getStorageSync (key: string) {
82
118
  }
83
119
 
84
120
  export function setStorage (options: Taro.setStorage.Option) {
121
+ const name = 'setStorage'
122
+ const { isExist, error } = checkContextExist(name, true)
123
+
124
+ if (!isExist) {
125
+ return error
126
+ }
127
+
85
128
  const { key, data, success, fail, complete } = options || {}
86
- const handle = new MethodHandler({ name: 'setStorage', success, fail, complete })
129
+ const handle = new MethodHandler({ name, success, fail, complete })
87
130
 
88
131
  return new Promise((resolve, reject) => {
89
132
  try {
90
- validateParams('setStorage', options, storageSchema)
133
+ validateParams(name, options, storageSchema)
91
134
  } catch (error) {
92
135
  const res = { errMsg: error.message }
93
136
  return handle.fail(res, { resolve, reject })
@@ -105,14 +148,21 @@ export function setStorage (options: Taro.setStorage.Option) {
105
148
  }
106
149
 
107
150
  export function setStorageSync (key: string, data: any) {
151
+ const name = 'setStorageSync'
152
+ const { isExist, error } = checkContextExist(name, false)
153
+
154
+ if (!isExist) {
155
+ return error
156
+ }
157
+
108
158
  if (!key) {
109
- throw new Error('setStorageSync:fail key error: key should be String')
159
+ throw new Error(`${name}:fail key error: key should be String`)
110
160
  }
111
161
 
112
162
  const preferences = getPreferences()
113
163
 
114
164
  if (!preferences) {
115
- throw new Error('setStorageSync:fail:preferences is null')
165
+ throw new Error(`${name}:fail:preferences is null`)
116
166
  }
117
167
 
118
168
  preferences.putSync(key, data)
@@ -120,12 +170,19 @@ export function setStorageSync (key: string, data: any) {
120
170
  }
121
171
 
122
172
  export function removeStorage (options: Taro.removeStorage.Option) {
173
+ const name = 'removeStorage'
174
+ const { isExist, error } = checkContextExist(name, true)
175
+
176
+ if (!isExist) {
177
+ return error
178
+ }
179
+
123
180
  const { key, success, fail, complete } = options || {}
124
- const handle = new MethodHandler({ name: 'removeStorage', success, fail, complete })
181
+ const handle = new MethodHandler({ name, success, fail, complete })
125
182
 
126
183
  return new Promise((resolve, reject) => {
127
184
  try {
128
- validateParams('removeStorage', options, storageSchema)
185
+ validateParams(name, options, storageSchema)
129
186
  } catch (error) {
130
187
  const res = { errMsg: error.message }
131
188
  return handle.fail(res, { resolve, reject })
@@ -143,14 +200,21 @@ export function removeStorage (options: Taro.removeStorage.Option) {
143
200
  }
144
201
 
145
202
  export function removeStorageSync (key: string) {
203
+ const name = 'removeStorageSync'
204
+ const { isExist, error } = checkContextExist(name, false)
205
+
206
+ if (!isExist) {
207
+ return error
208
+ }
209
+
146
210
  if (!key) {
147
- throw new Error('removeStorageSync:fail key error: key should be String')
211
+ throw new Error(`${name}:fail key error: key should be String`)
148
212
  }
149
213
 
150
214
  const preferences = getPreferences()
151
215
 
152
216
  if (!preferences) {
153
- throw new Error('removeStorageSync:fail:preferences is null')
217
+ throw new Error(`${name}:fail:preferences is null`)
154
218
  }
155
219
 
156
220
  preferences.deleteSync(key)
@@ -158,8 +222,15 @@ export function removeStorageSync (key: string) {
158
222
  }
159
223
 
160
224
  export function clearStorage (options: Taro.clearStorage.Option) {
225
+ const name = 'clearStorage'
226
+ const { isExist, error } = checkContextExist(name, true)
227
+
228
+ if (!isExist) {
229
+ return error
230
+ }
231
+
161
232
  const { success, fail, complete } = options || {}
162
- const handle = new MethodHandler({ name: 'clearStorage', success, fail, complete })
233
+ const handle = new MethodHandler({ name, success, fail, complete })
163
234
 
164
235
  return new Promise((resolve, reject) => {
165
236
  const preferences = getPreferences()
@@ -174,10 +245,17 @@ export function clearStorage (options: Taro.clearStorage.Option) {
174
245
  }
175
246
 
176
247
  export function clearStorageSync () {
248
+ const name = 'clearStorageSync'
249
+ const { isExist, error } = checkContextExist(name, false)
250
+
251
+ if (!isExist) {
252
+ return error
253
+ }
254
+
177
255
  const preferences = getPreferences()
178
256
 
179
257
  if (!preferences) {
180
- throw new Error('clearStorageSync:fail:preferences is null')
258
+ throw new Error(`${name}:fail:preferences is null`)
181
259
  }
182
260
 
183
261
  preferences.clearSync()
@@ -501,14 +501,14 @@ declare namespace apis {
501
501
  const getShareInfo: (option?: {}, ...args: any[]) => Promise<Partial<ICallbackResult> & Record<string, unknown> & ICallbackResult>;
502
502
  /** 验证私密消息。 */
503
503
  const authPrivateMessage: (option?: {}, ...args: any[]) => Promise<ICallbackResult & Record<string, unknown>>;
504
- function getStorage<T = any>(options: Taro.getStorage.Option<T>): Promise<unknown>;
504
+ function getStorage<T = any>(options: Taro.getStorage.Option<T>): Promise<unknown> | undefined;
505
505
  function getStorageSync(key: string): any;
506
- function setStorage(options: Taro.setStorage.Option): Promise<unknown>;
507
- function setStorageSync(key: string, data: any): void;
508
- function removeStorage(options: Taro.removeStorage.Option): Promise<unknown>;
509
- function removeStorageSync(key: string): void;
510
- function clearStorage(options: Taro.clearStorage.Option): Promise<unknown>;
511
- function clearStorageSync(): void;
506
+ function setStorage(options: Taro.setStorage.Option): Promise<unknown> | undefined;
507
+ function setStorageSync(key: string, data: any): Promise<never> | undefined;
508
+ function removeStorage(options: Taro.removeStorage.Option): Promise<unknown> | undefined;
509
+ function removeStorageSync(key: string): Promise<never> | undefined;
510
+ function clearStorage(options: Taro.clearStorage.Option): Promise<unknown> | undefined;
511
+ function clearStorageSync(): Promise<never> | undefined;
512
512
  const getStorageInfoSync: (option?: {}, ...args: any[]) => Promise<Partial<ICallbackResult> & Record<string, unknown> & ICallbackResult>;
513
513
  const getStorageInfo: (option?: {}, ...args: any[]) => Promise<Partial<ICallbackResult> & Record<string, unknown> & ICallbackResult>;
514
514
  const createBufferURL: (option?: {}, ...args: any[]) => Promise<Partial<ICallbackResult> & Record<string, unknown> & ICallbackResult>;
@@ -3241,7 +3241,7 @@ Current.contextPromise.then((ctx) => {
3241
3241
  });
3242
3242
  function getPreferences() {
3243
3243
  try {
3244
- if (!preferences) {
3244
+ if (!preferences && context) {
3245
3245
  const data = bundleManager.getBundleInfoForSelfSync(bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION);
3246
3246
  preferences = dataPreferences.getPreferencesSync(context, { name: `${data.appInfo.uid}Store` });
3247
3247
  }
@@ -3254,12 +3254,37 @@ function getPreferences() {
3254
3254
  const storageSchema = {
3255
3255
  key: 'String'
3256
3256
  };
3257
+ function checkContextExist(api, isAsync = false) {
3258
+ if (!context) {
3259
+ const message = `${api} 调用失败,Taro 不支持过早地调用 ${api},请确保页面已经渲染完成再调用此 API`;
3260
+ if (isAsync) {
3261
+ return {
3262
+ isExist: false,
3263
+ error: Promise.reject(new Error(message))
3264
+ };
3265
+ }
3266
+ else {
3267
+ console.warn(message);
3268
+ return {
3269
+ isExist: false,
3270
+ };
3271
+ }
3272
+ }
3273
+ return {
3274
+ isExist: true,
3275
+ };
3276
+ }
3257
3277
  function getStorage(options) {
3278
+ const name = 'getStorage';
3279
+ const { isExist, error } = checkContextExist(name, true);
3280
+ if (!isExist) {
3281
+ return error;
3282
+ }
3258
3283
  const { key, success, fail, complete } = options || {};
3259
- const handle = new MethodHandler({ name: 'getStorage', success, fail, complete });
3284
+ const handle = new MethodHandler({ name, success, fail, complete });
3260
3285
  return new Promise((resolve, reject) => {
3261
3286
  try {
3262
- validateParams('getStorage', options, storageSchema);
3287
+ validateParams(name, options, storageSchema);
3263
3288
  }
3264
3289
  catch (error) {
3265
3290
  const res = { errMsg: error.message };
@@ -3278,12 +3303,17 @@ function getStorage(options) {
3278
3303
  });
3279
3304
  }
3280
3305
  function getStorageSync(key) {
3306
+ const name = 'getStorageSync';
3307
+ const { isExist, error } = checkContextExist(name, false);
3308
+ if (!isExist) {
3309
+ return error;
3310
+ }
3281
3311
  if (!key) {
3282
- throw new Error('getStorageSync:fail parameter error: parameter should be String');
3312
+ throw new Error(`${name}:fail parameter error: parameter should be String`);
3283
3313
  }
3284
3314
  const preferences = getPreferences();
3285
3315
  if (!preferences) {
3286
- throw new Error('getStorageSync:fail:preferences is null');
3316
+ throw new Error(`${name}:fail:preferences is null`);
3287
3317
  }
3288
3318
  const data = preferences.getSync(key, null);
3289
3319
  if (data) {
@@ -3294,11 +3324,16 @@ function getStorageSync(key) {
3294
3324
  }
3295
3325
  }
3296
3326
  function setStorage(options) {
3327
+ const name = 'setStorage';
3328
+ const { isExist, error } = checkContextExist(name, true);
3329
+ if (!isExist) {
3330
+ return error;
3331
+ }
3297
3332
  const { key, data, success, fail, complete } = options || {};
3298
- const handle = new MethodHandler({ name: 'setStorage', success, fail, complete });
3333
+ const handle = new MethodHandler({ name, success, fail, complete });
3299
3334
  return new Promise((resolve, reject) => {
3300
3335
  try {
3301
- validateParams('setStorage', options, storageSchema);
3336
+ validateParams(name, options, storageSchema);
3302
3337
  }
3303
3338
  catch (error) {
3304
3339
  const res = { errMsg: error.message };
@@ -3313,22 +3348,32 @@ function setStorage(options) {
3313
3348
  });
3314
3349
  }
3315
3350
  function setStorageSync(key, data) {
3351
+ const name = 'setStorageSync';
3352
+ const { isExist, error } = checkContextExist(name, false);
3353
+ if (!isExist) {
3354
+ return error;
3355
+ }
3316
3356
  if (!key) {
3317
- throw new Error('setStorageSync:fail key error: key should be String');
3357
+ throw new Error(`${name}:fail key error: key should be String`);
3318
3358
  }
3319
3359
  const preferences = getPreferences();
3320
3360
  if (!preferences) {
3321
- throw new Error('setStorageSync:fail:preferences is null');
3361
+ throw new Error(`${name}:fail:preferences is null`);
3322
3362
  }
3323
3363
  preferences.putSync(key, data);
3324
3364
  preferences.flush();
3325
3365
  }
3326
3366
  function removeStorage(options) {
3367
+ const name = 'removeStorage';
3368
+ const { isExist, error } = checkContextExist(name, true);
3369
+ if (!isExist) {
3370
+ return error;
3371
+ }
3327
3372
  const { key, success, fail, complete } = options || {};
3328
- const handle = new MethodHandler({ name: 'removeStorage', success, fail, complete });
3373
+ const handle = new MethodHandler({ name, success, fail, complete });
3329
3374
  return new Promise((resolve, reject) => {
3330
3375
  try {
3331
- validateParams('removeStorage', options, storageSchema);
3376
+ validateParams(name, options, storageSchema);
3332
3377
  }
3333
3378
  catch (error) {
3334
3379
  const res = { errMsg: error.message };
@@ -3343,19 +3388,29 @@ function removeStorage(options) {
3343
3388
  });
3344
3389
  }
3345
3390
  function removeStorageSync(key) {
3391
+ const name = 'removeStorageSync';
3392
+ const { isExist, error } = checkContextExist(name, false);
3393
+ if (!isExist) {
3394
+ return error;
3395
+ }
3346
3396
  if (!key) {
3347
- throw new Error('removeStorageSync:fail key error: key should be String');
3397
+ throw new Error(`${name}:fail key error: key should be String`);
3348
3398
  }
3349
3399
  const preferences = getPreferences();
3350
3400
  if (!preferences) {
3351
- throw new Error('removeStorageSync:fail:preferences is null');
3401
+ throw new Error(`${name}:fail:preferences is null`);
3352
3402
  }
3353
3403
  preferences.deleteSync(key);
3354
3404
  preferences.flush();
3355
3405
  }
3356
3406
  function clearStorage(options) {
3407
+ const name = 'clearStorage';
3408
+ const { isExist, error } = checkContextExist(name, true);
3409
+ if (!isExist) {
3410
+ return error;
3411
+ }
3357
3412
  const { success, fail, complete } = options || {};
3358
- const handle = new MethodHandler({ name: 'clearStorage', success, fail, complete });
3413
+ const handle = new MethodHandler({ name, success, fail, complete });
3359
3414
  return new Promise((resolve, reject) => {
3360
3415
  const preferences = getPreferences();
3361
3416
  if (!preferences)
@@ -3366,9 +3421,14 @@ function clearStorage(options) {
3366
3421
  });
3367
3422
  }
3368
3423
  function clearStorageSync() {
3424
+ const name = 'clearStorageSync';
3425
+ const { isExist, error } = checkContextExist(name, false);
3426
+ if (!isExist) {
3427
+ return error;
3428
+ }
3369
3429
  const preferences = getPreferences();
3370
3430
  if (!preferences) {
3371
- throw new Error('clearStorageSync:fail:preferences is null');
3431
+ throw new Error(`${name}:fail:preferences is null`);
3372
3432
  }
3373
3433
  preferences.clearSync();
3374
3434
  preferences.flush();