@dcloudio/uni-quickapp-webview 2.0.1-alpha-34620220415002 → 2.0.1-alpha-34820220426002

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.
Files changed (2) hide show
  1. package/dist/index.js +130 -2
  2. package/package.json +2 -2
package/dist/index.js CHANGED
@@ -314,7 +314,7 @@ const promiseInterceptor = {
314
314
  };
315
315
 
316
316
  const SYNC_API_RE =
317
- /^\$|Window$|WindowStyle$|sendHostEvent|sendNativeEvent|restoreGlobal|getCurrentSubNVue|getMenuButtonBoundingClientRect|^report|interceptors|Interceptor$|getSubNVueById|requireNativePlugin|upx2px|hideKeyboard|canIUse|^create|Sync$|Manager$|base64ToArrayBuffer|arrayBufferToBase64|getLocale|setLocale/;
317
+ /^\$|Window$|WindowStyle$|sendHostEvent|sendNativeEvent|restoreGlobal|requireGlobal|getCurrentSubNVue|getMenuButtonBoundingClientRect|^report|interceptors|Interceptor$|getSubNVueById|requireNativePlugin|upx2px|hideKeyboard|canIUse|^create|Sync$|Manager$|base64ToArrayBuffer|arrayBufferToBase64|getLocale|setLocale|invokePushCallback/;
318
318
 
319
319
  const CONTEXT_API_RE = /^create|Manager$/;
320
320
 
@@ -855,8 +855,136 @@ var eventApi = /*#__PURE__*/Object.freeze({
855
855
  $emit: $emit
856
856
  });
857
857
 
858
+ /**
859
+ * 框架内 try-catch
860
+ */
861
+ /**
862
+ * 开发者 try-catch
863
+ */
864
+ function tryCatch (fn) {
865
+ return function () {
866
+ try {
867
+ return fn.apply(fn, arguments)
868
+ } catch (e) {
869
+ // TODO
870
+ console.error(e);
871
+ }
872
+ }
873
+ }
874
+
875
+ function getApiCallbacks (params) {
876
+ const apiCallbacks = {};
877
+ for (const name in params) {
878
+ const param = params[name];
879
+ if (isFn(param)) {
880
+ apiCallbacks[name] = tryCatch(param);
881
+ delete params[name];
882
+ }
883
+ }
884
+ return apiCallbacks
885
+ }
886
+
887
+ let cid;
888
+ let cidErrMsg;
889
+
890
+ function normalizePushMessage (message) {
891
+ try {
892
+ return JSON.parse(message)
893
+ } catch (e) {}
894
+ return message
895
+ }
896
+
897
+ function invokePushCallback (
898
+ args
899
+ ) {
900
+ if (args.type === 'clientId') {
901
+ cid = args.cid;
902
+ cidErrMsg = args.errMsg;
903
+ invokeGetPushCidCallbacks(cid, args.errMsg);
904
+ } else if (args.type === 'pushMsg') {
905
+ onPushMessageCallbacks.forEach((callback) => {
906
+ callback({
907
+ type: 'receive',
908
+ data: normalizePushMessage(args.message)
909
+ });
910
+ });
911
+ } else if (args.type === 'click') {
912
+ onPushMessageCallbacks.forEach((callback) => {
913
+ callback({
914
+ type: 'click',
915
+ data: normalizePushMessage(args.message)
916
+ });
917
+ });
918
+ }
919
+ }
920
+
921
+ const getPushCidCallbacks = [];
922
+
923
+ function invokeGetPushCidCallbacks (cid, errMsg) {
924
+ getPushCidCallbacks.forEach((callback) => {
925
+ callback(cid, errMsg);
926
+ });
927
+ getPushCidCallbacks.length = 0;
928
+ }
929
+
930
+ function getPushCid (args) {
931
+ if (!isPlainObject(args)) {
932
+ args = {};
933
+ }
934
+ const {
935
+ success,
936
+ fail,
937
+ complete
938
+ } = getApiCallbacks(args);
939
+ const hasSuccess = isFn(success);
940
+ const hasFail = isFn(fail);
941
+ const hasComplete = isFn(complete);
942
+ getPushCidCallbacks.push((cid, errMsg) => {
943
+ let res;
944
+ if (cid) {
945
+ res = {
946
+ errMsg: 'getPushCid:ok',
947
+ cid
948
+ };
949
+ hasSuccess && success(res);
950
+ } else {
951
+ res = {
952
+ errMsg: 'getPushCid:fail' + (errMsg ? ' ' + errMsg : '')
953
+ };
954
+ hasFail && fail(res);
955
+ }
956
+ hasComplete && complete(res);
957
+ });
958
+ if (typeof cid !== 'undefined') {
959
+ Promise.resolve().then(() => invokeGetPushCidCallbacks(cid, cidErrMsg));
960
+ }
961
+ }
962
+
963
+ const onPushMessageCallbacks = [];
964
+ // 不使用 defineOnApi 实现,是因为 defineOnApi 依赖 UniServiceJSBridge ,该对象目前在小程序上未提供,故简单实现
965
+ const onPushMessage = (fn) => {
966
+ if (onPushMessageCallbacks.indexOf(fn) === -1) {
967
+ onPushMessageCallbacks.push(fn);
968
+ }
969
+ };
970
+
971
+ const offPushMessage = (fn) => {
972
+ if (!fn) {
973
+ onPushMessageCallbacks.length = 0;
974
+ } else {
975
+ const index = onPushMessageCallbacks.indexOf(fn);
976
+ if (index > -1) {
977
+ onPushMessageCallbacks.splice(index, 1);
978
+ }
979
+ }
980
+ };
981
+
858
982
  var api = /*#__PURE__*/Object.freeze({
859
- __proto__: null
983
+ __proto__: null,
984
+ getPushCid: getPushCid,
985
+ onPushMessage: onPushMessage,
986
+ offPushMessage: offPushMessage,
987
+ invokePushCallback: invokePushCallback
860
988
  });
861
989
 
862
990
  const MPPage = Page;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dcloudio/uni-quickapp-webview",
3
- "version": "2.0.1-alpha-34620220415002",
3
+ "version": "2.0.1-alpha-34820220426002",
4
4
  "description": "uni-app quickapp-webview",
5
5
  "main": "dist/index.js",
6
6
  "repository": {
@@ -17,5 +17,5 @@
17
17
  "name": "quickapp-webview",
18
18
  "title": "快应用(Webview)版"
19
19
  },
20
- "gitHead": "c2c5b0345d72c1cfb51645719ce260a5c1d0d0f7"
20
+ "gitHead": "a372254d8032fd7d4b9c2bde5dfec0fef7ce0d84"
21
21
  }