@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.
package/dist/runtime.js CHANGED
@@ -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();