Comparing version 1.2.2 to 1.3.0
@@ -7,1 +7,3 @@ import { EvtLike, Evt, VoidEvt } from "./Evt"; | ||
export * from "./util"; | ||
import * as inDepth from "../tools/inDepth"; | ||
export { inDepth }; |
@@ -16,2 +16,4 @@ "use strict"; | ||
__export(require("./util")); | ||
var inDepth = require("../tools/inDepth"); | ||
exports.inDepth = inDepth; | ||
//# sourceMappingURL=index.js.map |
@@ -11,12 +11,12 @@ import { Evt } from "./Evt"; | ||
export interface IObservable<T> { | ||
readonly value: T; | ||
readonly val: T; | ||
/** when value changed post the new value */ | ||
readonly evt: NonPostable<Evt<T>>; | ||
/** when value changed post the new value and the value it previously replaced */ | ||
readonly evtChangeDiff: NonPostable<Evt<IObservable.ChangeDiff<T>>>; | ||
/** when value changed post the new value */ | ||
readonly evtChange: NonPostable<Evt<T>>; | ||
readonly evtDiff: NonPostable<Evt<IObservable.Diff<T>>>; | ||
} | ||
export declare namespace IObservable { | ||
type ChangeDiff<T> = { | ||
newValue: T; | ||
previousValue: T; | ||
type Diff<T> = { | ||
currVal: T; | ||
prevVal: T; | ||
}; | ||
@@ -26,12 +26,13 @@ } | ||
export declare class Observable<T> implements IObservable<T> { | ||
private readonly areSame; | ||
private readonly same; | ||
/*** https://docs.evt.land/api/observable#observable-from */ | ||
static readonly from: typeof from; | ||
private readonly evtChangeDiff_post; | ||
readonly evtChangeDiff: IObservable<T>["evtChangeDiff"]; | ||
readonly evtChange: IObservable<T>["evtChange"]; | ||
readonly value: T; | ||
constructor(initialValue: T, areSame?: (currentValue: T, newValue: T) => boolean); | ||
readonly evtDiff: IObservable<T>["evtDiff"]; | ||
readonly evt: IObservable<T>["evt"]; | ||
readonly val: T; | ||
private setVal; | ||
constructor(initialValue: T, same?: (currentValue: T, newValue: T) => boolean); | ||
/** Return true if the value have been changed */ | ||
onPotentialChange(newValue: T): boolean; | ||
update(val: T): boolean; | ||
} |
@@ -7,27 +7,30 @@ "use strict"; | ||
var observableFrom_1 = require("./util/observableFrom"); | ||
var inDepth = require("../tools/inDepth"); | ||
; | ||
/** https://docs.evt.land/api/observable */ | ||
var Observable = /** @class */ (function () { | ||
function Observable(initialValue, areSame) { | ||
if (areSame === void 0) { areSame = function (currentValue, newValue) { return currentValue === newValue; }; } | ||
this.areSame = areSame; | ||
function Observable(initialValue, same) { | ||
if (same === void 0) { same = inDepth.same; } | ||
this.same = same; | ||
{ | ||
var evtChangeDiff_1 = new Evt_2.Evt(); | ||
this.evtChangeDiff_post = function (changeDiff) { return evtChangeDiff_1.post(changeDiff); }; | ||
this.evtChange = evtChangeDiff_1.pipe(function (_a) { | ||
var newValue = _a.newValue; | ||
return [newValue]; | ||
this.evt = evtChangeDiff_1.pipe(function (_a) { | ||
var currVal = _a.currVal; | ||
return [currVal]; | ||
}); | ||
this.evtChangeDiff = evtChangeDiff_1; | ||
this.evtDiff = evtChangeDiff_1; | ||
} | ||
overwriteReadonlyProp_1.overwriteReadonlyProp(this, "value", initialValue); | ||
this.setVal(initialValue); | ||
} | ||
Observable.prototype.setVal = function (val) { | ||
return overwriteReadonlyProp_1.overwriteReadonlyProp(this, "val", inDepth.copy(val, { "freeze": true })); | ||
}; | ||
/** Return true if the value have been changed */ | ||
Observable.prototype.onPotentialChange = function (newValue) { | ||
if (this.areSame(this.value, newValue)) { | ||
Observable.prototype.update = function (val) { | ||
if (this.same(this.val, val)) { | ||
return false; | ||
} | ||
var previousValue = this.value; | ||
overwriteReadonlyProp_1.overwriteReadonlyProp(this, "value", newValue); | ||
this.evtChangeDiff_post({ previousValue: previousValue, newValue: newValue }); | ||
var prevVal = this.val; | ||
this.evtChangeDiff_post({ prevVal: prevVal, "currVal": this.setVal(val) }); | ||
return true; | ||
@@ -34,0 +37,0 @@ }; |
@@ -7,4 +7,4 @@ declare type EvtLike<T> = { | ||
declare type ObservableLike<T> = { | ||
value: T; | ||
evtChange: { | ||
val: T; | ||
evt: { | ||
attach(ctx: CtxLike<any>, callback: (data: T) => void): void; | ||
@@ -11,0 +11,0 @@ attach(callback: (data: T) => void): void; |
@@ -8,3 +8,3 @@ "use strict"; | ||
var obs = new importProxy_1.importProxy.Observable(initialValue, areSame); | ||
evt.attach(function (data) { return obs.onPotentialChange(data); }); | ||
evt.attach(function (data) { return obs.update(data); }); | ||
return obs; | ||
@@ -18,9 +18,9 @@ } | ||
if (!!ctx) { | ||
obs.evtChange.attach(ctx, callback); | ||
obs.evt.attach(ctx, callback); | ||
} | ||
else { | ||
obs.evtChange.attach(callback); | ||
obs.evt.attach(callback); | ||
} | ||
} | ||
return fromEvtImpl(evtDelegate, transform(obs.value), areSame); | ||
return fromEvtImpl(evtDelegate, transform(obs.val), areSame); | ||
} | ||
@@ -27,0 +27,0 @@ function from(p1, p2, p3, p4) { |
export declare const overwriteReadonlyProp: <T extends { | ||
[key: string]: any; | ||
}, K extends keyof T>(obj: T, propertyName: K, value: T[K]) => void; | ||
}, K extends keyof T>(obj: T, propertyName: K, value: T[K]) => T[K]; |
@@ -18,3 +18,3 @@ "use strict"; | ||
if (obj[propertyName] === value) { | ||
return; | ||
return value; | ||
} | ||
@@ -25,3 +25,4 @@ } | ||
Object.defineProperty(obj, propertyName, __assign(__assign({}, Object.getOwnPropertyDescriptor(obj, propertyName)), { value: value })); | ||
return value; | ||
}; | ||
//# sourceMappingURL=overwriteReadonlyProp.js.map |
@@ -28,4 +28,3 @@ "use strict"; | ||
var partition_1 = require("./partition"); | ||
var inDepthComparison_1 = require("../inDepthComparison"); | ||
var assertRepresentsSameData = inDepthComparison_1.assertRepresentsSameDataFactory({ "takeIntoAccountArraysOrdering": false }).assertRepresentsSameData; | ||
var same_1 = require("../inDepth/same"); | ||
if (typeof require !== "undefined" && | ||
@@ -117,11 +116,8 @@ typeof module !== "undefined" && | ||
typeSafety_1.assert((_5 = ["a", "b"]).reduce.apply(_5, __spread(index_1.sameAs(["a", "b", "c"]))) === false); | ||
assertRepresentsSameData({ | ||
"got": (_6 = ["bob", "alice"]).reduce.apply(_6, __spread(index_1.diff(["bob", "louis"]))), | ||
"expected": { | ||
"added": ["louis"], | ||
"removed": ["alice"] | ||
} | ||
}); | ||
typeSafety_1.assert(same_1.same((_6 = ["bob", "alice"]).reduce.apply(_6, __spread(index_1.diff(["bob", "louis"]))), { | ||
"added": ["louis"], | ||
"removed": ["alice"] | ||
}, { "takeIntoAccountArraysOrdering": false })); | ||
console.log("PASS"); | ||
} | ||
//# sourceMappingURL=test.js.map |
"use strict"; | ||
exports.__esModule = true; | ||
var typeSafety_1 = require("../typeSafety"); | ||
var inDepthComparison_1 = require("../inDepthComparison"); | ||
var same_1 = require("../inDepth/same"); | ||
function getPromiseAssertionApi(params) { | ||
var areEquals = params === undefined ? | ||
function (o1, o2) { return o1 === o2; } : | ||
inDepthComparison_1.representsSameDataFactory({ | ||
"takeIntoAccountArraysOrdering": params.takeIntoAccountArraysOrdering | ||
}).representsSameData; | ||
var is = !!params ? same_1.sameFactory(params).same : (function (o1, o2) { return o1 === o2; }); | ||
function mustResolve(params) { | ||
@@ -19,3 +15,3 @@ var _a; | ||
} | ||
typeSafety_1.assert(areEquals(data, params.expectedData), "Not equals expected value"); | ||
typeSafety_1.assert(is(data, params.expectedData), "Not equals expected value"); | ||
return data; | ||
@@ -31,3 +27,3 @@ }); | ||
if ("expectedRejectedValue" in params) { | ||
typeSafety_1.assert(areEquals(error, params.expectedRejectedValue)); | ||
typeSafety_1.assert(is(error, params.expectedRejectedValue)); | ||
} | ||
@@ -34,0 +30,0 @@ }); |
{ | ||
"name": "evt", | ||
"version": "1.2.2", | ||
"version": "1.3.0", | ||
"description": "Type safe replacement for node's EventEmitter", | ||
@@ -5,0 +5,0 @@ "repository": { |
@@ -6,7 +6,8 @@ | ||
export { CtxLike, Ctx, VoidCtxLike, VoidCtx} from "./Ctx"; | ||
export * from "./types"; | ||
export { IObservable, Observable } from "./Observable"; | ||
export * from "./util"; | ||
export * from "./util"; | ||
import * as inDepth from "../tools/inDepth"; | ||
export { inDepth }; |
@@ -6,3 +6,4 @@ | ||
import { importProxy } from "./importProxy"; | ||
import { from } from "./util/observableFrom"; | ||
import { from } from "./util/observableFrom"; | ||
import * as inDepth from "../tools/inDepth"; | ||
@@ -17,13 +18,13 @@ | ||
export interface IObservable<T> { | ||
readonly value: T; | ||
readonly val: T; | ||
/** when value changed post the new value */ | ||
readonly evt: NonPostable<Evt<T>>; | ||
/** when value changed post the new value and the value it previously replaced */ | ||
readonly evtChangeDiff: NonPostable<Evt<IObservable.ChangeDiff<T>>>; | ||
/** when value changed post the new value */ | ||
readonly evtChange: NonPostable<Evt<T>>; | ||
readonly evtDiff: NonPostable<Evt<IObservable.Diff<T>>>; | ||
}; | ||
export namespace IObservable { | ||
export type ChangeDiff<T> = { | ||
newValue: T; | ||
previousValue: T; | ||
export type Diff<T> = { | ||
currVal: T; | ||
prevVal: T; | ||
} | ||
@@ -38,15 +39,22 @@ } | ||
private readonly evtChangeDiff_post: (data: IObservable.ChangeDiff<T>) => void; | ||
private readonly evtChangeDiff_post: (data: IObservable.Diff<T>) => void; | ||
public readonly evtChangeDiff: IObservable<T>["evtChangeDiff"]; | ||
public readonly evtChange: IObservable<T>["evtChange"]; | ||
public readonly evtDiff: IObservable<T>["evtDiff"]; | ||
public readonly evt: IObservable<T>["evt"]; | ||
//NOTE: Not really readonly but we want to prevent user from setting the value | ||
//manually and we cant user accessor because we target es3. | ||
public readonly value!: T; | ||
public readonly val!: T; | ||
private setVal(val: T): T { | ||
return overwriteReadonlyProp( | ||
this, | ||
"val", | ||
inDepth.copy(val, { "freeze": true }) | ||
); | ||
} | ||
constructor( | ||
initialValue: T, | ||
private readonly areSame: (currentValue: T, newValue: T) => boolean = | ||
(currentValue, newValue) => currentValue === newValue | ||
private readonly same: (currentValue: T, newValue: T) => boolean = inDepth.same | ||
) { | ||
@@ -56,29 +64,28 @@ | ||
const evtChangeDiff: Evt<IObservable.ChangeDiff<T>> = new Evt(); | ||
const evtChangeDiff: Evt<IObservable.Diff<T>> = new Evt(); | ||
this.evtChangeDiff_post = changeDiff => evtChangeDiff.post(changeDiff); | ||
this.evtChange = evtChangeDiff.pipe(({ newValue }) => [newValue]); | ||
this.evt = evtChangeDiff.pipe(({ currVal }) => [currVal]); | ||
this.evtChangeDiff = evtChangeDiff; | ||
this.evtDiff = evtChangeDiff; | ||
} | ||
overwriteReadonlyProp(this, "value", initialValue); | ||
this.setVal(initialValue); | ||
} | ||
/** Return true if the value have been changed */ | ||
public onPotentialChange(newValue: T): boolean { | ||
public update(val: T): boolean { | ||
if (this.areSame(this.value, newValue)) { | ||
if (this.same(this.val, val)) { | ||
return false; | ||
} | ||
const previousValue = this.value; | ||
const prevVal = this.val; | ||
overwriteReadonlyProp(this, "value", newValue); | ||
this.evtChangeDiff_post({ prevVal, "currVal": this.setVal(val) }); | ||
this.evtChangeDiff_post({ previousValue, newValue }); | ||
return true; | ||
@@ -85,0 +92,0 @@ |
@@ -13,6 +13,6 @@ | ||
type ObservableLike<T> = { | ||
value: T; | ||
evtChange: { | ||
attach(ctx: CtxLike<any>, callback: (data: T) => void): void; | ||
attach(callback: (data: T) => void): void; | ||
val: T; | ||
evt: { | ||
attach(ctx: CtxLike<any>, callback: (data: T) => void): void; | ||
attach(callback: (data: T) => void): void; | ||
}; | ||
@@ -29,3 +29,3 @@ }; | ||
evt.attach((data: T) => obs.onPotentialChange(data)); | ||
evt.attach((data: T) => obs.update(data)); | ||
@@ -51,5 +51,5 @@ return obs; | ||
if (!!ctx) { | ||
obs.evtChange.attach(ctx, callback); | ||
obs.evt.attach(ctx, callback); | ||
} else { | ||
obs.evtChange.attach(callback); | ||
obs.evt.attach(callback); | ||
} | ||
@@ -61,3 +61,3 @@ | ||
evtDelegate, | ||
transform(obs.value), | ||
transform(obs.val), | ||
areSame | ||
@@ -64,0 +64,0 @@ ); |
@@ -6,3 +6,3 @@ | ||
value: T[K] | ||
): void => { | ||
): T[K] => { | ||
@@ -14,3 +14,3 @@ try { | ||
if (obj[propertyName] === value) { | ||
return; | ||
return value; | ||
} | ||
@@ -30,2 +30,4 @@ | ||
return value; | ||
}; |
import { assert } from "../typeSafety"; | ||
import {every, allEqualsTo, allEquals, removeDuplicates, partition, allUniq, and, count, includes, or, sameAs, diff} from "./index"; | ||
import { every, allEqualsTo, allEquals, removeDuplicates, partition, allUniq, and, count, includes, or, sameAs, diff } from "./index"; | ||
import { arrEvery } from "./every"; | ||
import { arrPartition } from "./partition"; | ||
import { assertRepresentsSameDataFactory } from "../inDepthComparison"; | ||
import { same } from "../inDepth/same"; | ||
const { assertRepresentsSameData } = assertRepresentsSameDataFactory({ "takeIntoAccountArraysOrdering": false }); | ||
if ( | ||
@@ -192,9 +190,12 @@ typeof require !== "undefined" && | ||
assertRepresentsSameData({ | ||
"got": ["bob", "alice"].reduce(...diff<string>(["bob", "louis"])), | ||
"expected": { | ||
"added": [ "louis" ], | ||
"removed": [ "alice" ] | ||
} | ||
}); | ||
assert( | ||
same( | ||
["bob", "alice"].reduce(...diff<string>(["bob", "louis"])), | ||
{ | ||
"added": ["louis"], | ||
"removed": ["alice"] | ||
}, | ||
{ "takeIntoAccountArraysOrdering": false } | ||
) | ||
); | ||
@@ -201,0 +202,0 @@ console.log("PASS"); |
import { assert } from "../typeSafety"; | ||
import { representsSameDataFactory } from "../inDepthComparison"; | ||
import { sameFactory } from "../inDepth/same"; | ||
@@ -9,11 +9,4 @@ export function getPromiseAssertionApi( | ||
const areEquals: <T>(o1: T, o2: T) => boolean = | ||
params === undefined ? | ||
(o1, o2) => o1 === o2 : | ||
representsSameDataFactory({ | ||
"takeIntoAccountArraysOrdering": params.takeIntoAccountArraysOrdering | ||
}).representsSameData | ||
; | ||
const is = !!params ? sameFactory(params).same : (<T>(o1: T, o2: T) => o1 === o2); | ||
function mustResolve<T>( | ||
@@ -34,3 +27,3 @@ params: { | ||
} | ||
assert(areEquals(data, params.expectedData), "Not equals expected value"); | ||
assert(is(data, params.expectedData), "Not equals expected value"); | ||
return data; | ||
@@ -61,3 +54,3 @@ }); | ||
assert( | ||
areEquals( | ||
is( | ||
error, | ||
@@ -64,0 +57,0 @@ params.expectedRejectedValue |
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
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
1894666
271
39216