moose-decorators
TypeScript icon, indicating that this package has built-in type declarations

1.0.3 • Public • Published

MOOSE DECORATORS: A specific way to improve your application

Copyright 2024 Moose Software Inc. All rights reserved.

Moose Decorators is a project that provides decorators or annotations for JavaScript language. It contains many decorators are used to validate dto (data transfer object) from backend, improve your application such as ioc (invention of control) or caching...

Installing

For the latest stable version:

npm install moose-decorators

Usage

Cache:

Cache is used to cache returned values from methods such as getter or native methods, furthermore, it also is used to cache observable value of rxjs to improve your application because we don't need to re-call api methods that it always returns the constant value.

Without timeout:

import { Cache } from 'moose-decorators';

class Test {
  @Cache()
  get heroes(): string {
    const num = Math.random();
    return `id: ${num}, name: superman`;
  }
}

const test: Test = new Test();
const heroes1: string = test.heroes;
const heroes2: string = test.heroes;

expect(heroes1).toBe(heroes2);

With timeout:

import { Cache } from 'moose-decorators';

class Test {
  @Cache({
    timeout: 1000
  })
  get heroes(): string {
    const num = Math.random();
    return `id: ${num}, name: superman`;
  }
}

const test: Test = new Test();
const heroes1: string = test.heroes;
setTimeout((): void => {
  // Should be called when the timeout expires
  const heroes2: string = test.heroes;
  expect(heroes1).not.toBe(heroes2);
}, 2000);

Because JavaScript is a no-strict programming language, so you have to use a unique method name for every cached method, otherwise please use the id of the cache instead

import { Cache } from 'moose-decorators';

class Test1 {
  @Cache({
    id: 'test1#heroes'
  })
  get heroes(): string {
    const num = Math.random();
    return `id: ${num}, name: superman`;
  }
}

const test1: Test1 = new Test1();
const test1_heroes1: string = test1.heroes;
const test1_heroes2: string = test1.heroes;

expect(test1_heroes1).toBe(test1_heroes2);

class Test2 {
  @Cache({
    id: 'test2#heroes'
  })
  get heroes(): string {
    const num = Math.random();
    return `id: ${num}, name: superman`;
  }
}

const test2: Test2 = new Test2();
const test2_heroes1: string = test2.heroes;
const test2_heroes2: string = test2.heroes;

expect(test2_heroes1).toBe(test2_heroes2);

GLOBAL CONFIGURATION: add the moose.conf.js file and modify the content

module.exports = {
  cache: {
    timeout: 1000 // The default timeout in milliseconds of the cache
  }
};

Package Sidebar

Install

npm i moose-decorators

Weekly Downloads

5

Version

1.0.3

License

MIT

Unpacked Size

53.1 kB

Total Files

35

Last publish

Collaborators

  • doquocviet.sept3rd