Skip to content

Commit

Permalink
fix(fix empty callback): throw error when cb is not function
Browse files Browse the repository at this point in the history
  • Loading branch information
dreambo8563 committed Feb 19, 2019
1 parent 6b9ffe9 commit 0c5da85
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ interface ILSWatcher {
off(key: string, handler: Symbol): void;
}
class LSWatcher {
// 内置事件队列
// internal event queue 内置事件队列
private queue: Map<string, Map<Symbol, Function>> = new Map();
private prefix: string;
private storageObj: Storage;
constructor(options: lsOption) {
const { prefix, storage } = options;
this.prefix = prefix || "";
const storagePrefix = storage || "local";
if (!["local", "session"].includes(storagePrefix)) {
if (["local", "session"].indexOf(storagePrefix) === -1) {
throw new Error("storage param should be 'local' or 'session'");
}
switch (storagePrefix) {
Expand All @@ -40,6 +40,9 @@ class LSWatcher {
}
// 注册事件
on(key: string, fn: Function, immediate: boolean = false): Symbol {
if (typeof fn !== "function") {
throw new Error("the second arg should be the callback function");
}
const handler = Symbol(fn.name);
const keyInQueue = this.queue.get(this.prefix + key);
if (keyInQueue) {
Expand Down

0 comments on commit 0c5da85

Please sign in to comment.