Skip to content

Commit

Permalink
Automatically detect changes from the browser (#384)
Browse files Browse the repository at this point in the history
* Automatically detect changes from the browser

* Update size limits
  • Loading branch information
eXon committed Aug 22, 2023
1 parent 2aca85e commit 7816526
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 15 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@
"size-limit": [
{
"path": "./packages/universal-cookie/lib/index.js",
"limit": "3 KB"
"limit": "4 KB"
},
{
"path": "./packages/universal-cookie-express/lib/index.js",
"limit": "3 KB"
"limit": "4 KB"
},
{
"path": "./packages/universal-cookie-koa/lib/index.js",
"limit": "3 KB"
"limit": "4 KB"
},
{
"path": "./packages/react-cookie/lib/index.js",
Expand Down
5 changes: 1 addition & 4 deletions packages/react-cookie-demo/src/components/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,14 @@ import { useCookies } from 'react-cookie';
import NameForm from './NameForm';

function App() {
const [cookies, setCookie, removeCookie, updateCookies] = useCookies([
'name',
]);
const [cookies, setCookie, removeCookie] = useCookies(['name']);

function onChange(newName) {
setCookie('name', newName, { path: '/' });
}

function onExternalCall() {
document.cookie = 'name=Meow; path=/';
updateCookies();
}

function onClear() {
Expand Down
2 changes: 1 addition & 1 deletion packages/react-cookie/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ Remove a cookie

### `updateCookies()`

Read back the cookies and trigger changes accordingly. Useful is you change cookies server side or with other parties.
Read back the cookies from the browser and triggers the change listeners. This should normally not be necessary because this library detects cookie changes automatically.

## `withCookies(Component)`

Expand Down
2 changes: 1 addition & 1 deletion packages/react-cookie/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-cookie",
"version": "6.0.1",
"version": "6.1.0",
"description": "Universal cookies for React",
"main": "cjs/index.js",
"module": "es6/index.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/universal-cookie-express/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "universal-cookie-express",
"version": "6.0.1",
"version": "6.1.0",
"description": "Hook cookies get/set on Express for server-rendering",
"main": "cjs/index.js",
"module": "es6/index.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/universal-cookie-koa/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "universal-cookie-koa",
"version": "6.0.1",
"version": "6.1.0",
"description": "Hook cookies get/set on Koa for server-rendering",
"main": "cjs/index.js",
"module": "es6/index.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/universal-cookie/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ Remove a listener from the change callback.

### `update()`

Read back the cookies and trigger changes accordingly. Useful is you change cookies server side or with other parties.
Read back the cookies from the browser and triggers the change listeners. This should normally not be necessary because this library detects cookie changes automatically.

## Browser Example

Expand Down
2 changes: 1 addition & 1 deletion packages/universal-cookie/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "universal-cookie",
"version": "6.0.1",
"version": "6.1.0",
"description": "Universal cookies for JavaScript",
"main": "cjs/index.js",
"module": "es6/index.js",
Expand Down
31 changes: 29 additions & 2 deletions packages/universal-cookie/src/Cookies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export default class Cookies {
private cookies: { [name: string]: Cookie };
private defaultSetOptions: CookieSetOptions;
private changeListeners: CookieChangeListener[] = [];
private pollingInterval?: NodeJS.Timeout;

private HAS_DOCUMENT_COOKIE: boolean = false;

Expand Down Expand Up @@ -47,6 +48,16 @@ export default class Cookies {
});
}

private _startPolling() {
this.pollingInterval = setInterval(this.update, 300);
}

private _stopPolling() {
if (this.pollingInterval) {
clearInterval(this.pollingInterval);
}
}

public get(name: string, options?: CookieGetOptions): any;
public get<T>(name: string, options?: CookieGetOptions): T;
public get(name: string, options: CookieGetOptions = {}) {
Expand Down Expand Up @@ -111,24 +122,40 @@ export default class Cookies {
this._emitChange({ name, value: undefined, options });
}

public update() {
public update = () => {
if (!this.HAS_DOCUMENT_COOKIE) {
return;
}

const previousCookies = this.cookies;
this.cookies = cookie.parse(document.cookie);
this._checkChanges(previousCookies);
}
};

public addChangeListener(callback: CookieChangeListener) {
this.changeListeners.push(callback);

if (this.changeListeners.length === 1) {
if (typeof window === 'object' && 'cookieStore' in window) {
(window.cookieStore as any).addEventListener('change', this.update);
} else {
this._startPolling();
}
}
}

public removeChangeListener(callback: CookieChangeListener) {
const idx = this.changeListeners.indexOf(callback);
if (idx >= 0) {
this.changeListeners.splice(idx, 1);
}

if (this.changeListeners.length === 0) {
if (typeof window === 'object' && 'cookieStore' in window) {
(window.cookieStore as any).removeEventListener('change', this.update);
} else {
this._stopPolling();
}
}
}
}

0 comments on commit 7816526

Please sign in to comment.