@blocksuite/store
Advanced tools
Comparing version
import { Disposable } from './disposable.js'; | ||
export declare class Signal<T = void> implements Disposable { | ||
private emitting; | ||
private callbacks; | ||
private disposables; | ||
private _emitting; | ||
private _callbacks; | ||
private _disposables; | ||
static fromEvent<N extends keyof WindowEventMap>(element: Window, eventName: N, options?: boolean | AddEventListenerOptions): Signal<WindowEventMap[N]>; | ||
@@ -7,0 +7,0 @@ static fromEvent<N extends keyof HTMLElementEventMap>(element: HTMLElement, eventName: N, eventOptions?: boolean | AddEventListenerOptions): Signal<HTMLElementEventMap[N]>; |
@@ -6,5 +6,5 @@ import { flattenDisposable } from './disposable.js'; | ||
constructor() { | ||
this.emitting = false; | ||
this.callbacks = []; | ||
this.disposables = []; | ||
this._emitting = false; | ||
this._callbacks = []; | ||
this._disposables = []; | ||
} | ||
@@ -17,3 +17,3 @@ static fromEvent(element, eventName, eventOptions) { | ||
element.addEventListener(eventName, handler, eventOptions); | ||
signal.disposables.push({ | ||
signal._disposables.push({ | ||
dispose: () => { | ||
@@ -28,3 +28,3 @@ element.removeEventListener(eventName, handler); | ||
// if result is disposed, dispose this too | ||
result.disposables.push({ dispose: () => this.dispose() }); | ||
result._disposables.push({ dispose: () => this.dispose() }); | ||
this.on((v) => { | ||
@@ -38,18 +38,18 @@ if (testFun(v)) { | ||
on(callback) { | ||
if (this.emitting) { | ||
const newCallback = [...this.callbacks, callback]; | ||
this.callbacks = newCallback; | ||
if (this._emitting) { | ||
const newCallback = [...this._callbacks, callback]; | ||
this._callbacks = newCallback; | ||
} | ||
else { | ||
this.callbacks.push(callback); | ||
this._callbacks.push(callback); | ||
} | ||
return { | ||
dispose: () => { | ||
if (this.emitting) { | ||
this.callbacks = this.callbacks.filter(v => v !== callback); | ||
if (this._emitting) { | ||
this._callbacks = this._callbacks.filter(v => v !== callback); | ||
} | ||
else { | ||
const index = this.callbacks.indexOf(callback); | ||
const index = this._callbacks.indexOf(callback); | ||
if (index > -1) { | ||
this.callbacks.splice(index, 1); // remove one item only | ||
this._callbacks.splice(index, 1); // remove one item only | ||
} | ||
@@ -71,18 +71,18 @@ } | ||
unshift(callback) { | ||
if (this.emitting) { | ||
const newCallback = [callback, ...this.callbacks]; | ||
this.callbacks = newCallback; | ||
if (this._emitting) { | ||
const newCallback = [callback, ...this._callbacks]; | ||
this._callbacks = newCallback; | ||
} | ||
else { | ||
this.callbacks.unshift(callback); | ||
this._callbacks.unshift(callback); | ||
} | ||
return { | ||
dispose: () => { | ||
if (this.emitting) { | ||
this.callbacks = this.callbacks.filter(v => v !== callback); | ||
if (this._emitting) { | ||
this._callbacks = this._callbacks.filter(v => v !== callback); | ||
} | ||
else { | ||
const index = this.callbacks.indexOf(callback); | ||
const index = this._callbacks.indexOf(callback); | ||
if (index > -1) { | ||
this.callbacks.splice(index, 1); // remove one item only | ||
this._callbacks.splice(index, 1); // remove one item only | ||
} | ||
@@ -94,5 +94,5 @@ } | ||
emit(v) { | ||
const prevEmitting = this.emitting; | ||
this.emitting = true; | ||
this.callbacks.forEach(f => { | ||
const prevEmitting = this._emitting; | ||
this._emitting = true; | ||
this._callbacks.forEach(f => { | ||
try { | ||
@@ -105,11 +105,11 @@ f(v); | ||
}); | ||
this.emitting = prevEmitting; | ||
this._emitting = prevEmitting; | ||
} | ||
pipe(that) { | ||
this.callbacks.push(v => that.emit(v)); | ||
this._callbacks.push(v => that.emit(v)); | ||
return this; | ||
} | ||
dispose() { | ||
flattenDisposable(this.disposables).dispose(); | ||
this.callbacks.length = 0; | ||
flattenDisposable(this._disposables).dispose(); | ||
this._callbacks.length = 0; | ||
} | ||
@@ -116,0 +116,0 @@ toDispose(disposables) { |
{ | ||
"name": "@blocksuite/store", | ||
"version": "0.3.0-20221225075400-9710eea", | ||
"version": "0.3.0-20221225111525-1d208f1", | ||
"description": "BlockSuite data store built for general purpose state management.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -6,5 +6,5 @@ import { Disposable, flattenDisposable } from './disposable.js'; | ||
export class Signal<T = void> implements Disposable { | ||
private emitting = false; | ||
private callbacks: ((v: T) => unknown)[] = []; | ||
private disposables: Disposable[] = []; | ||
private _emitting = false; | ||
private _callbacks: ((v: T) => unknown)[] = []; | ||
private _disposables: Disposable[] = []; | ||
static fromEvent<N extends keyof WindowEventMap>( | ||
@@ -30,3 +30,3 @@ element: Window, | ||
(element as HTMLElement).addEventListener(eventName, handler, eventOptions); | ||
signal.disposables.push({ | ||
signal._disposables.push({ | ||
dispose: () => { | ||
@@ -42,3 +42,3 @@ (element as HTMLElement).removeEventListener(eventName, handler); | ||
// if result is disposed, dispose this too | ||
result.disposables.push({ dispose: () => this.dispose() }); | ||
result._disposables.push({ dispose: () => this.dispose() }); | ||
@@ -55,16 +55,16 @@ this.on((v: T) => { | ||
on(callback: (v: T) => unknown): Disposable { | ||
if (this.emitting) { | ||
const newCallback = [...this.callbacks, callback]; | ||
this.callbacks = newCallback; | ||
if (this._emitting) { | ||
const newCallback = [...this._callbacks, callback]; | ||
this._callbacks = newCallback; | ||
} else { | ||
this.callbacks.push(callback); | ||
this._callbacks.push(callback); | ||
} | ||
return { | ||
dispose: () => { | ||
if (this.emitting) { | ||
this.callbacks = this.callbacks.filter(v => v !== callback); | ||
if (this._emitting) { | ||
this._callbacks = this._callbacks.filter(v => v !== callback); | ||
} else { | ||
const index = this.callbacks.indexOf(callback); | ||
const index = this._callbacks.indexOf(callback); | ||
if (index > -1) { | ||
this.callbacks.splice(index, 1); // remove one item only | ||
this._callbacks.splice(index, 1); // remove one item only | ||
} | ||
@@ -88,16 +88,16 @@ } | ||
unshift(callback: (v: T) => unknown): Disposable { | ||
if (this.emitting) { | ||
const newCallback = [callback, ...this.callbacks]; | ||
this.callbacks = newCallback; | ||
if (this._emitting) { | ||
const newCallback = [callback, ...this._callbacks]; | ||
this._callbacks = newCallback; | ||
} else { | ||
this.callbacks.unshift(callback); | ||
this._callbacks.unshift(callback); | ||
} | ||
return { | ||
dispose: () => { | ||
if (this.emitting) { | ||
this.callbacks = this.callbacks.filter(v => v !== callback); | ||
if (this._emitting) { | ||
this._callbacks = this._callbacks.filter(v => v !== callback); | ||
} else { | ||
const index = this.callbacks.indexOf(callback); | ||
const index = this._callbacks.indexOf(callback); | ||
if (index > -1) { | ||
this.callbacks.splice(index, 1); // remove one item only | ||
this._callbacks.splice(index, 1); // remove one item only | ||
} | ||
@@ -110,5 +110,5 @@ } | ||
emit(v: T) { | ||
const prevEmitting = this.emitting; | ||
this.emitting = true; | ||
this.callbacks.forEach(f => { | ||
const prevEmitting = this._emitting; | ||
this._emitting = true; | ||
this._callbacks.forEach(f => { | ||
try { | ||
@@ -120,7 +120,7 @@ f(v); | ||
}); | ||
this.emitting = prevEmitting; | ||
this._emitting = prevEmitting; | ||
} | ||
pipe(that: Signal<T>): Signal<T> { | ||
this.callbacks.push(v => that.emit(v)); | ||
this._callbacks.push(v => that.emit(v)); | ||
return this; | ||
@@ -130,4 +130,4 @@ } | ||
dispose() { | ||
flattenDisposable(this.disposables).dispose(); | ||
this.callbacks.length = 0; | ||
flattenDisposable(this._disposables).dispose(); | ||
this._callbacks.length = 0; | ||
} | ||
@@ -134,0 +134,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
406252
0.02%