blow-data
Version:
Data access layer for Blow.
57 lines (56 loc) • 1.61 kB
JavaScript
'use strict';
const rxjs_1 = require('rxjs');
exports.ConnectionStatus = {
NotConnected: 'NotConnected',
Connected: 'Connected'
};
class Connection {
constructor(options) {
this._status = new rxjs_1.BehaviorSubject(exports.ConnectionStatus.NotConnected);
this._options = options;
}
get name() {
return this._options.name;
}
get adapter() {
return this._adapter;
}
get status() {
return this._status;
}
get isConnected() {
return this._status.getValue() === exports.ConnectionStatus.Connected;
}
init() {
this._options.adapter
.init(this._options.params)
.subscribe(adapter => {
this._adapter = adapter;
this._status.next(exports.ConnectionStatus.Connected);
});
return rxjs_1.Observable.create(observer => {
this.status
.filter(status => status === exports.ConnectionStatus.Connected)
.subscribe(() => {
observer.next(this);
observer.complete();
});
});
}
attach(model) {
Object.defineProperty(model, '_connection', {
configurable: false,
enumerable: false,
writable: true,
value: this
});
model.metadata.buildPropertyId(this.adapter.idPropertyName, this.adapter.idPropertyType);
}
static init(options) {
return this.create(options).init();
}
static create(options) {
return new this(options);
}
}
exports.Connection = Connection;