memory-orm
Version:
client side ORM + map reduce
164 lines (163 loc) • 4.05 kB
JavaScript
Object.defineProperty(exports, '__esModule', { value: true })
exports.State = exports.step = exports.Metadata = exports.PureObject = exports.Rule = void 0
const tslib_1 = require('tslib')
const debounce_1 = tslib_1.__importDefault(require('lodash/debounce'))
const datum_1 = require('./datum')
const userdata_1 = require('./userdata')
var rule_1 = require('./rule')
Object.defineProperty(exports, 'Rule', {
enumerable: true,
get: function () {
return rule_1.Rule
},
})
let $react_listeners = []
let $step = 0
function PureObject() {
return Object.create(null)
}
exports.PureObject = PureObject
class Metadata {
static bless(o) {
Reflect.setPrototypeOf(o, this.prototype)
o.write_at = 0
o.depth = 0
o.pack || (o.pack = PureObject())
return o
}
json() {
return JSON.stringify(this, (key, val) => {
if (val && val.meta && val.item && val.$group && val.meta == this) {
const { item, $group } = val
return { item, $group }
} else {
return val
}
})
}
}
exports.Metadata = Metadata
function META(meta = {}) {
Metadata.bless(meta)
return meta
}
function step() {
return ++$step
}
exports.step = step
function cache(type) {
return function (list) {
const o = exports.State[type].pack
if (!o[list]) {
const oo = (o[list] = PureObject())
oo.$sort = PureObject()
oo.$memory = PureObject()
oo.$format = PureObject()
}
return o[list]
}
}
class StateManager {
constructor() {
this.journal = cache('$journal')
this.base = cache('$base')
this.step = PureObject()
this.$journal = META()
this.$base = META()
this.notify_for_react = debounce_1.default(() => {
for (const o of $react_listeners) {
const e = {}
let changed = false
for (const key in o.state) {
if ('step_' != key.slice(0, 4)) {
continue
}
const list = key.slice(5)
const val = this.step[list]
if (o.state[key] < val) {
e[key] = val
changed = true
}
}
if (changed) {
o.setState(e)
}
}
}, 1)
const that = this
this.mixin = {
data() {
return { $step: that.step }
},
}
}
meta() {
return this.$journal
}
transaction(cb, meta) {
const pack = (this.$journal = META(meta))
if (pack.depth++) {
console.warn('nested transactions.')
}
cb(pack)
pack.depth--
this.$journal = META()
return pack
}
store(meta) {
if (!(meta === null || meta === void 0 ? void 0 : meta.pack)) return false
for (const list in meta.pack) {
const { $sort, $memory, $format } = meta.pack[list]
const finder = userdata_1.Finder[list]
if (!finder) {
console.error('not found Finder', list, userdata_1.Finder)
continue
}
const set = userdata_1.Set[finder.$name.base]
if (!set) {
console.error('not found Set', list, userdata_1.Set)
continue
}
const { model } = set
const base = this.base(list)
const journal = this.journal(list)
for (const key in $sort) {
const o = $sort[key]
base.$sort[key] = o
journal.$sort[key] = o
}
for (const key in $format) {
const o = $format[key]
base.$format[key] = o
journal.$format[key] = o
}
for (const key in $memory) {
const o = $memory[key]
datum_1.Datum.bless(o, meta, model)
base.$memory[key] = o
journal.$memory[key] = o
}
set.clear_cache()
}
return true
}
join({ react }) {
if (react) {
$react_listeners.push(react)
}
}
bye({ react }) {
if (react) {
$react_listeners = $react_listeners.filter((o) => o != react)
}
}
notify(list) {
this.step[list] = step()
if ($react_listeners.length) {
this.notify_for_react()
}
}
}
exports.State = new StateManager()
//# sourceMappingURL=mem.js.map