-
Notifications
You must be signed in to change notification settings - Fork 17
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
Monitoring Cookies feature #46
Comments
Yes! This is an amazing feature and I do plan to tackle it soon. I've just been so busy but I will make sure to prioritize this in my todo list. Thanks for opening issue though so we have this feature tracked. |
Hint for documents, where CookieStore class should likely extend EventTarget (yes, there is a polyfill for that too): const {get, set} = Object.getOwnPropertyDescriptor(Document.prototype, 'cookie');
Object.defineProperty(Document.prototype, 'cookie', {
get,
set(value) {
const prev = get.call(this);
set.call(this, value);
const curr = get.call(this);
if (prev !== curr) {
const event = new CustomEvent('change');
event.changed = [];
event.deleted = [];
// add changed/deleted logic
cookieStore.dispatchEvent(event);
}
}
}); The Service Worker variant might not be possible, as if I remember correctly there's no I hope any of this hints helps 👋 |
P.S. to batch multiple changes/delete at once, the dispatch event could be delayed a part let timer = 0;
const changed = [];
const deleted = [];
const dispatch = () => {
timer = 0;
const event = new CustomEvent('change');
event.changed = changed.splice(0);
event.deleted = deleted.splice(0);
cookieStore.dispatchEvent(event);
};
// ... previous code ...
if (prev !== curr) {
// populate arrays with more entries
changed.push( ... );
deleted.push( ... );
if (!timer)
timer = setTimeout(dispatch, 1000 / 60);
} Above logic will dispatch batched changes, keeping both list of changes clean per each dispatch and in between dispatches. |
P.S.2 if anyone agrees with this proposal I might file a PR although I don't TS much so I hope hints are already helpful, thanks. |
This is very helpful @WebReflection. It looks like I may have some time to try to tackle this in the next couple of weeks (if @keithamus and his crew don't get to it before me 😉) |
Since #116 has been merged, could this one be marked as resolved? And maybe a new version could be released? 😉 |
My understanding of Cookie Store API is that if you change a cookie from what ever method (Javascript or HTTP response), you'll get a signal through the CookieStore.change_event no matter the "tab" or ServiceWorker you are in. Despite that the @keithamus ' commit/MR is really useful within a given Disclaimer: but all of this is based on my non-tested assumption. Someone to confirm ? |
That's my understanding and interpretation of the spec too. This polyfill can really only do a "best effort" by overriding the I'll add it might be possible to get the events working cross-worker and cross-tab with postMessage and |
The current Cookie Store API draft defines a Monitoring Cookies feature.
It allows user to "observe changes to cookies" and therefore "avoid polling".
As far as I saw, this is not (yet) implemented in this library. It would be great to have this feature.
The text was updated successfully, but these errors were encountered: