@creately/lschannel-es6
Advanced tools
Comparing version 2.0.0 to 2.0.2
@@ -1,2 +0,2 @@ | ||
import { Subject } from 'rxjs/Rx'; | ||
import { Subject } from 'rxjs'; | ||
export declare class Channel<T> extends Subject<T> { | ||
@@ -6,6 +6,12 @@ key: string; | ||
static create<T>(key: string): Channel<T>; | ||
private static randomId; | ||
private prefix; | ||
private counter; | ||
private constructor(); | ||
close(): void; | ||
next(data: T): void; | ||
private emit(data); | ||
private randomId(); | ||
private init; | ||
private handler; | ||
private emit; | ||
private nextId; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const Rx_1 = require("rxjs/Rx"); | ||
const Rx_2 = require("rxjs/Rx"); | ||
class Channel extends Rx_2.Subject { | ||
const rxjs_1 = require("rxjs"); | ||
// Channel | ||
// Channel ... | ||
class Channel extends rxjs_1.Subject { | ||
// constructor | ||
@@ -10,6 +11,5 @@ constructor(key) { | ||
this.key = key; | ||
Rx_1.Observable.fromEvent(window, 'storage') | ||
.filter(e => e.key === this.key) | ||
.map(e => JSON.parse(e.newValue).data) | ||
.subscribe(data => this.emit(data)); | ||
this.prefix = Channel.randomId(16); | ||
this.counter = 0; | ||
this.handler = this.handler.bind(this); | ||
} | ||
@@ -26,4 +26,21 @@ // create | ||
this.instances.set(key, newChannel); | ||
newChannel.init(); | ||
return newChannel; | ||
} | ||
// randomId | ||
// randomId generates a reasonably random string id with given length. | ||
static randomId(n) { | ||
let id = ''; | ||
while (id.length < n) { | ||
id += Math.random() | ||
.toString(16) | ||
.slice(2); | ||
} | ||
return id.slice(0, n); | ||
} | ||
// close | ||
// close ... | ||
close() { | ||
window.removeEventListener('storage', this.handler); | ||
} | ||
// next | ||
@@ -33,3 +50,3 @@ // next sets the value on localStorage for given key which | ||
next(data) { | ||
const event = { id: this.randomId(), data }; | ||
const event = { id: this.nextId(), data }; | ||
const value = JSON.stringify(event); | ||
@@ -39,2 +56,16 @@ localStorage.setItem(this.key, value); | ||
} | ||
// init | ||
// init ... | ||
init() { | ||
window.addEventListener('storage', this.handler); | ||
} | ||
// handler | ||
// handler ... | ||
handler(e) { | ||
if (e.key !== this.key) { | ||
return; | ||
} | ||
const event = JSON.parse(e.newValue); | ||
this.emit(event.data); | ||
} | ||
// emit | ||
@@ -45,13 +76,6 @@ // emit emits the data to all subscribers in this window/tab. | ||
} | ||
// randomId | ||
// randomId creates a random string which will be used as event ids. | ||
randomId() { | ||
let id = ''; | ||
while (id.length < 16) { | ||
const r = Math.random() | ||
.toString(36) | ||
.slice(2); | ||
id += r.slice(0, 16 - id.length); | ||
} | ||
return id; | ||
// nextId | ||
// nextId ... | ||
nextId() { | ||
return this.prefix + ++this.counter; | ||
} | ||
@@ -58,0 +82,0 @@ } |
{ | ||
"name": "@creately/lschannel-es6", | ||
"version": "2.0.0", | ||
"version": "2.0.2", | ||
"description": "", | ||
@@ -24,20 +24,19 @@ "main": "dist/index.js", | ||
"devDependencies": { | ||
"@types/jasmine": "^2.8.6", | ||
"istanbul-instrumenter-loader": "^3.0.0", | ||
"jasmine-core": "^2.9.1", | ||
"karma": "^1.7.1", | ||
"@types/jasmine": "^2.8.8", | ||
"istanbul-instrumenter-loader": "^3.0.1", | ||
"jasmine-core": "^3.2.1", | ||
"karma": "^3.0.0", | ||
"karma-chrome-launcher": "^2.2.0", | ||
"karma-coverage-istanbul-reporter": "^1.4.1", | ||
"karma-jasmine": "^1.1.1", | ||
"karma-webpack": "^2.0.9", | ||
"prettier": "^1.10.2", | ||
"karma-coverage-istanbul-reporter": "^2.0.3", | ||
"karma-jasmine": "^1.1.2", | ||
"karma-webpack": "^3.0.2", | ||
"prettier": "^1.14.2", | ||
"rimraf": "^2.6.2", | ||
"ts-loader": "^2.3.7", | ||
"typescript": "^2.7.1", | ||
"webpack": "^3.10.0" | ||
"ts-loader": "^4.5.0", | ||
"typescript": "^3.0.3", | ||
"webpack": "^4.17.1" | ||
}, | ||
"dependencies": { | ||
"rxjs": "^6.0.0", | ||
"rxjs-compat": "^6.0.0" | ||
"rxjs": "^6.0.0" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
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
7642
1
95
6
- Removedrxjs-compat@^6.0.0
- Removedrxjs-compat@6.6.7(transitive)