npm.io
3.1.0 • Published 4 months ago

abstract-object

Licence
MIT
Version
3.1.0
Deps
4
Size
49 kB
Vulns
0
Weekly
0
Stars
2

AbtractObject Build Status npm downloads license

AbstractObject with Object State Supports and free method provides.

The derived class should overwrite the initialize and finalize methods.

State-able Ability

add the Object State Supports and free method to your class directly.

You must call the _constructor of the state-able ability in the constructor method.

import {stateable} from 'abstract-object'

export class MyStateObject {
  constructor() {
    this._constructor.apply(this, arguments)
  }
}
stateable(MyStateObject)

export default MyStateObject

Let state-able object supports the event.

import {stateable} from 'abstract-object'
import {eventable} from 'events-ex'

class MyObject {
  constructor() {
    this._constructor.apply(this, arguments)
  }
}
stateable(MyObject)
eventable(MyObject)

AbstractObject

  • Methods:

    • create(class, ...): the create class method uses to create a new object instance(the util.createObject is the same function).
      • class: the class constructor to create a new instance.
      • ...: the left arguments will be passed into the class constructor.
    • createWith(class, arguments): the createWith class method uses to create a new object instance(the util.createObjectWith is the same function).
      • class: the class constructor to create a new instance.
      • arguments (array): the arguments will be passed into the class constructor.
    • initialize(...): abstract initialization method after a new instance creating.
      • ...: the constructor's arguments should be passed into initialize method.
    • finalize(...): abstract finalization method before the instance destroying.
      • ...: the free(destroy)'s arguments should be passed into finalize method.
    • (deprecated) init(...): abstract initialization method after a new instance creating.
      • init method is deprecated, pls use initialize method instead
      • ...: the constructor's arguments should be passed into init method.
    • (deprecated) final(...): abstract finalization method before the instance destroying.
      • final method is deprecated, pls use finalize instead
      • ...: the free(destroy)'s arguments should be passed into final method.
    • free(...): free the class instance.
      • ...: optional arguments will be passed into final method to process.
    • isIniting(), isInited(),isDestroying(), isDestroyed() object state testing methods:
      • to test object state
  • only added/injected eventable ability:

    • Methods:
      • dispatch(event, args[, callback]): dispath an event or callback
        • event: the event name
        • args: the args are passed to event or callback
        • callback: optional, it will not dispatch event if the callback is exists, unless the callback return false.
      • dispatchError(error[, callback]):
        • error: the error instance.
        • callback: optional, it will not dispatch 'error' event if the callback is exists, unless the callback return false.
    • Events:
      • 'initing': emit before the initialize method
      • 'inited': emit after the initialize method
      • 'destroying': emit before the finalize method
      • 'destroyed': emit after the finalize method

Usage

import {AbstractObject} from 'abstract-object'

class MyObject extends AbstractObject {
  initialize(a, b) {
    this.a = a
    this.b = b
    this.cache = {}
  }
  finalize() {
    this.cache = null
  }
}
const myObj = new MyObject(1, 2)

RefObject

RefObject has been moved to ref-object

The RefObject is derived from AbstractObject. and add the RefCount and AddRef/Release Supports.

  • methods:
    • release()/free(): Decrements reference count for this instance. If it is becoming less than 0, the object would be (self) destroyed.
    • addRef(): Increments the reference count for this instance and returns the new reference count.

AbstractError Classes

It has been moved to abstract-error.

Changes

V3.x
  • broken change Remove deprecated ref-object.js , RefObject.js and eventable-ref-object.js files
  • broken change Remove deprecated util to util-ex package.
  • broken change Remove deprecated Error to abstract-error package.
V2.1.x
  • add the state-able ability to any class.
  • decoupled the abstract-object completely.
  • All parts can be used individually.
    • stateable = require('abstract-object/ability')
    • eventable = require('events-ex/eventable')
    • refCountable = require('ref-object/ability')
V2.x
  • separate eventable from AbstractObject
  • separate eventable from RefObject too
  • add the eventable function to eventable any class('abstract-object/eventable').
    • use the eventable plugin(events-ex) to implement eventable object.
V1.x
  • AbstractObject inherits from EventEmitter.
  • RefObject inherits from AbstractObject
  • AbstractError

Keywords