Skip to content

Commit

Permalink
add typescript support
Browse files Browse the repository at this point in the history
  • Loading branch information
griffithtp committed May 16, 2020
1 parent fa5b0e8 commit 8088fe9
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 14 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
/lib
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
windowpubsub
===
# windowpubsub

Simple and lightweight Pub/Sub library using Window event listener and storage event with zero dependencies.

This enable applications to subscribe to events across different windows or tabs.
Some use cases are for shopping carts updates or theme switching.

#### Subscribing
`subscribe(topic, callbackFunction);`

`subscribe(topic, callbackFunction);`

```
import localpubsub from 'localpubsub';
Expand All @@ -20,13 +19,15 @@ localpubsub.subscribe('my topic', displayMessage);
```

#### Publishing

`publish(topic, message);`

```
localpubsub.publish('my topic', 'Hello');
```

#### Unsubscribe

`unsubscribe(topic);`

```
Expand Down
14 changes: 14 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 10 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
{
"name": "windowpubsub",
"version": "0.0.2",
"description": "",
"main": "index.js",
"version": "0.1.0",
"description": "Simple and lightweight Pub/Sub library using Window event listener and storage event with zero dependencies.",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "echo \"Error: no test specified\" && exit 1",
"build": "tsc"
},
"repository": {
"type": "git",
Expand All @@ -21,5 +23,8 @@
"bugs": {
"url": "https://github.com/griffithtp/widowpubsub/issues"
},
"homepage": "https://github.com/griffithtp/windowpubsub#readme"
"homepage": "https://github.com/griffithtp/windowpubsub#readme",
"devDependencies": {
"typescript": "^3.9.2"
}
}
13 changes: 7 additions & 6 deletions index.js → src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
let topics = {};
let topics: Record<string, unknown> = {};
// type Record<K extends string | number | symbol, T> = { [P in K]: T; }

function subscribe(topic, listener) {
function subscribe(topic: string, listener: ({}) => {}) {
if (!topics.hasOwnProperty(topic)) {
topics[topic] = listener;
}
Expand All @@ -21,27 +22,27 @@ function subscribe(topic, listener) {
};
}

function processCustomEvent(topic, cb, result) {
function processCustomEvent(topic: string, cb: ({}) => {}, result: any) {
if (topic === result.type) {
return cb({ topic, value: result.detail, eventObject: result });
}
}

function processStorage(topic, cb, result) {
function processStorage(topic: string, cb: ({}) => {}, result: any) {
if (topic === result.key) {
return cb({ topic, value: result.newValue, eventObject: result });
}
}

function publish(eventName, eventValue) {
function publish(eventName: string, eventValue: any) {
if (topics[eventName]) {
window.localStorage.setItem(eventName, eventValue);
const customEvent = new CustomEvent(eventName, { detail: eventValue });
window.dispatchEvent(customEvent);
}
}

function unsubscribe(subscribeKey) {
function unsubscribe(subscribeKey: string) {
window.localStorage.removeItem(subscribeKey);
delete topics[subscribeKey];
}
Expand Down
11 changes: 11 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"declaration": true,
"outDir": "./lib",
"strict": true
},
"include": ["src"],
"exclude": ["node_modules", "**/__tests__/*"]
}

0 comments on commit 8088fe9

Please sign in to comment.