@warp-drive/core 5.7.0-alpha.11 → 5.7.0-alpha.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/declarations/graph/-private/-utils.d.ts +1 -1
- package/declarations/graph/-private/graph.d.ts +2 -0
- package/declarations/reactive/-private/record.d.ts +1 -0
- package/declarations/store/-private/caches/identifier-cache.d.ts +1 -0
- package/declarations/store/-private/caches/instance-cache.d.ts +4 -2
- package/declarations/store/-private/store-service.d.ts +55 -12
- package/declarations/store/-private.d.ts +0 -2
- package/declarations/types/-private.d.ts +1 -1
- package/dist/graph/-private.js +10 -14
- package/dist/index.js +1 -2
- package/dist/reactive.js +10 -1916
- package/dist/{request-state-CeN66aML.js → request-state-BWYju5O9.js} +2604 -304
- package/dist/store/-private.js +1 -2
- package/dist/types/-private.js +1 -1
- package/package.json +3 -3
- package/declarations/store/-private/caches/cache-utils.d.ts +0 -12
- package/dist/handler-SdXlte1w.js +0 -339
|
@@ -1,12 +1,14 @@
|
|
|
1
|
-
import { withBrand, EnableHydration, SkipCache } from './types/request.js';
|
|
2
|
-
import { deprecate, warn } from '@ember/debug';
|
|
1
|
+
import { withBrand, EnableHydration, STRUCTURED, SkipCache } from './types/request.js';
|
|
3
2
|
import { macroCondition, getGlobalConfig, dependencySatisfies, importSync } from '@embroider/macros';
|
|
3
|
+
import { deprecate, warn } from '@ember/debug';
|
|
4
4
|
import { setLogging, getRuntimeConfig } from './types/runtime.js';
|
|
5
5
|
import { getOrSetGlobal, peekTransient, setTransient } from './types/-private.js';
|
|
6
6
|
import { CACHE_OWNER, DEBUG_STALE_CACHE_OWNER, DEBUG_IDENTIFIER_BUCKET, DEBUG_CLIENT_ORIGINATED } from './types/identifier.js';
|
|
7
7
|
import { dasherize } from './utils/string.js';
|
|
8
|
-
import { a as createSignal, b as consumeSignal, n as notifySignal, c as createMemo,
|
|
8
|
+
import { a as createSignal, b as consumeSignal, n as notifySignal, c as createMemo, A as ARRAY_SIGNAL, O as OBJECT_SIGNAL, d as willSyncFlushWatchers } from "./configure-B48bFHOl.js";
|
|
9
9
|
import { g as getPromiseResult, s as setPromiseResult } from "./context-COmAnXUQ.js";
|
|
10
|
+
import { RecordStore } from './types/symbols.js';
|
|
11
|
+
import { S as SOURCE$1, C as Context, D as Destroy, a as Checkout } from "./symbols-BoONANuz.js";
|
|
10
12
|
function coerceId(id) {
|
|
11
13
|
if (macroCondition(getGlobalConfig().WarpDrive.deprecations.DEPRECATE_NON_STRICT_ID)) {
|
|
12
14
|
let normalized;
|
|
@@ -1482,115 +1484,2166 @@ defineGate(ReactiveDocument.prototype, 'meta', {
|
|
|
1482
1484
|
return data.meta;
|
|
1483
1485
|
}
|
|
1484
1486
|
});
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
cache: ['white', 'white', 'inherit', 'magenta', 'inherit']
|
|
1492
|
-
};
|
|
1493
|
-
const BG_COLORS = {
|
|
1494
|
-
TEXT: 'transparent',
|
|
1495
|
-
notify: ['dimgray', 'cadetblue', 'transparent', 'transparent', 'transparent'],
|
|
1496
|
-
'reactive-ui': ['dimgray', 'cadetblue', 'transparent', 'transparent', 'transparent'],
|
|
1497
|
-
graph: ['dimgray', 'cadetblue', 'transparent', 'transparent', 'transparent'],
|
|
1498
|
-
request: ['dimgray', 'cadetblue', 'transparent', 'transparent', 'transparent'],
|
|
1499
|
-
cache: ['dimgray', 'cadetblue', 'transparent', 'transparent', 'transparent']
|
|
1500
|
-
};
|
|
1501
|
-
const NOTIFY_BORDER = {
|
|
1502
|
-
TEXT: 0,
|
|
1503
|
-
notify: [3, 2, 0, 0, 0],
|
|
1504
|
-
'reactive-ui': [3, 2, 0, 0, 0],
|
|
1505
|
-
graph: [3, 2, 0, 0, 0],
|
|
1506
|
-
request: [3, 2, 0, 0, 0],
|
|
1507
|
-
cache: [3, 2, 0, 0, 0]
|
|
1508
|
-
};
|
|
1509
|
-
const LIGHT_DARK_ALT = {
|
|
1510
|
-
lightgreen: 'green',
|
|
1511
|
-
green: 'lightgreen'
|
|
1512
|
-
};
|
|
1513
|
-
function badge(isLight, color, bgColor, border) {
|
|
1514
|
-
return [`color: ${correctColor(isLight, color)}; background-color: ${correctColor(isLight, bgColor)}; padding: ${border}px ${2 * border}px; border-radius: ${border}px;`, `color: ${TEXT_COLORS.TEXT}; background-color: ${BG_COLORS.TEXT};`];
|
|
1515
|
-
}
|
|
1516
|
-
function colorForBucket(isLight, scope, bucket) {
|
|
1517
|
-
if (scope === 'notify') {
|
|
1518
|
-
return bucket === 'added' ? badge(isLight, 'lightgreen', 'transparent', 0) : bucket === 'removed' ? badge(isLight, 'red', 'transparent', 0) : badge(isLight, TEXT_COLORS[scope][2], BG_COLORS[scope][2], NOTIFY_BORDER[scope][2]);
|
|
1519
|
-
}
|
|
1520
|
-
if (scope === 'reactive-ui') {
|
|
1521
|
-
return bucket === 'created' ? badge(isLight, 'lightgreen', 'transparent', 0) : bucket === 'disconnected' ? badge(isLight, 'red', 'transparent', 0) : badge(isLight, TEXT_COLORS[scope][2], BG_COLORS[scope][2], NOTIFY_BORDER[scope][2]);
|
|
1522
|
-
}
|
|
1523
|
-
if (scope === 'cache') {
|
|
1524
|
-
return bucket === 'inserted' ? badge(isLight, 'lightgreen', 'transparent', 0) : bucket === 'removed' ? badge(isLight, 'red', 'transparent', 0) : badge(isLight, TEXT_COLORS[scope][2], BG_COLORS[scope][2], NOTIFY_BORDER[scope][2]);
|
|
1525
|
-
}
|
|
1526
|
-
return badge(isLight, TEXT_COLORS[scope][3], BG_COLORS[scope][3], NOTIFY_BORDER[scope][3]);
|
|
1487
|
+
function getAliasField(context) {
|
|
1488
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
1489
|
+
{
|
|
1490
|
+
throw new Error(`Alias field access is not implemented`);
|
|
1491
|
+
}
|
|
1492
|
+
})() : {};
|
|
1527
1493
|
}
|
|
1528
|
-
function
|
|
1529
|
-
|
|
1530
|
-
|
|
1494
|
+
function setAliasField(context) {
|
|
1495
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
1496
|
+
{
|
|
1497
|
+
throw new Error(`Alias field setting is not implemented`);
|
|
1498
|
+
}
|
|
1499
|
+
})() : {};
|
|
1500
|
+
return false;
|
|
1531
1501
|
}
|
|
1532
|
-
function
|
|
1533
|
-
|
|
1534
|
-
console.log(..._log(scope, prefix, subScop1, subScop2, subScop3, subScop4));
|
|
1502
|
+
function isExtensionProp(extensions, prop) {
|
|
1503
|
+
return Boolean(extensions && typeof prop !== 'number' && extensions.has(prop));
|
|
1535
1504
|
}
|
|
1536
|
-
function
|
|
1537
|
-
|
|
1538
|
-
|
|
1505
|
+
function performObjectExtensionGet(receiver, extensions, signals, prop) {
|
|
1506
|
+
const desc = extensions.get(prop);
|
|
1507
|
+
switch (desc.kind) {
|
|
1508
|
+
case 'method':
|
|
1509
|
+
{
|
|
1510
|
+
return desc.fn;
|
|
1511
|
+
}
|
|
1512
|
+
case 'readonly-value':
|
|
1513
|
+
{
|
|
1514
|
+
return desc.value;
|
|
1515
|
+
}
|
|
1516
|
+
case 'mutable-value':
|
|
1517
|
+
{
|
|
1518
|
+
const signal = getOrCreateInternalSignal(signals, receiver, prop, desc.value);
|
|
1519
|
+
// we don't consume this signal, since its not a true local.
|
|
1520
|
+
return signal.value;
|
|
1521
|
+
}
|
|
1522
|
+
case 'readonly-field':
|
|
1523
|
+
case 'mutable-field':
|
|
1524
|
+
{
|
|
1525
|
+
return desc.get.call(receiver);
|
|
1526
|
+
}
|
|
1527
|
+
case 'writeonly-field':
|
|
1528
|
+
{
|
|
1529
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
1530
|
+
{
|
|
1531
|
+
throw new Error(`Cannot get extended field ${String(prop)} as its definition has only a setter`);
|
|
1532
|
+
}
|
|
1533
|
+
})() : {};
|
|
1534
|
+
return undefined;
|
|
1535
|
+
}
|
|
1536
|
+
default:
|
|
1537
|
+
{
|
|
1538
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
1539
|
+
{
|
|
1540
|
+
throw new Error(`Unhandled extension kind ${desc.kind}`);
|
|
1541
|
+
}
|
|
1542
|
+
})() : {};
|
|
1543
|
+
return undefined;
|
|
1544
|
+
}
|
|
1539
1545
|
}
|
|
1540
|
-
return color in LIGHT_DARK_ALT ? LIGHT_DARK_ALT[color] : color;
|
|
1541
1546
|
}
|
|
1542
|
-
function
|
|
1543
|
-
|
|
1544
|
-
|
|
1547
|
+
function performExtensionSet(receiver, extensions, signals, prop, value) {
|
|
1548
|
+
const desc = extensions.get(prop);
|
|
1549
|
+
switch (desc.kind) {
|
|
1550
|
+
case 'method':
|
|
1551
|
+
case 'readonly-value':
|
|
1552
|
+
case 'readonly-field':
|
|
1553
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
1554
|
+
{
|
|
1555
|
+
throw new Error(`Cannot set extension field ${String(prop)} as it is a ${desc.kind}`);
|
|
1556
|
+
}
|
|
1557
|
+
})() : {};
|
|
1558
|
+
return false;
|
|
1559
|
+
case 'mutable-value':
|
|
1560
|
+
{
|
|
1561
|
+
const signal = getOrCreateInternalSignal(signals, receiver, prop, desc.value);
|
|
1562
|
+
if (signal.value !== value) {
|
|
1563
|
+
// we don't notify this signal, since its not a true local.
|
|
1564
|
+
signal.value = value;
|
|
1565
|
+
}
|
|
1566
|
+
return true;
|
|
1567
|
+
}
|
|
1568
|
+
case 'writeonly-field':
|
|
1569
|
+
case 'mutable-field':
|
|
1570
|
+
{
|
|
1571
|
+
desc.set.call(receiver, value);
|
|
1572
|
+
return true;
|
|
1573
|
+
}
|
|
1574
|
+
default:
|
|
1575
|
+
{
|
|
1576
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
1577
|
+
{
|
|
1578
|
+
throw new Error(`Unhandled extension kind ${desc.kind}`);
|
|
1579
|
+
}
|
|
1580
|
+
})() : {};
|
|
1581
|
+
return false;
|
|
1582
|
+
}
|
|
1545
1583
|
}
|
|
1546
|
-
return false;
|
|
1547
1584
|
}
|
|
1548
|
-
function
|
|
1549
|
-
const
|
|
1550
|
-
switch (
|
|
1551
|
-
case '
|
|
1552
|
-
case 'notify':
|
|
1585
|
+
function performArrayExtensionGet(receiver, extensions, signals, prop, _SIGNAL, boundFns, transaction) {
|
|
1586
|
+
const desc = extensions.get(prop);
|
|
1587
|
+
switch (desc.kind) {
|
|
1588
|
+
case 'method':
|
|
1553
1589
|
{
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
|
|
1590
|
+
let fn = boundFns.get(prop);
|
|
1591
|
+
if (fn === undefined) {
|
|
1592
|
+
fn = function () {
|
|
1593
|
+
consumeInternalSignal(_SIGNAL);
|
|
1594
|
+
transaction(true);
|
|
1595
|
+
const result = Reflect.apply(desc.fn, receiver, arguments);
|
|
1596
|
+
transaction(false);
|
|
1597
|
+
return result;
|
|
1598
|
+
};
|
|
1599
|
+
boundFns.set(prop, fn);
|
|
1600
|
+
}
|
|
1601
|
+
return fn;
|
|
1557
1602
|
}
|
|
1558
|
-
case '
|
|
1603
|
+
case 'mutable-field':
|
|
1604
|
+
case 'readonly-field':
|
|
1559
1605
|
{
|
|
1560
|
-
|
|
1561
|
-
|
|
1606
|
+
return desc.get.call(receiver);
|
|
1607
|
+
}
|
|
1608
|
+
case 'readonly-value':
|
|
1609
|
+
{
|
|
1610
|
+
return desc.value;
|
|
1611
|
+
}
|
|
1612
|
+
case 'mutable-value':
|
|
1613
|
+
{
|
|
1614
|
+
const signal = getOrCreateInternalSignal(signals, receiver, prop, desc.value);
|
|
1615
|
+
// we don't consume this signal, since its not a true local.
|
|
1616
|
+
return signal.value;
|
|
1617
|
+
}
|
|
1618
|
+
case 'writeonly-field':
|
|
1619
|
+
{
|
|
1620
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
1621
|
+
{
|
|
1622
|
+
throw new Error(`Cannot get extended field ${String(prop)} as its definition has only a setter`);
|
|
1623
|
+
}
|
|
1624
|
+
})() : {};
|
|
1625
|
+
return undefined;
|
|
1626
|
+
}
|
|
1627
|
+
default:
|
|
1628
|
+
{
|
|
1629
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
1630
|
+
{
|
|
1631
|
+
throw new Error(`Unhandled extension kind ${desc.kind}`);
|
|
1632
|
+
}
|
|
1633
|
+
})() : {};
|
|
1634
|
+
return undefined;
|
|
1562
1635
|
}
|
|
1563
1636
|
}
|
|
1564
|
-
return [];
|
|
1565
1637
|
}
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1638
|
+
const ARRAY_GETTER_METHODS$1 = new Set([Symbol.iterator, 'concat', 'entries', 'every', 'fill', 'filter', 'find', 'findIndex', 'flat', 'flatMap', 'forEach', 'includes', 'indexOf', 'join', 'keys', 'lastIndexOf', 'map', 'reduce', 'reduceRight', 'slice', 'some', 'values']);
|
|
1639
|
+
// const ARRAY_SETTER_METHODS = new Set<KeyType>(['push', 'pop', 'unshift', 'shift', 'splice', 'sort']);
|
|
1640
|
+
const SYNC_PROPS$1 = new Set(['[]', 'length']);
|
|
1641
|
+
function isArrayGetter$1(prop) {
|
|
1642
|
+
return ARRAY_GETTER_METHODS$1.has(prop);
|
|
1643
|
+
}
|
|
1644
|
+
const ARRAY_SETTER_METHODS$1 = new Set(['push', 'pop', 'unshift', 'shift', 'splice', 'sort']);
|
|
1645
|
+
function isArraySetter$1(prop) {
|
|
1646
|
+
return ARRAY_SETTER_METHODS$1.has(prop);
|
|
1647
|
+
}
|
|
1648
|
+
|
|
1649
|
+
// function isSelfProp<T extends object>(self: T, prop: KeyType): prop is keyof T {
|
|
1650
|
+
// return prop in self;
|
|
1651
|
+
// }
|
|
1652
|
+
|
|
1653
|
+
function convertToInt$1(prop) {
|
|
1654
|
+
if (typeof prop === 'symbol') return null;
|
|
1655
|
+
const num = Number(prop);
|
|
1656
|
+
if (isNaN(num)) return null;
|
|
1657
|
+
return num % 1 === 0 ? num : null;
|
|
1658
|
+
}
|
|
1659
|
+
function safeForEach$1(instance, arr, store, callback, target) {
|
|
1660
|
+
if (target === undefined) {
|
|
1661
|
+
target = null;
|
|
1574
1662
|
}
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
}
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1663
|
+
// clone to prevent mutation
|
|
1664
|
+
arr = arr.slice();
|
|
1665
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
1666
|
+
if (!test) {
|
|
1667
|
+
throw new Error('`forEach` expects a function as first argument.');
|
|
1668
|
+
}
|
|
1669
|
+
})(typeof callback === 'function') : {};
|
|
1670
|
+
|
|
1671
|
+
// because we retrieveLatest above we need not worry if array is mutated during iteration
|
|
1672
|
+
// by unloadRecord/rollbackAttributes
|
|
1673
|
+
// push/add/removeObject may still be problematic
|
|
1674
|
+
// but this is a more traditionally expected forEach bug.
|
|
1675
|
+
const length = arr.length; // we need to access length to ensure we are consumed
|
|
1676
|
+
|
|
1677
|
+
for (let index = 0; index < length; index++) {
|
|
1678
|
+
callback.call(target, arr[index], index, instance);
|
|
1679
|
+
}
|
|
1680
|
+
return instance;
|
|
1681
|
+
}
|
|
1682
|
+
// eslint-disable-next-line @typescript-eslint/no-extraneous-class
|
|
1683
|
+
class ManagedArray {
|
|
1684
|
+
constructor(context, owner, data) {
|
|
1685
|
+
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
1686
|
+
const self = this;
|
|
1687
|
+
this[SOURCE$1] = data?.slice();
|
|
1688
|
+
const IS_EDITABLE = context.editable ?? false;
|
|
1689
|
+
this[Context] = context;
|
|
1690
|
+
const schema = context.store.schema;
|
|
1691
|
+
const cache = context.store.cache;
|
|
1692
|
+
const {
|
|
1693
|
+
field
|
|
1694
|
+
} = context;
|
|
1695
|
+
const signals = withSignalStore(this);
|
|
1696
|
+
let _SIGNAL = null;
|
|
1697
|
+
const boundFns = new Map();
|
|
1698
|
+
this.identifier = context.resourceKey;
|
|
1699
|
+
this.path = context.path;
|
|
1700
|
+
this.owner = owner;
|
|
1701
|
+
let transaction = false;
|
|
1702
|
+
const KeyMode = field.options?.key ?? '@identity';
|
|
1703
|
+
// listener.
|
|
1704
|
+
const RefStorage = KeyMode === '@identity' ? WeakMap :
|
|
1705
|
+
// CAUTION CAUTION CAUTION
|
|
1706
|
+
// this is a pile of lies
|
|
1707
|
+
// the Map is Map<string, WeakRef<ReactiveResource>>
|
|
1708
|
+
// but TS does not understand how to juggle modes like this
|
|
1709
|
+
// internal to a method like ours without us duplicating the code
|
|
1710
|
+
// into two separate methods.
|
|
1711
|
+
Map;
|
|
1712
|
+
const ManagedRecordRefs = field.kind === 'schema-array' ? new RefStorage() : null;
|
|
1713
|
+
const extensions = context.legacy ? schema.CAUTION_MEGA_DANGER_ZONE_arrayExtensions(field) : null;
|
|
1714
|
+
const proxy = new Proxy(this[SOURCE$1], {
|
|
1715
|
+
get(target, prop, receiver) {
|
|
1716
|
+
if (prop === ARRAY_SIGNAL) {
|
|
1717
|
+
return _SIGNAL;
|
|
1718
|
+
}
|
|
1719
|
+
if (prop === 'identifier') {
|
|
1720
|
+
return self.identifier;
|
|
1721
|
+
}
|
|
1722
|
+
if (prop === 'owner') {
|
|
1723
|
+
return self.owner;
|
|
1724
|
+
}
|
|
1725
|
+
const index = convertToInt$1(prop);
|
|
1726
|
+
if (_SIGNAL.isStale && (index !== null || SYNC_PROPS$1.has(prop) || isArrayGetter$1(prop))) {
|
|
1727
|
+
_SIGNAL.isStale = false;
|
|
1728
|
+
const newData = cache.getAttr(context.resourceKey, context.path);
|
|
1729
|
+
if (newData && newData !== self[SOURCE$1]) {
|
|
1730
|
+
self[SOURCE$1].length = 0;
|
|
1731
|
+
self[SOURCE$1].push(...newData);
|
|
1732
|
+
}
|
|
1733
|
+
}
|
|
1734
|
+
if (prop === 'length') {
|
|
1735
|
+
return consumeInternalSignal(_SIGNAL), target.length;
|
|
1736
|
+
}
|
|
1737
|
+
if (prop === '[]') return consumeInternalSignal(_SIGNAL), receiver;
|
|
1738
|
+
if (index !== null) {
|
|
1739
|
+
if (!transaction) {
|
|
1740
|
+
consumeInternalSignal(_SIGNAL);
|
|
1741
|
+
}
|
|
1742
|
+
const rawValue = target[index];
|
|
1743
|
+
if (field.kind === 'array') {
|
|
1744
|
+
if (field.type) {
|
|
1745
|
+
const transform = schema.transformation(field);
|
|
1746
|
+
return transform.hydrate(rawValue, field.options ?? null, self.owner);
|
|
1747
|
+
}
|
|
1748
|
+
return rawValue;
|
|
1749
|
+
}
|
|
1750
|
+
|
|
1751
|
+
/**
|
|
1752
|
+
* When the array is polymorphic, we need to determine the real type
|
|
1753
|
+
* in order to apply the correct identity as schema-object identity
|
|
1754
|
+
* is only required to be unique by type
|
|
1755
|
+
*/
|
|
1756
|
+
let objectType;
|
|
1757
|
+
if (field.options?.polymorphic) {
|
|
1758
|
+
const typePath = field.options.type ?? 'type';
|
|
1759
|
+
// if we are polymorphic, then context.field.options.type will
|
|
1760
|
+
// either specify a path on the rawValue to use as the type, defaulting to "type" or
|
|
1761
|
+
// the special string "@hash" which tells us to treat field.type as a hashFn name with which
|
|
1762
|
+
// to calc the type.
|
|
1763
|
+
if (typePath === '@hash') {
|
|
1764
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
1765
|
+
if (!test) {
|
|
1766
|
+
throw new Error(`Expected the field to define a hashFn as its type`);
|
|
1767
|
+
}
|
|
1768
|
+
})(field.type) : {};
|
|
1769
|
+
const hashFn = schema.hashFn({
|
|
1770
|
+
type: field.type
|
|
1771
|
+
});
|
|
1772
|
+
// TODO consider if there are better options and name args we could provide.
|
|
1773
|
+
objectType = hashFn(rawValue, null, null);
|
|
1774
|
+
} else {
|
|
1775
|
+
objectType = rawValue[typePath];
|
|
1776
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
1777
|
+
if (!test) {
|
|
1778
|
+
throw new Error(`Expected the type path for the field to be a value on the raw object`);
|
|
1779
|
+
}
|
|
1780
|
+
})(typePath && objectType && typeof objectType === 'string') : {};
|
|
1781
|
+
}
|
|
1782
|
+
} else {
|
|
1783
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
1784
|
+
if (!test) {
|
|
1785
|
+
throw new Error(`A non-polymorphic SchemaArrayField must provide a SchemaObject type in its definition`);
|
|
1786
|
+
}
|
|
1787
|
+
})(field.type) : {};
|
|
1788
|
+
objectType = field.type;
|
|
1789
|
+
}
|
|
1790
|
+
|
|
1791
|
+
/**
|
|
1792
|
+
* When KeyMode=@hash the ReactiveResource is keyed into
|
|
1793
|
+
* ManagedRecordRefs by the return value of @hash on the rawValue.
|
|
1794
|
+
*
|
|
1795
|
+
* This means that we could find a way to only recompute the identity
|
|
1796
|
+
* when ARRAY_SIGNAL is dirty if hash performance becomes a bottleneck.
|
|
1797
|
+
*/
|
|
1798
|
+
let schemaObjectKeyValue;
|
|
1799
|
+
if (KeyMode === '@hash') {
|
|
1800
|
+
const hashField = schema.resource({
|
|
1801
|
+
type: objectType
|
|
1802
|
+
}).identity;
|
|
1803
|
+
const hashFn = schema.hashFn(hashField);
|
|
1804
|
+
schemaObjectKeyValue = hashFn(rawValue, hashField.options ?? null, hashField.name);
|
|
1805
|
+
} else {
|
|
1806
|
+
// if mode is not @identity or @index, then access the key path.
|
|
1807
|
+
// we should assert that `mode` is a string
|
|
1808
|
+
// it should read directly from the cache value for that field (e.g. no derivation, no transformation)
|
|
1809
|
+
// and, we likely should lookup the associated field and throw an error IF
|
|
1810
|
+
// the given field does not exist OR
|
|
1811
|
+
// the field is anything other than a GenericField or LegacyAttributeField.
|
|
1812
|
+
if (macroCondition(getGlobalConfig().WarpDrive.env.DEBUG)) {
|
|
1813
|
+
const isPathKeyMode = KeyMode !== '@identity' && KeyMode !== '@index';
|
|
1814
|
+
if (isPathKeyMode) {
|
|
1815
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
1816
|
+
if (!test) {
|
|
1817
|
+
throw new Error('mode must be a string');
|
|
1818
|
+
}
|
|
1819
|
+
})(typeof KeyMode === 'string' && KeyMode !== '') : {};
|
|
1820
|
+
const modeField = schema.fields({
|
|
1821
|
+
type: objectType
|
|
1822
|
+
}).get(KeyMode);
|
|
1823
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
1824
|
+
if (!test) {
|
|
1825
|
+
throw new Error('field must exist in schema');
|
|
1826
|
+
}
|
|
1827
|
+
})(modeField) : {};
|
|
1828
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
1829
|
+
if (!test) {
|
|
1830
|
+
throw new Error('field must be a GenericField or LegacyAttributeField');
|
|
1831
|
+
}
|
|
1832
|
+
})(modeField.kind === 'field' || modeField.kind === 'attribute') : {};
|
|
1833
|
+
}
|
|
1834
|
+
}
|
|
1835
|
+
schemaObjectKeyValue = KeyMode === '@identity' ? rawValue : KeyMode === '@index' ? index : rawValue[KeyMode];
|
|
1836
|
+
}
|
|
1837
|
+
if (!schemaObjectKeyValue) {
|
|
1838
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
1839
|
+
{
|
|
1840
|
+
throw new Error(`Unexpected out of bounds access on SchemaArray`);
|
|
1841
|
+
}
|
|
1842
|
+
})() : {};
|
|
1843
|
+
return undefined;
|
|
1844
|
+
}
|
|
1845
|
+
const recordRef = ManagedRecordRefs.get(schemaObjectKeyValue);
|
|
1846
|
+
const record = recordRef?.value.deref();
|
|
1847
|
+
|
|
1848
|
+
// confirm the type and key still match
|
|
1849
|
+
if (record && recordRef.type === objectType && recordRef.identity === schemaObjectKeyValue) {
|
|
1850
|
+
if (recordRef.index !== index) {
|
|
1851
|
+
recordRef.index = index;
|
|
1852
|
+
recordRef.context.path[recordRef.context.path.length - 1] = index;
|
|
1853
|
+
}
|
|
1854
|
+
return record;
|
|
1855
|
+
} else if (record) {
|
|
1856
|
+
// TODO schedule idle once we can
|
|
1857
|
+
void Promise.resolve().then(() => {
|
|
1858
|
+
record[Destroy]();
|
|
1859
|
+
});
|
|
1860
|
+
}
|
|
1861
|
+
const recordPath = context.path.slice();
|
|
1862
|
+
// this is a dirty lie since path is string[] but really we
|
|
1863
|
+
// should change the types for paths to `Array<string | number>`
|
|
1864
|
+
recordPath.push(index);
|
|
1865
|
+
const objectContext = {
|
|
1866
|
+
store: context.store,
|
|
1867
|
+
resourceKey: context.resourceKey,
|
|
1868
|
+
modeName: context.modeName,
|
|
1869
|
+
legacy: context.legacy,
|
|
1870
|
+
editable: context.editable,
|
|
1871
|
+
path: recordPath,
|
|
1872
|
+
field: field,
|
|
1873
|
+
value: objectType
|
|
1874
|
+
};
|
|
1875
|
+
const schemaObject = new ReactiveResource(objectContext);
|
|
1876
|
+
ManagedRecordRefs.set(schemaObjectKeyValue, {
|
|
1877
|
+
type: objectType,
|
|
1878
|
+
identity: schemaObjectKeyValue,
|
|
1879
|
+
index,
|
|
1880
|
+
context: objectContext,
|
|
1881
|
+
value: new WeakRef(schemaObject)
|
|
1882
|
+
});
|
|
1883
|
+
return schemaObject;
|
|
1884
|
+
}
|
|
1885
|
+
if (isArrayGetter$1(prop)) {
|
|
1886
|
+
let fn = boundFns.get(prop);
|
|
1887
|
+
if (fn === undefined) {
|
|
1888
|
+
if (prop === 'forEach') {
|
|
1889
|
+
fn = function () {
|
|
1890
|
+
consumeInternalSignal(_SIGNAL);
|
|
1891
|
+
transaction = true;
|
|
1892
|
+
const result = safeForEach$1(receiver, target, context.store, arguments[0], arguments[1]);
|
|
1893
|
+
transaction = false;
|
|
1894
|
+
return result;
|
|
1895
|
+
};
|
|
1896
|
+
} else {
|
|
1897
|
+
fn = function () {
|
|
1898
|
+
consumeInternalSignal(_SIGNAL);
|
|
1899
|
+
// array functions must run through Reflect to work properly
|
|
1900
|
+
// binding via other means will not work.
|
|
1901
|
+
transaction = true;
|
|
1902
|
+
const result = Reflect.apply(target[prop], receiver, arguments);
|
|
1903
|
+
transaction = false;
|
|
1904
|
+
return result;
|
|
1905
|
+
};
|
|
1906
|
+
}
|
|
1907
|
+
boundFns.set(prop, fn);
|
|
1908
|
+
}
|
|
1909
|
+
return fn;
|
|
1910
|
+
}
|
|
1911
|
+
if (isArraySetter$1(prop)) {
|
|
1912
|
+
let fn = boundFns.get(prop);
|
|
1913
|
+
if (fn === undefined) {
|
|
1914
|
+
fn = function () {
|
|
1915
|
+
if (!IS_EDITABLE) {
|
|
1916
|
+
throw new Error(`Mutating this array via ${String(prop)} is not allowed because the record is not editable`);
|
|
1917
|
+
}
|
|
1918
|
+
consumeInternalSignal(_SIGNAL);
|
|
1919
|
+
transaction = true;
|
|
1920
|
+
const result = Reflect.apply(target[prop], receiver, arguments);
|
|
1921
|
+
transaction = false;
|
|
1922
|
+
return result;
|
|
1923
|
+
};
|
|
1924
|
+
boundFns.set(prop, fn);
|
|
1925
|
+
}
|
|
1926
|
+
return fn;
|
|
1927
|
+
}
|
|
1928
|
+
if (isExtensionProp(extensions, prop)) {
|
|
1929
|
+
return performArrayExtensionGet(receiver, extensions, signals, prop, _SIGNAL, boundFns, v => void (transaction = v));
|
|
1930
|
+
}
|
|
1931
|
+
return Reflect.get(target, prop, receiver);
|
|
1932
|
+
},
|
|
1933
|
+
set(target, prop, value, receiver) {
|
|
1934
|
+
if (!IS_EDITABLE) {
|
|
1935
|
+
let errorPath = context.resourceKey.type;
|
|
1936
|
+
if (context.path) {
|
|
1937
|
+
errorPath = context.path[context.path.length - 1];
|
|
1938
|
+
}
|
|
1939
|
+
throw new Error(`Cannot set ${String(prop)} on ${errorPath} because the record is not editable`);
|
|
1940
|
+
}
|
|
1941
|
+
if (prop === 'identifier') {
|
|
1942
|
+
self.identifier = value;
|
|
1943
|
+
return true;
|
|
1944
|
+
}
|
|
1945
|
+
if (prop === 'owner') {
|
|
1946
|
+
self.owner = value;
|
|
1947
|
+
return true;
|
|
1948
|
+
}
|
|
1949
|
+
if (isExtensionProp(extensions, prop)) {
|
|
1950
|
+
return performExtensionSet(receiver, extensions, signals, prop, value);
|
|
1951
|
+
}
|
|
1952
|
+
const reflect = Reflect.set(target, prop, value, receiver);
|
|
1953
|
+
if (reflect) {
|
|
1954
|
+
if (!field.type) {
|
|
1955
|
+
cache.setAttr(context.resourceKey, context.path, self[SOURCE$1]);
|
|
1956
|
+
_SIGNAL.isStale = true;
|
|
1957
|
+
return true;
|
|
1958
|
+
}
|
|
1959
|
+
let rawValue = self[SOURCE$1];
|
|
1960
|
+
if (field.kind !== 'schema-array') {
|
|
1961
|
+
const transform = schema.transformation(field);
|
|
1962
|
+
if (!transform) {
|
|
1963
|
+
throw new Error(`No '${field.type}' transform defined for use by ${context.resourceKey.type}.${String(prop)}`);
|
|
1964
|
+
}
|
|
1965
|
+
rawValue = self[SOURCE$1].map(item => transform.serialize(item, field.options ?? null, self.owner));
|
|
1966
|
+
}
|
|
1967
|
+
cache.setAttr(context.resourceKey, context.path, rawValue);
|
|
1968
|
+
_SIGNAL.isStale = true;
|
|
1969
|
+
}
|
|
1970
|
+
return reflect;
|
|
1971
|
+
},
|
|
1972
|
+
has(target, prop) {
|
|
1973
|
+
if (prop === 'identifier' || prop === 'owner' || prop === ARRAY_SIGNAL) {
|
|
1974
|
+
return true;
|
|
1975
|
+
}
|
|
1976
|
+
return Reflect.has(target, prop);
|
|
1977
|
+
}
|
|
1978
|
+
});
|
|
1979
|
+
|
|
1980
|
+
// we entangle the signal on the returned proxy since that is
|
|
1981
|
+
// the object that other code will be interfacing with.
|
|
1982
|
+
_SIGNAL = entangleSignal(signals, proxy, ARRAY_SIGNAL, undefined);
|
|
1983
|
+
return proxy;
|
|
1984
|
+
}
|
|
1985
|
+
}
|
|
1986
|
+
|
|
1987
|
+
// this will error if someone tries to call
|
|
1988
|
+
// A(identifierArray) since it is not configurable
|
|
1989
|
+
// which is preferable to the `meta` override we used
|
|
1990
|
+
// before which required importing all of Ember
|
|
1991
|
+
const desc$1 = {
|
|
1992
|
+
enumerable: true,
|
|
1993
|
+
configurable: false,
|
|
1994
|
+
get: function () {
|
|
1995
|
+
// here to support computed chains
|
|
1996
|
+
// and {{#each}}
|
|
1997
|
+
if (macroCondition(getGlobalConfig().WarpDrive.deprecations.DEPRECATE_COMPUTED_CHAINS)) {
|
|
1998
|
+
return this;
|
|
1999
|
+
}
|
|
2000
|
+
}
|
|
2001
|
+
};
|
|
2002
|
+
// compat(desc);
|
|
2003
|
+
Object.defineProperty(ManagedArray.prototype, '[]', desc$1);
|
|
2004
|
+
function getArrayField(context) {
|
|
2005
|
+
const signal = entangleSignal(context.signals, context.record, context.path.at(-1), null);
|
|
2006
|
+
// the thing we hand out needs to know its owner and path in a private manner
|
|
2007
|
+
// its "address" is the parent identifier (identifier) + field name (field.name)
|
|
2008
|
+
// in the nested object case field name here is the full dot path from root resource to this value
|
|
2009
|
+
// its "key" is the field on the parent record
|
|
2010
|
+
// its "owner" is the parent record
|
|
2011
|
+
const {
|
|
2012
|
+
record
|
|
2013
|
+
} = context;
|
|
2014
|
+
let managedArray = signal.value;
|
|
2015
|
+
if (managedArray) {
|
|
2016
|
+
return managedArray;
|
|
2017
|
+
} else {
|
|
2018
|
+
const {
|
|
2019
|
+
store,
|
|
2020
|
+
resourceKey,
|
|
2021
|
+
path,
|
|
2022
|
+
field
|
|
2023
|
+
} = context;
|
|
2024
|
+
const {
|
|
2025
|
+
cache
|
|
2026
|
+
} = store;
|
|
2027
|
+
let rawValue = context.editable ? cache.getAttr(resourceKey, path) : cache.getRemoteAttr(resourceKey, path);
|
|
2028
|
+
if (!rawValue && field.kind === 'schema-array' && field.options?.defaultValue) {
|
|
2029
|
+
rawValue = [];
|
|
2030
|
+
}
|
|
2031
|
+
if (!rawValue) {
|
|
2032
|
+
return null;
|
|
2033
|
+
}
|
|
2034
|
+
managedArray = new ManagedArray(context, record, rawValue);
|
|
2035
|
+
signal.value = managedArray;
|
|
2036
|
+
}
|
|
2037
|
+
return managedArray;
|
|
2038
|
+
}
|
|
2039
|
+
function setArrayField(context) {
|
|
2040
|
+
const {
|
|
2041
|
+
field,
|
|
2042
|
+
record,
|
|
2043
|
+
value
|
|
2044
|
+
} = context;
|
|
2045
|
+
const {
|
|
2046
|
+
cache,
|
|
2047
|
+
schema
|
|
2048
|
+
} = context.store;
|
|
2049
|
+
const fieldSignal = peekInternalSignal(context.signals, context.path.at(-1));
|
|
2050
|
+
const peeked = fieldSignal?.value;
|
|
2051
|
+
const transform = field.type ? schema.transformation(field) : null;
|
|
2052
|
+
const rawValue = field.type ? value.map(item => transform.serialize(item, field.options ?? null, record)) : value?.slice();
|
|
2053
|
+
cache.setAttr(context.resourceKey, context.path, rawValue);
|
|
2054
|
+
if (peeked) {
|
|
2055
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
2056
|
+
if (!test) {
|
|
2057
|
+
throw new Error(`Expected the peekManagedArray for ${field.kind} to return a ManagedArray`);
|
|
2058
|
+
}
|
|
2059
|
+
})(ARRAY_SIGNAL in peeked) : {};
|
|
2060
|
+
const arrSignal = peeked[ARRAY_SIGNAL];
|
|
2061
|
+
arrSignal.isStale = true;
|
|
2062
|
+
// TODO run array destroy?
|
|
2063
|
+
}
|
|
2064
|
+
if (!Array.isArray(rawValue) && fieldSignal) {
|
|
2065
|
+
fieldSignal.value = null;
|
|
2066
|
+
}
|
|
2067
|
+
return true;
|
|
2068
|
+
}
|
|
2069
|
+
function getAttributeField(context) {
|
|
2070
|
+
entangleSignal(context.signals, context.record, context.path.at(-1), null);
|
|
2071
|
+
const {
|
|
2072
|
+
cache
|
|
2073
|
+
} = context.store;
|
|
2074
|
+
return context.editable ? cache.getAttr(context.resourceKey, context.path) : cache.getRemoteAttr(context.resourceKey, context.path);
|
|
2075
|
+
}
|
|
2076
|
+
function setAttributeField(context) {
|
|
2077
|
+
context.store.cache.setAttr(context.resourceKey, context.path, context.value);
|
|
2078
|
+
return true;
|
|
2079
|
+
}
|
|
2080
|
+
const InvalidKinds = ['alias', 'derived', '@local'];
|
|
2081
|
+
function isInvalidKind(kind) {
|
|
2082
|
+
return InvalidKinds.includes(kind);
|
|
2083
|
+
}
|
|
2084
|
+
function isNonIdentityCacheableField(field) {
|
|
2085
|
+
return !isInvalidKind(field.kind) && field.kind !== '@id' && field.kind !== '@hash';
|
|
2086
|
+
}
|
|
2087
|
+
function getFieldCacheKeyStrict(field) {
|
|
2088
|
+
return field.sourceKey || field.name;
|
|
2089
|
+
}
|
|
2090
|
+
function getFieldCacheKey(field) {
|
|
2091
|
+
return 'sourceKey' in field && field.sourceKey ? field.sourceKey : field.name;
|
|
2092
|
+
}
|
|
2093
|
+
function getBelongsToField(context) {
|
|
2094
|
+
entangleSignal(context.signals, context.record, context.path.at(-1), null);
|
|
2095
|
+
const {
|
|
2096
|
+
field,
|
|
2097
|
+
resourceKey,
|
|
2098
|
+
store
|
|
2099
|
+
} = context;
|
|
2100
|
+
const {
|
|
2101
|
+
schema,
|
|
2102
|
+
cache
|
|
2103
|
+
} = store;
|
|
2104
|
+
if (field.options.linksMode) {
|
|
2105
|
+
const rawValue = context.editable ? cache.getRelationship(resourceKey, getFieldCacheKeyStrict(field)) : cache.getRemoteRelationship(resourceKey, getFieldCacheKeyStrict(field));
|
|
2106
|
+
return rawValue.data ? store.peekRecord(rawValue.data) : null;
|
|
2107
|
+
}
|
|
2108
|
+
|
|
2109
|
+
// FIXME move this to a "LegacyMode" make this part of "PolarisMode"
|
|
2110
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
2111
|
+
if (!test) {
|
|
2112
|
+
throw new Error(`Can only use belongsTo fields when the resource is in legacy mode`);
|
|
2113
|
+
}
|
|
2114
|
+
})(context.legacy) : {};
|
|
2115
|
+
return schema._kind('@legacy', 'belongsTo').get(store, context.record, resourceKey, field);
|
|
2116
|
+
}
|
|
2117
|
+
function setBelongsToField(context) {
|
|
2118
|
+
const {
|
|
2119
|
+
store
|
|
2120
|
+
} = context;
|
|
2121
|
+
const {
|
|
2122
|
+
schema
|
|
2123
|
+
} = store;
|
|
2124
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
2125
|
+
if (!test) {
|
|
2126
|
+
throw new Error(`Can only mutate belongsTo fields when the resource is in legacy mode`);
|
|
2127
|
+
}
|
|
2128
|
+
})(context.legacy) : {};
|
|
2129
|
+
schema._kind('@legacy', 'belongsTo').set(store, context.record, context.resourceKey, context.field, context.value);
|
|
2130
|
+
return true;
|
|
2131
|
+
}
|
|
2132
|
+
function getCollectionField(context) {
|
|
2133
|
+
entangleSignal(context.signals, context.record, context.path.at(-1), null);
|
|
2134
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
2135
|
+
{
|
|
2136
|
+
throw new Error(`Accessing collection fields is not yet implemented`);
|
|
2137
|
+
}
|
|
2138
|
+
})() : {};
|
|
2139
|
+
}
|
|
2140
|
+
function setCollectionField(context) {
|
|
2141
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
2142
|
+
{
|
|
2143
|
+
throw new Error(`Setting collection fields is not yet implemented`);
|
|
2144
|
+
}
|
|
2145
|
+
})() : {};
|
|
2146
|
+
return false;
|
|
2147
|
+
}
|
|
2148
|
+
function getDerivedField(context) {
|
|
2149
|
+
const {
|
|
2150
|
+
schema
|
|
2151
|
+
} = context.store;
|
|
2152
|
+
return schema.derivation(context.field)(context.record, context.field.options ?? null, context.field.name);
|
|
2153
|
+
}
|
|
2154
|
+
function setDerivedField(context) {
|
|
2155
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
2156
|
+
{
|
|
2157
|
+
throw new Error(`ILLEGAL SET: Cannot set '${context.path.join('.')}' on '${context.resourceKey.type}' as ${context.field.kind} fields are not mutable`);
|
|
2158
|
+
}
|
|
2159
|
+
})() : {};
|
|
2160
|
+
return false;
|
|
2161
|
+
}
|
|
2162
|
+
function getGenericField(context) {
|
|
2163
|
+
entangleSignal(context.signals, context.record, context.path.at(-1), null);
|
|
2164
|
+
const {
|
|
2165
|
+
cache,
|
|
2166
|
+
schema
|
|
2167
|
+
} = context.store;
|
|
2168
|
+
const rawValue = context.editable ? cache.getAttr(context.resourceKey, context.path) : cache.getRemoteAttr(context.resourceKey, context.path);
|
|
2169
|
+
const {
|
|
2170
|
+
field
|
|
2171
|
+
} = context;
|
|
2172
|
+
if (!field.type) {
|
|
2173
|
+
return rawValue;
|
|
2174
|
+
}
|
|
2175
|
+
const transform = schema.transformation(field);
|
|
2176
|
+
return transform.hydrate(rawValue, field.options ?? null, context.record);
|
|
2177
|
+
}
|
|
2178
|
+
function setGenericField(context) {
|
|
2179
|
+
const {
|
|
2180
|
+
cache,
|
|
2181
|
+
schema
|
|
2182
|
+
} = context.store;
|
|
2183
|
+
const {
|
|
2184
|
+
field
|
|
2185
|
+
} = context;
|
|
2186
|
+
if (!field.type) {
|
|
2187
|
+
cache.setAttr(context.resourceKey, context.path, context.value);
|
|
2188
|
+
return true;
|
|
2189
|
+
}
|
|
2190
|
+
const transform = schema.transformation(field);
|
|
2191
|
+
const rawValue = transform.serialize(context.value, field.options ?? null, context.record);
|
|
2192
|
+
cache.setAttr(context.resourceKey, context.path, rawValue);
|
|
2193
|
+
return true;
|
|
2194
|
+
}
|
|
2195
|
+
class ManyArrayManager {
|
|
2196
|
+
constructor(record, editable) {
|
|
2197
|
+
const context = record[Context];
|
|
2198
|
+
this.record = record;
|
|
2199
|
+
this.store = context.store;
|
|
2200
|
+
this.identifier = context.resourceKey;
|
|
2201
|
+
this.editable = editable;
|
|
2202
|
+
}
|
|
2203
|
+
_syncArray(array) {
|
|
2204
|
+
const method = this.editable ? 'getRelationship' : 'getRemoteRelationship';
|
|
2205
|
+
// FIXME field needs to use sourceKey
|
|
2206
|
+
const rawValue = this.store.cache[method](this.identifier, array.key);
|
|
2207
|
+
if (rawValue.meta) {
|
|
2208
|
+
array.meta = rawValue.meta;
|
|
2209
|
+
}
|
|
2210
|
+
if (rawValue.links) {
|
|
2211
|
+
array.links = rawValue.links;
|
|
2212
|
+
}
|
|
2213
|
+
const currentState = array[SOURCE];
|
|
2214
|
+
|
|
2215
|
+
// unlike in the normal RecordArray case, we don't need to divorce the reference
|
|
2216
|
+
// because we don't need to worry about associate/disassociate since the graph
|
|
2217
|
+
// takes care of that for us
|
|
2218
|
+
if (currentState !== rawValue.data) {
|
|
2219
|
+
currentState.length = 0;
|
|
2220
|
+
fastPush(currentState, rawValue.data);
|
|
2221
|
+
}
|
|
2222
|
+
}
|
|
2223
|
+
reloadHasMany(key, options) {
|
|
2224
|
+
// FIXME field needs to use sourceKey
|
|
2225
|
+
const field = this.store.schema.fields(this.identifier).get(key);
|
|
2226
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
2227
|
+
if (!test) {
|
|
2228
|
+
throw new Error(`Expected a hasMany field for ${key}`);
|
|
2229
|
+
}
|
|
2230
|
+
})(field?.kind === 'hasMany') : {};
|
|
2231
|
+
const cacheOptions = options ? extractCacheOptions(options) : {
|
|
2232
|
+
reload: true
|
|
2233
|
+
};
|
|
2234
|
+
cacheOptions.types = [field.type];
|
|
2235
|
+
const rawValue = this.store.cache.getRelationship(this.identifier, key);
|
|
2236
|
+
const req = {
|
|
2237
|
+
url: getRelatedLink(rawValue),
|
|
2238
|
+
op: 'findHasMany',
|
|
2239
|
+
method: 'GET',
|
|
2240
|
+
records: rawValue.data,
|
|
2241
|
+
cacheOptions,
|
|
2242
|
+
options: {
|
|
2243
|
+
field,
|
|
2244
|
+
identifier: this.identifier,
|
|
2245
|
+
links: rawValue.links,
|
|
2246
|
+
meta: rawValue.meta
|
|
2247
|
+
},
|
|
2248
|
+
[EnableHydration]: false
|
|
2249
|
+
};
|
|
2250
|
+
return this.store.request(req);
|
|
2251
|
+
}
|
|
2252
|
+
mutate(mutation) {
|
|
2253
|
+
this.store.cache.mutate(mutation);
|
|
2254
|
+
}
|
|
2255
|
+
}
|
|
2256
|
+
function getRelatedLink(resource) {
|
|
2257
|
+
const related = resource.links?.related;
|
|
2258
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
2259
|
+
if (!test) {
|
|
2260
|
+
throw new Error(`Expected a related link`);
|
|
2261
|
+
}
|
|
2262
|
+
})(related) : {};
|
|
2263
|
+
return typeof related === 'object' ? related.href : related;
|
|
2264
|
+
}
|
|
2265
|
+
function extractCacheOptions(options) {
|
|
2266
|
+
const cacheOptions = {};
|
|
2267
|
+
if ('reload' in options) {
|
|
2268
|
+
cacheOptions.reload = options.reload;
|
|
2269
|
+
}
|
|
2270
|
+
if ('backgroundReload' in options) {
|
|
2271
|
+
cacheOptions.backgroundReload = options.backgroundReload;
|
|
2272
|
+
}
|
|
2273
|
+
return cacheOptions;
|
|
2274
|
+
}
|
|
2275
|
+
function getHasManyField(context) {
|
|
2276
|
+
const signal = entangleSignal(context.signals, context.record, context.path.at(-1), null);
|
|
2277
|
+
const {
|
|
2278
|
+
store,
|
|
2279
|
+
field
|
|
2280
|
+
} = context;
|
|
2281
|
+
if (field.options.linksMode) {
|
|
2282
|
+
const {
|
|
2283
|
+
record
|
|
2284
|
+
} = context;
|
|
2285
|
+
// the thing we hand out needs to know its owner and path in a private manner
|
|
2286
|
+
// its "address" is the parent identifier (identifier) + field name (field.name)
|
|
2287
|
+
// in the nested object case field name here is the full dot path from root resource to this value
|
|
2288
|
+
// its "key" is the field on the parent record
|
|
2289
|
+
// its "owner" is the parent record
|
|
2290
|
+
|
|
2291
|
+
const cached = signal.value;
|
|
2292
|
+
if (cached) {
|
|
2293
|
+
return cached;
|
|
2294
|
+
}
|
|
2295
|
+
const {
|
|
2296
|
+
editable,
|
|
2297
|
+
resourceKey
|
|
2298
|
+
} = context;
|
|
2299
|
+
const {
|
|
2300
|
+
cache
|
|
2301
|
+
} = store;
|
|
2302
|
+
const rawValue = cache.getRelationship(resourceKey, getFieldCacheKeyStrict(field));
|
|
2303
|
+
if (!rawValue) {
|
|
2304
|
+
return null;
|
|
2305
|
+
}
|
|
2306
|
+
const managedArray = new RelatedCollection({
|
|
2307
|
+
store,
|
|
2308
|
+
type: field.type,
|
|
2309
|
+
identifier: resourceKey,
|
|
2310
|
+
cache,
|
|
2311
|
+
field: context.legacy ? field : undefined,
|
|
2312
|
+
// we divorce the reference here because ManyArray mutates the target directly
|
|
2313
|
+
// before sending the mutation op to the cache. We may be able to avoid this in the future
|
|
2314
|
+
identifiers: rawValue.data?.slice(),
|
|
2315
|
+
key: field.name,
|
|
2316
|
+
meta: rawValue.meta || null,
|
|
2317
|
+
links: rawValue.links || null,
|
|
2318
|
+
isPolymorphic: field.options.polymorphic ?? false,
|
|
2319
|
+
isAsync: field.options.async ?? false,
|
|
2320
|
+
// TODO: Grab the proper value
|
|
2321
|
+
_inverseIsAsync: false,
|
|
2322
|
+
// @ts-expect-error Typescript doesn't have a way for us to thread the generic backwards so it infers unknown instead of T
|
|
2323
|
+
manager: new ManyArrayManager(record, editable),
|
|
2324
|
+
isLoaded: true,
|
|
2325
|
+
allowMutation: editable
|
|
2326
|
+
});
|
|
2327
|
+
signal.value = managedArray;
|
|
2328
|
+
return managedArray;
|
|
2329
|
+
}
|
|
2330
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
2331
|
+
if (!test) {
|
|
2332
|
+
throw new Error(`Can only use hasMany fields when the resource is in legacy mode`);
|
|
2333
|
+
}
|
|
2334
|
+
})(context.legacy) : {};
|
|
2335
|
+
return store.schema._kind('@legacy', 'hasMany').get(store, context.record, context.resourceKey, field);
|
|
2336
|
+
}
|
|
2337
|
+
function setHasManyField(context) {
|
|
2338
|
+
const {
|
|
2339
|
+
store
|
|
2340
|
+
} = context;
|
|
2341
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
2342
|
+
if (!test) {
|
|
2343
|
+
throw new Error(`Can only use hasMany fields when the resource is in legacy mode`);
|
|
2344
|
+
}
|
|
2345
|
+
})(context.legacy) : {};
|
|
2346
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
2347
|
+
if (!test) {
|
|
2348
|
+
throw new Error(`You must pass an array of records to set a hasMany relationship`);
|
|
2349
|
+
}
|
|
2350
|
+
})(Array.isArray(context.value)) : {};
|
|
2351
|
+
store.schema._kind('@legacy', 'hasMany').set(store, context.record, context.resourceKey, context.field, context.value);
|
|
2352
|
+
return true;
|
|
2353
|
+
}
|
|
2354
|
+
function getHashField(context) {
|
|
2355
|
+
const {
|
|
2356
|
+
field,
|
|
2357
|
+
path,
|
|
2358
|
+
resourceKey
|
|
2359
|
+
} = context;
|
|
2360
|
+
const {
|
|
2361
|
+
schema,
|
|
2362
|
+
cache
|
|
2363
|
+
} = context.store;
|
|
2364
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
2365
|
+
if (!test) {
|
|
2366
|
+
throw new Error(`Cannot use a ${field.kind} directly on a resource.`);
|
|
2367
|
+
}
|
|
2368
|
+
})(Array.isArray(path) && path.length > 1) : {};
|
|
2369
|
+
const realPath = path.slice(0, -1);
|
|
2370
|
+
const rawData = context.editable ? cache.getAttr(resourceKey, realPath) : cache.getRemoteAttr(resourceKey, realPath);
|
|
2371
|
+
return schema.hashFn(field)(rawData, field.options ?? null, field.name ?? null);
|
|
2372
|
+
}
|
|
2373
|
+
function setHashField(context) {
|
|
2374
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
2375
|
+
{
|
|
2376
|
+
throw new Error(`ILLEGAL SET: Cannot set '${Array.isArray(context.path) ? context.path.join('.') : context.path}' on '${context.resourceKey.type}' as ${context.field.kind} fields are not mutable`);
|
|
2377
|
+
}
|
|
2378
|
+
})() : {};
|
|
2379
|
+
return false;
|
|
2380
|
+
}
|
|
2381
|
+
function getIdentityField(context) {
|
|
2382
|
+
entangleSignal(context.signals, context.record, '@identity', null);
|
|
2383
|
+
return context.resourceKey.id;
|
|
2384
|
+
}
|
|
2385
|
+
function setIdentityField(context) {
|
|
2386
|
+
const {
|
|
2387
|
+
value,
|
|
2388
|
+
resourceKey,
|
|
2389
|
+
store
|
|
2390
|
+
} = context;
|
|
2391
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
2392
|
+
if (!test) {
|
|
2393
|
+
throw new Error(`Expected to receive a string id`);
|
|
2394
|
+
}
|
|
2395
|
+
})(typeof value === 'string' && value.length) : {};
|
|
2396
|
+
const normalizedId = String(value);
|
|
2397
|
+
const didChange = normalizedId !== resourceKey.id;
|
|
2398
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
2399
|
+
if (!test) {
|
|
2400
|
+
throw new Error(`Cannot set ${resourceKey.type} record's id to ${normalizedId}, because id is already ${resourceKey.id}`);
|
|
2401
|
+
}
|
|
2402
|
+
})(!didChange || resourceKey.id === null) : {};
|
|
2403
|
+
if (normalizedId !== null && didChange) {
|
|
2404
|
+
store._instanceCache.setRecordId(resourceKey, normalizedId);
|
|
2405
|
+
store.notifications.notify(resourceKey, 'identity');
|
|
2406
|
+
}
|
|
2407
|
+
return true;
|
|
2408
|
+
}
|
|
2409
|
+
function getLocalField(context) {
|
|
2410
|
+
const {
|
|
2411
|
+
field
|
|
2412
|
+
} = context;
|
|
2413
|
+
const signal = getOrCreateInternalSignal(context.signals, context.record, field.name, field.options?.defaultValue ?? null);
|
|
2414
|
+
consumeInternalSignal(signal);
|
|
2415
|
+
return signal.value;
|
|
2416
|
+
}
|
|
2417
|
+
function setLocalField(context) {
|
|
2418
|
+
const {
|
|
2419
|
+
value
|
|
2420
|
+
} = context;
|
|
2421
|
+
const signal = getOrCreateInternalSignal(context.signals, context.record, context.field.name, value);
|
|
2422
|
+
if (signal.value !== value) {
|
|
2423
|
+
signal.value = value;
|
|
2424
|
+
notifyInternalSignal(signal);
|
|
2425
|
+
}
|
|
2426
|
+
return true;
|
|
2427
|
+
}
|
|
2428
|
+
const ObjectSymbols = new Set([OBJECT_SIGNAL, Context, SOURCE$1]);
|
|
2429
|
+
|
|
2430
|
+
// const ignoredGlobalFields = new Set<string>(['setInterval', 'nodeType', 'nodeName', 'length', 'document', STRUCTURED]);
|
|
2431
|
+
|
|
2432
|
+
// eslint-disable-next-line @typescript-eslint/no-extraneous-class
|
|
2433
|
+
class ManagedObject {
|
|
2434
|
+
constructor(context) {
|
|
2435
|
+
const {
|
|
2436
|
+
field,
|
|
2437
|
+
path
|
|
2438
|
+
} = context;
|
|
2439
|
+
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
2440
|
+
const self = this;
|
|
2441
|
+
this[SOURCE$1] = Object.assign({}, context.value);
|
|
2442
|
+
const signals = withSignalStore(this);
|
|
2443
|
+
const _SIGNAL = this[OBJECT_SIGNAL] = entangleSignal(signals, this, OBJECT_SIGNAL, undefined);
|
|
2444
|
+
this[Context] = context;
|
|
2445
|
+
const identifier = context.resourceKey;
|
|
2446
|
+
const {
|
|
2447
|
+
cache,
|
|
2448
|
+
schema
|
|
2449
|
+
} = context.store;
|
|
2450
|
+
|
|
2451
|
+
// prettier-ignore
|
|
2452
|
+
const extensions = !context.legacy ? null : schema.CAUTION_MEGA_DANGER_ZONE_objectExtensions(field, null);
|
|
2453
|
+
const proxy = new Proxy(this[SOURCE$1], {
|
|
2454
|
+
ownKeys() {
|
|
2455
|
+
return Object.keys(self[SOURCE$1]);
|
|
2456
|
+
},
|
|
2457
|
+
has(target, prop) {
|
|
2458
|
+
return prop in self[SOURCE$1];
|
|
2459
|
+
},
|
|
2460
|
+
getOwnPropertyDescriptor(target, prop) {
|
|
2461
|
+
return {
|
|
2462
|
+
writable: context.editable,
|
|
2463
|
+
enumerable: true,
|
|
2464
|
+
configurable: true
|
|
2465
|
+
};
|
|
2466
|
+
},
|
|
2467
|
+
get(target, prop, receiver) {
|
|
2468
|
+
if (ObjectSymbols.has(prop)) {
|
|
2469
|
+
return self[prop];
|
|
2470
|
+
}
|
|
2471
|
+
if (prop === Symbol.toPrimitive) {
|
|
2472
|
+
return () => null;
|
|
2473
|
+
}
|
|
2474
|
+
if (prop === Symbol.toStringTag) {
|
|
2475
|
+
return `ManagedObject<${identifier.type}:${identifier.id} (${identifier.lid})>`;
|
|
2476
|
+
}
|
|
2477
|
+
if (prop === 'constructor') {
|
|
2478
|
+
return Object;
|
|
2479
|
+
}
|
|
2480
|
+
if (prop === 'toString') {
|
|
2481
|
+
return function () {
|
|
2482
|
+
return `ManagedObject<${identifier.type}:${identifier.id} (${identifier.lid})>`;
|
|
2483
|
+
};
|
|
2484
|
+
}
|
|
2485
|
+
if (prop === 'toHTML') {
|
|
2486
|
+
return function () {
|
|
2487
|
+
return '<span>ManagedObject</span>';
|
|
2488
|
+
};
|
|
2489
|
+
}
|
|
2490
|
+
if (_SIGNAL.isStale) {
|
|
2491
|
+
_SIGNAL.isStale = false;
|
|
2492
|
+
let newData = cache.getAttr(identifier, path);
|
|
2493
|
+
if (newData && newData !== self[SOURCE$1]) {
|
|
2494
|
+
if (field.type) {
|
|
2495
|
+
const transform = schema.transformation(field);
|
|
2496
|
+
newData = transform.hydrate(newData, field.options ?? null, context.record);
|
|
2497
|
+
}
|
|
2498
|
+
self[SOURCE$1] = Object.assign({}, newData); // Add type assertion for newData
|
|
2499
|
+
}
|
|
2500
|
+
}
|
|
2501
|
+
|
|
2502
|
+
// toJSON and extensions need to come after we update data if stale
|
|
2503
|
+
if (prop === 'toJSON') {
|
|
2504
|
+
return function () {
|
|
2505
|
+
return structuredClone(self[SOURCE$1]);
|
|
2506
|
+
};
|
|
2507
|
+
}
|
|
2508
|
+
|
|
2509
|
+
// we always defer to data before extensions
|
|
2510
|
+
if (prop in self[SOURCE$1]) {
|
|
2511
|
+
consumeInternalSignal(_SIGNAL);
|
|
2512
|
+
return self[SOURCE$1][prop];
|
|
2513
|
+
}
|
|
2514
|
+
if (isExtensionProp(extensions, prop)) {
|
|
2515
|
+
return performObjectExtensionGet(receiver, extensions, signals, prop);
|
|
2516
|
+
}
|
|
2517
|
+
return Reflect.get(target, prop, receiver);
|
|
2518
|
+
},
|
|
2519
|
+
set(target, prop, value, receiver) {
|
|
2520
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
2521
|
+
if (!test) {
|
|
2522
|
+
throw new Error(`Cannot set read-only property '${String(prop)}' on ManagedObject`);
|
|
2523
|
+
}
|
|
2524
|
+
})(context.editable) : {};
|
|
2525
|
+
|
|
2526
|
+
// since objects function as dictionaries, we can't defer to schema/data before extensions
|
|
2527
|
+
// unless the prop is in the existing data.
|
|
2528
|
+
if (!(prop in self[SOURCE$1]) && isExtensionProp(extensions, prop)) {
|
|
2529
|
+
return performExtensionSet(receiver, extensions, signals, prop, value);
|
|
2530
|
+
}
|
|
2531
|
+
const reflect = Reflect.set(target, prop, value, receiver);
|
|
2532
|
+
if (!reflect) {
|
|
2533
|
+
return false;
|
|
2534
|
+
}
|
|
2535
|
+
if (!field.type) {
|
|
2536
|
+
cache.setAttr(identifier, path, self[SOURCE$1]);
|
|
2537
|
+
} else {
|
|
2538
|
+
const transform = schema.transformation(field);
|
|
2539
|
+
const val = transform.serialize(self[SOURCE$1], field.options ?? null, context.record);
|
|
2540
|
+
cache.setAttr(identifier, path, val);
|
|
2541
|
+
}
|
|
2542
|
+
_SIGNAL.isStale = true;
|
|
2543
|
+
return true;
|
|
2544
|
+
}
|
|
2545
|
+
});
|
|
2546
|
+
return proxy;
|
|
2547
|
+
}
|
|
2548
|
+
}
|
|
2549
|
+
const ManagedObjectMap = getOrSetGlobal('ManagedObjectMap', new Map());
|
|
2550
|
+
function peekManagedObject(record, field) {
|
|
2551
|
+
const managedObjectMapForRecord = ManagedObjectMap.get(record);
|
|
2552
|
+
if (managedObjectMapForRecord) {
|
|
2553
|
+
return managedObjectMapForRecord.get(field.name);
|
|
2554
|
+
}
|
|
2555
|
+
}
|
|
2556
|
+
function getObjectField(context) {
|
|
2557
|
+
entangleSignal(context.signals, context.record, context.path.at(-1), null);
|
|
2558
|
+
const {
|
|
2559
|
+
record,
|
|
2560
|
+
field
|
|
2561
|
+
} = context;
|
|
2562
|
+
const managedObjectMapForRecord = ManagedObjectMap.get(record);
|
|
2563
|
+
let managedObject;
|
|
2564
|
+
if (managedObjectMapForRecord) {
|
|
2565
|
+
managedObject = managedObjectMapForRecord.get(field.name);
|
|
2566
|
+
}
|
|
2567
|
+
if (managedObject) {
|
|
2568
|
+
return managedObject;
|
|
2569
|
+
} else {
|
|
2570
|
+
const {
|
|
2571
|
+
store,
|
|
2572
|
+
resourceKey,
|
|
2573
|
+
path
|
|
2574
|
+
} = context;
|
|
2575
|
+
const {
|
|
2576
|
+
cache,
|
|
2577
|
+
schema
|
|
2578
|
+
} = store;
|
|
2579
|
+
let rawValue = context.editable ? cache.getAttr(resourceKey, path) : cache.getRemoteAttr(resourceKey, path);
|
|
2580
|
+
if (!rawValue) {
|
|
2581
|
+
return null;
|
|
2582
|
+
}
|
|
2583
|
+
if (field.type) {
|
|
2584
|
+
const transform = schema.transformation(field);
|
|
2585
|
+
rawValue = transform.hydrate(rawValue, field.options ?? null, record);
|
|
2586
|
+
}
|
|
2587
|
+
managedObject = new ManagedObject({
|
|
2588
|
+
store,
|
|
2589
|
+
resourceKey,
|
|
2590
|
+
modeName: context.modeName,
|
|
2591
|
+
legacy: context.legacy,
|
|
2592
|
+
editable: context.editable,
|
|
2593
|
+
path,
|
|
2594
|
+
field,
|
|
2595
|
+
record,
|
|
2596
|
+
signals: context.signals,
|
|
2597
|
+
value: rawValue
|
|
2598
|
+
});
|
|
2599
|
+
if (!managedObjectMapForRecord) {
|
|
2600
|
+
ManagedObjectMap.set(record, new Map([[field.name, managedObject]]));
|
|
2601
|
+
} else {
|
|
2602
|
+
managedObjectMapForRecord.set(field.name, managedObject);
|
|
2603
|
+
}
|
|
2604
|
+
}
|
|
2605
|
+
return managedObject;
|
|
2606
|
+
}
|
|
2607
|
+
function setObjectField(context) {
|
|
2608
|
+
const {
|
|
2609
|
+
field,
|
|
2610
|
+
value,
|
|
2611
|
+
record
|
|
2612
|
+
} = context;
|
|
2613
|
+
const {
|
|
2614
|
+
cache,
|
|
2615
|
+
schema
|
|
2616
|
+
} = context.store;
|
|
2617
|
+
if (!field.type) {
|
|
2618
|
+
let newValue = value;
|
|
2619
|
+
if (value !== null) {
|
|
2620
|
+
newValue = {
|
|
2621
|
+
...value
|
|
2622
|
+
};
|
|
2623
|
+
} else {
|
|
2624
|
+
ManagedObjectMap.delete(record);
|
|
2625
|
+
}
|
|
2626
|
+
cache.setAttr(context.resourceKey, context.path, newValue);
|
|
2627
|
+
const peeked = peekManagedObject(record, field);
|
|
2628
|
+
if (peeked) {
|
|
2629
|
+
const objSignal = peeked[OBJECT_SIGNAL];
|
|
2630
|
+
objSignal.isStale = true;
|
|
2631
|
+
}
|
|
2632
|
+
return true;
|
|
2633
|
+
}
|
|
2634
|
+
const transform = schema.transformation(field);
|
|
2635
|
+
const rawValue = transform.serialize({
|
|
2636
|
+
...value
|
|
2637
|
+
}, field.options ?? null, record);
|
|
2638
|
+
cache.setAttr(context.resourceKey, context.path, rawValue);
|
|
2639
|
+
const peeked = peekManagedObject(record, field);
|
|
2640
|
+
if (peeked) {
|
|
2641
|
+
const objSignal = peeked[OBJECT_SIGNAL];
|
|
2642
|
+
objSignal.isStale = true;
|
|
2643
|
+
}
|
|
2644
|
+
return true;
|
|
2645
|
+
}
|
|
2646
|
+
|
|
2647
|
+
// TODO probably this should just be a Document
|
|
2648
|
+
// but its separate until we work out the lid situation
|
|
2649
|
+
class ResourceRelationship {
|
|
2650
|
+
constructor(context) {
|
|
2651
|
+
const {
|
|
2652
|
+
store,
|
|
2653
|
+
resourceKey
|
|
2654
|
+
} = context;
|
|
2655
|
+
const {
|
|
2656
|
+
cache
|
|
2657
|
+
} = store;
|
|
2658
|
+
const name = getFieldCacheKeyStrict(context.field);
|
|
2659
|
+
const rawValue = context.editable ? cache.getRelationship(resourceKey, name) : cache.getRemoteRelationship(resourceKey, name);
|
|
2660
|
+
|
|
2661
|
+
// TODO setup true lids for relationship documents
|
|
2662
|
+
// @ts-expect-error we need to give relationship documents a lid
|
|
2663
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
2664
|
+
this.lid = rawValue.lid ?? rawValue.links?.self ?? `relationship:${resourceKey.lid}.${name}`;
|
|
2665
|
+
this.data = rawValue.data ? store.peekRecord(rawValue.data) : null;
|
|
2666
|
+
this.name = name;
|
|
2667
|
+
if (macroCondition(getGlobalConfig().WarpDrive.env.DEBUG)) {
|
|
2668
|
+
this.links = Object.freeze(Object.assign({}, rawValue.links));
|
|
2669
|
+
this.meta = Object.freeze(Object.assign({}, rawValue.meta));
|
|
2670
|
+
} else {
|
|
2671
|
+
this.links = rawValue.links ?? {};
|
|
2672
|
+
this.meta = rawValue.meta ?? {};
|
|
2673
|
+
}
|
|
2674
|
+
this[Context] = context;
|
|
2675
|
+
}
|
|
2676
|
+
fetch(options) {
|
|
2677
|
+
const url = options?.url ?? getHref(this.links.related) ?? getHref(this.links.self) ?? null;
|
|
2678
|
+
if (!url) {
|
|
2679
|
+
throw new Error(`Cannot ${options?.method ?? 'fetch'} ${this[Context].resourceKey.type}.${String(this.name)} because it has no related link`);
|
|
2680
|
+
}
|
|
2681
|
+
const request = Object.assign({
|
|
2682
|
+
url,
|
|
2683
|
+
method: 'GET'
|
|
2684
|
+
}, options);
|
|
2685
|
+
return this[Context].store.request(request);
|
|
2686
|
+
}
|
|
2687
|
+
}
|
|
2688
|
+
defineSignal(ResourceRelationship.prototype, 'data', null);
|
|
2689
|
+
defineSignal(ResourceRelationship.prototype, 'links', null);
|
|
2690
|
+
defineSignal(ResourceRelationship.prototype, 'meta', null);
|
|
2691
|
+
function getHref(link) {
|
|
2692
|
+
if (!link) {
|
|
2693
|
+
return null;
|
|
2694
|
+
}
|
|
2695
|
+
if (typeof link === 'string') {
|
|
2696
|
+
return link;
|
|
2697
|
+
}
|
|
2698
|
+
return link.href;
|
|
2699
|
+
}
|
|
2700
|
+
function getResourceField(context) {
|
|
2701
|
+
entangleSignal(context.signals, context.record, context.path.at(-1), null);
|
|
2702
|
+
return new ResourceRelationship(context);
|
|
2703
|
+
}
|
|
2704
|
+
function setResourceField(context) {
|
|
2705
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
2706
|
+
{
|
|
2707
|
+
throw new Error(`setting resource relationships is not yet supported`);
|
|
2708
|
+
}
|
|
2709
|
+
})() : {};
|
|
2710
|
+
return false;
|
|
2711
|
+
}
|
|
2712
|
+
function setSchemaArrayField(context) {
|
|
2713
|
+
const arrayValue = context.value?.slice();
|
|
2714
|
+
const fieldSignal = peekInternalSignal(context.signals, context.path.at(-1));
|
|
2715
|
+
const peeked = fieldSignal?.value;
|
|
2716
|
+
context.store.cache.setAttr(context.resourceKey, context.path, arrayValue);
|
|
2717
|
+
if (peeked) {
|
|
2718
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
2719
|
+
if (!test) {
|
|
2720
|
+
throw new Error(`Expected the peekManagedArray for ${context.field.kind} to return a ManagedArray`);
|
|
2721
|
+
}
|
|
2722
|
+
})(ARRAY_SIGNAL in peeked) : {};
|
|
2723
|
+
const arrSignal = peeked[ARRAY_SIGNAL];
|
|
2724
|
+
arrSignal.isStale = true;
|
|
2725
|
+
if (!Array.isArray(arrayValue)) {
|
|
2726
|
+
fieldSignal.value = null;
|
|
2727
|
+
}
|
|
2728
|
+
}
|
|
2729
|
+
return true;
|
|
2730
|
+
}
|
|
2731
|
+
function getSchemaObjectField(context) {
|
|
2732
|
+
const signal = entangleSignal(context.signals, context.record, context.path.at(-1), null);
|
|
2733
|
+
const {
|
|
2734
|
+
store,
|
|
2735
|
+
resourceKey,
|
|
2736
|
+
path,
|
|
2737
|
+
field
|
|
2738
|
+
} = context;
|
|
2739
|
+
const {
|
|
2740
|
+
cache
|
|
2741
|
+
} = store;
|
|
2742
|
+
let rawValue = context.editable ? cache.getAttr(resourceKey, path) : cache.getRemoteAttr(resourceKey, path);
|
|
2743
|
+
if (!rawValue && !field.options?.polymorphic && field.options?.defaultValue) {
|
|
2744
|
+
rawValue = {};
|
|
2745
|
+
}
|
|
2746
|
+
if (!rawValue) {
|
|
2747
|
+
if (signal.value) {
|
|
2748
|
+
const value = signal.value;
|
|
2749
|
+
// TODO if we had idle scheduling this should be done there.
|
|
2750
|
+
void Promise.resolve().then(() => {
|
|
2751
|
+
value.value[Destroy]();
|
|
2752
|
+
});
|
|
2753
|
+
signal.value = null;
|
|
2754
|
+
}
|
|
2755
|
+
return null;
|
|
2756
|
+
}
|
|
2757
|
+
const {
|
|
2758
|
+
schema
|
|
2759
|
+
} = store;
|
|
2760
|
+
let objectType;
|
|
2761
|
+
if (field.options?.polymorphic) {
|
|
2762
|
+
const typePath = field.options.type ?? 'type';
|
|
2763
|
+
// if we are polymorphic, then context.field.options.type will
|
|
2764
|
+
// either specify a path on the rawValue to use as the type, defaulting to "type" or
|
|
2765
|
+
// the special string "@hash" which tells us to treat field.type as a hashFn name with which
|
|
2766
|
+
// to calc the type.
|
|
2767
|
+
if (typePath === '@hash') {
|
|
2768
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
2769
|
+
if (!test) {
|
|
2770
|
+
throw new Error(`Expected the field to define a hashFn as its type`);
|
|
2771
|
+
}
|
|
2772
|
+
})(field.type) : {};
|
|
2773
|
+
const hashFn = schema.hashFn({
|
|
2774
|
+
type: field.type
|
|
2775
|
+
});
|
|
2776
|
+
// TODO consider if there are better options and name args we could provide.
|
|
2777
|
+
objectType = hashFn(rawValue, null, null);
|
|
2778
|
+
} else {
|
|
2779
|
+
objectType = rawValue[typePath];
|
|
2780
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
2781
|
+
if (!test) {
|
|
2782
|
+
throw new Error(`Expected the type path for the field to be a value on the raw object`);
|
|
2783
|
+
}
|
|
2784
|
+
})(typePath && objectType && typeof objectType === 'string') : {};
|
|
2785
|
+
}
|
|
2786
|
+
} else {
|
|
2787
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
2788
|
+
if (!test) {
|
|
2789
|
+
throw new Error(`A non-polymorphic SchemaObjectField must provide a SchemaObject type in its definition`);
|
|
2790
|
+
}
|
|
2791
|
+
})(field.type) : {};
|
|
2792
|
+
objectType = field.type;
|
|
2793
|
+
}
|
|
2794
|
+
const hashField = schema.resource({
|
|
2795
|
+
type: objectType
|
|
2796
|
+
}).identity;
|
|
2797
|
+
const identity = hashField ? schema.hashFn(hashField)(rawValue, hashField.options ?? null, hashField.name) : field.name;
|
|
2798
|
+
const cachedSchemaObject = signal.value;
|
|
2799
|
+
if (cachedSchemaObject) {
|
|
2800
|
+
if (cachedSchemaObject.type === objectType && cachedSchemaObject.identity === identity) {
|
|
2801
|
+
return cachedSchemaObject.value;
|
|
2802
|
+
} else {
|
|
2803
|
+
// TODO if we had idle scheduling this should be done there.
|
|
2804
|
+
void Promise.resolve().then(() => {
|
|
2805
|
+
cachedSchemaObject.value[Destroy]();
|
|
2806
|
+
});
|
|
2807
|
+
}
|
|
2808
|
+
}
|
|
2809
|
+
const schemaObject = new ReactiveResource({
|
|
2810
|
+
store: context.store,
|
|
2811
|
+
resourceKey: context.resourceKey,
|
|
2812
|
+
modeName: context.modeName,
|
|
2813
|
+
legacy: context.legacy,
|
|
2814
|
+
editable: context.editable,
|
|
2815
|
+
path: context.path,
|
|
2816
|
+
field: context.field,
|
|
2817
|
+
value: objectType
|
|
2818
|
+
});
|
|
2819
|
+
signal.value = {
|
|
2820
|
+
type: objectType,
|
|
2821
|
+
identity: identity,
|
|
2822
|
+
value: schemaObject
|
|
2823
|
+
};
|
|
2824
|
+
return schemaObject;
|
|
2825
|
+
}
|
|
2826
|
+
function setSchemaObjectField(context) {
|
|
2827
|
+
const {
|
|
2828
|
+
store,
|
|
2829
|
+
value
|
|
2830
|
+
} = context;
|
|
2831
|
+
let newValue = value;
|
|
2832
|
+
if (value !== null) {
|
|
2833
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
2834
|
+
if (!test) {
|
|
2835
|
+
throw new Error(`Expected value to be an object`);
|
|
2836
|
+
}
|
|
2837
|
+
})(typeof value === 'object') : {};
|
|
2838
|
+
newValue = {
|
|
2839
|
+
...value
|
|
2840
|
+
};
|
|
2841
|
+
// FIXME the case of field.type to string here is likely incorrect
|
|
2842
|
+
const schemaFields = store.schema.fields({
|
|
2843
|
+
type: context.field.type
|
|
2844
|
+
});
|
|
2845
|
+
for (const key of Object.keys(newValue)) {
|
|
2846
|
+
if (!schemaFields.has(key)) {
|
|
2847
|
+
throw new Error(`Field ${key} does not exist on schema object ${context.field.type}`);
|
|
2848
|
+
}
|
|
2849
|
+
}
|
|
2850
|
+
} else {
|
|
2851
|
+
ManagedObjectMap.delete(context.record);
|
|
2852
|
+
}
|
|
2853
|
+
store.cache.setAttr(context.resourceKey, context.path, newValue);
|
|
2854
|
+
// const peeked = peekManagedObject(self, field);
|
|
2855
|
+
// if (peeked) {
|
|
2856
|
+
// const objSignal = peeked[OBJECT_SIGNAL];
|
|
2857
|
+
// objSignal.isStale = true;
|
|
2858
|
+
// }
|
|
2859
|
+
return true;
|
|
2860
|
+
}
|
|
2861
|
+
const DefaultMode = {
|
|
2862
|
+
'@hash': {
|
|
2863
|
+
get: getHashField,
|
|
2864
|
+
set: setHashField,
|
|
2865
|
+
mutable: false,
|
|
2866
|
+
enumerable: false,
|
|
2867
|
+
serializable: false
|
|
2868
|
+
},
|
|
2869
|
+
'@id': {
|
|
2870
|
+
get: getIdentityField,
|
|
2871
|
+
set: setIdentityField,
|
|
2872
|
+
mutable: true,
|
|
2873
|
+
enumerable: true,
|
|
2874
|
+
serializable: true
|
|
2875
|
+
},
|
|
2876
|
+
'@local': {
|
|
2877
|
+
get: getLocalField,
|
|
2878
|
+
set: setLocalField,
|
|
2879
|
+
mutable: true,
|
|
2880
|
+
enumerable: false,
|
|
2881
|
+
serializable: false
|
|
2882
|
+
},
|
|
2883
|
+
alias: {
|
|
2884
|
+
get: getAliasField,
|
|
2885
|
+
set: setAliasField,
|
|
2886
|
+
mutable: true,
|
|
2887
|
+
enumerable: true,
|
|
2888
|
+
serializable: false
|
|
2889
|
+
},
|
|
2890
|
+
array: {
|
|
2891
|
+
get: getArrayField,
|
|
2892
|
+
set: setArrayField,
|
|
2893
|
+
mutable: true,
|
|
2894
|
+
enumerable: true,
|
|
2895
|
+
serializable: true
|
|
2896
|
+
},
|
|
2897
|
+
attribute: {
|
|
2898
|
+
get: getAttributeField,
|
|
2899
|
+
set: setAttributeField,
|
|
2900
|
+
mutable: true,
|
|
2901
|
+
enumerable: true,
|
|
2902
|
+
serializable: true
|
|
2903
|
+
},
|
|
2904
|
+
belongsTo: {
|
|
2905
|
+
get: getBelongsToField,
|
|
2906
|
+
set: setBelongsToField,
|
|
2907
|
+
mutable: true,
|
|
2908
|
+
enumerable: true,
|
|
2909
|
+
serializable: true
|
|
2910
|
+
},
|
|
2911
|
+
collection: {
|
|
2912
|
+
get: getCollectionField,
|
|
2913
|
+
set: setCollectionField,
|
|
2914
|
+
mutable: true,
|
|
2915
|
+
enumerable: true,
|
|
2916
|
+
serializable: true
|
|
2917
|
+
},
|
|
2918
|
+
derived: {
|
|
2919
|
+
get: getDerivedField,
|
|
2920
|
+
set: setDerivedField,
|
|
2921
|
+
mutable: true,
|
|
2922
|
+
enumerable: true,
|
|
2923
|
+
serializable: false
|
|
2924
|
+
},
|
|
2925
|
+
field: {
|
|
2926
|
+
get: getGenericField,
|
|
2927
|
+
set: setGenericField,
|
|
2928
|
+
mutable: true,
|
|
2929
|
+
enumerable: true,
|
|
2930
|
+
serializable: true
|
|
2931
|
+
},
|
|
2932
|
+
hasMany: {
|
|
2933
|
+
get: getHasManyField,
|
|
2934
|
+
set: setHasManyField,
|
|
2935
|
+
mutable: true,
|
|
2936
|
+
enumerable: true,
|
|
2937
|
+
serializable: true
|
|
2938
|
+
},
|
|
2939
|
+
object: {
|
|
2940
|
+
get: getObjectField,
|
|
2941
|
+
set: setObjectField,
|
|
2942
|
+
mutable: true,
|
|
2943
|
+
enumerable: true,
|
|
2944
|
+
serializable: true
|
|
2945
|
+
},
|
|
2946
|
+
resource: {
|
|
2947
|
+
get: getResourceField,
|
|
2948
|
+
set: setResourceField,
|
|
2949
|
+
mutable: true,
|
|
2950
|
+
enumerable: true,
|
|
2951
|
+
serializable: true
|
|
2952
|
+
},
|
|
2953
|
+
'schema-array': {
|
|
2954
|
+
get: getArrayField,
|
|
2955
|
+
set: setSchemaArrayField,
|
|
2956
|
+
mutable: true,
|
|
2957
|
+
enumerable: true,
|
|
2958
|
+
serializable: true
|
|
2959
|
+
},
|
|
2960
|
+
'schema-object': {
|
|
2961
|
+
get: getSchemaObjectField,
|
|
2962
|
+
set: setSchemaObjectField,
|
|
2963
|
+
mutable: true,
|
|
2964
|
+
enumerable: true,
|
|
2965
|
+
serializable: true
|
|
2966
|
+
}
|
|
2967
|
+
};
|
|
2968
|
+
const IgnoredGlobalFields = new Set(['length', 'nodeType', 'then', 'setInterval', 'document', STRUCTURED]);
|
|
2969
|
+
const symbolList = [Context, Destroy, RecordStore, Checkout];
|
|
2970
|
+
const RecordSymbols = new Set(symbolList);
|
|
2971
|
+
function isPathMatch(a, b) {
|
|
2972
|
+
return a.length === b.length && a.every((v, i) => v === b[i]);
|
|
2973
|
+
}
|
|
2974
|
+
function isNonEnumerableProp(prop) {
|
|
2975
|
+
return prop === 'constructor' || prop === 'prototype' || prop === '__proto__' || prop === 'toString' || prop === 'toJSON' || prop === 'toHTML' || typeof prop === 'symbol';
|
|
2976
|
+
}
|
|
2977
|
+
const Editables = new Map();
|
|
2978
|
+
/**
|
|
2979
|
+
* A class that uses a the ResourceSchema for a ResourceType
|
|
2980
|
+
* and a ResouceKey to transform data from the cache into a rich, reactive
|
|
2981
|
+
* object.
|
|
2982
|
+
*
|
|
2983
|
+
* This class is not directly instantiable. To use it, you should
|
|
2984
|
+
* configure the store's `instantiateRecord` and `teardownRecord` hooks
|
|
2985
|
+
* with the matching hooks provided by this package.
|
|
2986
|
+
*
|
|
2987
|
+
* @hideconstructor
|
|
2988
|
+
* @public
|
|
2989
|
+
*/
|
|
2990
|
+
// eslint-disable-next-line @typescript-eslint/no-extraneous-class
|
|
2991
|
+
class ReactiveResource {
|
|
2992
|
+
constructor(context) {
|
|
2993
|
+
const resourceKey = context.resourceKey;
|
|
2994
|
+
const isEmbedded = context.path !== null;
|
|
2995
|
+
const schema = context.store.schema;
|
|
2996
|
+
const objectType = isEmbedded ? context.value : resourceKey.type;
|
|
2997
|
+
const ResourceSchema = schema.resource(isEmbedded ? {
|
|
2998
|
+
type: objectType
|
|
2999
|
+
} : resourceKey);
|
|
3000
|
+
const identityField = ResourceSchema.identity;
|
|
3001
|
+
const BoundFns = new Map();
|
|
3002
|
+
|
|
3003
|
+
// prettier-ignore
|
|
3004
|
+
const extensions = !context.legacy ? null : isEmbedded ? schema.CAUTION_MEGA_DANGER_ZONE_objectExtensions(context.field, objectType) : schema.CAUTION_MEGA_DANGER_ZONE_resourceExtensions(resourceKey);
|
|
3005
|
+
this[Context] = context;
|
|
3006
|
+
this[RecordStore] = context.store;
|
|
3007
|
+
const fields = isEmbedded ? schema.fields({
|
|
3008
|
+
type: objectType
|
|
3009
|
+
}) : schema.fields(resourceKey);
|
|
3010
|
+
const method = typeof schema.cacheFields === 'function' ? 'cacheFields' : 'fields';
|
|
3011
|
+
const cacheFields = isEmbedded ? schema[method]({
|
|
3012
|
+
type: objectType
|
|
3013
|
+
}) : schema[method](resourceKey);
|
|
3014
|
+
const signals = withSignalStore(this);
|
|
3015
|
+
this.___notifications = context.store.notifications.subscribe(resourceKey, (_, type, key) => {
|
|
3016
|
+
switch (type) {
|
|
3017
|
+
case 'identity':
|
|
3018
|
+
{
|
|
3019
|
+
if (isEmbedded || !identityField) return; // base paths never apply to embedded records
|
|
3020
|
+
|
|
3021
|
+
if (identityField.name && identityField.kind === '@id') {
|
|
3022
|
+
const signal = signals.get('@identity');
|
|
3023
|
+
if (signal) {
|
|
3024
|
+
notifyInternalSignal(signal);
|
|
3025
|
+
}
|
|
3026
|
+
}
|
|
3027
|
+
break;
|
|
3028
|
+
}
|
|
3029
|
+
case 'attributes':
|
|
3030
|
+
if (key) {
|
|
3031
|
+
if (Array.isArray(key)) {
|
|
3032
|
+
if (context.path === null) return; // deep paths will be handled by embedded records
|
|
3033
|
+
// TODO we should have the notification manager
|
|
3034
|
+
// ensure it is safe for each callback to mutate this array
|
|
3035
|
+
if (isPathMatch(context.path, key)) {
|
|
3036
|
+
// handle the notification
|
|
3037
|
+
// TODO we should likely handle this notification here
|
|
3038
|
+
// also we should add a LOGGING flag
|
|
3039
|
+
// eslint-disable-next-line no-console
|
|
3040
|
+
console.warn(`Notification unhandled for ${key.join(',')} on ${resourceKey.type}`, proxy);
|
|
3041
|
+
return;
|
|
3042
|
+
}
|
|
3043
|
+
|
|
3044
|
+
// TODO we should add a LOGGING flag
|
|
3045
|
+
// console.log(`Deep notification skipped for ${key.join('.')} on ${identifier.type}`, proxy);
|
|
3046
|
+
// deep notify the key path
|
|
3047
|
+
} else {
|
|
3048
|
+
if (isEmbedded) return; // base paths never apply to embedded records
|
|
3049
|
+
|
|
3050
|
+
// TODO determine what LOGGING flag to wrap this in if any
|
|
3051
|
+
// console.log(`Notification for ${key} on ${identifier.type}`, proxy);
|
|
3052
|
+
const signal = signals.get(key);
|
|
3053
|
+
if (signal) {
|
|
3054
|
+
notifyInternalSignal(signal);
|
|
3055
|
+
}
|
|
3056
|
+
const field = cacheFields.get(key);
|
|
3057
|
+
if (field?.kind === 'array' || field?.kind === 'schema-array') {
|
|
3058
|
+
const peeked = signal?.value;
|
|
3059
|
+
if (peeked) {
|
|
3060
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
3061
|
+
if (!test) {
|
|
3062
|
+
throw new Error(`Expected the peekManagedArray for ${field.kind} to return a ManagedArray`);
|
|
3063
|
+
}
|
|
3064
|
+
})(ARRAY_SIGNAL in peeked) : {};
|
|
3065
|
+
const arrSignal = peeked[ARRAY_SIGNAL];
|
|
3066
|
+
notifyInternalSignal(arrSignal);
|
|
3067
|
+
}
|
|
3068
|
+
}
|
|
3069
|
+
if (field?.kind === 'object') {
|
|
3070
|
+
const peeked = peekManagedObject(proxy, field);
|
|
3071
|
+
if (peeked) {
|
|
3072
|
+
const objSignal = peeked[OBJECT_SIGNAL];
|
|
3073
|
+
notifyInternalSignal(objSignal);
|
|
3074
|
+
}
|
|
3075
|
+
}
|
|
3076
|
+
}
|
|
3077
|
+
}
|
|
3078
|
+
break;
|
|
3079
|
+
case 'relationships':
|
|
3080
|
+
if (key) {
|
|
3081
|
+
if (Array.isArray(key)) ;else {
|
|
3082
|
+
if (isEmbedded) return; // base paths never apply to embedded records
|
|
3083
|
+
|
|
3084
|
+
const field = cacheFields.get(key);
|
|
3085
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
3086
|
+
if (!test) {
|
|
3087
|
+
throw new Error(`Expected relationship ${key} to be the name of a field`);
|
|
3088
|
+
}
|
|
3089
|
+
})(field) : {};
|
|
3090
|
+
if (field.kind === 'belongsTo') {
|
|
3091
|
+
// TODO determine what LOGGING flag to wrap this in if any
|
|
3092
|
+
// console.log(`Notification for ${key} on ${identifier.type}`, proxy);
|
|
3093
|
+
const signal = signals.get(key);
|
|
3094
|
+
if (signal) {
|
|
3095
|
+
notifyInternalSignal(signal);
|
|
3096
|
+
}
|
|
3097
|
+
// FIXME
|
|
3098
|
+
} else if (field.kind === 'resource') ;else if (field.kind === 'hasMany') {
|
|
3099
|
+
if (field.options.linksMode) {
|
|
3100
|
+
const signal = signals.get(key);
|
|
3101
|
+
if (signal) {
|
|
3102
|
+
const peeked = signal.value;
|
|
3103
|
+
if (peeked) {
|
|
3104
|
+
notifyInternalSignal(peeked[ARRAY_SIGNAL]);
|
|
3105
|
+
}
|
|
3106
|
+
}
|
|
3107
|
+
return;
|
|
3108
|
+
}
|
|
3109
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
3110
|
+
if (!test) {
|
|
3111
|
+
throw new Error(`Can only use hasMany fields when the resource is in legacy mode`);
|
|
3112
|
+
}
|
|
3113
|
+
})(context.legacy) : {};
|
|
3114
|
+
if (schema._kind('@legacy', 'hasMany').notify(context.store, proxy, resourceKey, field)) {
|
|
3115
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
3116
|
+
if (!test) {
|
|
3117
|
+
throw new Error(`Expected options to exist on relationship meta`);
|
|
3118
|
+
}
|
|
3119
|
+
})(field.options) : {};
|
|
3120
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
3121
|
+
if (!test) {
|
|
3122
|
+
throw new Error(`Expected async to exist on relationship meta options`);
|
|
3123
|
+
}
|
|
3124
|
+
})('async' in field.options) : {};
|
|
3125
|
+
if (field.options.async) {
|
|
3126
|
+
const signal = signals.get(key);
|
|
3127
|
+
if (signal) {
|
|
3128
|
+
notifyInternalSignal(signal);
|
|
3129
|
+
}
|
|
3130
|
+
}
|
|
3131
|
+
}
|
|
3132
|
+
} else if (field.kind === 'collection') ;
|
|
3133
|
+
}
|
|
3134
|
+
}
|
|
3135
|
+
break;
|
|
3136
|
+
}
|
|
3137
|
+
});
|
|
3138
|
+
const proxy = new Proxy(this, {
|
|
3139
|
+
ownKeys() {
|
|
3140
|
+
const identityKey = identityField?.name;
|
|
3141
|
+
const keys = Array.from(fields.keys());
|
|
3142
|
+
if (identityKey) {
|
|
3143
|
+
keys.unshift(identityKey);
|
|
3144
|
+
}
|
|
3145
|
+
return keys;
|
|
3146
|
+
},
|
|
3147
|
+
has(target, prop) {
|
|
3148
|
+
if (prop === Destroy || prop === Checkout) {
|
|
3149
|
+
return true;
|
|
3150
|
+
}
|
|
3151
|
+
return fields.has(prop);
|
|
3152
|
+
},
|
|
3153
|
+
getOwnPropertyDescriptor(target, prop) {
|
|
3154
|
+
const schemaForField = prop === identityField?.name ? identityField : fields.get(prop);
|
|
3155
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
3156
|
+
if (!test) {
|
|
3157
|
+
throw new Error(`No field named ${String(prop)} on ${resourceKey.type}`);
|
|
3158
|
+
}
|
|
3159
|
+
})(schemaForField) : {};
|
|
3160
|
+
if (isNonEnumerableProp(prop)) {
|
|
3161
|
+
return {
|
|
3162
|
+
writable: false,
|
|
3163
|
+
enumerable: false,
|
|
3164
|
+
configurable: true
|
|
3165
|
+
};
|
|
3166
|
+
}
|
|
3167
|
+
switch (schemaForField.kind) {
|
|
3168
|
+
case 'derived':
|
|
3169
|
+
return {
|
|
3170
|
+
writable: false,
|
|
3171
|
+
enumerable: true,
|
|
3172
|
+
configurable: true
|
|
3173
|
+
};
|
|
3174
|
+
case '@id':
|
|
3175
|
+
return {
|
|
3176
|
+
writable: resourceKey.id === null,
|
|
3177
|
+
enumerable: true,
|
|
3178
|
+
configurable: true
|
|
3179
|
+
};
|
|
3180
|
+
case '@local':
|
|
3181
|
+
case 'field':
|
|
3182
|
+
case 'attribute':
|
|
3183
|
+
case 'resource':
|
|
3184
|
+
case 'alias':
|
|
3185
|
+
case 'belongsTo':
|
|
3186
|
+
case 'hasMany':
|
|
3187
|
+
case 'collection':
|
|
3188
|
+
case 'schema-array':
|
|
3189
|
+
case 'array':
|
|
3190
|
+
case 'schema-object':
|
|
3191
|
+
case 'object':
|
|
3192
|
+
return {
|
|
3193
|
+
writable: context.editable,
|
|
3194
|
+
enumerable: true,
|
|
3195
|
+
configurable: true
|
|
3196
|
+
};
|
|
3197
|
+
default:
|
|
3198
|
+
return {
|
|
3199
|
+
writable: false,
|
|
3200
|
+
enumerable: false,
|
|
3201
|
+
configurable: false
|
|
3202
|
+
};
|
|
3203
|
+
}
|
|
3204
|
+
},
|
|
3205
|
+
get(target, prop, receiver) {
|
|
3206
|
+
if (RecordSymbols.has(prop)) {
|
|
3207
|
+
if (prop === Destroy) {
|
|
3208
|
+
return () => _DESTROY(receiver);
|
|
3209
|
+
}
|
|
3210
|
+
if (prop === Checkout) {
|
|
3211
|
+
return () => Promise.resolve(_CHECKOUT(receiver));
|
|
3212
|
+
}
|
|
3213
|
+
return target[prop];
|
|
3214
|
+
}
|
|
3215
|
+
if (prop === Signals) {
|
|
3216
|
+
return signals;
|
|
3217
|
+
}
|
|
3218
|
+
|
|
3219
|
+
// TODO make this a symbol
|
|
3220
|
+
if (prop === '___notifications') {
|
|
3221
|
+
return target.___notifications;
|
|
3222
|
+
}
|
|
3223
|
+
|
|
3224
|
+
// ReactiveResource reserves use of keys that begin with these characters
|
|
3225
|
+
// for its own usage.
|
|
3226
|
+
// _, @, $, *
|
|
3227
|
+
|
|
3228
|
+
const maybeField = prop === identityField?.name ? identityField : fields.get(prop);
|
|
3229
|
+
if (!maybeField) {
|
|
3230
|
+
if (IgnoredGlobalFields.has(prop)) {
|
|
3231
|
+
return undefined;
|
|
3232
|
+
}
|
|
3233
|
+
|
|
3234
|
+
/////////////////////////////////////////////////////////////
|
|
3235
|
+
//// Note these bound function behaviors are essentially ////
|
|
3236
|
+
//// built-in but overrideable derivations. ////
|
|
3237
|
+
//// ////
|
|
3238
|
+
//// The bar for this has to be "basic expectations of ////
|
|
3239
|
+
/// an object" – very, very high ////
|
|
3240
|
+
/////////////////////////////////////////////////////////////
|
|
3241
|
+
|
|
3242
|
+
if (prop === Symbol.toStringTag || prop === 'toString') {
|
|
3243
|
+
let fn = BoundFns.get('toString');
|
|
3244
|
+
if (!fn) {
|
|
3245
|
+
fn = function () {
|
|
3246
|
+
entangleSignal(signals, receiver, '@identity', null);
|
|
3247
|
+
return `Record<${resourceKey.type}:${resourceKey.id} (${resourceKey.lid})>`;
|
|
3248
|
+
};
|
|
3249
|
+
BoundFns.set(prop, fn);
|
|
3250
|
+
}
|
|
3251
|
+
return fn;
|
|
3252
|
+
}
|
|
3253
|
+
if (prop === 'toHTML') {
|
|
3254
|
+
let fn = BoundFns.get('toHTML');
|
|
3255
|
+
if (!fn) {
|
|
3256
|
+
fn = function () {
|
|
3257
|
+
entangleSignal(signals, receiver, '@identity', null);
|
|
3258
|
+
return `<span>Record<${resourceKey.type}:${resourceKey.id} (${resourceKey.lid})></span>`;
|
|
3259
|
+
};
|
|
3260
|
+
BoundFns.set(prop, fn);
|
|
3261
|
+
}
|
|
3262
|
+
return fn;
|
|
3263
|
+
}
|
|
3264
|
+
if (prop === 'toJSON') {
|
|
3265
|
+
let fn = BoundFns.get('toJSON');
|
|
3266
|
+
if (!fn) {
|
|
3267
|
+
fn = function () {
|
|
3268
|
+
const json = {};
|
|
3269
|
+
for (const key in receiver) {
|
|
3270
|
+
json[key] = receiver[key];
|
|
3271
|
+
}
|
|
3272
|
+
return json;
|
|
3273
|
+
};
|
|
3274
|
+
BoundFns.set(prop, fn);
|
|
3275
|
+
}
|
|
3276
|
+
return fn;
|
|
3277
|
+
}
|
|
3278
|
+
if (prop === Symbol.toPrimitive) return () => null;
|
|
3279
|
+
if (prop === Symbol.iterator) {
|
|
3280
|
+
let fn = BoundFns.get(Symbol.iterator);
|
|
3281
|
+
if (!fn) {
|
|
3282
|
+
fn = function* () {
|
|
3283
|
+
for (const key in receiver) {
|
|
3284
|
+
yield [key, receiver[key]];
|
|
3285
|
+
}
|
|
3286
|
+
};
|
|
3287
|
+
BoundFns.set(Symbol.iterator, fn);
|
|
3288
|
+
}
|
|
3289
|
+
return fn;
|
|
3290
|
+
}
|
|
3291
|
+
if (prop === 'constructor') {
|
|
3292
|
+
return ReactiveResource;
|
|
3293
|
+
}
|
|
3294
|
+
if (isExtensionProp(extensions, prop)) {
|
|
3295
|
+
return performObjectExtensionGet(receiver, extensions, signals, prop);
|
|
3296
|
+
}
|
|
3297
|
+
|
|
3298
|
+
// too many things check for random symbols
|
|
3299
|
+
if (typeof prop === 'symbol') return undefined;
|
|
3300
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
3301
|
+
{
|
|
3302
|
+
throw new Error(`No field named ${String(prop)} on ${context.path ? context.value : resourceKey.type}`);
|
|
3303
|
+
}
|
|
3304
|
+
})() : {};
|
|
3305
|
+
return undefined;
|
|
3306
|
+
}
|
|
3307
|
+
const field = maybeField.kind === 'alias' ? maybeField.options : maybeField;
|
|
3308
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
3309
|
+
if (!test) {
|
|
3310
|
+
throw new Error(`Alias fields cannot alias '@id' '@local' '@hash' or 'derived' fields`);
|
|
3311
|
+
}
|
|
3312
|
+
})(maybeField.kind !== 'alias' || !['@id', '@local', '@hash', 'derived'].includes(maybeField.options.kind)) : {};
|
|
3313
|
+
/**
|
|
3314
|
+
* Prop Array is the path from a resource to the field including
|
|
3315
|
+
* intermediate "links" on arrays,objects,schema-arrays and schema-objects.
|
|
3316
|
+
*
|
|
3317
|
+
* E.g. in the following
|
|
3318
|
+
*
|
|
3319
|
+
* ```
|
|
3320
|
+
* const user = {
|
|
3321
|
+
* addresses: [{
|
|
3322
|
+
* street: 'Sunset Blvd',
|
|
3323
|
+
* zip: 90210
|
|
3324
|
+
* }]
|
|
3325
|
+
* }
|
|
3326
|
+
* ```
|
|
3327
|
+
*
|
|
3328
|
+
* The propArray for "street" is ['addresses', 0, 'street']
|
|
3329
|
+
*
|
|
3330
|
+
* Prop Array follows the `cache` path to the value, not the ui path.
|
|
3331
|
+
* Thus, if `addresses` has a sourceKey of `user_addresses` and
|
|
3332
|
+
* `zip` has a sourceKey of `zip_code` then the propArray for "zip" is
|
|
3333
|
+
* ['user_addresses', 0, 'zip_code']
|
|
3334
|
+
*/
|
|
3335
|
+
const propArray = context.path?.slice() ?? [];
|
|
3336
|
+
// we use the field.name instead of prop here because we want to use the cache-path not
|
|
3337
|
+
// the record path.
|
|
3338
|
+
// SAFETY: we lie as string here because if we were to get null
|
|
3339
|
+
// we would be in a field kind that won't use the propArray below.
|
|
3340
|
+
const fieldCacheKey = getFieldCacheKey(field);
|
|
3341
|
+
propArray.push(fieldCacheKey);
|
|
3342
|
+
switch (field.kind) {
|
|
3343
|
+
case '@id':
|
|
3344
|
+
case '@hash':
|
|
3345
|
+
case '@local':
|
|
3346
|
+
case 'derived':
|
|
3347
|
+
case 'field':
|
|
3348
|
+
case 'attribute':
|
|
3349
|
+
case 'schema-array':
|
|
3350
|
+
case 'array':
|
|
3351
|
+
case 'schema-object':
|
|
3352
|
+
case 'object':
|
|
3353
|
+
case 'resource':
|
|
3354
|
+
case 'belongsTo':
|
|
3355
|
+
case 'hasMany':
|
|
3356
|
+
case 'collection':
|
|
3357
|
+
return DefaultMode[field.kind].get({
|
|
3358
|
+
store: context.store,
|
|
3359
|
+
resourceKey: resourceKey,
|
|
3360
|
+
modeName: context.modeName,
|
|
3361
|
+
legacy: context.legacy,
|
|
3362
|
+
editable: context.editable,
|
|
3363
|
+
path: propArray,
|
|
3364
|
+
field: field,
|
|
3365
|
+
record: receiver,
|
|
3366
|
+
signals,
|
|
3367
|
+
value: null
|
|
3368
|
+
});
|
|
3369
|
+
default:
|
|
3370
|
+
assertNeverField(resourceKey, field, propArray);
|
|
3371
|
+
}
|
|
3372
|
+
},
|
|
3373
|
+
set(target, prop, value, receiver) {
|
|
3374
|
+
// the only "editable" prop as it is currently a proxy for "isDestroyed"
|
|
3375
|
+
if (prop === '___notifications') {
|
|
3376
|
+
target[prop] = value;
|
|
3377
|
+
return true;
|
|
3378
|
+
}
|
|
3379
|
+
if (!context.editable) {
|
|
3380
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
3381
|
+
{
|
|
3382
|
+
throw new Error(`Cannot set ${String(prop)} on ${context.path !== null ? context.value : resourceKey.type} because the record is not editable`);
|
|
3383
|
+
}
|
|
3384
|
+
})() : {};
|
|
3385
|
+
return false;
|
|
3386
|
+
}
|
|
3387
|
+
const maybeField = prop === identityField?.name ? identityField : fields.get(prop);
|
|
3388
|
+
if (!maybeField) {
|
|
3389
|
+
const type = context.path !== null ? context.value : resourceKey.type;
|
|
3390
|
+
if (isExtensionProp(extensions, prop)) {
|
|
3391
|
+
return performExtensionSet(receiver, extensions, signals, prop, value);
|
|
3392
|
+
}
|
|
3393
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
3394
|
+
{
|
|
3395
|
+
throw new Error(`There is no settable field named ${String(prop)} on ${type}`);
|
|
3396
|
+
}
|
|
3397
|
+
})() : {};
|
|
3398
|
+
return false;
|
|
3399
|
+
}
|
|
3400
|
+
const field = maybeField.kind === 'alias' ? maybeField.options : maybeField;
|
|
3401
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
3402
|
+
if (!test) {
|
|
3403
|
+
throw new Error(`Alias fields cannot alias '@id' '@local' '@hash' or 'derived' fields`);
|
|
3404
|
+
}
|
|
3405
|
+
})(maybeField.kind !== 'alias' || !['@id', '@local', '@hash', 'derived'].includes(maybeField.options.kind)) : {};
|
|
3406
|
+
/**
|
|
3407
|
+
* Prop Array is the path from a resource to the field including
|
|
3408
|
+
* intermediate "links" on arrays,objects,schema-arrays and schema-objects.
|
|
3409
|
+
*
|
|
3410
|
+
* E.g. in the following
|
|
3411
|
+
*
|
|
3412
|
+
* ```
|
|
3413
|
+
* const user = {
|
|
3414
|
+
* addresses: [{
|
|
3415
|
+
* street: 'Sunset Blvd',
|
|
3416
|
+
* zip: 90210
|
|
3417
|
+
* }]
|
|
3418
|
+
* }
|
|
3419
|
+
* ```
|
|
3420
|
+
*
|
|
3421
|
+
* The propArray for "street" is ['addresses', 0, 'street']
|
|
3422
|
+
*
|
|
3423
|
+
* Prop Array follows the `cache` path to the value, not the ui path.
|
|
3424
|
+
* Thus, if `addresses` has a sourceKey of `user_addresses` and
|
|
3425
|
+
* `zip` has a sourceKey of `zip_code` then the propArray for "zip" is
|
|
3426
|
+
* ['user_addresses', 0, 'zip_code']
|
|
3427
|
+
*/
|
|
3428
|
+
const propArray = context.path?.slice() ?? [];
|
|
3429
|
+
// we use the field.name instead of prop here because we want to use the cache-path not
|
|
3430
|
+
// the record path.
|
|
3431
|
+
// SAFETY: we lie as string here because if we were to get null
|
|
3432
|
+
// we would be in a field kind that won't use the propArray below.
|
|
3433
|
+
const fieldCacheKey = getFieldCacheKey(field);
|
|
3434
|
+
propArray.push(fieldCacheKey);
|
|
3435
|
+
switch (field.kind) {
|
|
3436
|
+
case '@id':
|
|
3437
|
+
case '@hash':
|
|
3438
|
+
case '@local':
|
|
3439
|
+
case 'field':
|
|
3440
|
+
case 'attribute':
|
|
3441
|
+
case 'derived':
|
|
3442
|
+
case 'array':
|
|
3443
|
+
case 'schema-array':
|
|
3444
|
+
case 'schema-object':
|
|
3445
|
+
case 'object':
|
|
3446
|
+
case 'resource':
|
|
3447
|
+
case 'belongsTo':
|
|
3448
|
+
case 'hasMany':
|
|
3449
|
+
case 'collection':
|
|
3450
|
+
return DefaultMode[field.kind].set({
|
|
3451
|
+
store: context.store,
|
|
3452
|
+
resourceKey: resourceKey,
|
|
3453
|
+
modeName: context.modeName,
|
|
3454
|
+
legacy: context.legacy,
|
|
3455
|
+
editable: context.editable,
|
|
3456
|
+
path: propArray,
|
|
3457
|
+
field: field,
|
|
3458
|
+
record: receiver,
|
|
3459
|
+
signals,
|
|
3460
|
+
value
|
|
3461
|
+
});
|
|
3462
|
+
default:
|
|
3463
|
+
return assertNeverField(resourceKey, field, propArray);
|
|
3464
|
+
}
|
|
3465
|
+
}
|
|
3466
|
+
});
|
|
3467
|
+
if (macroCondition(getGlobalConfig().WarpDrive.env.DEBUG)) {
|
|
3468
|
+
Object.defineProperty(this, '__SHOW_ME_THE_DATA_(debug mode only)__', {
|
|
3469
|
+
enumerable: false,
|
|
3470
|
+
configurable: true,
|
|
3471
|
+
get() {
|
|
3472
|
+
const data = {};
|
|
3473
|
+
for (const key of fields.keys()) {
|
|
3474
|
+
data[key] = proxy[key];
|
|
3475
|
+
}
|
|
3476
|
+
return data;
|
|
3477
|
+
}
|
|
3478
|
+
});
|
|
3479
|
+
}
|
|
3480
|
+
return proxy;
|
|
3481
|
+
}
|
|
3482
|
+
}
|
|
3483
|
+
function _CHECKOUT(record) {
|
|
3484
|
+
const context = record[Context];
|
|
3485
|
+
|
|
3486
|
+
// IF we are already the editable record, throw an error
|
|
3487
|
+
if (context.editable) {
|
|
3488
|
+
throw new Error(`Cannot checkout an already editable record`);
|
|
3489
|
+
}
|
|
3490
|
+
const editable = Editables.get(record);
|
|
3491
|
+
if (editable) {
|
|
3492
|
+
return editable;
|
|
3493
|
+
}
|
|
3494
|
+
if (context.path !== null) {
|
|
3495
|
+
throw new Error(`Cannot checkout an embedded record`);
|
|
3496
|
+
}
|
|
3497
|
+
const editableRecord = new ReactiveResource({
|
|
3498
|
+
store: context.store,
|
|
3499
|
+
resourceKey: context.resourceKey,
|
|
3500
|
+
modeName: context.legacy ? 'legacy' : 'polaris',
|
|
3501
|
+
legacy: context.legacy,
|
|
3502
|
+
editable: true,
|
|
3503
|
+
path: null,
|
|
3504
|
+
field: null,
|
|
3505
|
+
value: null
|
|
3506
|
+
});
|
|
3507
|
+
setRecordIdentifier(editableRecord, recordIdentifierFor(record));
|
|
3508
|
+
Editables.set(record, editableRecord);
|
|
3509
|
+
return editableRecord;
|
|
3510
|
+
}
|
|
3511
|
+
function _DESTROY(record) {
|
|
3512
|
+
if (record[Context].legacy) {
|
|
3513
|
+
// @ts-expect-error
|
|
3514
|
+
record.isDestroying = true;
|
|
3515
|
+
// @ts-expect-error
|
|
3516
|
+
record.isDestroyed = true;
|
|
3517
|
+
} else if (!record[Context].editable) {
|
|
3518
|
+
const editable = Editables.get(record);
|
|
3519
|
+
if (editable) {
|
|
3520
|
+
_DESTROY(editable);
|
|
3521
|
+
removeRecordIdentifier(editable);
|
|
3522
|
+
}
|
|
3523
|
+
}
|
|
3524
|
+
record[Context].store.notifications.unsubscribe(record.___notifications);
|
|
3525
|
+
record.___notifications = null;
|
|
3526
|
+
|
|
3527
|
+
// FIXME we need a way to also unsubscribe all SchemaObjects when the primary
|
|
3528
|
+
// resource is destroyed.
|
|
3529
|
+
}
|
|
3530
|
+
function assertNeverField(identifier, field, path) {
|
|
3531
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
3532
|
+
{
|
|
3533
|
+
throw new Error(`Cannot use unknown field kind ${field.kind} on <${identifier.type}>.${Array.isArray(path) ? path.join('.') : path}`);
|
|
3534
|
+
}
|
|
3535
|
+
})() : {};
|
|
3536
|
+
return false;
|
|
3537
|
+
}
|
|
3538
|
+
const TEXT_COLORS = {
|
|
3539
|
+
TEXT: 'inherit',
|
|
3540
|
+
notify: ['white', 'white', 'inherit', 'magenta', 'inherit'],
|
|
3541
|
+
'reactive-ui': ['white', 'white', 'inherit', 'magenta', 'inherit'],
|
|
3542
|
+
graph: ['white', 'white', 'inherit', 'magenta', 'inherit'],
|
|
3543
|
+
request: ['white', 'white', 'inherit', 'magenta', 'inherit'],
|
|
3544
|
+
cache: ['white', 'white', 'inherit', 'magenta', 'inherit']
|
|
3545
|
+
};
|
|
3546
|
+
const BG_COLORS = {
|
|
3547
|
+
TEXT: 'transparent',
|
|
3548
|
+
notify: ['dimgray', 'cadetblue', 'transparent', 'transparent', 'transparent'],
|
|
3549
|
+
'reactive-ui': ['dimgray', 'cadetblue', 'transparent', 'transparent', 'transparent'],
|
|
3550
|
+
graph: ['dimgray', 'cadetblue', 'transparent', 'transparent', 'transparent'],
|
|
3551
|
+
request: ['dimgray', 'cadetblue', 'transparent', 'transparent', 'transparent'],
|
|
3552
|
+
cache: ['dimgray', 'cadetblue', 'transparent', 'transparent', 'transparent']
|
|
3553
|
+
};
|
|
3554
|
+
const NOTIFY_BORDER = {
|
|
3555
|
+
TEXT: 0,
|
|
3556
|
+
notify: [3, 2, 0, 0, 0],
|
|
3557
|
+
'reactive-ui': [3, 2, 0, 0, 0],
|
|
3558
|
+
graph: [3, 2, 0, 0, 0],
|
|
3559
|
+
request: [3, 2, 0, 0, 0],
|
|
3560
|
+
cache: [3, 2, 0, 0, 0]
|
|
3561
|
+
};
|
|
3562
|
+
const LIGHT_DARK_ALT = {
|
|
3563
|
+
lightgreen: 'green',
|
|
3564
|
+
green: 'lightgreen'
|
|
3565
|
+
};
|
|
3566
|
+
function badge(isLight, color, bgColor, border) {
|
|
3567
|
+
return [`color: ${correctColor(isLight, color)}; background-color: ${correctColor(isLight, bgColor)}; padding: ${border}px ${2 * border}px; border-radius: ${border}px;`, `color: ${TEXT_COLORS.TEXT}; background-color: ${BG_COLORS.TEXT};`];
|
|
3568
|
+
}
|
|
3569
|
+
function colorForBucket(isLight, scope, bucket) {
|
|
3570
|
+
if (scope === 'notify') {
|
|
3571
|
+
return bucket === 'added' ? badge(isLight, 'lightgreen', 'transparent', 0) : bucket === 'removed' ? badge(isLight, 'red', 'transparent', 0) : badge(isLight, TEXT_COLORS[scope][2], BG_COLORS[scope][2], NOTIFY_BORDER[scope][2]);
|
|
3572
|
+
}
|
|
3573
|
+
if (scope === 'reactive-ui') {
|
|
3574
|
+
return bucket === 'created' ? badge(isLight, 'lightgreen', 'transparent', 0) : bucket === 'disconnected' ? badge(isLight, 'red', 'transparent', 0) : badge(isLight, TEXT_COLORS[scope][2], BG_COLORS[scope][2], NOTIFY_BORDER[scope][2]);
|
|
3575
|
+
}
|
|
3576
|
+
if (scope === 'cache') {
|
|
3577
|
+
return bucket === 'inserted' ? badge(isLight, 'lightgreen', 'transparent', 0) : bucket === 'removed' ? badge(isLight, 'red', 'transparent', 0) : badge(isLight, TEXT_COLORS[scope][2], BG_COLORS[scope][2], NOTIFY_BORDER[scope][2]);
|
|
3578
|
+
}
|
|
3579
|
+
return badge(isLight, TEXT_COLORS[scope][3], BG_COLORS[scope][3], NOTIFY_BORDER[scope][3]);
|
|
3580
|
+
}
|
|
3581
|
+
function logGroup(scope, prefix, subScop1, subScop2, subScop3, subScop4) {
|
|
3582
|
+
// eslint-disable-next-line no-console
|
|
3583
|
+
console.groupCollapsed(..._log(scope, prefix, subScop1, subScop2, subScop3, subScop4));
|
|
3584
|
+
}
|
|
3585
|
+
function log(scope, prefix, subScop1, subScop2, subScop3, subScop4) {
|
|
3586
|
+
// eslint-disable-next-line no-console
|
|
3587
|
+
console.log(..._log(scope, prefix, subScop1, subScop2, subScop3, subScop4));
|
|
3588
|
+
}
|
|
3589
|
+
function correctColor(isLight, color) {
|
|
3590
|
+
if (!isLight) {
|
|
3591
|
+
return color;
|
|
3592
|
+
}
|
|
3593
|
+
return color in LIGHT_DARK_ALT ? LIGHT_DARK_ALT[color] : color;
|
|
3594
|
+
}
|
|
3595
|
+
function isLightMode() {
|
|
3596
|
+
if (window?.matchMedia?.('(prefers-color-scheme: light)').matches) {
|
|
3597
|
+
return true;
|
|
3598
|
+
}
|
|
3599
|
+
return false;
|
|
3600
|
+
}
|
|
3601
|
+
function _log(scope, prefix, subScop1, subScop2, subScop3, subScop4) {
|
|
3602
|
+
const isLight = isLightMode();
|
|
3603
|
+
switch (scope) {
|
|
3604
|
+
case 'reactive-ui':
|
|
3605
|
+
case 'notify':
|
|
3606
|
+
{
|
|
3607
|
+
const scopePath = prefix ? `[${prefix}] ${scope}` : scope;
|
|
3608
|
+
const path = subScop4 ? `${subScop3}.${subScop4}` : subScop3;
|
|
3609
|
+
return [`%c@warp%c-%cdrive%c %c${scopePath}%c %c${subScop1}%c %c${subScop2}%c %c${path}%c`, ...badge(isLight, 'lightgreen', 'transparent', 0), ...badge(isLight, 'magenta', 'transparent', 0), ...badge(isLight, TEXT_COLORS[scope][0], BG_COLORS[scope][0], NOTIFY_BORDER[scope][0]), ...badge(isLight, TEXT_COLORS[scope][1], BG_COLORS[scope][1], NOTIFY_BORDER[scope][1]), ...badge(isLight, TEXT_COLORS[scope][2], BG_COLORS[scope][2], NOTIFY_BORDER[scope][2]), ...colorForBucket(isLight, scope, path)];
|
|
3610
|
+
}
|
|
3611
|
+
case 'cache':
|
|
3612
|
+
{
|
|
3613
|
+
const scopePath = prefix ? `${scope} (${prefix})` : scope;
|
|
3614
|
+
return [`%c@warp%c-%cdrive%c %c${scopePath}%c %c${subScop1}%c %c${subScop2}%c %c${subScop3}%c %c${subScop4}%c`, ...badge(isLight, 'lightgreen', 'transparent', 0), ...badge(isLight, 'magenta', 'transparent', 0), ...badge(isLight, TEXT_COLORS[scope][0], BG_COLORS[scope][0], NOTIFY_BORDER[scope][0]), ...badge(isLight, TEXT_COLORS[scope][1], BG_COLORS[scope][1], NOTIFY_BORDER[scope][1]), ...badge(isLight, TEXT_COLORS[scope][2], BG_COLORS[scope][2], NOTIFY_BORDER[scope][2]), ...colorForBucket(isLight, scope, subScop3), ...badge(isLight, TEXT_COLORS[scope][4], BG_COLORS[scope][4], NOTIFY_BORDER[scope][4])];
|
|
3615
|
+
}
|
|
3616
|
+
}
|
|
3617
|
+
return [];
|
|
3618
|
+
}
|
|
3619
|
+
class CacheCapabilitiesManager {
|
|
3620
|
+
constructor(_store) {
|
|
3621
|
+
this._store = _store;
|
|
3622
|
+
this._willNotify = false;
|
|
3623
|
+
this._pendingNotifies = new Map();
|
|
3624
|
+
}
|
|
3625
|
+
get identifierCache() {
|
|
3626
|
+
return this._store.identifierCache;
|
|
3627
|
+
}
|
|
3628
|
+
_scheduleNotification(identifier, key) {
|
|
3629
|
+
let pending = this._pendingNotifies.get(identifier);
|
|
3630
|
+
if (!pending) {
|
|
3631
|
+
pending = new Set();
|
|
3632
|
+
this._pendingNotifies.set(identifier, pending);
|
|
3633
|
+
}
|
|
3634
|
+
pending.add(key);
|
|
3635
|
+
if (this._willNotify === true) {
|
|
3636
|
+
return;
|
|
3637
|
+
}
|
|
3638
|
+
this._willNotify = true;
|
|
3639
|
+
// it's possible a cache adhoc notifies us,
|
|
3640
|
+
// in which case we sync flush
|
|
3641
|
+
if (this._store._cbs) {
|
|
3642
|
+
this._store._schedule('notify', () => this._flushNotifications());
|
|
3643
|
+
} else {
|
|
3644
|
+
// TODO @runspired determine if relationship mutations should schedule
|
|
3645
|
+
// into join/run vs immediate flush
|
|
3646
|
+
this._flushNotifications();
|
|
1594
3647
|
}
|
|
1595
3648
|
}
|
|
1596
3649
|
_flushNotifications() {
|
|
@@ -1652,30 +3705,6 @@ if (macroCondition(getGlobalConfig().WarpDrive.deprecations.ENABLE_LEGACY_SCHEMA
|
|
|
1652
3705
|
return this._store.schema;
|
|
1653
3706
|
};
|
|
1654
3707
|
}
|
|
1655
|
-
|
|
1656
|
-
/*
|
|
1657
|
-
* Returns the Cache instance associated with a given
|
|
1658
|
-
* Model or Identifier
|
|
1659
|
-
*/
|
|
1660
|
-
|
|
1661
|
-
const CacheForIdentifierCache = getOrSetGlobal('CacheForIdentifierCache', new Map());
|
|
1662
|
-
function setCacheFor(identifier, cache) {
|
|
1663
|
-
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
1664
|
-
if (!test) {
|
|
1665
|
-
throw new Error(`Illegal set of identifier`);
|
|
1666
|
-
}
|
|
1667
|
-
})(!CacheForIdentifierCache.has(identifier) || CacheForIdentifierCache.get(identifier) === cache) : {};
|
|
1668
|
-
CacheForIdentifierCache.set(identifier, cache);
|
|
1669
|
-
}
|
|
1670
|
-
function removeRecordDataFor(identifier) {
|
|
1671
|
-
CacheForIdentifierCache.delete(identifier);
|
|
1672
|
-
}
|
|
1673
|
-
function peekCache(instance) {
|
|
1674
|
-
if (CacheForIdentifierCache.has(instance)) {
|
|
1675
|
-
return CacheForIdentifierCache.get(instance);
|
|
1676
|
-
}
|
|
1677
|
-
return null;
|
|
1678
|
-
}
|
|
1679
3708
|
function isDestroyable(record) {
|
|
1680
3709
|
return Boolean(record && typeof record === 'object' && typeof record.destroy === 'function');
|
|
1681
3710
|
}
|
|
@@ -1728,20 +3757,28 @@ function setRecordIdentifier(record, identifier) {
|
|
|
1728
3757
|
|
|
1729
3758
|
RecordCache.set(record, identifier);
|
|
1730
3759
|
}
|
|
3760
|
+
function removeRecordIdentifier(record) {
|
|
3761
|
+
if (macroCondition(getGlobalConfig().WarpDrive.env.DEBUG)) {
|
|
3762
|
+
if (!RecordCache.has(record)) {
|
|
3763
|
+
throw new Error(`${String(record)} had no assigned identifier to remove`);
|
|
3764
|
+
}
|
|
3765
|
+
}
|
|
3766
|
+
RecordCache.delete(record);
|
|
3767
|
+
}
|
|
1731
3768
|
const StoreMap = getOrSetGlobal('StoreMap', new Map());
|
|
1732
3769
|
|
|
1733
3770
|
/**
|
|
1734
3771
|
* We may eventually make this public, but its likely better for this to be killed off
|
|
1735
3772
|
* @internal
|
|
1736
3773
|
*/
|
|
1737
|
-
function storeFor(record) {
|
|
3774
|
+
function storeFor(record, ignoreMissing) {
|
|
1738
3775
|
const store = StoreMap.get(record);
|
|
1739
3776
|
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
1740
3777
|
if (!test) {
|
|
1741
3778
|
throw new Error(`A record in a disconnected state cannot utilize the store. This typically means the record has been destroyed, most commonly by unloading it.`);
|
|
1742
3779
|
}
|
|
1743
|
-
})(store) : {};
|
|
1744
|
-
return store;
|
|
3780
|
+
})(ignoreMissing || store) : {};
|
|
3781
|
+
return store ?? null;
|
|
1745
3782
|
}
|
|
1746
3783
|
class InstanceCache {
|
|
1747
3784
|
constructor(store) {
|
|
@@ -1812,32 +3849,10 @@ class InstanceCache {
|
|
|
1812
3849
|
}
|
|
1813
3850
|
return doc;
|
|
1814
3851
|
}
|
|
1815
|
-
getRecord(identifier
|
|
3852
|
+
getRecord(identifier) {
|
|
1816
3853
|
let record = this.__instances.record.get(identifier);
|
|
1817
3854
|
if (!record) {
|
|
1818
|
-
|
|
1819
|
-
if (!test) {
|
|
1820
|
-
throw new Error(`Cannot create a new record instance while the store is being destroyed`);
|
|
1821
|
-
}
|
|
1822
|
-
})(!this.store.isDestroying && !this.store.isDestroyed) : {};
|
|
1823
|
-
const cache = this.store.cache;
|
|
1824
|
-
setCacheFor(identifier, cache);
|
|
1825
|
-
record = this.store.instantiateRecord(identifier, properties || {});
|
|
1826
|
-
setRecordIdentifier(record, identifier);
|
|
1827
|
-
setCacheFor(record, cache);
|
|
1828
|
-
StoreMap.set(record, this.store);
|
|
1829
|
-
this.__instances.record.set(identifier, record);
|
|
1830
|
-
if (macroCondition(getGlobalConfig().WarpDrive.activeLogging.LOG_INSTANCE_CACHE)) {
|
|
1831
|
-
if (getGlobalConfig().WarpDrive.debug.LOG_INSTANCE_CACHE || globalThis.getWarpDriveRuntimeConfig().debug.LOG_INSTANCE_CACHE) {
|
|
1832
|
-
logGroup('reactive-ui', '', identifier.type, identifier.lid, 'created', '');
|
|
1833
|
-
// eslint-disable-next-line no-console
|
|
1834
|
-
console.log({
|
|
1835
|
-
properties
|
|
1836
|
-
});
|
|
1837
|
-
// eslint-disable-next-line no-console
|
|
1838
|
-
console.groupEnd();
|
|
1839
|
-
}
|
|
1840
|
-
}
|
|
3855
|
+
record = _createRecord(this, identifier, {});
|
|
1841
3856
|
}
|
|
1842
3857
|
return record;
|
|
1843
3858
|
}
|
|
@@ -1871,7 +3886,7 @@ class InstanceCache {
|
|
|
1871
3886
|
})(!isDestroyable(record) || record.isDestroyed || record.isDestroying) : {};
|
|
1872
3887
|
this.store._graph?.remove(identifier);
|
|
1873
3888
|
this.store.identifierCache.forgetRecordIdentifier(identifier);
|
|
1874
|
-
|
|
3889
|
+
StoreMap.delete(identifier);
|
|
1875
3890
|
this.store._requestCache._clearEntries(identifier);
|
|
1876
3891
|
if (macroCondition(getGlobalConfig().WarpDrive.activeLogging.LOG_INSTANCE_CACHE)) {
|
|
1877
3892
|
if (getGlobalConfig().WarpDrive.debug.LOG_INSTANCE_CACHE || globalThis.getWarpDriveRuntimeConfig().debug.LOG_INSTANCE_CACHE) {
|
|
@@ -1908,7 +3923,6 @@ class InstanceCache {
|
|
|
1908
3923
|
this.__instances.record.delete(identifier);
|
|
1909
3924
|
StoreMap.delete(record);
|
|
1910
3925
|
RecordCache.delete(record);
|
|
1911
|
-
removeRecordDataFor(record);
|
|
1912
3926
|
if (macroCondition(getGlobalConfig().WarpDrive.activeLogging.LOG_INSTANCE_CACHE)) {
|
|
1913
3927
|
if (getGlobalConfig().WarpDrive.debug.LOG_INSTANCE_CACHE || globalThis.getWarpDriveRuntimeConfig().debug.LOG_INSTANCE_CACHE) {
|
|
1914
3928
|
// eslint-disable-next-line no-console
|
|
@@ -1918,7 +3932,7 @@ class InstanceCache {
|
|
|
1918
3932
|
}
|
|
1919
3933
|
if (cache) {
|
|
1920
3934
|
cache.unloadRecord(identifier);
|
|
1921
|
-
|
|
3935
|
+
StoreMap.delete(identifier);
|
|
1922
3936
|
if (macroCondition(getGlobalConfig().WarpDrive.activeLogging.LOG_INSTANCE_CACHE)) {
|
|
1923
3937
|
if (getGlobalConfig().WarpDrive.debug.LOG_INSTANCE_CACHE || globalThis.getWarpDriveRuntimeConfig().debug.LOG_INSTANCE_CACHE) {
|
|
1924
3938
|
// eslint-disable-next-line no-console
|
|
@@ -2018,10 +4032,46 @@ class InstanceCache {
|
|
|
2018
4032
|
this.store.notifications.notify(identifier, 'identity');
|
|
2019
4033
|
}
|
|
2020
4034
|
}
|
|
4035
|
+
function getNewRecord(instances, identifier, properties) {
|
|
4036
|
+
let record = instances.__instances.record.get(identifier);
|
|
4037
|
+
if (!record) {
|
|
4038
|
+
record = _createRecord(instances, identifier, properties);
|
|
4039
|
+
if (record instanceof ReactiveResource && instances.store.schema.resource) {
|
|
4040
|
+
// this is a work around until we introduce a new async createRecord API
|
|
4041
|
+
const schema = instances.store.schema.resource(identifier);
|
|
4042
|
+
if (!schema.legacy) {
|
|
4043
|
+
const editable = _CHECKOUT(record);
|
|
4044
|
+
if (properties) {
|
|
4045
|
+
Object.assign(editable, properties);
|
|
4046
|
+
}
|
|
4047
|
+
return editable;
|
|
4048
|
+
}
|
|
4049
|
+
}
|
|
4050
|
+
}
|
|
4051
|
+
return record;
|
|
4052
|
+
}
|
|
4053
|
+
function _createRecord(instances, identifier, properties) {
|
|
4054
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
4055
|
+
if (!test) {
|
|
4056
|
+
throw new Error(`Cannot create a new record instance while the store is being destroyed`);
|
|
4057
|
+
}
|
|
4058
|
+
})(!instances.store.isDestroying && !instances.store.isDestroyed) : {};
|
|
4059
|
+
const record = instances.store.instantiateRecord(identifier, properties);
|
|
4060
|
+
setRecordIdentifier(record, identifier);
|
|
4061
|
+
StoreMap.set(record, instances.store);
|
|
4062
|
+
instances.__instances.record.set(identifier, record);
|
|
4063
|
+
if (macroCondition(getGlobalConfig().WarpDrive.activeLogging.LOG_INSTANCE_CACHE)) {
|
|
4064
|
+
if (getGlobalConfig().WarpDrive.debug.LOG_INSTANCE_CACHE || globalThis.getWarpDriveRuntimeConfig().debug.LOG_INSTANCE_CACHE) {
|
|
4065
|
+
logGroup('reactive-ui', '', identifier.type, identifier.lid, 'created', '');
|
|
4066
|
+
// eslint-disable-next-line no-console
|
|
4067
|
+
console.groupEnd();
|
|
4068
|
+
}
|
|
4069
|
+
}
|
|
4070
|
+
return record;
|
|
4071
|
+
}
|
|
2021
4072
|
function _clearCaches() {
|
|
2022
4073
|
RecordCache.clear();
|
|
2023
4074
|
StoreMap.clear();
|
|
2024
|
-
CacheForIdentifierCache.clear();
|
|
2025
4075
|
}
|
|
2026
4076
|
|
|
2027
4077
|
/**
|
|
@@ -2828,142 +4878,6 @@ class NotificationManager {
|
|
|
2828
4878
|
this._cache.clear();
|
|
2829
4879
|
}
|
|
2830
4880
|
}
|
|
2831
|
-
function isExtensionProp(extensions, prop) {
|
|
2832
|
-
return Boolean(extensions && typeof prop !== 'number' && extensions.has(prop));
|
|
2833
|
-
}
|
|
2834
|
-
function performObjectExtensionGet(receiver, extensions, signals, prop) {
|
|
2835
|
-
const desc = extensions.get(prop);
|
|
2836
|
-
switch (desc.kind) {
|
|
2837
|
-
case 'method':
|
|
2838
|
-
{
|
|
2839
|
-
return desc.fn;
|
|
2840
|
-
}
|
|
2841
|
-
case 'readonly-value':
|
|
2842
|
-
{
|
|
2843
|
-
return desc.value;
|
|
2844
|
-
}
|
|
2845
|
-
case 'mutable-value':
|
|
2846
|
-
{
|
|
2847
|
-
const signal = getOrCreateInternalSignal(signals, receiver, prop, desc.value);
|
|
2848
|
-
// we don't consume this signal, since its not a true local.
|
|
2849
|
-
return signal.value;
|
|
2850
|
-
}
|
|
2851
|
-
case 'readonly-field':
|
|
2852
|
-
case 'mutable-field':
|
|
2853
|
-
{
|
|
2854
|
-
return desc.get.call(receiver);
|
|
2855
|
-
}
|
|
2856
|
-
case 'writeonly-field':
|
|
2857
|
-
{
|
|
2858
|
-
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
2859
|
-
{
|
|
2860
|
-
throw new Error(`Cannot get extended field ${String(prop)} as its definition has only a setter`);
|
|
2861
|
-
}
|
|
2862
|
-
})() : {};
|
|
2863
|
-
return undefined;
|
|
2864
|
-
}
|
|
2865
|
-
default:
|
|
2866
|
-
{
|
|
2867
|
-
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
2868
|
-
{
|
|
2869
|
-
throw new Error(`Unhandled extension kind ${desc.kind}`);
|
|
2870
|
-
}
|
|
2871
|
-
})() : {};
|
|
2872
|
-
return undefined;
|
|
2873
|
-
}
|
|
2874
|
-
}
|
|
2875
|
-
}
|
|
2876
|
-
function performExtensionSet(receiver, extensions, signals, prop, value) {
|
|
2877
|
-
const desc = extensions.get(prop);
|
|
2878
|
-
switch (desc.kind) {
|
|
2879
|
-
case 'method':
|
|
2880
|
-
case 'readonly-value':
|
|
2881
|
-
case 'readonly-field':
|
|
2882
|
-
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
2883
|
-
{
|
|
2884
|
-
throw new Error(`Cannot set extension field ${String(prop)} as it is a ${desc.kind}`);
|
|
2885
|
-
}
|
|
2886
|
-
})() : {};
|
|
2887
|
-
return false;
|
|
2888
|
-
case 'mutable-value':
|
|
2889
|
-
{
|
|
2890
|
-
const signal = getOrCreateInternalSignal(signals, receiver, prop, desc.value);
|
|
2891
|
-
if (signal.value !== value) {
|
|
2892
|
-
// we don't notify this signal, since its not a true local.
|
|
2893
|
-
signal.value = value;
|
|
2894
|
-
}
|
|
2895
|
-
return true;
|
|
2896
|
-
}
|
|
2897
|
-
case 'writeonly-field':
|
|
2898
|
-
case 'mutable-field':
|
|
2899
|
-
{
|
|
2900
|
-
desc.set.call(receiver, value);
|
|
2901
|
-
return true;
|
|
2902
|
-
}
|
|
2903
|
-
default:
|
|
2904
|
-
{
|
|
2905
|
-
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
2906
|
-
{
|
|
2907
|
-
throw new Error(`Unhandled extension kind ${desc.kind}`);
|
|
2908
|
-
}
|
|
2909
|
-
})() : {};
|
|
2910
|
-
return false;
|
|
2911
|
-
}
|
|
2912
|
-
}
|
|
2913
|
-
}
|
|
2914
|
-
function performArrayExtensionGet(receiver, extensions, signals, prop, _SIGNAL, boundFns, transaction) {
|
|
2915
|
-
const desc = extensions.get(prop);
|
|
2916
|
-
switch (desc.kind) {
|
|
2917
|
-
case 'method':
|
|
2918
|
-
{
|
|
2919
|
-
let fn = boundFns.get(prop);
|
|
2920
|
-
if (fn === undefined) {
|
|
2921
|
-
fn = function () {
|
|
2922
|
-
consumeInternalSignal(_SIGNAL);
|
|
2923
|
-
transaction(true);
|
|
2924
|
-
const result = Reflect.apply(desc.fn, receiver, arguments);
|
|
2925
|
-
transaction(false);
|
|
2926
|
-
return result;
|
|
2927
|
-
};
|
|
2928
|
-
boundFns.set(prop, fn);
|
|
2929
|
-
}
|
|
2930
|
-
return fn;
|
|
2931
|
-
}
|
|
2932
|
-
case 'mutable-field':
|
|
2933
|
-
case 'readonly-field':
|
|
2934
|
-
{
|
|
2935
|
-
return desc.get.call(receiver);
|
|
2936
|
-
}
|
|
2937
|
-
case 'readonly-value':
|
|
2938
|
-
{
|
|
2939
|
-
return desc.value;
|
|
2940
|
-
}
|
|
2941
|
-
case 'mutable-value':
|
|
2942
|
-
{
|
|
2943
|
-
const signal = getOrCreateInternalSignal(signals, receiver, prop, desc.value);
|
|
2944
|
-
// we don't consume this signal, since its not a true local.
|
|
2945
|
-
return signal.value;
|
|
2946
|
-
}
|
|
2947
|
-
case 'writeonly-field':
|
|
2948
|
-
{
|
|
2949
|
-
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
2950
|
-
{
|
|
2951
|
-
throw new Error(`Cannot get extended field ${String(prop)} as its definition has only a setter`);
|
|
2952
|
-
}
|
|
2953
|
-
})() : {};
|
|
2954
|
-
return undefined;
|
|
2955
|
-
}
|
|
2956
|
-
default:
|
|
2957
|
-
{
|
|
2958
|
-
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
2959
|
-
{
|
|
2960
|
-
throw new Error(`Unhandled extension kind ${desc.kind}`);
|
|
2961
|
-
}
|
|
2962
|
-
})() : {};
|
|
2963
|
-
return undefined;
|
|
2964
|
-
}
|
|
2965
|
-
}
|
|
2966
|
-
}
|
|
2967
4881
|
|
|
2968
4882
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2969
4883
|
/*
|
|
@@ -3552,6 +5466,15 @@ class RecordArrayManager {
|
|
|
3552
5466
|
}
|
|
3553
5467
|
});
|
|
3554
5468
|
this._subscription = this.store.notifications.subscribe('resource', (identifier, type) => {
|
|
5469
|
+
const schema = this.store.schema.resource?.(identifier);
|
|
5470
|
+
// If we are a polaris mode schema
|
|
5471
|
+
// and we are in the `isNew` state, we are kept hidden from
|
|
5472
|
+
// record arrays.
|
|
5473
|
+
if (schema && (!('legacy' in schema) || !schema.legacy)) {
|
|
5474
|
+
if (this.store.cache.isNew(identifier)) {
|
|
5475
|
+
return;
|
|
5476
|
+
}
|
|
5477
|
+
}
|
|
3555
5478
|
if (type === 'added') {
|
|
3556
5479
|
this._visibilitySet.set(identifier, true);
|
|
3557
5480
|
this.identifierAdded(identifier);
|
|
@@ -4618,30 +6541,61 @@ class Store extends BaseClass {
|
|
|
4618
6541
|
}
|
|
4619
6542
|
|
|
4620
6543
|
/**
|
|
4621
|
-
|
|
4622
|
-
|
|
4623
|
-
|
|
6544
|
+
Creates a new record in the current store.
|
|
6545
|
+
> [!CAUTION]
|
|
6546
|
+
> This should not be used to mock records or to create
|
|
6547
|
+
> a record representing data that could be fetched from
|
|
6548
|
+
> the API.
|
|
6549
|
+
The properties passed to this method are set on
|
|
6550
|
+
the newly created record.
|
|
6551
|
+
For instance: to create a new `post`:
|
|
4624
6552
|
```js
|
|
4625
6553
|
store.createRecord('post', {
|
|
4626
|
-
title: '
|
|
6554
|
+
title: 'WarpDrive is Stellar!'
|
|
4627
6555
|
});
|
|
4628
6556
|
```
|
|
4629
|
-
|
|
6557
|
+
Relationships can be set during create. For instance,
|
|
6558
|
+
to create a new `post` that has an existing user as
|
|
6559
|
+
it's author:
|
|
4630
6560
|
```js
|
|
4631
|
-
|
|
4632
|
-
|
|
4633
|
-
title: '
|
|
6561
|
+
const user = store.peekRecord('user', '1');
|
|
6562
|
+
store.createRecord('post', {
|
|
6563
|
+
title: 'WarpDrive is Stellar!',
|
|
4634
6564
|
user: user
|
|
4635
6565
|
});
|
|
6566
|
+
```
|
|
6567
|
+
### lid handling
|
|
6568
|
+
All new records are assigned an `lid` that can be used to handle
|
|
6569
|
+
transactional saves of multiple records, or to link the data to
|
|
6570
|
+
other data in scenarios involving eventual-consistency or remote
|
|
6571
|
+
syncing.
|
|
6572
|
+
```ts
|
|
6573
|
+
const post = store.createRecord('post', {
|
|
6574
|
+
title: 'WarpDrive is Stellar!'
|
|
6575
|
+
});
|
|
6576
|
+
const { lid } = recordIdentifierFor(post);
|
|
6577
|
+
```
|
|
6578
|
+
The `lid` defaults to a uuidv4 string.
|
|
6579
|
+
In order to support receiving knowledge about unpersisted creates
|
|
6580
|
+
from other sources (say a different tab in the same web-browser),
|
|
6581
|
+
createRecord allows for the `lid` to be provided as part of an
|
|
6582
|
+
optional third argument. **If this lid already exists in the store
|
|
6583
|
+
an error will be thrown.**
|
|
6584
|
+
```ts
|
|
6585
|
+
const post = store.createRecord(
|
|
6586
|
+
'post',
|
|
6587
|
+
{ title: 'WarpDrive is Stellar!' },
|
|
6588
|
+
{ lid: '4d47bb88-931f-496e-986d-c4888cef7373' }
|
|
6589
|
+
);
|
|
4636
6590
|
```
|
|
4637
6591
|
@public
|
|
4638
|
-
@param
|
|
4639
|
-
@param
|
|
6592
|
+
@param type the name of the resource
|
|
6593
|
+
@param inputProperties a hash of properties to set on the
|
|
4640
6594
|
newly created record.
|
|
4641
|
-
@return
|
|
6595
|
+
@return a record in the "isNew" state
|
|
4642
6596
|
*/
|
|
4643
6597
|
|
|
4644
|
-
createRecord(type, inputProperties) {
|
|
6598
|
+
createRecord(type, inputProperties, context) {
|
|
4645
6599
|
if (macroCondition(getGlobalConfig().WarpDrive.env.DEBUG)) {
|
|
4646
6600
|
assertDestroyingStore(this, 'createRecord');
|
|
4647
6601
|
}
|
|
@@ -4695,11 +6649,22 @@ class Store extends BaseClass {
|
|
|
4695
6649
|
}
|
|
4696
6650
|
})(!identifier) : {};
|
|
4697
6651
|
}
|
|
6652
|
+
if (context?.lid) {
|
|
6653
|
+
const identifier = this.identifierCache.peekRecordIdentifier({
|
|
6654
|
+
lid: context?.lid
|
|
6655
|
+
});
|
|
6656
|
+
resource.lid = context.lid;
|
|
6657
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
6658
|
+
if (!test) {
|
|
6659
|
+
throw new Error(`The lid ${context.lid} has already been used with another '${identifier?.type}' record.`);
|
|
6660
|
+
}
|
|
6661
|
+
})(!identifier) : {};
|
|
6662
|
+
}
|
|
4698
6663
|
const identifier = this.identifierCache.createIdentifierForNewRecord(resource);
|
|
4699
6664
|
const cache = this.cache;
|
|
4700
6665
|
const createOptions = normalizeProperties(this, identifier, properties);
|
|
4701
6666
|
const resultProps = cache.clientDidCreate(identifier, createOptions);
|
|
4702
|
-
record = this._instanceCache
|
|
6667
|
+
record = getNewRecord(this._instanceCache, identifier, resultProps);
|
|
4703
6668
|
});
|
|
4704
6669
|
return record;
|
|
4705
6670
|
}
|
|
@@ -5890,7 +7855,7 @@ if (macroCondition(getGlobalConfig().WarpDrive.deprecations.ENABLE_LEGACY_REQUES
|
|
|
5890
7855
|
if (!test) {
|
|
5891
7856
|
throw new Error(`Unable to initiate save for a record in a disconnected state`);
|
|
5892
7857
|
}
|
|
5893
|
-
})(storeFor(record)) : {};
|
|
7858
|
+
})(storeFor(record, true)) : {};
|
|
5894
7859
|
const identifier = recordIdentifierFor(record);
|
|
5895
7860
|
const cache = this.cache;
|
|
5896
7861
|
if (!identifier) {
|
|
@@ -5936,6 +7901,341 @@ function upgradeInstanceCaches(cache) {
|
|
|
5936
7901
|
}
|
|
5937
7902
|
return withReferences;
|
|
5938
7903
|
}
|
|
7904
|
+
const MUTATION_OPS = new Set(['createRecord', 'updateRecord', 'deleteRecord']);
|
|
7905
|
+
function calcShouldFetch(store, request, hasCachedValue, identifier) {
|
|
7906
|
+
const {
|
|
7907
|
+
cacheOptions
|
|
7908
|
+
} = request;
|
|
7909
|
+
return request.op && MUTATION_OPS.has(request.op) || cacheOptions?.reload || !hasCachedValue || (store.lifetimes && identifier ? store.lifetimes.isHardExpired(identifier, store) : false);
|
|
7910
|
+
}
|
|
7911
|
+
function calcShouldBackgroundFetch(store, request, willFetch, identifier) {
|
|
7912
|
+
const {
|
|
7913
|
+
cacheOptions
|
|
7914
|
+
} = request;
|
|
7915
|
+
return cacheOptions?.backgroundReload || (store.lifetimes && identifier ? store.lifetimes.isSoftExpired(identifier, store) : false);
|
|
7916
|
+
}
|
|
7917
|
+
function isMutation(request) {
|
|
7918
|
+
return Boolean(request.op && MUTATION_OPS.has(request.op));
|
|
7919
|
+
}
|
|
7920
|
+
function isCacheAffecting(document) {
|
|
7921
|
+
if (!isMutation(document.request)) {
|
|
7922
|
+
return true;
|
|
7923
|
+
}
|
|
7924
|
+
// a mutation combined with a 204 has no cache impact when no known records were involved
|
|
7925
|
+
// a createRecord with a 201 with an empty response and no known records should similarly
|
|
7926
|
+
// have no cache impact
|
|
7927
|
+
|
|
7928
|
+
if (document.request.op === 'createRecord' && document.response?.status === 201) {
|
|
7929
|
+
return document.content ? Object.keys(document.content).length > 0 : false;
|
|
7930
|
+
}
|
|
7931
|
+
return document.response?.status !== 204;
|
|
7932
|
+
}
|
|
7933
|
+
function isAggregateError(error) {
|
|
7934
|
+
return error instanceof AggregateError || error.name === 'AggregateError' && Array.isArray(error.errors);
|
|
7935
|
+
}
|
|
7936
|
+
// TODO @runspired, consider if we should deep freeze errors (potentially only in debug) vs cloning them
|
|
7937
|
+
function cloneError(error) {
|
|
7938
|
+
const isAggregate = isAggregateError(error);
|
|
7939
|
+
const cloned = isAggregate ? new AggregateError(structuredClone(error.errors), error.message) : new Error(error.message);
|
|
7940
|
+
cloned.stack = error.stack;
|
|
7941
|
+
cloned.error = error.error;
|
|
7942
|
+
|
|
7943
|
+
// copy over enumerable properties
|
|
7944
|
+
Object.assign(cloned, error);
|
|
7945
|
+
return cloned;
|
|
7946
|
+
}
|
|
7947
|
+
function getPriority(identifier, deduped, priority) {
|
|
7948
|
+
if (identifier) {
|
|
7949
|
+
const existing = deduped.get(identifier);
|
|
7950
|
+
if (existing) {
|
|
7951
|
+
return existing.priority;
|
|
7952
|
+
}
|
|
7953
|
+
}
|
|
7954
|
+
return priority;
|
|
7955
|
+
}
|
|
7956
|
+
|
|
7957
|
+
/**
|
|
7958
|
+
* A CacheHandler that adds support for using an WarpDrive Cache with a RequestManager.
|
|
7959
|
+
*
|
|
7960
|
+
* This handler will only run when a request has supplied a `store` instance. Requests
|
|
7961
|
+
* issued by the store via `store.request()` will automatically have the `store` instance
|
|
7962
|
+
* attached to the request.
|
|
7963
|
+
*
|
|
7964
|
+
* ```ts
|
|
7965
|
+
* requestManager.request({
|
|
7966
|
+
* store: store,
|
|
7967
|
+
* url: '/api/posts',
|
|
7968
|
+
* method: 'GET'
|
|
7969
|
+
* });
|
|
7970
|
+
* ```
|
|
7971
|
+
*
|
|
7972
|
+
* When this handler elects to handle a request, it will return the raw `StructuredDocument`
|
|
7973
|
+
* unless the request has `[EnableHydration]` set to `true`. In this case, the handler will
|
|
7974
|
+
* return a `Document` instance that will automatically update the UI when the cache is updated
|
|
7975
|
+
* in the future and will hydrate any identifiers in the StructuredDocument into Record instances.
|
|
7976
|
+
*
|
|
7977
|
+
* When issuing a request via the store, [EnableHydration] is automatically set to `true`. This
|
|
7978
|
+
* means that if desired you can issue requests that utilize the cache without needing to also
|
|
7979
|
+
* utilize Record instances if desired.
|
|
7980
|
+
*
|
|
7981
|
+
* Said differently, you could elect to issue all requests via a RequestManager, without ever using
|
|
7982
|
+
* the store directly, by setting [EnableHydration] to `true` and providing a store instance. Not
|
|
7983
|
+
* necessarily the most useful thing, but the decoupled nature of the RequestManager and incremental-feature
|
|
7984
|
+
* approach of WarpDrive allows for this flexibility.
|
|
7985
|
+
*
|
|
7986
|
+
* ```ts
|
|
7987
|
+
* import { EnableHydration } from '@warp-drive/core/types/request';
|
|
7988
|
+
*
|
|
7989
|
+
* requestManager.request({
|
|
7990
|
+
* store: store,
|
|
7991
|
+
* url: '/api/posts',
|
|
7992
|
+
* method: 'GET',
|
|
7993
|
+
* [EnableHydration]: true
|
|
7994
|
+
* });
|
|
7995
|
+
*
|
|
7996
|
+
*/
|
|
7997
|
+
const CacheHandler = {
|
|
7998
|
+
request(context, next) {
|
|
7999
|
+
// if we have no cache or no cache-key skip cache handling
|
|
8000
|
+
if (!context.request.store || context.request.cacheOptions?.[SkipCache]) {
|
|
8001
|
+
return next(context.request);
|
|
8002
|
+
}
|
|
8003
|
+
const {
|
|
8004
|
+
store
|
|
8005
|
+
} = context.request;
|
|
8006
|
+
const identifier = store.identifierCache.getOrCreateDocumentIdentifier(context.request);
|
|
8007
|
+
if (identifier) {
|
|
8008
|
+
context.setIdentifier(identifier);
|
|
8009
|
+
}
|
|
8010
|
+
|
|
8011
|
+
// used to dedupe existing requests that match
|
|
8012
|
+
const DEDUPE = store.requestManager._deduped;
|
|
8013
|
+
const activeRequest = identifier && DEDUPE.get(identifier);
|
|
8014
|
+
const peeked = identifier ? store.cache.peekRequest(identifier) : null;
|
|
8015
|
+
|
|
8016
|
+
// determine if we should skip cache
|
|
8017
|
+
if (calcShouldFetch(store, context.request, !!peeked, identifier)) {
|
|
8018
|
+
if (activeRequest) {
|
|
8019
|
+
activeRequest.priority = {
|
|
8020
|
+
blocking: true
|
|
8021
|
+
};
|
|
8022
|
+
return activeRequest.promise;
|
|
8023
|
+
}
|
|
8024
|
+
let promise = fetchContentAndHydrate(next, context, identifier, {
|
|
8025
|
+
blocking: true
|
|
8026
|
+
});
|
|
8027
|
+
if (identifier) {
|
|
8028
|
+
promise = promise.finally(() => {
|
|
8029
|
+
DEDUPE.delete(identifier);
|
|
8030
|
+
store.notifications.notify(identifier, 'state');
|
|
8031
|
+
});
|
|
8032
|
+
DEDUPE.set(identifier, {
|
|
8033
|
+
priority: {
|
|
8034
|
+
blocking: true
|
|
8035
|
+
},
|
|
8036
|
+
promise
|
|
8037
|
+
});
|
|
8038
|
+
store.notifications.notify(identifier, 'state');
|
|
8039
|
+
}
|
|
8040
|
+
return promise;
|
|
8041
|
+
}
|
|
8042
|
+
|
|
8043
|
+
// if we have not skipped cache, determine if we should update behind the scenes
|
|
8044
|
+
if (calcShouldBackgroundFetch(store, context.request, false, identifier)) {
|
|
8045
|
+
let promise = activeRequest?.promise || fetchContentAndHydrate(next, context, identifier, {
|
|
8046
|
+
blocking: false
|
|
8047
|
+
});
|
|
8048
|
+
if (identifier && !activeRequest) {
|
|
8049
|
+
promise = promise.finally(() => {
|
|
8050
|
+
DEDUPE.delete(identifier);
|
|
8051
|
+
store.notifications.notify(identifier, 'state');
|
|
8052
|
+
});
|
|
8053
|
+
DEDUPE.set(identifier, {
|
|
8054
|
+
priority: {
|
|
8055
|
+
blocking: false
|
|
8056
|
+
},
|
|
8057
|
+
promise
|
|
8058
|
+
});
|
|
8059
|
+
store.notifications.notify(identifier, 'state');
|
|
8060
|
+
}
|
|
8061
|
+
store.requestManager._pending.set(context.id, promise);
|
|
8062
|
+
}
|
|
8063
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
8064
|
+
if (!test) {
|
|
8065
|
+
throw new Error(`Expected a peeked request to be present`);
|
|
8066
|
+
}
|
|
8067
|
+
})(peeked) : {};
|
|
8068
|
+
const shouldHydrate = context.request[EnableHydration] || false;
|
|
8069
|
+
context.setResponse(peeked.response);
|
|
8070
|
+
if ('error' in peeked) {
|
|
8071
|
+
const content = shouldHydrate ? maybeUpdateUiObjects(store, context.request, {
|
|
8072
|
+
shouldHydrate,
|
|
8073
|
+
identifier
|
|
8074
|
+
}, peeked.content) : peeked.content;
|
|
8075
|
+
const newError = cloneError(peeked);
|
|
8076
|
+
newError.content = content;
|
|
8077
|
+
throw newError;
|
|
8078
|
+
}
|
|
8079
|
+
const result = shouldHydrate ? maybeUpdateUiObjects(store, context.request, {
|
|
8080
|
+
shouldHydrate,
|
|
8081
|
+
identifier
|
|
8082
|
+
}, peeked.content) : peeked.content;
|
|
8083
|
+
return result;
|
|
8084
|
+
}
|
|
8085
|
+
};
|
|
8086
|
+
function maybeUpdateUiObjects(store, request, options, document) {
|
|
8087
|
+
const {
|
|
8088
|
+
identifier
|
|
8089
|
+
} = options;
|
|
8090
|
+
if (!document || !options.shouldHydrate) {
|
|
8091
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
8092
|
+
if (!test) {
|
|
8093
|
+
throw new Error(`The CacheHandler expected response content but none was found`);
|
|
8094
|
+
}
|
|
8095
|
+
})(!options.shouldHydrate) : {};
|
|
8096
|
+
return document ?? null;
|
|
8097
|
+
}
|
|
8098
|
+
if (identifier) {
|
|
8099
|
+
return store._instanceCache.getDocument(identifier);
|
|
8100
|
+
}
|
|
8101
|
+
|
|
8102
|
+
// if we don't have an identifier, we give the document
|
|
8103
|
+
// its own local cache
|
|
8104
|
+
return new ReactiveDocument(store, null, {
|
|
8105
|
+
request,
|
|
8106
|
+
document
|
|
8107
|
+
});
|
|
8108
|
+
}
|
|
8109
|
+
function updateCacheForSuccess(store, request, options, document) {
|
|
8110
|
+
let response = null;
|
|
8111
|
+
if (isMutation(request)) {
|
|
8112
|
+
const record = request.data?.record || request.records?.[0];
|
|
8113
|
+
if (record) {
|
|
8114
|
+
response = store.cache.didCommit(record, document);
|
|
8115
|
+
|
|
8116
|
+
// a mutation combined with a 204 has no cache impact when no known records were involved
|
|
8117
|
+
// a createRecord with a 201 with an empty response and no known records should similarly
|
|
8118
|
+
// have no cache impact
|
|
8119
|
+
} else if (isCacheAffecting(document)) {
|
|
8120
|
+
response = store.cache.put(document);
|
|
8121
|
+
}
|
|
8122
|
+
} else {
|
|
8123
|
+
response = store.cache.put(document);
|
|
8124
|
+
}
|
|
8125
|
+
return maybeUpdateUiObjects(store, request, options, response);
|
|
8126
|
+
}
|
|
8127
|
+
function handleFetchSuccess(store, context, options, document) {
|
|
8128
|
+
const {
|
|
8129
|
+
request
|
|
8130
|
+
} = context;
|
|
8131
|
+
store.requestManager._pending.delete(context.id);
|
|
8132
|
+
store._enableAsyncFlush = true;
|
|
8133
|
+
let response;
|
|
8134
|
+
store._join(() => {
|
|
8135
|
+
response = updateCacheForSuccess(store, request, options, document);
|
|
8136
|
+
});
|
|
8137
|
+
store._enableAsyncFlush = null;
|
|
8138
|
+
if (store.lifetimes?.didRequest) {
|
|
8139
|
+
store.lifetimes.didRequest(context.request, document.response, options.identifier, store);
|
|
8140
|
+
}
|
|
8141
|
+
const finalPriority = getPriority(options.identifier, store.requestManager._deduped, options.priority);
|
|
8142
|
+
if (finalPriority.blocking) {
|
|
8143
|
+
return response;
|
|
8144
|
+
} else {
|
|
8145
|
+
store.notifications._flush();
|
|
8146
|
+
}
|
|
8147
|
+
}
|
|
8148
|
+
function updateCacheForError(store, context, options, error) {
|
|
8149
|
+
let response;
|
|
8150
|
+
if (isMutation(context.request)) {
|
|
8151
|
+
// TODO similar to didCommit we should spec this to be similar to cache.put for handling full response
|
|
8152
|
+
// currently we let the response remain undefiend.
|
|
8153
|
+
const errors = error && error.content && typeof error.content === 'object' && 'errors' in error.content && Array.isArray(error.content.errors) ? error.content.errors : undefined;
|
|
8154
|
+
const record = context.request.data?.record || context.request.records?.[0];
|
|
8155
|
+
store.cache.commitWasRejected(record, errors);
|
|
8156
|
+
} else {
|
|
8157
|
+
response = store.cache.put(error);
|
|
8158
|
+
return maybeUpdateUiObjects(store, context.request, options, response);
|
|
8159
|
+
}
|
|
8160
|
+
}
|
|
8161
|
+
function handleFetchError(store, context, options, error) {
|
|
8162
|
+
store.requestManager._pending.delete(context.id);
|
|
8163
|
+
if (context.request.signal?.aborted) {
|
|
8164
|
+
throw error;
|
|
8165
|
+
}
|
|
8166
|
+
store._enableAsyncFlush = true;
|
|
8167
|
+
let response;
|
|
8168
|
+
store._join(() => {
|
|
8169
|
+
response = updateCacheForError(store, context, options, error);
|
|
8170
|
+
});
|
|
8171
|
+
store._enableAsyncFlush = null;
|
|
8172
|
+
if (options.identifier && store.lifetimes?.didRequest) {
|
|
8173
|
+
store.lifetimes.didRequest(context.request, error.response, options.identifier, store);
|
|
8174
|
+
}
|
|
8175
|
+
if (isMutation(context.request)) {
|
|
8176
|
+
throw error;
|
|
8177
|
+
}
|
|
8178
|
+
const finalPriority = getPriority(options.identifier, store.requestManager._deduped, options.priority);
|
|
8179
|
+
if (finalPriority.blocking) {
|
|
8180
|
+
const newError = cloneError(error);
|
|
8181
|
+
newError.content = response;
|
|
8182
|
+
throw newError;
|
|
8183
|
+
} else {
|
|
8184
|
+
store.notifications._flush();
|
|
8185
|
+
}
|
|
8186
|
+
}
|
|
8187
|
+
function fetchContentAndHydrate(next, context, identifier, priority) {
|
|
8188
|
+
const {
|
|
8189
|
+
store
|
|
8190
|
+
} = context.request;
|
|
8191
|
+
const shouldHydrate = context.request[EnableHydration] || false;
|
|
8192
|
+
const options = {
|
|
8193
|
+
shouldHydrate,
|
|
8194
|
+
identifier,
|
|
8195
|
+
priority
|
|
8196
|
+
};
|
|
8197
|
+
let isMut = false;
|
|
8198
|
+
if (isMutation(context.request)) {
|
|
8199
|
+
isMut = true;
|
|
8200
|
+
// TODO should we handle multiple records in request.records by iteratively calling willCommit for each
|
|
8201
|
+
const record = context.request.data?.record || context.request.records?.[0];
|
|
8202
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
8203
|
+
if (!test) {
|
|
8204
|
+
throw new Error(`Expected to receive a list of records included in the ${context.request.op} request`);
|
|
8205
|
+
}
|
|
8206
|
+
})(record || !shouldHydrate) : {};
|
|
8207
|
+
if (record) {
|
|
8208
|
+
store.cache.willCommit(record, context);
|
|
8209
|
+
}
|
|
8210
|
+
}
|
|
8211
|
+
if (store.lifetimes?.willRequest) {
|
|
8212
|
+
store.lifetimes.willRequest(context.request, identifier, store);
|
|
8213
|
+
}
|
|
8214
|
+
const promise = next(context.request).then(document => {
|
|
8215
|
+
return handleFetchSuccess(store, context, options, document);
|
|
8216
|
+
}, error => {
|
|
8217
|
+
return handleFetchError(store, context, options, error);
|
|
8218
|
+
});
|
|
8219
|
+
if (!isMut) {
|
|
8220
|
+
return promise;
|
|
8221
|
+
}
|
|
8222
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
8223
|
+
if (!test) {
|
|
8224
|
+
throw new Error(`Expected a mutation`);
|
|
8225
|
+
}
|
|
8226
|
+
})(isMutation(context.request)) : {};
|
|
8227
|
+
|
|
8228
|
+
// for mutations we need to enqueue the promise with the requestStateService
|
|
8229
|
+
// TODO should we enque a request per record in records?
|
|
8230
|
+
const record = context.request.data?.record || context.request.records?.[0];
|
|
8231
|
+
return store._requestCache._enqueue(promise, {
|
|
8232
|
+
data: [{
|
|
8233
|
+
op: 'saveRecord',
|
|
8234
|
+
recordIdentifier: record,
|
|
8235
|
+
options: undefined
|
|
8236
|
+
}]
|
|
8237
|
+
});
|
|
8238
|
+
}
|
|
5939
8239
|
function isNonEmptyString(str) {
|
|
5940
8240
|
return Boolean(str && typeof str === 'string');
|
|
5941
8241
|
}
|
|
@@ -7641,4 +9941,4 @@ function getRequestState(future) {
|
|
|
7641
9941
|
}
|
|
7642
9942
|
return state;
|
|
7643
9943
|
}
|
|
7644
|
-
export {
|
|
9944
|
+
export { peekInternalSignal as A, withSignalStore as B, CacheHandler as C, DISPOSE as D, notifyInternalSignal as E, consumeInternalSignal as F, getOrCreateInternalSignal as G, ReactiveResource as H, IdentifierArray as I, isNonIdentityCacheableField as J, getFieldCacheKeyStrict as K, setIdentifierGenerationMethod as L, MUTATE as M, setIdentifierUpdateMethod as N, setIdentifierForgetMethod as O, setIdentifierResetMethod as P, setKeyInfoForResource as Q, RecordArrayManager as R, Store as S, _clearCaches as _, isDocumentIdentifier as a, coerceId as b, constructResource as c, Collection as d, ensureStringId as e, SOURCE as f, fastPush as g, setRecordIdentifier as h, isStableIdentifier as i, StoreMap as j, RelatedCollection as k, log as l, logGroup as m, normalizeModelName as n, getPromiseState as o, createRequestSubscription as p, getRequestState as q, recordIdentifierFor as r, storeFor as s, memoized as t, gate as u, entangleSignal as v, defineSignal as w, defineGate as x, defineNonEnumerableSignal as y, Signals as z };
|