2.0.1 • Published 3 years ago

@enbock/state-value-observer v2.0.1

Weekly downloads
2
License
MIT
Repository
-
Last release
3 years ago

State Value Observer

Testing Publishing Scrutinizer Code Quality Code Coverage Build Status

The State Value Observer is an atomic implementation of the value observation pattern.

Feature

  • Implemented in dependency injection methodology.
  • Provide setter and getter functions of an observed value.
  • Provide interfaces for callbacks and callback adapters.
  • Provide an listener adapter.

Usage

const adapter: IObserverAdapter<string> = {
  onChange: function (newValue: string): void {
    console.log('Observed value was changed to:', newValue);
  }
};
const observer: Observer<string> = new Observer<string>('initial value', adapter);

observer.value = 'Hello World!';

Usage of Listener Adapter

const adapter: ListenerAdapter<string> = new ListenerAdapter<string>();
const observer: Observer<string> = new Observer<string>('initial value', adapter);

function valueConsumer(newValue: string): void {
  console.log('Observed value was changed to:', newValue);
}

adapter.addListener(valueConsumer.bind(window));

observer.value = 'Hello World!';

Listener Adapter in synchronous mode

const adapter: ListenerAdapter<string> = new ListenerAdapter<string>(false);
const observer: Observer<string> = new Observer<string>('initial value', adapter);

function valueConsumer(newValue: string): void {
  console.log('Observed value was changed to:', newValue);
}

adapter.addListener(valueConsumer.bind(window));

observer.value = 'Hello World!';

Testing

Using this library in your project

This library is providing in ECMAScript® 2020 language. When you use jest, you get this error by using my library:

  Details:
  
  <YOUR_PATH>\node_modules\@enbock\state-value-observer\ListenerAdapter.js:1
  export default class ListenerAdapter {
  ^^^^^^
  
  SyntaxError: Unexpected token 'export'
      at compileFunction (vm.js:341:18)

See more: https://jestjs.io/docs/en/tutorial-react-native#transformignorepatterns-customization

Reason and solution

Jest running internally on ES5, that does not know the ES6-imports.

Force converting ES6+ Libraries

To solve this, you have to exclude all my libraries from the exclusion-list:

"transformIgnorePatterns": [
  "/node_modules/(?!(@enbock)/)"
]
Let babel "learn" ES6+

babel.config.js

module.exports = {
  presets: [
    ['@babel/preset-env', {targets: {node: 'current'}}],
    '@babel/preset-typescript'
  ]
};

See more: https://github.com/facebook/jest#using-typescript

Run tests

yarn test

Building

yarn build
2.0.1

3 years ago

2.0.0

4 years ago

1.2.0

4 years ago

1.1.5

4 years ago

1.1.4

4 years ago

1.1.3

4 years ago

1.1.1

4 years ago

1.1.2

4 years ago

1.1.0

4 years ago

1.0.1-dev.1

4 years ago

1.0.1-dev.2

4 years ago

1.0.0

4 years ago

0.1.8

4 years ago

0.1.9

4 years ago

0.1.7

4 years ago

0.1.6

4 years ago

0.1.5

4 years ago

0.1.2

4 years ago

0.1.4

4 years ago

0.1.3

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago