1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var CacheItem_1 = require("./CacheItem");
|
4 | var cache_1 = require("./cache");
|
5 | exports.getCachedItem = function (uid, instance) {
|
6 | var currentNode = getCurrentNode(instance);
|
7 | return currentNode ? currentNode.items.get(String(uid)) : undefined;
|
8 | };
|
9 | exports.isOnCache = function (entity, instance) {
|
10 | var cachedItem = exports.getCachedItem(entity[cache_1.config.uidName], instance);
|
11 | return cachedItem && cachedItem.entity === entity;
|
12 | };
|
13 | exports.isOnFlushMap = function (entity, flushMap) {
|
14 | return !!flushMap.get(entity[cache_1.config.uid]);
|
15 | };
|
16 | exports.getItemFlushOrCached = function (uid, flushArgs) {
|
17 | if (uid) {
|
18 | var uuid = String(uid);
|
19 | var item = flushArgs.flushMap.get(uuid);
|
20 | if (!item) {
|
21 | item = exports.getCachedItem(uuid, flushArgs.instance);
|
22 | }
|
23 | if (item && Object.isFrozen(item)) {
|
24 | item = item.clone();
|
25 | }
|
26 | return item;
|
27 | }
|
28 | };
|
29 | function getCurrentNode(instance) {
|
30 | var currentNodeId = instance.thread.nodes[instance.thread.current];
|
31 | return currentNodeId >= 0 ? getRepoNode(currentNodeId, instance.repo) : undefined;
|
32 | }
|
33 | function getRepoNode(nodeId, repo) {
|
34 | return repo.get(nodeId);
|
35 | }
|
36 | exports.getCacheCurrentStack = function (instance) {
|
37 | var currentNode = getCurrentNode(instance);
|
38 | return currentNode ? currentNode.items : undefined;
|
39 | };
|
40 | exports.ensureItem = function (entity, flushArgs) {
|
41 | var itemUid = String(entity[cache_1.config.uidName]);
|
42 | var item = flushArgs.flushMap.get(itemUid);
|
43 | if (item) {
|
44 | return item;
|
45 | }
|
46 | var live = exports.getCachedItem(itemUid, flushArgs.instance);
|
47 | item = new CacheItem_1.default(entity, live);
|
48 | flushArgs.flushMap.set(itemUid, item);
|
49 | flushArgs.flushMap['__UPDATED__'] = true;
|
50 | return item;
|
51 | };
|
52 | exports.ensureOnFlushMap = function (entity, flushArgs) {
|
53 | var entityUid = String(entity[cache_1.config.uidName]);
|
54 | if (!flushArgs.flushMap.has(entityUid)) {
|
55 | exports.ensureItem(entity, flushArgs);
|
56 | }
|
57 | };
|