pas2js.storagebridge.main.pas 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. unit pas2js.storagebridge.main;
  2. {$mode ObjFPC}
  3. interface
  4. uses
  5. JS, Rtl.WorkerCommands, pas2js.storagebridge.shared, web;
  6. Type
  7. { TMainStorageBridge }
  8. TMainStorageBridge = class (TObject)
  9. Private
  10. class var _instance : TMainStorageBridge;
  11. private
  12. function GetStorage(aKind: Integer): TJSStorage;
  13. protected
  14. procedure HandleLocalStorageCommand(aCmd: TLocalStorageCommand); virtual;
  15. procedure RegisterMessageHandlers; virtual;
  16. public
  17. constructor create;
  18. class procedure Init;
  19. class property Instance : TMainStorageBridge Read _Instance;
  20. end;
  21. implementation
  22. { TMainStorageBridge }
  23. function TMainStorageBridge.GetStorage(aKind : Integer) : TJSStorage;
  24. begin
  25. Case aKind of
  26. Ord(skLocal) : Result:=Window.localStorage;
  27. Ord(skSession) : Result:=Window.sessionStorage;
  28. else
  29. Result:=nil;
  30. end;
  31. end;
  32. procedure TMainStorageBridge.HandleLocalStorageCommand(aCmd : TLocalStorageCommand);
  33. var
  34. lStorage : TJSStorage;
  35. i, lError, lResultLen : Integer;
  36. lResult : string;
  37. lArr : TJSUint16Array;
  38. begin
  39. lResult:='';
  40. lResultLen:=0;
  41. lStorage:=GetStorage(aCmd.Kind);
  42. lError:=ESTORAGE_SUCCESS;
  43. if lStorage=Nil then
  44. lError:=ESTORAGE_KIND
  45. else
  46. case aCmd.FuncName of
  47. FNClear :
  48. lStorage.Clear;
  49. FNLength :
  50. lResultLen:=lStorage.Length;
  51. FNKey :
  52. begin
  53. lResult:=lStorage.Key(Integer(aCmd.Args[0]));
  54. if isNull(lResult) then
  55. begin
  56. lResultLen:=cNULLLength;
  57. lResult:='';
  58. end
  59. else
  60. lResultLen:=Length(lResult);
  61. end;
  62. FNRemoveItem :
  63. lStorage.removeItem(String(aCmd.Args[0]));
  64. FNGetItem :
  65. begin
  66. lResult:=lStorage.getItem(String(aCmd.Args[0]));
  67. if isNull(lResult) then
  68. begin
  69. lResultLen:=cNULLLength;
  70. lResult:='';
  71. end
  72. else
  73. lResultLen:=Length(lResult);
  74. end;
  75. FNSetItem :
  76. begin
  77. lStorage.setItem(String(aCmd.Args[0]),String(aCmd.Args[1]));
  78. end;
  79. end;
  80. aCmd.ResultData[CallResult]:=lError;
  81. aCmd.ResultData[CallResultLen]:=lResultLen;
  82. if lResult<>'' then
  83. begin
  84. lArr:=TJSUint16Array.New(aCmd.ResultData.buffer,CallResultData*4);
  85. for I:=0 to Length(lResult)-1 do
  86. lArr[i]:=TJSString(lResult).charCodeAt(i);
  87. end;
  88. aCmd.ResultData[CallLock]:=1;
  89. TJSAtomics.notify(aCmd.ResultData,CallLock);
  90. end;
  91. procedure TMainStorageBridge.RegisterMessageHandlers;
  92. begin
  93. TCommandDispatcher.Instance.specialize AddCommandHandler<TLocalStorageCommand>(cmdLocalStorage,@HandleLocalStorageCommand);
  94. end;
  95. constructor TMainStorageBridge.create;
  96. begin
  97. RegisterMessageHandlers;
  98. end;
  99. class procedure TMainStorageBridge.Init;
  100. begin
  101. _Instance:=TMainStorageBridge.Create;
  102. end;
  103. initialization
  104. TMainStorageBridge.Init;
  105. end.