forked from royaltm/node-zmq-raft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpersistence_base.js
88 lines (74 loc) · 2.58 KB
/
persistence_base.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
* Copyright (c) 2016 Rafał Michalski <[email protected]>
*/
"use strict";
/*
Implementations must implement update(data) method.
Implementations may implement close() method.
*/
const { defineConst } = require('../utils/helpers');
const ReadyEmitter = require('../common/readyemitter');
class PersistenceBase extends ReadyEmitter {
/**
* Creates new instance
*
* `initial` should contain all the state properties set to their intial state.
*
* Implementations must call super(initial) in the constructor.
* Implementations should read stored properties values from the persistent storage
* and assign them to this instance.
* Implementations should call this[Symbol.for('setReady')]() upon successfull initialization.
* Implementations should call this.error(err) upon error while initializing.
*
* @param {Object} initial
* @return this
**/
constructor(initial) {
super();
if (!initial || 'object' !== typeof initial) return this.error(new Error("persistence: initial argument must be an object"));
initial = Object.assign({}, initial);
defineConst(this, 'defaultData', Object.freeze(initial));
Object.assign(this, initial);
}
/**
* closes this instance
*
* Implementations should close all resources asynchronously and resolve returned promise.
*
* @return {Promise}
**/
close() {
/* no-op */
return Promise.resolve();
}
/**
* update state
*
* Implementations should update state asynchronously and resolve returned promise when done.
* Not all the state properties may be set on `properties` argument.
* Implementations should retain current properties when argument's property value is undefined.
*
* `properties[propertyName] === undefined`.
*
* The state must be updated ACID'ly: Atomicly, Consistently, in Isolation and Durably.
* Asynchronous updates must be applied in FIFO order:
* First in (updated) - first on "plate" - first resolved.
* Only after all the updated properties has been transerred to some persistent storage,
* the implementations must assign new values to this instance.
*
* @param {Object} properties
* @return {Promise}
**/
update(properties) {
throw new Error("persistence.update: implement in subclass");
/* example
var data = {};
Object.getOwnPropertyNames(properties).forEach(name => {
if (properties[name] !== undefined) data[name] = properties[name];
});
return transferToStorageInFifoOrder(data)
.then(() => Object.assign(this, data));
*/
}
}
module.exports = exports = PersistenceBase;