Skip to content

Custom Property Manager [v2]

Adrien Castex edited this page Apr 21, 2018 · 1 revision

A Property Manager is an object instantiated for each resource in order to:

  • list the properties of this resource
  • get a specific property of this resource
  • set a property to this resource
  • remove a property of this resource

This object must be serializable in order to be saved with the architecture of the state of the server.

Implementation

interface IPropertyManager
{
    setProperty(name : string, value : ResourcePropertyValue, attributes : PropertyAttributes, callback : SimpleCallback) : void
    getProperty(name : string, callback : Return2Callback<ResourcePropertyValue, PropertyAttributes>) : void
    removeProperty(name : string, callback : SimpleCallback) : void
    getProperties(callback : ReturnCallback<PropertyBag>, byCopy ?: boolean) : void
}

interface PropertyBag
{
    [name : string] : {
        value : ResourcePropertyValue
        attributes ?: PropertyAttributes
    }
}

LocalPropertyManager

You can instantiate a basic Property Manager called LocalPropertyManager. It will store the properties in memory (in an object variable called properties of type PropertyBag) and manage the list.

Here is its implementation (which can be used as an example):

class LocalPropertyManager implements IPropertyManager
{
    properties : PropertyBag = { };
    
    constructor(serializedData ?: any)
    {
        if(serializedData)
            for(const name in serializedData)
                this[name] = serializedData[name];
    }

    setProperty(name : string, value : ResourcePropertyValue, attributes : PropertyAttributes, callback : SimpleCallback) : void
    {
        this.properties[name] = {
            value,
            attributes
        };
        callback(null);
    }

    getProperty(name : string, callback : Return2Callback<ResourcePropertyValue, PropertyAttributes>) : void
    {
        const property = this.properties[name];
        callback(property ? null : Errors.PropertyNotFound, property.value, property.attributes);
    }

    removeProperty(name : string, callback : SimpleCallback) : void
    {
        delete this.properties[name];
        callback(null);
    }

    getProperties(callback : ReturnCallback<PropertyBag>, byCopy : boolean = false) : void
    {
        callback(null, byCopy ? this.properties : JSON.parse(JSON.stringify(this.properties)));
    }
}
Clone this wiki locally