Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide a way to attach data to clients #1475

Open
jakearchibald opened this issue Oct 4, 2019 · 4 comments

Comments

@jakearchibald
Copy link
Contributor

commented Oct 4, 2019

Originally raised while discussing whether we should have client.isFrozen etc.

There's a lot of state data we could attach to clients, but developers also postMessage clients to 'ask' for some app-specific data, like "is the user currently typing a message?".

We could improve both situations by allowing developers to attach clonable data to a client.

Rough design that I haven't put much thought into:

// From a page (or other client):
await clients.setClientData({ foo: 'bar' });
// In a service worker:
const client = (await clients.matchAll())[0];
console.log(client.data?.foo); // 'bar' or undefined.

Only the client itself will be able to set client data.

My rough API proposal includes adding self.clients to windows/workers. We might be able to avoid adding the whole clients API if we want to take baby steps here.

@jakearchibald

This comment has been minimized.

Copy link
Contributor Author

commented Oct 4, 2019

We could add getClientData, but I wonder if it would encourage racing:

clients.getClientData().then(data => {
  data.hello = 'world';
  clients.setClientData(data);
});
//
clients.getClientData().then(data => {
  data.foo = 'baz';
  clients.setClientData(data);
});

We could ask folks to use weblocks, but if they're going to do coordination like that, they may as well just coordinate on a state object.

import stateData from './state.mjs';

stateData.hello = 'world';
clients.setClientData(stateData);
//
stateData.foo = 'baz';
clients.setClientData(stateData);
@surma

This comment has been minimized.

Copy link

commented Oct 6, 2019

Would this state data be in any way related to History API's state? If not, why not?

@asutherland

This comment has been minimized.

Copy link

commented Oct 6, 2019

Echoing a comment I made during the original discussion: a concern would be the size of the data if it's available synchronously from a snapshotted Client instance. For example, right now Firefox arbitrarily limits each History API entry's state to 2,097,152 bytes. This would be a lot of per-page memory for every clients.match() call to potentially entrain and/or have to ship around via IPC, especially for data that's intended for a page as its own consumer, not other pages.

@voxpelli

This comment has been minimized.

Copy link

commented Oct 7, 2019

I would advice against replicating the pattern of a global state that the History API opted for.

That state management in a way unfortunately mimics the global state that event listeners had before addEventListener() was introduced, where whenever you eg. added a new onclick event, you needed to ensure that you saved and wrapped any existing one, or else you would accidentally remove it.

If eg. a library decides that it fully owns the state management and itself never reads what anyone else has written there but rather plainly writes whatever it thinks the state should be from its perspective, then it can make it very hard for others to do anything powerful with it if that library becomes widely spread.

I eg. had en encounter with that many years ago with GitHub’s Pjax library: https://github.com/defunkt/jquery-pjax/pull/79/files#diff-256b2e7d3ffe31142c00fa086e15d3a6R128 It decided that it was the one and only true source of History API state, making it hard for other History API users to persist its state along side it, forcing me to fork the library for it to work and since I ultimately failed to upstream my patch I became forced to continue to use my fork for that site.

I’m not looking forward to relive such an experience with a new API if that can be avoided.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.