windowed-observable
Advanced tools
Comparing version 0.2.3 to 0.3.0
@@ -1,1 +0,1 @@ | ||
export { Observer, ObserversArray, EventsArray, handlerOptions, default as Observable, } from './observable'; | ||
export { Observer, ObserversArray, EventsArray, SubscriptionOptions, default as Observable, } from './observable'; |
@@ -1,4 +0,4 @@ | ||
declare const EVENTS = "__events__"; | ||
declare const SHARED = "__shared__"; | ||
declare const OBSERVERS = "__observers__"; | ||
export declare const EVENTS = "__events__"; | ||
export declare const SHARED = "__shared__"; | ||
export declare const OBSERVERS = "__observers__"; | ||
export declare type EventsArray = Array<any>; | ||
@@ -20,4 +20,5 @@ interface EventsArrayMap { | ||
} | ||
export interface handlerOptions { | ||
latest: boolean; | ||
export interface SubscriptionOptions { | ||
every?: boolean; | ||
latest?: boolean; | ||
} | ||
@@ -28,7 +29,10 @@ declare class Observable { | ||
constructor(namespace: string); | ||
events: EventsArray; | ||
observers: ObserversArray; | ||
namespace: string; | ||
private get events(); | ||
private get observers(); | ||
private set events(value); | ||
private set observers(value); | ||
set namespace(namespace: string); | ||
dispatch(data: any): void; | ||
subscribe(observer: Observer, options?: handlerOptions): void; | ||
publish(data: any): void; | ||
subscribe(observer: Observer, options?: SubscriptionOptions): void; | ||
unsubscribe(observer: Observer): void; | ||
@@ -35,0 +39,0 @@ clear(): void; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var EVENTS = '__events__'; | ||
var SHARED = '__shared__'; | ||
var OBSERVERS = '__observers__'; | ||
exports.EVENTS = '__events__'; | ||
exports.SHARED = '__shared__'; | ||
exports.OBSERVERS = '__observers__'; | ||
var Observable = /** @class */ (function () { | ||
@@ -12,15 +12,15 @@ function Observable(namespace) { | ||
Observable.initialize = function () { | ||
if (!window[SHARED]) | ||
window[SHARED] = {}; | ||
if (!window[SHARED][EVENTS]) | ||
window[SHARED][EVENTS] = {}; | ||
if (!window[SHARED][OBSERVERS]) | ||
window[SHARED][OBSERVERS] = {}; | ||
if (!window[exports.SHARED]) | ||
window[exports.SHARED] = {}; | ||
if (!window[exports.SHARED][exports.EVENTS]) | ||
window[exports.SHARED][exports.EVENTS] = {}; | ||
if (!window[exports.SHARED][exports.OBSERVERS]) | ||
window[exports.SHARED][exports.OBSERVERS] = {}; | ||
}; | ||
Object.defineProperty(Observable.prototype, "events", { | ||
get: function () { | ||
return window[SHARED][EVENTS][this._namespace] || []; | ||
return window[exports.SHARED][exports.EVENTS][this._namespace]; | ||
}, | ||
set: function (events) { | ||
window[SHARED][EVENTS][this._namespace] = events; | ||
window[exports.SHARED][exports.EVENTS][this._namespace] = events; | ||
}, | ||
@@ -32,6 +32,6 @@ enumerable: true, | ||
get: function () { | ||
return window[SHARED][OBSERVERS][this._namespace] || []; | ||
return window[exports.SHARED][exports.OBSERVERS][this._namespace]; | ||
}, | ||
set: function (listeners) { | ||
window[SHARED][OBSERVERS][this._namespace] = listeners; | ||
window[exports.SHARED][exports.OBSERVERS][this._namespace] = listeners; | ||
}, | ||
@@ -61,9 +61,24 @@ enumerable: true, | ||
}; | ||
Observable.prototype.publish = function (data) { | ||
if (this.observers.length > 0) { | ||
this.observers.forEach(function (observer) { return observer(data); }); | ||
} | ||
else { | ||
this.events = this.events.concat(data); | ||
} | ||
; | ||
}; | ||
Observable.prototype.subscribe = function (observer, options) { | ||
if (options === void 0) { options = { latest: false }; } | ||
var latest = options.latest; | ||
if (options === void 0) { options = { latest: false, every: false }; } | ||
var every = options.every, latest = options.latest; | ||
var events = this.events; | ||
if (latest && events.length > 0) { | ||
var lastEvent = events.pop(); | ||
observer(lastEvent); | ||
var hasOptions = latest || every; | ||
if (hasOptions && events.length > 0) { | ||
if (latest) { | ||
var lastEvent = events.pop(); | ||
observer(lastEvent); | ||
} | ||
if (every) { | ||
observer(events); | ||
} | ||
this.events = events; | ||
@@ -70,0 +85,0 @@ } |
{ | ||
"name": "windowed-observable", | ||
"version": "0.2.3", | ||
"version": "0.3.0", | ||
"description": "Simple messaging api", | ||
@@ -11,2 +11,3 @@ "main": "./dist/index.js", | ||
"scripts": { | ||
"test": "jest", | ||
"build": "tsc", | ||
@@ -26,4 +27,20 @@ "prepare": "yarn run build" | ||
"devDependencies": { | ||
"@types/jest": "^24.0.24", | ||
"jest": "^24.9.0", | ||
"ts-jest": "^24.2.0", | ||
"typescript": "^3.7.2" | ||
}, | ||
"jest": { | ||
"transform": { | ||
"^.+\\.ts?$": "ts-jest" | ||
}, | ||
"moduleFileExtensions": [ | ||
"ts", | ||
"js" | ||
], | ||
"testPathIgnorePatterns": [ | ||
"dist", | ||
"node_modules" | ||
] | ||
} | ||
} |
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
11936
13
217
4