use-broadcast-ts
Advanced tools
Comparing version 1.3.1 to 1.4.0
@@ -147,3 +147,8 @@ 'use strict'; | ||
const sharedImpl = (f, options) => (set, get, store) => { | ||
var _options$name; | ||
/** | ||
* Types | ||
*/ | ||
/** | ||
* Is the store synced with the other tabs | ||
@@ -160,9 +165,10 @@ */ | ||
/** | ||
* Create the broadcast channel | ||
* The broadcast channel name | ||
*/ | ||
const channel = new BroadcastChannel(options.name); | ||
const name = (_options$name = options == null ? void 0 : options.name) != null ? _options$name : f.toString(); | ||
/** | ||
* Types | ||
* Create the broadcast channel | ||
*/ | ||
const channel = new BroadcastChannel(name); | ||
@@ -187,3 +193,3 @@ /** | ||
*/ | ||
if (options.unsync) { | ||
if (options != null && options.unsync) { | ||
return; | ||
@@ -220,3 +226,3 @@ } | ||
channel.onmessage = e => { | ||
if (e.data.sync === options.name) { | ||
if (e.data.sync === name) { | ||
/** | ||
@@ -266,3 +272,3 @@ * If this tab / window is not the main, return | ||
channel.postMessage({ | ||
sync: options.name | ||
sync: name | ||
}); | ||
@@ -278,3 +284,3 @@ | ||
} | ||
}, (_options$mainTimeout = options.mainTimeout) != null ? _options$mainTimeout : 100); | ||
}, (_options$mainTimeout = options == null ? void 0 : options.mainTimeout) != null ? _options$mainTimeout : 100); | ||
}; | ||
@@ -281,0 +287,0 @@ |
@@ -7,3 +7,3 @@ import { StateCreator, StoreMutatorIdentifier } from 'zustand'; | ||
*/ | ||
name: string; | ||
name?: string; | ||
/** | ||
@@ -24,3 +24,3 @@ * Main timeout | ||
*/ | ||
export type Shared = <T, Mps extends [StoreMutatorIdentifier, unknown][] = [], Mcs extends [StoreMutatorIdentifier, unknown][] = []>(f: StateCreator<T, Mps, Mcs>, options: SharedOptions) => StateCreator<T, [], []>; | ||
export type Shared = <T, Mps extends [StoreMutatorIdentifier, unknown][] = [], Mcs extends [StoreMutatorIdentifier, unknown][] = []>(f: StateCreator<T, Mps, Mcs>, options?: SharedOptions) => StateCreator<T, [], []>; | ||
/** | ||
@@ -27,0 +27,0 @@ * Shared middleware |
{ | ||
"name": "use-broadcast-ts", | ||
"version": "1.3.1", | ||
"version": "1.4.0", | ||
"description": "Use the Broadcast Channel API in React easily with hooks or Zustand, and Typescript!", | ||
@@ -5,0 +5,0 @@ "type": "module", |
@@ -69,2 +69,6 @@ # use-broadcast-ts | ||
#### New in v1.4.0 | ||
- You don't have to specify a channel name anymore. The channel name is now automatically generated. **However, I strongly recommend you to use it.** | ||
## Usage with hooks | ||
@@ -124,3 +128,3 @@ | ||
(set, get, ...) => ..., | ||
options: SharedOptions | ||
options?: SharedOptions | ||
); | ||
@@ -137,3 +141,3 @@ ``` | ||
##### options.name (required) | ||
##### options.name | ||
@@ -140,0 +144,0 @@ Type: `string` |
@@ -8,3 +8,3 @@ import { StateCreator, StoreMutatorIdentifier } from 'zustand'; | ||
*/ | ||
name: string; | ||
name?: string; | ||
@@ -34,3 +34,3 @@ /** | ||
f: StateCreator<T, Mps, Mcs>, | ||
options: SharedOptions | ||
options?: SharedOptions | ||
) => StateCreator<T, [], []>; | ||
@@ -41,3 +41,3 @@ | ||
*/ | ||
type SharedImpl = <T>(f: StateCreator<T, [], []>, options: SharedOptions) => StateCreator<T, [], []>; | ||
type SharedImpl = <T>(f: StateCreator<T, [], []>, options?: SharedOptions) => StateCreator<T, [], []>; | ||
@@ -51,2 +51,7 @@ /** | ||
/** | ||
* Types | ||
*/ | ||
type Item = { [key: string]: unknown }; | ||
/** | ||
* Is the store synced with the other tabs | ||
@@ -63,10 +68,10 @@ */ | ||
/** | ||
* Create the broadcast channel | ||
* The broadcast channel name | ||
*/ | ||
const channel = new BroadcastChannel(options.name); | ||
const name = options?.name ?? f.toString(); | ||
/** | ||
* Types | ||
* Create the broadcast channel | ||
*/ | ||
type Item = { [key: string]: unknown }; | ||
const channel = new BroadcastChannel(name); | ||
@@ -91,3 +96,3 @@ /** | ||
*/ | ||
if (options.unsync) { | ||
if (options?.unsync) { | ||
return; | ||
@@ -121,3 +126,3 @@ } | ||
channel.onmessage = (e) => { | ||
if ((e.data as { sync: string }).sync === options.name) { | ||
if ((e.data as { sync: string }).sync === name) { | ||
/** | ||
@@ -163,3 +168,3 @@ * If this tab / window is not the main, return | ||
const synchronize = (): void => { | ||
channel.postMessage({ sync: options.name }); | ||
channel.postMessage({ sync: name }); | ||
@@ -174,3 +179,3 @@ /** | ||
} | ||
}, options.mainTimeout ?? 100); | ||
}, options?.mainTimeout ?? 100); | ||
}; | ||
@@ -177,0 +182,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
34682
1011
236