Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 6 of 6 total
Thread Storage events
Thu, Jul 20 2017 10:25 AMPermanent Link

Matthew Jones

https://html.spec.whatwg.org/multipage/webstorage.html#the-storage-event shows that you can get notified that a localstorage item has changed. One of the answers at https://stackoverflow.com/questions/4079280/javascript-communication-between-browser-tabs-windows shows this being used for inter-tab comms, which I need to prevent data loss.

Anyone know how to make this work in EWB?

--

Matthew Jones
Fri, Jul 21 2017 10:25 AMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Matthew,

<< https://html.spec.whatwg.org/multipage/webstorage.html#the-storage-event shows that you can get notified that a localstorage item has changed. One of the answers at https://stackoverflow.com/questions/4079280/javascript-communication-between-browser-tabs-windows shows this being used for inter-tab comms, which I need to prevent data loss. >>

What do you mean by "data loss" ?  Are you trying to keep the various tabs in sync with one another ?

<< Anyone know how to make this work in EWB? >>

Check out the TAddress component in the same unit (WebComps) and you'll see how to register a window-level event handler that will work like you need.  However, please note that this event is not consistently fired on all browsers that are IE9 and higher.  Some fire it for the current tab, while others do not.

Tim Young
Elevate Software
www.elevatesoft.com
Fri, Jul 21 2017 10:40 AMPermanent Link

Matthew Jones

Tim Young [Elevate Software] wrote:

> What do you mean by "data loss" ?  Are you trying to keep the various tabs in sync with one another ?

Yes, exactly so. Or rather, so that one tab knows what the other is working on, so they can keep away. Requirement of the tool!

> << Anyone know how to make this work in EWB? >>
>
> Check out the TAddress component in the same unit (WebComps) and you'll see how to register a window-level event handler that will work like you need.  However, please note that this event is not consistently fired on all browsers that are IE9 and higher.  Some fire it for the current tab, while others do not.

Perfect, thanks.

--

Matthew Jones
Mon, Jul 24 2017 8:20 AMPermanent Link

Matthew Jones

Tim Young [Elevate Software] wrote:

> Check out the TAddress component

My starter code for anyone else wanting this capability. Tim's note on the firing of it is important - I didn't think this was working in Chrome until I ran my app in the other tab and got the notifications.


unit uStorageMonitor;

interface

uses WebCore, WebDOM;

type

type

external TStorageEvent emit StorageEvent = class(TEvent)
 public
    { Properties }
    property key: String read;
    property oldValue: String read;
    property newValue: String read;

 end;

external TStorageEventHandler = function (event: TStorageEvent): Boolean of object;


TStorageMonitor = class
private
{$IFNDEF DESIGN}
   FStorageChangeHandler: TStorageEventHandler;
{$ENDIF}

{$IFNDEF DESIGN}
   function StorageChangeHandler(event: TStorageEvent): Boolean;
{$ENDIF}

public
   constructor Create; override;
   destructor Destroy; override;

   procedure UnprepareStorageMonitor(eventHandler : TStorageEventHandler);
   function PrepareStorageMonitor : TStorageEventHandler;

end;

implementation

uses uDebugReport;

const
  STORAGECHANGE_EVENT = 'storage';

{$IFNDEF DESIGN}
function TStorageMonitor.StorageChangeHandler(event: TStorageEvent): Boolean;
var
   nPos : Integer;
   szKey : String;
begin              
   szKey := event.key;
   nPos := Pos('libdoc.', szKey);
DebugReport('Storage change "' + szKey + '" found=' + IntToStr(nPos));
   if nPos> 0 then
   begin
DebugReport('Storage change "' + event.newValue + '"');
//        output.innerHTML = event.newValue;
   end;
   CancelEventBubble(event);
   Result:=True;
   SetEventResult(event,Result);
end;
{$ENDIF}


procedure TStorageMonitor.UnprepareStorageMonitor(eventHandler : TStorageEventHandler);
begin
  {$IFNDEF DESIGN}
  ClearWindowEventHandler(window,STORAGECHANGE_EVENT,TEventHandler(eventHandler));
  {$ENDIF}
end;

function TStorageMonitor.PrepareStorageMonitor : TStorageEventHandler;
begin
  {$IFNDEF DESIGN}
  Result:=TStorageEventHandler(SetWindowEventHandler(window,STORAGECHANGE_EVENT,StorageChangeHandler));
  {$ENDIF}
end;

constructor TStorageMonitor.Create;
begin
   FStorageChangeHandler := PrepareStorageMonitor;
end;

destructor TStorageMonitor.Destroy;
begin
   UnprepareStorageMonitor(FStorageChangeHandler);
end;

end.


--

Matthew Jones
Mon, Jul 24 2017 10:52 AMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Matthew,

<< My starter code for anyone else wanting this capability. Tim's note on the firing of it is important - I didn't think this was working in Chrome until I ran my app in the other tab and got the notifications. >>

Thanks for the code.

I'll work something into EWB once 2.07 is out (maybe earlier, if I get a chance).  There's also a good fix on Stack Overflow for the issue of firing for the current tab vs. other tabs:  just unregister the event handler before making any modifications for the current application, and then re-register the event handler afterward.

Tim Young
Elevate Software
www.elevatesoft.com
Mon, Jul 24 2017 10:57 AMPermanent Link

Matthew Jones

Tim Young [Elevate Software] wrote:

> There's also a good fix on Stack Overflow for the issue of firing for the current tab vs. other tabs:  just unregister the event handler before making any modifications for the current application, and then re-register the event handler afterward.

Not sure if that is a good fix or not - would like timing to know - but my fix was just to know whether it is "my" storage name or not. So I'm using "IPC.1", "IPC.2" etc, and in the notification I just check if it is mine or not, and ignore the ones by me. Perhaps not a solution for everyone, but it depends on your purpose of course.

--

Matthew Jones
Image