Skip to content

Commit

Permalink
feat(api): update IWatch & mixin, boolean returns
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Jan 28, 2018
1 parent 55ba0e1 commit bddd5ce
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
4 changes: 2 additions & 2 deletions packages/api/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ export interface IStack<V, T> {
export type Watch<T> = (id: string, oldState: T, newState: T) => void;

export interface IWatch<T> {
addWatch(id: string, fn: Watch<T>);
removeWatch(id: string);
addWatch(id: string, fn: Watch<T>): boolean;
removeWatch(id: string): boolean;
notifyWatches(oldState: T, newState: T);
}
10 changes: 9 additions & 1 deletion packages/api/src/mixins/iwatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,19 @@ import { mixin } from "../mixin";
export const IWatch = mixin({
addWatch(id: string, fn: (id: string, oldState: any, newState: any) => void) {
this._watches = this._watches || {};
if (this._watches[id]) {
return false;
}
this._watches[id] = fn;
return true;
},
removeWatch(id: string) {
this._watches = this._watches || {};
delete this._watches[id];
if (this._watches[id]) {
delete this._watches[id];
return true;
}
return false;
},
notifyWatches(oldState: any, newState: any) {
const w = (this._watches = this._watches || {}),
Expand Down

0 comments on commit bddd5ce

Please sign in to comment.