Skip to content

Commit

Permalink
Merge pull request #3 from oriSomething/fix-bundle
Browse files Browse the repository at this point in the history
Bundle fixes
  • Loading branch information
oriSomething authored Dec 12, 2020
2 parents 704aff1 + d22f349 commit 74c060b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 27 deletions.
48 changes: 24 additions & 24 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,22 @@ class StorageMap {
private _map = new Map<string, string>();

//#region Listeners
clearListeners = new Set<() => void>();
propertyChangeListeners = new Set<
private _clearListeners = new Set<() => void>();
private _propertyChangeListeners = new Set<
(key: string, value: string | null) => void
>();

onPropertyChange(
listener: (key: string, value: string | null) => void
): () => void {
this._propertyChangeListeners.add(listener);
return () => void this._propertyChangeListeners.delete(listener);
}

onClearChange(listener: () => void): () => void {
this._clearListeners.add(listener);
return () => void this._clearListeners.delete(listener);
}
//#endregion

//#region Map needed functionality
Expand All @@ -44,8 +56,8 @@ class StorageMap {
const v = getString(value);
this._map.set(k, v);

if (this.propertyChangeListeners.size !== 0) {
for (let cb of this.propertyChangeListeners) {
if (this._propertyChangeListeners.size !== 0) {
for (let cb of this._propertyChangeListeners) {
cb(k, v);
}
}
Expand All @@ -56,17 +68,17 @@ class StorageMap {
clear() {
this._map.clear();

if (this.clearListeners.size !== 0) {
for (let cb of this.clearListeners) cb();
if (this._clearListeners.size !== 0) {
for (let cb of this._clearListeners) cb();
}
}

delete(key: PropertyKey) {
const k = getString(key);
const returnValue = this._map.delete(k);

if (this.propertyChangeListeners.size !== 0) {
for (let cb of this.propertyChangeListeners) {
if (this._propertyChangeListeners.size !== 0) {
for (let cb of this._propertyChangeListeners) {
cb(k, null);
}
}
Expand Down Expand Up @@ -232,28 +244,16 @@ export function subscribePropertyChange(
storage: BrowserStorage,
listener: (key: string, value: string | null) => void
): () => void {
let s: StorageMap | undefined = getStoragePrivateStorageMap(storage);
s.propertyChangeListeners.add(listener);

return () => {
if (s != null && s.propertyChangeListeners.delete(listener)) {
s = undefined;
}
};
let s = getStoragePrivateStorageMap(storage);
return s.onPropertyChange(listener);
}

export function subscribeClear(
storage: BrowserStorage,
listener: () => void
): () => void {
let s: StorageMap | undefined = getStoragePrivateStorageMap(storage);
s.clearListeners.add(listener);

return () => {
if (s != null && s.clearListeners.delete(listener)) {
s = undefined;
}
};
let s = getStoragePrivateStorageMap(storage);
return s.onClearChange(listener);
}

/**
Expand Down
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
"node": ">= 12"
},
"scripts": {
"prepack": "rm -rf dist && yarn run build",
"build": "microbundle",
"prepack": "yarn run build",
"build": "rm -rf dist && microbundle --target node --compress",
"lint": "eslint lib/**/*.{js,ts} --max-warnings 0",
"pretest": "yarn run build",
"test": "ava"
Expand Down Expand Up @@ -52,5 +52,8 @@
},
"browserslist": [
"node 12"
]
],
"mangle": {
"regex": "^_"
}
}

0 comments on commit 74c060b

Please sign in to comment.