mirror of
https://gitlab.com/freepascal.org/fpc/pas2js.git
synced 2025-08-16 04:09:03 +02:00
rtl: added TBrowserApp.RegisterServiceWorker
This commit is contained in:
parent
4c501a5ed9
commit
49e4bf63ab
@ -1,4 +1,4 @@
|
|||||||
unit browserapp;
|
unit BrowserApp;
|
||||||
|
|
||||||
{$mode objfpc}
|
{$mode objfpc}
|
||||||
|
|
||||||
@ -8,6 +8,7 @@ uses
|
|||||||
Classes, SysUtils, Types, JS, web, CustApp;
|
Classes, SysUtils, Types, JS, web, CustApp;
|
||||||
|
|
||||||
type
|
type
|
||||||
|
TServiceWorkerRegisteredEvent = reference to procedure(Registration: TJSServiceWorkerRegistration);
|
||||||
|
|
||||||
{ TBrowserApplication }
|
{ TBrowserApplication }
|
||||||
|
|
||||||
@ -23,14 +24,14 @@ type
|
|||||||
procedure GetEnvironmentList(List: TStrings; NamesOnly: Boolean); override;
|
procedure GetEnvironmentList(List: TStrings; NamesOnly: Boolean); override;
|
||||||
procedure ShowException(E: Exception); override;
|
procedure ShowException(E: Exception); override;
|
||||||
procedure HandleException(Sender: TObject); override;
|
procedure HandleException(Sender: TObject); override;
|
||||||
|
function RegisterServiceWorker(aFile: String;
|
||||||
|
const aOnRegistered: TServiceWorkerRegisteredEvent = Nil; DoLogging: Boolean = false) : Boolean;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure ReloadEnvironmentStrings;
|
procedure ReloadEnvironmentStrings;
|
||||||
|
|
||||||
implementation
|
implementation
|
||||||
|
|
||||||
uses Rtl.BrowserLoadHelper;
|
|
||||||
|
|
||||||
var
|
var
|
||||||
EnvNames: TJSObject;
|
EnvNames: TJSObject;
|
||||||
Params : TStringDynArray;
|
Params : TStringDynArray;
|
||||||
@ -172,6 +173,33 @@ begin
|
|||||||
inherited HandleException(Sender);
|
inherited HandleException(Sender);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
function TBrowserApplication.RegisterServiceWorker(aFile: String;
|
||||||
|
const aOnRegistered: TServiceWorkerRegisteredEvent; DoLogging: Boolean
|
||||||
|
): Boolean;
|
||||||
|
begin
|
||||||
|
// register service worker if supported
|
||||||
|
Result:=HasServiceWorker;
|
||||||
|
if Result then
|
||||||
|
Window.addEventListener('load',
|
||||||
|
procedure()
|
||||||
|
begin
|
||||||
|
Window.navigator.serviceWorker
|
||||||
|
.register(aFile)
|
||||||
|
._then(TJSPromiseResolver(procedure(Registration: TJSServiceWorkerRegistration)
|
||||||
|
begin
|
||||||
|
if DoLogging then
|
||||||
|
console.log('service worker registered');
|
||||||
|
if Assigned(aOnRegistered) then
|
||||||
|
aOnRegistered(Registration);
|
||||||
|
end))
|
||||||
|
.catch(TJSPromiseResolver(procedure(err: JSValue)
|
||||||
|
begin
|
||||||
|
if DoLogging then
|
||||||
|
console.log('service worker not registered: '+String(err));
|
||||||
|
end));
|
||||||
|
end);
|
||||||
|
end;
|
||||||
|
|
||||||
initialization
|
initialization
|
||||||
IsConsole:=true;
|
IsConsole:=true;
|
||||||
OnParamCount:=@GetParamCount;
|
OnParamCount:=@GetParamCount;
|
||||||
|
Loading…
Reference in New Issue
Block a user