comparison events.pde @ 115:653ce10184e5

Improve error handling of local storage saving/loading.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 06 Jul 2018 18:58:28 +0300
parents a0d0994e9bac
children 050a1ca8b106
comparison
equal deleted inserted replaced
114:019909b7ab6e 115:653ce10184e5
902 g_data[int('S')] = 0; 902 g_data[int('S')] = 0;
903 g_data[int('s')] = 0; 903 g_data[int('s')] = 0;
904 904
905 if (mpHaveLocalStorage()) 905 if (mpHaveLocalStorage())
906 { 906 {
907 var fdata = mpGetNativeImage(); 907 try
908 if (fdata != null)
909 { 908 {
909 var fdata = mpGetNativeImage();
910 var fstr = JSON.stringify(fdata); 910 var fstr = JSON.stringify(fdata);
911 localStorage.setItem("MPIMAGE_DATA"+ str(machine), fstr); 911 localStorage.setItem("MPIMAGE_DATA"+ str(machine), fstr);
912 message("Saved to lstorage"); 912 message("Saved to lstorage");
913 } 913 }
914 else 914 catch (err)
915 {
915 message("ERROR"); 916 message("ERROR");
917 }
916 } 918 }
917 else 919 else
918 message("Lstorage is not available"); 920 message("Lstorage is not available");
919 } 921 }
920 922
921 if (g_data[int('L')] == 1) 923 if (g_data[int('L')] == 1)
922 { 924 {
923 g_data[int('L')] = 0; 925 g_data[int('L')] = 0;
924 if (mpHaveLocalStorage()) 926 if (mpHaveLocalStorage())
925 { 927 {
926 var fstr = localStorage.getItem("MPIMAGE_DATA"+ str(machine)); 928 try
927 if (fstr != null && typeof(fstr) == "string")
928 { 929 {
930 var fstr = localStorage.getItem("MPIMAGE_DATA"+ str(machine));
929 var fdata = JSON.parse(fstr); 931 var fdata = JSON.parse(fstr);
930 if (typeof(fdata) == "object" && 932 if (typeof(fdata) == "object" &&
931 Array.isArray(fdata)) 933 Array.isArray(fdata))
932 { 934 {
933 int res = mpSetNativeImage(fdata, false); 935 int res = mpSetNativeImage(fdata, false);
937 message("Loaded lstorage"); 939 message("Loaded lstorage");
938 } 940 }
939 else 941 else
940 message("Invalid data in lstorage"); 942 message("Invalid data in lstorage");
941 } 943 }
942 else 944 catch (err)
945 {
943 message("No data in lstorage"); 946 message("No data in lstorage");
947 }
944 } 948 }
945 else 949 else
946 message("Lstorage is not available"); 950 message("Lstorage is not available");
947 } 951 }
948 } 952 }