ZenFS expects null in case something does not exist

This commit is contained in:
Michael Van Canneyt 2025-05-26 17:52:35 +02:00
parent d2d95b7255
commit 1227e89c30
3 changed files with 29 additions and 14 deletions

View File

@ -64,7 +64,7 @@ begin
lResult:=lStorage.Key(Integer(aCmd.Args[0]));
if isNull(lResult) then
begin
lResultLen:=0;
lResultLen:=cNULLLength;
lResult:='';
end
else
@ -77,7 +77,7 @@ begin
lResult:=lStorage.getItem(String(aCmd.Args[0]));
if isNull(lResult) then
begin
lResultLen:=0;
lResultLen:=cNULLLength;
lResult:='';
end
else

View File

@ -18,7 +18,7 @@ const
FNRemoveItem = 'remove_item';
FNKey = 'key';
LocalStorageBufferSize = 1024*1024; // 1 mb
LocalStorageBufferSize = 4*1024*1024; // 4 mb
{
Result data:
@ -35,6 +35,8 @@ const
ESTORAGE_SUCCESS = 0;
ESTORAGE_KIND = -1;
cNULLLength = -1;
Type
TStorageKind = (skLocal,skSession);

View File

@ -6,7 +6,7 @@ unit pas2js.storagebridge.worker;
interface
uses
types, js, webworker, rtl.WorkerCommands, pas2js.storagebridge.shared;
types, js, weborworker, webworker, rtl.WorkerCommands, pas2js.storagebridge.shared;
Type
TMainThreadCallFunc = reference to function: Integer;
@ -19,6 +19,7 @@ Type
Private
FCallID : Integer;
FKind : TStorageKind;
FDecoder : TJSTextDecoder;
Protected
function DoMainThreadBlockingCall(const aFuncName: String; aArgs: array of JSValue): Integer;
public
@ -30,8 +31,8 @@ Type
public
constructor create(aKind : TStorageKind);
class procedure init;
function Key(aKey : Integer) : string;
function GetItem(aKey : String) : String;
function Key(aKey : Integer) : JSValue;
function GetItem(aKey : String) : JSValue;
procedure SetItem(aKey,aValue : String);
procedure RemoveItem(aKey : String);
function count : Integer;
@ -66,6 +67,7 @@ constructor TWorkerStorageBridge.create(aKind: TStorageKind);
begin
FKind:=aKind;
FResultData:=TJSInt32Array.New(_AtomicBuffer);
FDecoder:=TJSTextDecoder.New('utf-16');
end;
procedure TWorkerStorageBridge.clear;
@ -91,32 +93,43 @@ begin
DoMainThreadBlockingCall(FNRemoveItem,[aKey]);
end;
function TWorkerStorageBridge.Key(aKey: Integer): string;
function TWorkerStorageBridge.Key(aKey: Integer): JSValue;
var
lResultLen : integer;
lStringArray :TJSUint16Array;
begin
if DoMainThreadBlockingCall(FNKey,[aKey])<>0 then
Exit('');
Exit(null);
lResultLen:=Self.FResultData[CallResultLen];
if lResultLen<=0 then
Exit(null);
if lResultLen<=0 then
Exit('');
lStringArray:=TJSUint16Array.new(_AtomicBuffer, CallResultData*4, lResultLen);
Result := String(TJSFunction(@TJSString.fromCharCode).apply(nil, TJSValueDynArray(lStringArray)));
Result := FDecoder.Decode(lStringArray);
end;
function TWorkerStorageBridge.GetItem(aKey: String): String;
function TWorkerStorageBridge.GetItem(aKey: String): JSValue;
var
lResultLen : integer;
lStringArray :TJSUint16Array;
lNew,lStringArray :TJSUint16Array;
lBuf : TJSArrayBuffer;
begin
if DoMainThreadBlockingCall(FNGetItem,[aKey])<>0 then
Exit('');
Exit(null);
lResultLen:=Self.FResultData[CallResultLen];
if lResultLen<=0 then
if lResultLen=-1 then
Exit(null);
if lResultLen=0 then
Exit('');
lStringArray:=TJSUint16Array.new(_AtomicBuffer, CallResultData*4, lResultLen);
Result := String(TJSFunction(@TJSString.fromCharCode).apply(nil, TJSValueDynArray(lStringArray)));
lBuf:=TJSArrayBuffer.new(lResultLen*2);
lNew:=TJSUint16Array.new(lbuf);
lnew._set(lStringArray);
Result := FDecoder.Decode(lNew);
end;
class procedure TWorkerStorageBridge.init;