New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

easy-observer

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

easy-observer - npm Package Compare versions

Comparing version 0.3.0 to 0.4.0

.eslintrc.js

2

index.js

@@ -1,4 +0,4 @@

"use strict";
'use strict';
export { observeValue } from './libs/observe-value';
export { observeFn } from './libs/observe-function';
export { observable } from './libs/observable';

@@ -1,2 +0,2 @@

"use strict";
'use strict';

@@ -3,0 +3,0 @@ const key = '___observers___';

@@ -1,2 +0,2 @@

"use strict";
'use strict';
import { observeValue } from './observe-value';

@@ -3,0 +3,0 @@

@@ -1,13 +0,37 @@

"use strict";
'use strict';
import { CombineObserver } from './observers/combine-observer';
let currentFn = null;
let currentInstance = null;
export const collectObserver = (observer) => {
if (!currentFn) return;
observer.register(currentFn);
class FunctionObserver {
fn;
combinedObserver = new CombineObserver();
constructor(fn) {
this.fn = fn;
this.observe = ::this.observe;
this.combinedObserver.register(this.observe);
this.observe();
}
observe() {
this.clearObservers();
currentInstance = this;
this.fn();
currentInstance = null;
}
add(observer) {
this.combinedObserver.add(observer);
}
clearObservers() {
if (this.combinedObserver) this.combinedObserver.unobserve();
}
}
export const collectObserver = observer => {
if (currentInstance) currentInstance.add(observer);
};
export const observeFn = fn => {
currentFn = fn;
fn();
currentFn = null;
};
export const observeFn = fn => new FunctionObserver(fn);

@@ -1,2 +0,2 @@

"use strict";
'use strict';
import { getObserver } from './get-observer';

@@ -3,0 +3,0 @@ import { ValueObserver } from './observers/value-observer';

@@ -1,3 +0,5 @@

"use strict";
'use strict';
import { collectObserver } from '../observe-function';
import { unqiue } from './id';
import { bindFn, bindThis } from './function-bind';

@@ -10,4 +12,9 @@ const defaultDescriptor = {

@bindFn
export class ValueObserver {
@unqiue id;
descriptor;
listeners = [];
static from(target, property) {

@@ -18,5 +25,2 @@ const descriptor = Object.getOwnPropertyDescriptor(target, property);

descriptor;
listeners = [];
constructor(descriptor) {

@@ -33,2 +37,3 @@ this.setDescriptor(descriptor);

@bindThis
getter() {

@@ -39,2 +44,3 @@ collectObserver(this);

@bindThis
setter(value) {

@@ -53,2 +59,7 @@ if (value === this.value) return;

unregister(listener) {
const index = this.listeners.indexOf(listener);
this.listeners.splice(index, 1);
}
get propertyDescriptor() {

@@ -55,0 +66,0 @@ return {

{
"name": "easy-observer",
"version": "0.3.0",
"version": "0.4.0",
"description": "A single observer",
"main": "index.js",
"scripts": {
"test": "mocha --compilers js:babel-register"
"test": "npm run lint && mocha --compilers js:babel-register",
"lint": "eslint ./**/*.js --fix"
},

@@ -26,3 +27,6 @@ "keywords": [

"babel-cli": "^6.10.1",
"babel-eslint": "7",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-plugin-transform-decorators": "^6.24.1",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-plugin-transform-function-bind": "^6.22.0",

@@ -33,5 +37,9 @@ "babel-preset-es2015": "^6.9.0",

"chai": "^3.5.0",
"eslint": "3.x",
"eslint-plugin-jasmine": "^2.6.2",
"mocha": "^2.5.3"
},
"dependencies": {}
"dependencies": {
"obj-id": "^0.5.1"
}
}

@@ -53,3 +53,3 @@ ## Easy Observer

```
```bash
npm install

@@ -56,0 +56,0 @@

@@ -1,2 +0,2 @@

"use strict";
'use strict';
import { expect } from 'chai';

@@ -29,2 +29,23 @@ import { observeFn, observable } from '../index';

it('should not trigger when the oberved object is replaced', () => {
const a = observable({ name: 'first', age: 2 });
const b = observable({ target: a });
let count = 0;
observeFn(() => {
b.target.name;
count++;
});
a.name = 'hello';
expect(count).to.equal(2);
b.target = {};
expect(count).to.equal(3);
a.name = 'world';
expect(count).to.equal(3);
});
});

@@ -1,3 +0,2 @@

"use strict";
'use strict';
import { observeValue } from '../index';

@@ -27,2 +26,11 @@ import { expect } from 'chai';

it('should not trigger listener when remove it', () => {
let count = 0;
const listener = () => count++;
const observer = observeValue(item, 'name', listener);
item.name = name;
observer.unregister(listener);
expect(count).to.equal(1);
});
it('should save the previous anwser when observeValue a property', (done) => {

@@ -51,3 +59,2 @@ let person = { name: 'Hello' };

it('should throw an exception when observer a un-configurable property.', () => {

@@ -58,3 +65,2 @@ Object.defineProperty(item, 'name', {

});
expect(() => observeValue(item, 'name', emptyListener))

@@ -71,3 +77,2 @@ .to.throw('Easy-observer can not observeValue a un-configurable property.');

});
expect(() => observeValue(item, 'name', emptyListener))

@@ -82,3 +87,3 @@ .to.throw('Easy-observer can not observeValue a un-writable property.');

observeValue(item, 'name', (previous, current) => {
observeValue(item, 'name', (previous) => {
firstDefer.resolve(previous);

@@ -85,0 +90,0 @@ });

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc