@warp-drive/core 5.6.0-alpha.5 → 5.6.0-beta.0
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/README.md +8 -7
- package/cjs-dist/addon-shim.cjs +1 -0
- package/cjs-dist/cjs-set-config.cjs +1 -0
- package/declarations/build-config/babel-macros.d.ts +1 -0
- package/declarations/build-config/canary-features.d.ts +1 -0
- package/declarations/build-config/debugging.d.ts +1 -0
- package/declarations/build-config/deprecations.d.ts +1 -0
- package/declarations/build-config/env.d.ts +1 -0
- package/declarations/build-config/macros.d.ts +1 -0
- package/declarations/build-config.d.ts +1 -0
- package/declarations/configure.d.ts +7 -0
- package/declarations/graph/-private/-diff.d.ts +32 -0
- package/declarations/graph/-private/-edge-definition.d.ts +148 -0
- package/declarations/graph/-private/-state.d.ts +96 -0
- package/declarations/graph/-private/-utils.d.ts +31 -0
- package/declarations/graph/-private/coerce-id.d.ts +10 -0
- package/declarations/graph/-private/debug/assert-polymorphic-type.d.ts +18 -0
- package/declarations/graph/-private/edges/collection.d.ts +39 -0
- package/declarations/graph/-private/edges/implicit.d.ts +43 -0
- package/declarations/graph/-private/edges/resource.d.ts +24 -0
- package/declarations/graph/-private/graph.d.ts +90 -0
- package/declarations/graph/-private/normalize-link.d.ts +8 -0
- package/declarations/graph/-private/operations/add-to-related-records.d.ts +4 -0
- package/declarations/graph/-private/operations/merge-identifier.d.ts +3 -0
- package/declarations/graph/-private/operations/remove-from-related-records.d.ts +4 -0
- package/declarations/graph/-private/operations/replace-related-record.d.ts +3 -0
- package/declarations/graph/-private/operations/replace-related-records.d.ts +62 -0
- package/declarations/graph/-private/operations/update-relationship.d.ts +13 -0
- package/declarations/graph/-private.d.ts +13 -0
- package/declarations/index.d.ts +14 -3
- package/declarations/reactive/-private/document.d.ts +146 -0
- package/declarations/reactive/-private/fields/compute.d.ts +43 -0
- package/declarations/reactive/-private/fields/extension.d.ts +8 -0
- package/declarations/reactive/-private/fields/managed-array.d.ts +22 -0
- package/declarations/reactive/-private/fields/managed-object.d.ts +21 -0
- package/declarations/reactive/-private/fields/many-array-manager.d.ts +19 -0
- package/declarations/reactive/-private/hooks.d.ts +5 -0
- package/declarations/reactive/-private/record.d.ts +64 -0
- package/declarations/reactive/-private/schema.d.ts +271 -0
- package/declarations/reactive/-private/symbols.d.ts +36 -0
- package/declarations/reactive/-private.d.ts +1 -0
- package/declarations/reactive.d.ts +4 -0
- package/declarations/request/-private/context.d.ts +34 -35
- package/declarations/request/-private/debug.d.ts +2 -3
- package/declarations/request/-private/fetch.d.ts +24 -36
- package/declarations/request/-private/future.d.ts +3 -4
- package/declarations/request/-private/manager.d.ts +159 -132
- package/declarations/request/-private/promise-cache.d.ts +21 -13
- package/declarations/request/-private/types.d.ts +124 -123
- package/declarations/request/-private/utils.d.ts +8 -9
- package/declarations/request.d.ts +5 -433
- package/declarations/store/-private/cache-handler/handler.d.ts +62 -0
- package/declarations/store/-private/cache-handler/types.d.ts +98 -0
- package/declarations/store/-private/cache-handler/utils.d.ts +32 -0
- package/declarations/store/-private/caches/cache-utils.d.ts +12 -0
- package/declarations/store/-private/caches/identifier-cache.d.ts +304 -0
- package/declarations/store/-private/caches/instance-cache.d.ts +65 -0
- package/declarations/store/-private/caches/resource-utils.d.ts +9 -0
- package/declarations/store/-private/debug/utils.d.ts +6 -0
- package/declarations/store/-private/default-cache-policy.d.ts +384 -0
- package/declarations/store/-private/legacy-model-support/record-reference.d.ts +159 -0
- package/declarations/store/-private/legacy-model-support/shim-model-class.d.ts +17 -0
- package/declarations/store/-private/managers/cache-capabilities-manager.d.ts +25 -0
- package/declarations/store/-private/managers/cache-manager.d.ts +443 -0
- package/declarations/store/-private/managers/notification-manager.d.ts +98 -0
- package/declarations/store/-private/managers/record-array-manager.d.ts +97 -0
- package/declarations/store/-private/network/request-cache.d.ts +107 -0
- package/declarations/store/-private/new-core-tmp/promise-state.d.ts +263 -0
- package/declarations/store/-private/new-core-tmp/reactivity/configure.d.ts +176 -0
- package/declarations/store/-private/new-core-tmp/reactivity/internal.d.ts +169 -0
- package/declarations/store/-private/new-core-tmp/reactivity/signal.d.ts +35 -0
- package/declarations/store/-private/new-core-tmp/request-state.d.ts +277 -0
- package/declarations/store/-private/new-core-tmp/request-subscription.d.ts +261 -0
- package/declarations/store/-private/record-arrays/identifier-array.d.ts +147 -0
- package/declarations/store/-private/record-arrays/many-array.d.ts +197 -0
- package/declarations/store/-private/record-arrays/native-proxy-type-fix.d.ts +126 -0
- package/declarations/store/-private/store-service.d.ts +1605 -0
- package/declarations/store/-private/utils/coerce-id.d.ts +10 -0
- package/declarations/store/-private/utils/construct-resource.d.ts +6 -0
- package/declarations/store/-private/utils/is-non-empty-string.d.ts +1 -0
- package/declarations/store/-private/utils/normalize-model-name.d.ts +1 -0
- package/declarations/store/-private/utils/uuid-polyfill.d.ts +1 -0
- package/declarations/store/-private.d.ts +31 -0
- package/declarations/store/-types/q/cache-capabilities-manager.d.ts +99 -0
- package/declarations/store/-types/q/ds-model.d.ts +21 -0
- package/declarations/store/-types/q/identifier.d.ts +20 -0
- package/declarations/store/-types/q/record-instance.d.ts +23 -0
- package/declarations/store/-types/q/schema-service.d.ts +354 -0
- package/declarations/store/-types/q/store.d.ts +32 -0
- package/declarations/store.d.ts +1 -0
- package/declarations/types/-private.d.ts +16 -8
- package/declarations/types/cache/aliases.d.ts +11 -1
- package/declarations/types/cache/change.d.ts +4 -5
- package/declarations/types/cache/mutations.d.ts +51 -28
- package/declarations/types/cache/operations.d.ts +60 -47
- package/declarations/types/cache/relationship.d.ts +11 -9
- package/declarations/types/cache.d.ts +495 -484
- package/declarations/types/graph.d.ts +31 -40
- package/declarations/types/identifier.d.ts +83 -82
- package/declarations/types/json/raw.d.ts +1 -2
- package/declarations/types/params.d.ts +4 -5
- package/declarations/types/record.d.ts +117 -76
- package/declarations/types/request.d.ts +289 -266
- package/declarations/types/runtime.d.ts +8 -9
- package/declarations/types/schema/concepts.d.ts +19 -13
- package/declarations/types/schema/fields.d.ts +1712 -1741
- package/declarations/types/schema/fields.type-test.d.ts +0 -1
- package/declarations/types/spec/document.d.ts +28 -22
- package/declarations/types/spec/error.d.ts +16 -17
- package/declarations/types/spec/json-api-raw.d.ts +102 -102
- package/declarations/types/symbols.d.ts +74 -75
- package/declarations/types/utils.d.ts +5 -5
- package/declarations/types.d.ts +10 -7
- package/declarations/utils/string.d.ts +48 -0
- package/dist/build-config/babel-macros.js +1 -0
- package/dist/build-config/canary-features.js +1 -0
- package/dist/build-config/debugging.js +1 -0
- package/dist/build-config/deprecations.js +1 -0
- package/dist/build-config/env.js +1 -0
- package/dist/build-config/macros.js +1 -0
- package/dist/build-config.js +1 -0
- package/dist/configure-B48bFHOl.js +181 -0
- package/dist/configure.js +1 -0
- package/dist/{context-DE5sFezZ.js → context-COmAnXUQ.js} +2 -2
- package/dist/graph/-private.js +3372 -0
- package/dist/handler-C2T-IyJK.js +339 -0
- package/dist/index.js +37 -104
- package/dist/reactive/-private.js +1 -0
- package/dist/reactive.js +1988 -0
- package/dist/request-state-CjLph1LP.js +8139 -0
- package/dist/request.js +1 -1
- package/dist/store/-private.js +3 -0
- package/dist/store.js +545 -0
- package/dist/symbols-SIstXMLI.js +44 -0
- package/dist/types/-private.js +3 -3
- package/dist/types/record.js +1 -1
- package/dist/types/request.js +2 -2
- package/dist/types/runtime.js +1 -1
- package/dist/types/schema/fields.js +8 -41
- package/dist/types/symbols.js +3 -3
- package/dist/utils/string.js +92 -0
- package/logos/NCC-1701-a-blue.svg +4 -0
- package/logos/NCC-1701-a-gold.svg +4 -0
- package/logos/NCC-1701-a-gold_100.svg +1 -0
- package/logos/NCC-1701-a-gold_base-64.txt +1 -0
- package/logos/NCC-1701-a.svg +4 -0
- package/logos/README.md +4 -0
- package/logos/docs-badge.svg +2 -0
- package/logos/ember-data-logo-dark.svg +12 -0
- package/logos/ember-data-logo-light.svg +12 -0
- package/logos/github-header.svg +444 -0
- package/logos/social1.png +0 -0
- package/logos/social2.png +0 -0
- package/logos/warp-drive-logo-dark.svg +4 -0
- package/logos/warp-drive-logo-gold.svg +4 -0
- package/package.json +11 -8
- package/declarations/index.d.ts.map +0 -1
- package/declarations/request/-private/context.d.ts.map +0 -1
- package/declarations/request/-private/debug.d.ts.map +0 -1
- package/declarations/request/-private/fetch.d.ts.map +0 -1
- package/declarations/request/-private/future.d.ts.map +0 -1
- package/declarations/request/-private/manager.d.ts.map +0 -1
- package/declarations/request/-private/promise-cache.d.ts.map +0 -1
- package/declarations/request/-private/types.d.ts.map +0 -1
- package/declarations/request/-private/utils.d.ts.map +0 -1
- package/declarations/request.d.ts.map +0 -1
- package/declarations/types/-private.d.ts.map +0 -1
- package/declarations/types/cache/aliases.d.ts.map +0 -1
- package/declarations/types/cache/change.d.ts.map +0 -1
- package/declarations/types/cache/mutations.d.ts.map +0 -1
- package/declarations/types/cache/operations.d.ts.map +0 -1
- package/declarations/types/cache/relationship.d.ts.map +0 -1
- package/declarations/types/cache.d.ts.map +0 -1
- package/declarations/types/graph.d.ts.map +0 -1
- package/declarations/types/identifier.d.ts.map +0 -1
- package/declarations/types/json/raw.d.ts.map +0 -1
- package/declarations/types/params.d.ts.map +0 -1
- package/declarations/types/record.d.ts.map +0 -1
- package/declarations/types/record.type-test.d.ts +0 -2
- package/declarations/types/record.type-test.d.ts.map +0 -1
- package/declarations/types/request.d.ts.map +0 -1
- package/declarations/types/request.type-test.d.ts +0 -2
- package/declarations/types/request.type-test.d.ts.map +0 -1
- package/declarations/types/runtime.d.ts.map +0 -1
- package/declarations/types/schema/concepts.d.ts.map +0 -1
- package/declarations/types/schema/fields.d.ts.map +0 -1
- package/declarations/types/schema/fields.type-test.d.ts.map +0 -1
- package/declarations/types/spec/document.d.ts.map +0 -1
- package/declarations/types/spec/error.d.ts.map +0 -1
- package/declarations/types/spec/json-api-raw.d.ts.map +0 -1
- package/declarations/types/symbols.d.ts.map +0 -1
- package/declarations/types/utils.d.ts.map +0 -1
- package/declarations/types.d.ts.map +0 -1
- package/dist/context-DE5sFezZ.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/request.js.map +0 -1
- package/dist/types/-private.js.map +0 -1
- package/dist/types/cache/aliases.js.map +0 -1
- package/dist/types/cache/change.js.map +0 -1
- package/dist/types/cache/mutations.js.map +0 -1
- package/dist/types/cache/operations.js.map +0 -1
- package/dist/types/cache/relationship.js.map +0 -1
- package/dist/types/cache.js.map +0 -1
- package/dist/types/graph.js.map +0 -1
- package/dist/types/identifier.js.map +0 -1
- package/dist/types/json/raw.js.map +0 -1
- package/dist/types/params.js.map +0 -1
- package/dist/types/record.js.map +0 -1
- package/dist/types/request.js.map +0 -1
- package/dist/types/runtime.js.map +0 -1
- package/dist/types/schema/concepts.js.map +0 -1
- package/dist/types/schema/fields.js.map +0 -1
- package/dist/types/schema/fields.type-test.js.map +0 -1
- package/dist/types/spec/document.js.map +0 -1
- package/dist/types/spec/error.js.map +0 -1
- package/dist/types/spec/json-api-raw.js.map +0 -1
- package/dist/types/symbols.js.map +0 -1
- package/dist/types/utils.js.map +0 -1
- package/dist/types.js.map +0 -1
package/dist/reactive.js
ADDED
|
@@ -0,0 +1,1988 @@
|
|
|
1
|
+
import { isResourceSchema } from './types/schema/fields.js';
|
|
2
|
+
import { E as withSignalStore, Q as isExtensionProp, T as performExtensionSet, G as consumeInternalSignal, U as performArrayExtensionGet, x as entangleSignal, V as performObjectExtensionGet, d as SOURCE$1, f as fastPush, y as defineSignal, l as RelatedCollection, H as getOrCreateInternalSignal, F as notifyInternalSignal, A as Signals, h as setRecordIdentifier, r as recordIdentifierFor } from "./request-state-CjLph1LP.js";
|
|
3
|
+
import { EnableHydration, STRUCTURED } from './types/request.js';
|
|
4
|
+
import { macroCondition, getGlobalConfig } from '@embroider/macros';
|
|
5
|
+
import { deprecate } from '@ember/debug';
|
|
6
|
+
import './utils/string.js';
|
|
7
|
+
import { A as ARRAY_SIGNAL, O as OBJECT_SIGNAL, c as createMemo } from "./configure-B48bFHOl.js";
|
|
8
|
+
import { RecordStore, Type } from './types/symbols.js';
|
|
9
|
+
import { getOrSetGlobal } from './types/-private.js';
|
|
10
|
+
import { S as SOURCE, E as Editable, L as Legacy, I as Identifier, P as Parent, a as EmbeddedPath, D as Destroy, C as Checkout, b as EmbeddedField } from "./symbols-SIstXMLI.js";
|
|
11
|
+
import './index.js';
|
|
12
|
+
const ARRAY_GETTER_METHODS = 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']);
|
|
13
|
+
// const ARRAY_SETTER_METHODS = new Set<KeyType>(['push', 'pop', 'unshift', 'shift', 'splice', 'sort']);
|
|
14
|
+
const SYNC_PROPS = new Set(['[]', 'length']);
|
|
15
|
+
function isArrayGetter(prop) {
|
|
16
|
+
return ARRAY_GETTER_METHODS.has(prop);
|
|
17
|
+
}
|
|
18
|
+
const ARRAY_SETTER_METHODS = new Set(['push', 'pop', 'unshift', 'shift', 'splice', 'sort']);
|
|
19
|
+
function isArraySetter(prop) {
|
|
20
|
+
return ARRAY_SETTER_METHODS.has(prop);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// function isSelfProp<T extends object>(self: T, prop: KeyType): prop is keyof T {
|
|
24
|
+
// return prop in self;
|
|
25
|
+
// }
|
|
26
|
+
|
|
27
|
+
function convertToInt(prop) {
|
|
28
|
+
if (typeof prop === 'symbol') return null;
|
|
29
|
+
const num = Number(prop);
|
|
30
|
+
if (isNaN(num)) return null;
|
|
31
|
+
return num % 1 === 0 ? num : null;
|
|
32
|
+
}
|
|
33
|
+
function safeForEach(instance, arr, store, callback, target) {
|
|
34
|
+
if (target === undefined) {
|
|
35
|
+
target = null;
|
|
36
|
+
}
|
|
37
|
+
// clone to prevent mutation
|
|
38
|
+
arr = arr.slice();
|
|
39
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
40
|
+
if (!test) {
|
|
41
|
+
throw new Error('`forEach` expects a function as first argument.');
|
|
42
|
+
}
|
|
43
|
+
})(typeof callback === 'function') : {};
|
|
44
|
+
|
|
45
|
+
// because we retrieveLatest above we need not worry if array is mutated during iteration
|
|
46
|
+
// by unloadRecord/rollbackAttributes
|
|
47
|
+
// push/add/removeObject may still be problematic
|
|
48
|
+
// but this is a more traditionally expected forEach bug.
|
|
49
|
+
const length = arr.length; // we need to access length to ensure we are consumed
|
|
50
|
+
|
|
51
|
+
for (let index = 0; index < length; index++) {
|
|
52
|
+
callback.call(target, arr[index], index, instance);
|
|
53
|
+
}
|
|
54
|
+
return instance;
|
|
55
|
+
}
|
|
56
|
+
// eslint-disable-next-line @typescript-eslint/no-extraneous-class
|
|
57
|
+
class ManagedArray {
|
|
58
|
+
constructor(store, schema, cache, field, data, identifier, path, owner, isSchemaArray, editable, legacy) {
|
|
59
|
+
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
60
|
+
const self = this;
|
|
61
|
+
this[SOURCE] = data?.slice();
|
|
62
|
+
const IS_EDITABLE = this[Editable] = editable ?? false;
|
|
63
|
+
this[Legacy] = legacy;
|
|
64
|
+
const signals = withSignalStore(this);
|
|
65
|
+
let _SIGNAL = null;
|
|
66
|
+
const boundFns = new Map();
|
|
67
|
+
this.identifier = identifier;
|
|
68
|
+
this.path = path;
|
|
69
|
+
this.owner = owner;
|
|
70
|
+
let transaction = false;
|
|
71
|
+
const mode = field.options?.key ?? '@identity';
|
|
72
|
+
const RefStorage = mode === '@identity' ? WeakMap :
|
|
73
|
+
// CAUTION CAUTION CAUTION
|
|
74
|
+
// this is a pile of lies
|
|
75
|
+
// the Map is Map<string, WeakRef<ReactiveResource>>
|
|
76
|
+
// but TS does not understand how to juggle modes like this
|
|
77
|
+
// internal to a method like ours without us duplicating the code
|
|
78
|
+
// into two separate methods.
|
|
79
|
+
Map;
|
|
80
|
+
const ManagedRecordRefs = isSchemaArray ? new RefStorage() : null;
|
|
81
|
+
const extensions = legacy ? schema.CAUTION_MEGA_DANGER_ZONE_arrayExtensions(field) : null;
|
|
82
|
+
const proxy = new Proxy(this[SOURCE], {
|
|
83
|
+
get(target, prop, receiver) {
|
|
84
|
+
if (prop === ARRAY_SIGNAL) {
|
|
85
|
+
return _SIGNAL;
|
|
86
|
+
}
|
|
87
|
+
if (prop === 'identifier') {
|
|
88
|
+
return self.identifier;
|
|
89
|
+
}
|
|
90
|
+
if (prop === 'owner') {
|
|
91
|
+
return self.owner;
|
|
92
|
+
}
|
|
93
|
+
const index = convertToInt(prop);
|
|
94
|
+
if (_SIGNAL.isStale && (index !== null || SYNC_PROPS.has(prop) || isArrayGetter(prop))) {
|
|
95
|
+
_SIGNAL.isStale = false;
|
|
96
|
+
const newData = cache.getAttr(identifier, path);
|
|
97
|
+
if (newData && newData !== self[SOURCE]) {
|
|
98
|
+
self[SOURCE].length = 0;
|
|
99
|
+
self[SOURCE].push(...newData);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
if (prop === 'length') {
|
|
103
|
+
return consumeInternalSignal(_SIGNAL), target.length;
|
|
104
|
+
}
|
|
105
|
+
if (prop === '[]') return consumeInternalSignal(_SIGNAL), receiver;
|
|
106
|
+
if (index !== null) {
|
|
107
|
+
let val;
|
|
108
|
+
if (mode === '@hash') {
|
|
109
|
+
val = target[index];
|
|
110
|
+
const hashField = schema.resource({
|
|
111
|
+
type: field.type
|
|
112
|
+
}).identity;
|
|
113
|
+
const hashFn = schema.hashFn(hashField);
|
|
114
|
+
val = hashFn(val, null, null);
|
|
115
|
+
} else {
|
|
116
|
+
// if mode is not @identity or @index, then access the key path.
|
|
117
|
+
// we should assert that `mode` is a string
|
|
118
|
+
// it should read directly from the cache value for that field (e.g. no derivation, no transformation)
|
|
119
|
+
// and, we likely should lookup the associated field and throw an error IF
|
|
120
|
+
// the given field does not exist OR
|
|
121
|
+
// the field is anything other than a GenericField or LegacyAttributeField.
|
|
122
|
+
if (mode !== '@identity' && mode !== '@index') {
|
|
123
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
124
|
+
if (!test) {
|
|
125
|
+
throw new Error('mode must be a string');
|
|
126
|
+
}
|
|
127
|
+
})(typeof mode === 'string') : {};
|
|
128
|
+
const modeField = schema.resource({
|
|
129
|
+
type: field.type
|
|
130
|
+
}).fields.find(f => f.name === mode);
|
|
131
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
132
|
+
if (!test) {
|
|
133
|
+
throw new Error('field must exist in schema');
|
|
134
|
+
}
|
|
135
|
+
})(modeField) : {};
|
|
136
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
137
|
+
if (!test) {
|
|
138
|
+
throw new Error('field must be a GenericField or LegacyAttributeField');
|
|
139
|
+
}
|
|
140
|
+
})(modeField.kind === 'field' || modeField.kind === 'attribute') : {};
|
|
141
|
+
}
|
|
142
|
+
val = mode === '@identity' ? target[index] : mode === '@index' ? '@index' : target[index][mode];
|
|
143
|
+
}
|
|
144
|
+
if (isSchemaArray) {
|
|
145
|
+
if (!transaction) {
|
|
146
|
+
consumeInternalSignal(_SIGNAL);
|
|
147
|
+
}
|
|
148
|
+
if (val) {
|
|
149
|
+
const recordRef = ManagedRecordRefs.get(val);
|
|
150
|
+
let record = recordRef?.deref();
|
|
151
|
+
if (!record) {
|
|
152
|
+
const recordPath = path.slice();
|
|
153
|
+
// this is a dirty lie since path is string[] but really we
|
|
154
|
+
// should change the types for paths to `Array<string | number>`
|
|
155
|
+
// TODO we should allow the schema for the field to define a "key"
|
|
156
|
+
// for stability. Default should be `@identity` which means that
|
|
157
|
+
// same object reference from cache should result in same ReactiveResource
|
|
158
|
+
// embedded object.
|
|
159
|
+
recordPath.push(index);
|
|
160
|
+
const recordIdentifier = self.owner[Identifier] || self.owner[Parent];
|
|
161
|
+
record = new ReactiveResource(store, recordIdentifier, {
|
|
162
|
+
[Editable]: self.owner[Editable],
|
|
163
|
+
[Legacy]: self.owner[Legacy]
|
|
164
|
+
}, true, field, recordPath);
|
|
165
|
+
// if mode is not @identity or @index, then access the key path now
|
|
166
|
+
// to determine the key value.
|
|
167
|
+
// chris says we can implement this as a special kind `@hash` which
|
|
168
|
+
// would be a function that only has access to the cache value and not
|
|
169
|
+
// the record itself, so derivation is possible but intentionally limited
|
|
170
|
+
// and non-reactive?
|
|
171
|
+
ManagedRecordRefs.set(val, new WeakRef(record));
|
|
172
|
+
}
|
|
173
|
+
return record;
|
|
174
|
+
}
|
|
175
|
+
return val;
|
|
176
|
+
}
|
|
177
|
+
if (!transaction) {
|
|
178
|
+
consumeInternalSignal(_SIGNAL);
|
|
179
|
+
}
|
|
180
|
+
if (field.type) {
|
|
181
|
+
const transform = schema.transformation(field);
|
|
182
|
+
return transform.hydrate(val, field.options ?? null, self.owner);
|
|
183
|
+
}
|
|
184
|
+
return val;
|
|
185
|
+
}
|
|
186
|
+
if (isArrayGetter(prop)) {
|
|
187
|
+
let fn = boundFns.get(prop);
|
|
188
|
+
if (fn === undefined) {
|
|
189
|
+
if (prop === 'forEach') {
|
|
190
|
+
fn = function () {
|
|
191
|
+
consumeInternalSignal(_SIGNAL);
|
|
192
|
+
transaction = true;
|
|
193
|
+
const result = safeForEach(receiver, target, store, arguments[0], arguments[1]);
|
|
194
|
+
transaction = false;
|
|
195
|
+
return result;
|
|
196
|
+
};
|
|
197
|
+
} else {
|
|
198
|
+
fn = function () {
|
|
199
|
+
consumeInternalSignal(_SIGNAL);
|
|
200
|
+
// array functions must run through Reflect to work properly
|
|
201
|
+
// binding via other means will not work.
|
|
202
|
+
transaction = true;
|
|
203
|
+
const result = Reflect.apply(target[prop], receiver, arguments);
|
|
204
|
+
transaction = false;
|
|
205
|
+
return result;
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
boundFns.set(prop, fn);
|
|
209
|
+
}
|
|
210
|
+
return fn;
|
|
211
|
+
}
|
|
212
|
+
if (isArraySetter(prop)) {
|
|
213
|
+
let fn = boundFns.get(prop);
|
|
214
|
+
if (fn === undefined) {
|
|
215
|
+
fn = function () {
|
|
216
|
+
if (!IS_EDITABLE) {
|
|
217
|
+
throw new Error(`Mutating this array via ${String(prop)} is not allowed because the record is not editable`);
|
|
218
|
+
}
|
|
219
|
+
consumeInternalSignal(_SIGNAL);
|
|
220
|
+
transaction = true;
|
|
221
|
+
const result = Reflect.apply(target[prop], receiver, arguments);
|
|
222
|
+
transaction = false;
|
|
223
|
+
return result;
|
|
224
|
+
};
|
|
225
|
+
boundFns.set(prop, fn);
|
|
226
|
+
}
|
|
227
|
+
return fn;
|
|
228
|
+
}
|
|
229
|
+
if (isExtensionProp(extensions, prop)) {
|
|
230
|
+
return performArrayExtensionGet(receiver, extensions, signals, prop, _SIGNAL, boundFns, v => void (transaction = v));
|
|
231
|
+
}
|
|
232
|
+
return Reflect.get(target, prop, receiver);
|
|
233
|
+
},
|
|
234
|
+
set(target, prop, value, receiver) {
|
|
235
|
+
if (!IS_EDITABLE) {
|
|
236
|
+
let errorPath = identifier.type;
|
|
237
|
+
if (path) {
|
|
238
|
+
errorPath = path[path.length - 1];
|
|
239
|
+
}
|
|
240
|
+
throw new Error(`Cannot set ${String(prop)} on ${errorPath} because the record is not editable`);
|
|
241
|
+
}
|
|
242
|
+
if (prop === 'identifier') {
|
|
243
|
+
self.identifier = value;
|
|
244
|
+
return true;
|
|
245
|
+
}
|
|
246
|
+
if (prop === 'owner') {
|
|
247
|
+
self.owner = value;
|
|
248
|
+
return true;
|
|
249
|
+
}
|
|
250
|
+
if (isExtensionProp(extensions, prop)) {
|
|
251
|
+
return performExtensionSet(receiver, extensions, signals, prop, value);
|
|
252
|
+
}
|
|
253
|
+
const reflect = Reflect.set(target, prop, value, receiver);
|
|
254
|
+
if (reflect) {
|
|
255
|
+
if (!field.type) {
|
|
256
|
+
cache.setAttr(identifier, path, self[SOURCE]);
|
|
257
|
+
_SIGNAL.isStale = true;
|
|
258
|
+
return true;
|
|
259
|
+
}
|
|
260
|
+
let rawValue = self[SOURCE];
|
|
261
|
+
if (!isSchemaArray) {
|
|
262
|
+
const transform = schema.transformation(field);
|
|
263
|
+
if (!transform) {
|
|
264
|
+
throw new Error(`No '${field.type}' transform defined for use by ${identifier.type}.${String(prop)}`);
|
|
265
|
+
}
|
|
266
|
+
rawValue = self[SOURCE].map(item => transform.serialize(item, field.options ?? null, self.owner));
|
|
267
|
+
}
|
|
268
|
+
cache.setAttr(identifier, path, rawValue);
|
|
269
|
+
_SIGNAL.isStale = true;
|
|
270
|
+
}
|
|
271
|
+
return reflect;
|
|
272
|
+
},
|
|
273
|
+
has(target, prop) {
|
|
274
|
+
if (prop === 'identifier' || prop === 'owner' || prop === ARRAY_SIGNAL) {
|
|
275
|
+
return true;
|
|
276
|
+
}
|
|
277
|
+
return Reflect.has(target, prop);
|
|
278
|
+
}
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
// we entangle the signal on the returned proxy since that is
|
|
282
|
+
// the object that other code will be interfacing with.
|
|
283
|
+
_SIGNAL = entangleSignal(signals, proxy, ARRAY_SIGNAL, undefined);
|
|
284
|
+
return proxy;
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
// this will error if someone tries to call
|
|
289
|
+
// A(identifierArray) since it is not configurable
|
|
290
|
+
// which is preferable to the `meta` override we used
|
|
291
|
+
// before which required importing all of Ember
|
|
292
|
+
const desc = {
|
|
293
|
+
enumerable: true,
|
|
294
|
+
configurable: false,
|
|
295
|
+
get: function () {
|
|
296
|
+
// here to support computed chains
|
|
297
|
+
// and {{#each}}
|
|
298
|
+
if (macroCondition(getGlobalConfig().WarpDrive.deprecations.DEPRECATE_COMPUTED_CHAINS)) {
|
|
299
|
+
return this;
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
};
|
|
303
|
+
// compat(desc);
|
|
304
|
+
Object.defineProperty(ManagedArray.prototype, '[]', desc);
|
|
305
|
+
const ObjectSymbols = new Set([OBJECT_SIGNAL, Parent, SOURCE, Editable, EmbeddedPath]);
|
|
306
|
+
|
|
307
|
+
// const ignoredGlobalFields = new Set<string>(['setInterval', 'nodeType', 'nodeName', 'length', 'document', STRUCTURED]);
|
|
308
|
+
|
|
309
|
+
// eslint-disable-next-line @typescript-eslint/no-extraneous-class
|
|
310
|
+
class ManagedObject {
|
|
311
|
+
constructor(schema, cache, field, data, identifier, path, owner, editable, legacy) {
|
|
312
|
+
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
313
|
+
const self = this;
|
|
314
|
+
this[SOURCE] = {
|
|
315
|
+
...data
|
|
316
|
+
};
|
|
317
|
+
const signals = withSignalStore(this);
|
|
318
|
+
const _SIGNAL = this[OBJECT_SIGNAL] = entangleSignal(signals, this, OBJECT_SIGNAL, undefined);
|
|
319
|
+
this[Editable] = editable;
|
|
320
|
+
this[Legacy] = legacy;
|
|
321
|
+
this[Parent] = identifier;
|
|
322
|
+
this[EmbeddedPath] = path;
|
|
323
|
+
|
|
324
|
+
// prettier-ignore
|
|
325
|
+
const extensions = !legacy ? null : schema.CAUTION_MEGA_DANGER_ZONE_objectExtensions(field);
|
|
326
|
+
const proxy = new Proxy(this[SOURCE], {
|
|
327
|
+
ownKeys() {
|
|
328
|
+
return Object.keys(self[SOURCE]);
|
|
329
|
+
},
|
|
330
|
+
has(target, prop) {
|
|
331
|
+
return prop in self[SOURCE];
|
|
332
|
+
},
|
|
333
|
+
getOwnPropertyDescriptor(target, prop) {
|
|
334
|
+
return {
|
|
335
|
+
writable: editable,
|
|
336
|
+
enumerable: true,
|
|
337
|
+
configurable: true
|
|
338
|
+
};
|
|
339
|
+
},
|
|
340
|
+
get(target, prop, receiver) {
|
|
341
|
+
if (ObjectSymbols.has(prop)) {
|
|
342
|
+
return self[prop];
|
|
343
|
+
}
|
|
344
|
+
if (prop === Symbol.toPrimitive) {
|
|
345
|
+
return () => null;
|
|
346
|
+
}
|
|
347
|
+
if (prop === Symbol.toStringTag) {
|
|
348
|
+
return `ManagedObject<${identifier.type}:${identifier.id} (${identifier.lid})>`;
|
|
349
|
+
}
|
|
350
|
+
if (prop === 'constructor') {
|
|
351
|
+
return Object;
|
|
352
|
+
}
|
|
353
|
+
if (prop === 'toString') {
|
|
354
|
+
return function () {
|
|
355
|
+
return `ManagedObject<${identifier.type}:${identifier.id} (${identifier.lid})>`;
|
|
356
|
+
};
|
|
357
|
+
}
|
|
358
|
+
if (prop === 'toHTML') {
|
|
359
|
+
return function () {
|
|
360
|
+
return '<span>ManagedObject</span>';
|
|
361
|
+
};
|
|
362
|
+
}
|
|
363
|
+
if (_SIGNAL.isStale) {
|
|
364
|
+
_SIGNAL.isStale = false;
|
|
365
|
+
let newData = cache.getAttr(identifier, path);
|
|
366
|
+
if (newData && newData !== self[SOURCE]) {
|
|
367
|
+
if (field.type) {
|
|
368
|
+
const transform = schema.transformation(field);
|
|
369
|
+
newData = transform.hydrate(newData, field.options ?? null, owner);
|
|
370
|
+
}
|
|
371
|
+
self[SOURCE] = {
|
|
372
|
+
...newData
|
|
373
|
+
}; // Add type assertion for newData
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
// toJSON and extensions need to come after we update data if stale
|
|
378
|
+
if (prop === 'toJSON') {
|
|
379
|
+
return function () {
|
|
380
|
+
return structuredClone(self[SOURCE]);
|
|
381
|
+
};
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
// we always defer to data before extensions
|
|
385
|
+
if (prop in self[SOURCE]) {
|
|
386
|
+
consumeInternalSignal(_SIGNAL);
|
|
387
|
+
return self[SOURCE][prop];
|
|
388
|
+
}
|
|
389
|
+
if (isExtensionProp(extensions, prop)) {
|
|
390
|
+
return performObjectExtensionGet(receiver, extensions, signals, prop);
|
|
391
|
+
}
|
|
392
|
+
return Reflect.get(target, prop, receiver);
|
|
393
|
+
},
|
|
394
|
+
set(target, prop, value, receiver) {
|
|
395
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
396
|
+
if (!test) {
|
|
397
|
+
throw new Error(`Cannot set read-only property '${String(prop)}' on ManagedObject`);
|
|
398
|
+
}
|
|
399
|
+
})(editable) : {};
|
|
400
|
+
|
|
401
|
+
// since objects function as dictionaries, we can't defer to schema/data before extensions
|
|
402
|
+
// unless the prop is in the existing data.
|
|
403
|
+
if (!(prop in self[SOURCE]) && isExtensionProp(extensions, prop)) {
|
|
404
|
+
return performExtensionSet(receiver, extensions, signals, prop, value);
|
|
405
|
+
}
|
|
406
|
+
const reflect = Reflect.set(target, prop, value, receiver);
|
|
407
|
+
if (!reflect) {
|
|
408
|
+
return false;
|
|
409
|
+
}
|
|
410
|
+
if (!field.type) {
|
|
411
|
+
cache.setAttr(identifier, path, self[SOURCE]);
|
|
412
|
+
} else {
|
|
413
|
+
const transform = schema.transformation(field);
|
|
414
|
+
const val = transform.serialize(self[SOURCE], field.options ?? null, owner);
|
|
415
|
+
cache.setAttr(identifier, path, val);
|
|
416
|
+
}
|
|
417
|
+
_SIGNAL.isStale = true;
|
|
418
|
+
return true;
|
|
419
|
+
}
|
|
420
|
+
});
|
|
421
|
+
return proxy;
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
class ManyArrayManager {
|
|
425
|
+
constructor(record, editable) {
|
|
426
|
+
this.record = record;
|
|
427
|
+
this.store = record[RecordStore];
|
|
428
|
+
this.identifier = record[Identifier];
|
|
429
|
+
this.editable = editable;
|
|
430
|
+
}
|
|
431
|
+
_syncArray(array) {
|
|
432
|
+
const method = this.editable ? 'getRelationship' : 'getRemoteRelationship';
|
|
433
|
+
const rawValue = this.store.cache[method](this.identifier, array.key);
|
|
434
|
+
if (rawValue.meta) {
|
|
435
|
+
array.meta = rawValue.meta;
|
|
436
|
+
}
|
|
437
|
+
if (rawValue.links) {
|
|
438
|
+
array.links = rawValue.links;
|
|
439
|
+
}
|
|
440
|
+
const currentState = array[SOURCE$1];
|
|
441
|
+
|
|
442
|
+
// unlike in the normal RecordArray case, we don't need to divorce the reference
|
|
443
|
+
// because we don't need to worry about associate/disassociate since the graph
|
|
444
|
+
// takes care of that for us
|
|
445
|
+
if (currentState !== rawValue.data) {
|
|
446
|
+
currentState.length = 0;
|
|
447
|
+
fastPush(currentState, rawValue.data);
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
reloadHasMany(key, options) {
|
|
451
|
+
const field = this.store.schema.fields(this.identifier).get(key);
|
|
452
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
453
|
+
if (!test) {
|
|
454
|
+
throw new Error(`Expected a hasMany field for ${key}`);
|
|
455
|
+
}
|
|
456
|
+
})(field?.kind === 'hasMany') : {};
|
|
457
|
+
const cacheOptions = options ? extractCacheOptions(options) : {
|
|
458
|
+
reload: true
|
|
459
|
+
};
|
|
460
|
+
cacheOptions.types = [field.type];
|
|
461
|
+
const rawValue = this.store.cache.getRelationship(this.identifier, key);
|
|
462
|
+
const req = {
|
|
463
|
+
url: getRelatedLink(rawValue),
|
|
464
|
+
op: 'findHasMany',
|
|
465
|
+
method: 'GET',
|
|
466
|
+
records: rawValue.data,
|
|
467
|
+
cacheOptions,
|
|
468
|
+
options: {
|
|
469
|
+
field,
|
|
470
|
+
identifier: this.identifier,
|
|
471
|
+
links: rawValue.links,
|
|
472
|
+
meta: rawValue.meta
|
|
473
|
+
},
|
|
474
|
+
[EnableHydration]: false
|
|
475
|
+
};
|
|
476
|
+
return this.store.request(req);
|
|
477
|
+
}
|
|
478
|
+
mutate(mutation) {
|
|
479
|
+
this.store.cache.mutate(mutation);
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
function getRelatedLink(resource) {
|
|
483
|
+
const related = resource.links?.related;
|
|
484
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
485
|
+
if (!test) {
|
|
486
|
+
throw new Error(`Expected a related link`);
|
|
487
|
+
}
|
|
488
|
+
})(related) : {};
|
|
489
|
+
return typeof related === 'object' ? related.href : related;
|
|
490
|
+
}
|
|
491
|
+
function extractCacheOptions(options) {
|
|
492
|
+
const cacheOptions = {};
|
|
493
|
+
if ('reload' in options) {
|
|
494
|
+
cacheOptions.reload = options.reload;
|
|
495
|
+
}
|
|
496
|
+
if ('backgroundReload' in options) {
|
|
497
|
+
cacheOptions.backgroundReload = options.backgroundReload;
|
|
498
|
+
}
|
|
499
|
+
return cacheOptions;
|
|
500
|
+
}
|
|
501
|
+
const ManagedArrayMap = getOrSetGlobal('ManagedArrayMap', new Map());
|
|
502
|
+
const ManagedObjectMap = getOrSetGlobal('ManagedObjectMap', new Map());
|
|
503
|
+
function computeLocal(record, field, prop) {
|
|
504
|
+
const signals = withSignalStore(record);
|
|
505
|
+
const signal = getOrCreateInternalSignal(signals, record, prop, field.options?.defaultValue ?? null);
|
|
506
|
+
consumeInternalSignal(signal);
|
|
507
|
+
return signal.value;
|
|
508
|
+
}
|
|
509
|
+
function peekManagedArray(record, field) {
|
|
510
|
+
const managedArrayMapForRecord = ManagedArrayMap.get(record);
|
|
511
|
+
if (managedArrayMapForRecord) {
|
|
512
|
+
return managedArrayMapForRecord.get(field.name);
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
function peekManagedObject(record, field) {
|
|
516
|
+
const managedObjectMapForRecord = ManagedObjectMap.get(record);
|
|
517
|
+
if (managedObjectMapForRecord) {
|
|
518
|
+
return managedObjectMapForRecord.get(field.name);
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
function computeField(schema, cache, record, identifier, field, prop, editable) {
|
|
522
|
+
const rawValue = editable ? cache.getAttr(identifier, prop) : cache.getRemoteAttr(identifier, prop);
|
|
523
|
+
if (!field.type) {
|
|
524
|
+
return rawValue;
|
|
525
|
+
}
|
|
526
|
+
const transform = schema.transformation(field);
|
|
527
|
+
return transform.hydrate(rawValue, field.options ?? null, record);
|
|
528
|
+
}
|
|
529
|
+
function computeArray(store, schema, cache, record, identifier, field, path, editable, legacy) {
|
|
530
|
+
const isSchemaArray = field.kind === 'schema-array';
|
|
531
|
+
// the thing we hand out needs to know its owner and path in a private manner
|
|
532
|
+
// its "address" is the parent identifier (identifier) + field name (field.name)
|
|
533
|
+
// in the nested object case field name here is the full dot path from root resource to this value
|
|
534
|
+
// its "key" is the field on the parent record
|
|
535
|
+
// its "owner" is the parent record
|
|
536
|
+
|
|
537
|
+
const managedArrayMapForRecord = ManagedArrayMap.get(record);
|
|
538
|
+
let managedArray;
|
|
539
|
+
if (managedArrayMapForRecord) {
|
|
540
|
+
managedArray = managedArrayMapForRecord.get(field.name);
|
|
541
|
+
}
|
|
542
|
+
if (managedArray) {
|
|
543
|
+
return managedArray;
|
|
544
|
+
} else {
|
|
545
|
+
const rawValue = editable ? cache.getAttr(identifier, path) : cache.getRemoteAttr(identifier, path);
|
|
546
|
+
if (!rawValue) {
|
|
547
|
+
return null;
|
|
548
|
+
}
|
|
549
|
+
managedArray = new ManagedArray(store, schema, cache, field, rawValue, identifier, path, record, isSchemaArray, editable, legacy);
|
|
550
|
+
if (!managedArrayMapForRecord) {
|
|
551
|
+
ManagedArrayMap.set(record, new Map([[field.name, managedArray]]));
|
|
552
|
+
} else {
|
|
553
|
+
managedArrayMapForRecord.set(field.name, managedArray);
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
return managedArray;
|
|
557
|
+
}
|
|
558
|
+
function computeObject(schema, cache, record, identifier, field, path, editable, legacy) {
|
|
559
|
+
const managedObjectMapForRecord = ManagedObjectMap.get(record);
|
|
560
|
+
let managedObject;
|
|
561
|
+
if (managedObjectMapForRecord) {
|
|
562
|
+
managedObject = managedObjectMapForRecord.get(field.name);
|
|
563
|
+
}
|
|
564
|
+
if (managedObject) {
|
|
565
|
+
return managedObject;
|
|
566
|
+
} else {
|
|
567
|
+
let rawValue = editable ? cache.getAttr(identifier, path) : cache.getRemoteAttr(identifier, path);
|
|
568
|
+
if (!rawValue) {
|
|
569
|
+
return null;
|
|
570
|
+
}
|
|
571
|
+
if (field.type) {
|
|
572
|
+
const transform = schema.transformation(field);
|
|
573
|
+
rawValue = transform.hydrate(rawValue, field.options ?? null, record);
|
|
574
|
+
}
|
|
575
|
+
managedObject = new ManagedObject(schema, cache, field, rawValue, identifier, path, record, editable, legacy);
|
|
576
|
+
if (!managedObjectMapForRecord) {
|
|
577
|
+
ManagedObjectMap.set(record, new Map([[field.name, managedObject]]));
|
|
578
|
+
} else {
|
|
579
|
+
managedObjectMapForRecord.set(field.name, managedObject);
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
return managedObject;
|
|
583
|
+
}
|
|
584
|
+
function computeSchemaObject(store, cache, record, identifier, field, path, legacy, editable) {
|
|
585
|
+
const schemaObjectMapForRecord = ManagedObjectMap.get(record);
|
|
586
|
+
let schemaObject;
|
|
587
|
+
if (schemaObjectMapForRecord) {
|
|
588
|
+
schemaObject = schemaObjectMapForRecord.get(field.name);
|
|
589
|
+
}
|
|
590
|
+
if (schemaObject) {
|
|
591
|
+
return schemaObject;
|
|
592
|
+
} else {
|
|
593
|
+
const rawValue = editable ? cache.getAttr(identifier, path) : cache.getRemoteAttr(identifier, path);
|
|
594
|
+
if (!rawValue) {
|
|
595
|
+
return null;
|
|
596
|
+
}
|
|
597
|
+
const embeddedPath = path.slice();
|
|
598
|
+
schemaObject = new ReactiveResource(store, identifier, {
|
|
599
|
+
[Editable]: editable,
|
|
600
|
+
[Legacy]: legacy
|
|
601
|
+
}, true, field, embeddedPath);
|
|
602
|
+
}
|
|
603
|
+
if (!schemaObjectMapForRecord) {
|
|
604
|
+
ManagedObjectMap.set(record, new Map([[field.name, schemaObject]]));
|
|
605
|
+
} else {
|
|
606
|
+
schemaObjectMapForRecord.set(field.name, schemaObject);
|
|
607
|
+
}
|
|
608
|
+
return schemaObject;
|
|
609
|
+
}
|
|
610
|
+
function computeAttribute(cache, identifier, prop, editable) {
|
|
611
|
+
return editable ? cache.getAttr(identifier, prop) : cache.getRemoteAttr(identifier, prop);
|
|
612
|
+
}
|
|
613
|
+
function computeDerivation(schema, record, identifier, field, prop) {
|
|
614
|
+
return schema.derivation(field)(record, field.options ?? null, prop);
|
|
615
|
+
}
|
|
616
|
+
// TODO probably this should just be a Document
|
|
617
|
+
// but its separate until we work out the lid situation
|
|
618
|
+
class ResourceRelationship {
|
|
619
|
+
constructor(store, cache, parent, identifier, field, name, editable) {
|
|
620
|
+
const rawValue = editable ? cache.getRelationship(identifier, name) : cache.getRemoteRelationship(identifier, name);
|
|
621
|
+
|
|
622
|
+
// TODO setup true lids for relationship documents
|
|
623
|
+
// @ts-expect-error we need to give relationship documents a lid
|
|
624
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
625
|
+
this.lid = rawValue.lid ?? rawValue.links?.self ?? `relationship:${identifier.lid}.${name}`;
|
|
626
|
+
this.data = rawValue.data ? store.peekRecord(rawValue.data) : null;
|
|
627
|
+
this.name = name;
|
|
628
|
+
if (macroCondition(getGlobalConfig().WarpDrive.env.DEBUG)) {
|
|
629
|
+
this.links = Object.freeze(Object.assign({}, rawValue.links));
|
|
630
|
+
this.meta = Object.freeze(Object.assign({}, rawValue.meta));
|
|
631
|
+
} else {
|
|
632
|
+
this.links = rawValue.links ?? {};
|
|
633
|
+
this.meta = rawValue.meta ?? {};
|
|
634
|
+
}
|
|
635
|
+
this[RecordStore] = store;
|
|
636
|
+
this[Parent] = parent;
|
|
637
|
+
}
|
|
638
|
+
fetch(options) {
|
|
639
|
+
const url = options?.url ?? getHref(this.links.related) ?? getHref(this.links.self) ?? null;
|
|
640
|
+
if (!url) {
|
|
641
|
+
throw new Error(`Cannot ${options?.method ?? 'fetch'} ${this[Parent][Identifier].type}.${String(this.name)} because it has no related link`);
|
|
642
|
+
}
|
|
643
|
+
const request = Object.assign({
|
|
644
|
+
url,
|
|
645
|
+
method: 'GET'
|
|
646
|
+
}, options);
|
|
647
|
+
return this[RecordStore].request(request);
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
defineSignal(ResourceRelationship.prototype, 'data', null);
|
|
651
|
+
defineSignal(ResourceRelationship.prototype, 'links', null);
|
|
652
|
+
defineSignal(ResourceRelationship.prototype, 'meta', null);
|
|
653
|
+
function getHref(link) {
|
|
654
|
+
if (!link) {
|
|
655
|
+
return null;
|
|
656
|
+
}
|
|
657
|
+
if (typeof link === 'string') {
|
|
658
|
+
return link;
|
|
659
|
+
}
|
|
660
|
+
return link.href;
|
|
661
|
+
}
|
|
662
|
+
function computeResource(store, cache, parent, identifier, field, prop, editable) {
|
|
663
|
+
if (field.kind !== 'resource') {
|
|
664
|
+
throw new Error(`The schema for ${identifier.type}.${String(prop)} is not a resource relationship`);
|
|
665
|
+
}
|
|
666
|
+
return new ResourceRelationship(store, cache, parent, identifier, field, prop, editable);
|
|
667
|
+
}
|
|
668
|
+
function computeHasMany(store, schema, cache, record, identifier, field, path, editable, legacy) {
|
|
669
|
+
// the thing we hand out needs to know its owner and path in a private manner
|
|
670
|
+
// its "address" is the parent identifier (identifier) + field name (field.name)
|
|
671
|
+
// in the nested object case field name here is the full dot path from root resource to this value
|
|
672
|
+
// its "key" is the field on the parent record
|
|
673
|
+
// its "owner" is the parent record
|
|
674
|
+
|
|
675
|
+
const managedArrayMapForRecord = ManagedArrayMap.get(record);
|
|
676
|
+
let managedArray;
|
|
677
|
+
if (managedArrayMapForRecord) {
|
|
678
|
+
managedArray = managedArrayMapForRecord.get(field.name);
|
|
679
|
+
}
|
|
680
|
+
if (managedArray) {
|
|
681
|
+
return managedArray;
|
|
682
|
+
} else {
|
|
683
|
+
const rawValue = cache.getRelationship(identifier, field.name);
|
|
684
|
+
if (!rawValue) {
|
|
685
|
+
return null;
|
|
686
|
+
}
|
|
687
|
+
managedArray = new RelatedCollection({
|
|
688
|
+
store,
|
|
689
|
+
type: field.type,
|
|
690
|
+
identifier,
|
|
691
|
+
cache,
|
|
692
|
+
field: legacy ? field : undefined,
|
|
693
|
+
// we divorce the reference here because ManyArray mutates the target directly
|
|
694
|
+
// before sending the mutation op to the cache. We may be able to avoid this in the future
|
|
695
|
+
identifiers: rawValue.data?.slice(),
|
|
696
|
+
key: field.name,
|
|
697
|
+
meta: rawValue.meta || null,
|
|
698
|
+
links: rawValue.links || null,
|
|
699
|
+
isPolymorphic: field.options.polymorphic ?? false,
|
|
700
|
+
isAsync: field.options.async ?? false,
|
|
701
|
+
// TODO: Grab the proper value
|
|
702
|
+
_inverseIsAsync: false,
|
|
703
|
+
// @ts-expect-error Typescript doesn't have a way for us to thread the generic backwards so it infers unknown instead of T
|
|
704
|
+
manager: new ManyArrayManager(record, editable),
|
|
705
|
+
isLoaded: true,
|
|
706
|
+
allowMutation: editable
|
|
707
|
+
});
|
|
708
|
+
if (!managedArrayMapForRecord) {
|
|
709
|
+
ManagedArrayMap.set(record, new Map([[field.name, managedArray]]));
|
|
710
|
+
} else {
|
|
711
|
+
managedArrayMapForRecord.set(field.name, managedArray);
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
return managedArray;
|
|
715
|
+
}
|
|
716
|
+
const IgnoredGlobalFields = new Set(['length', 'nodeType', 'then', 'setInterval', 'document', STRUCTURED]);
|
|
717
|
+
const symbolList = [Destroy, RecordStore, Identifier, Editable, Parent, Checkout, Legacy, EmbeddedPath, EmbeddedField];
|
|
718
|
+
const RecordSymbols = new Set(symbolList);
|
|
719
|
+
function isPathMatch(a, b) {
|
|
720
|
+
return a.length === b.length && a.every((v, i) => v === b[i]);
|
|
721
|
+
}
|
|
722
|
+
function isNonEnumerableProp(prop) {
|
|
723
|
+
return prop === 'constructor' || prop === 'prototype' || prop === '__proto__' || prop === 'toString' || prop === 'toJSON' || prop === 'toHTML' || typeof prop === 'symbol';
|
|
724
|
+
}
|
|
725
|
+
const Editables = new WeakMap();
|
|
726
|
+
/**
|
|
727
|
+
* A class that uses a the ResourceSchema for a ResourceType
|
|
728
|
+
* and a ResouceKey to transform data from the cache into a rich, reactive
|
|
729
|
+
* object.
|
|
730
|
+
*
|
|
731
|
+
* This class is not directly instantiable. To use it, you should
|
|
732
|
+
* configure the store's `instantiateRecord` and `teardownRecord` hooks
|
|
733
|
+
* with the matching hooks provided by this package.
|
|
734
|
+
*
|
|
735
|
+
* @hideconstructor
|
|
736
|
+
* @public
|
|
737
|
+
*/
|
|
738
|
+
// eslint-disable-next-line @typescript-eslint/no-extraneous-class
|
|
739
|
+
class ReactiveResource {
|
|
740
|
+
constructor(store, identifier, Mode, isEmbedded = false, embeddedField = null, embeddedPath = null) {
|
|
741
|
+
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
742
|
+
const self = this;
|
|
743
|
+
this[RecordStore] = store;
|
|
744
|
+
if (isEmbedded) {
|
|
745
|
+
this[Parent] = identifier;
|
|
746
|
+
} else {
|
|
747
|
+
this[Identifier] = identifier;
|
|
748
|
+
}
|
|
749
|
+
const IS_EDITABLE = this[Editable] = Mode[Editable] ?? false;
|
|
750
|
+
this[Legacy] = Mode[Legacy] ?? false;
|
|
751
|
+
const schema = store.schema;
|
|
752
|
+
const cache = store.cache;
|
|
753
|
+
const identityField = schema.resource(isEmbedded ? embeddedField : identifier).identity;
|
|
754
|
+
const BoundFns = new Map();
|
|
755
|
+
|
|
756
|
+
// prettier-ignore
|
|
757
|
+
const extensions = !Mode[Legacy] ? null : isEmbedded ? schema.CAUTION_MEGA_DANGER_ZONE_objectExtensions(embeddedField) : schema.CAUTION_MEGA_DANGER_ZONE_resourceExtensions(identifier);
|
|
758
|
+
this[EmbeddedField] = embeddedField;
|
|
759
|
+
this[EmbeddedPath] = embeddedPath;
|
|
760
|
+
const fields = isEmbedded ? schema.fields(embeddedField) : schema.fields(identifier);
|
|
761
|
+
const signals = withSignalStore(this);
|
|
762
|
+
const proxy = new Proxy(this, {
|
|
763
|
+
ownKeys() {
|
|
764
|
+
const identityKey = identityField?.name;
|
|
765
|
+
const keys = Array.from(fields.keys());
|
|
766
|
+
if (identityKey) {
|
|
767
|
+
keys.unshift(identityKey);
|
|
768
|
+
}
|
|
769
|
+
return keys;
|
|
770
|
+
},
|
|
771
|
+
has(target, prop) {
|
|
772
|
+
if (prop === Destroy || prop === Checkout) {
|
|
773
|
+
return true;
|
|
774
|
+
}
|
|
775
|
+
return fields.has(prop);
|
|
776
|
+
},
|
|
777
|
+
getOwnPropertyDescriptor(target, prop) {
|
|
778
|
+
const schemaForField = prop === identityField?.name ? identityField : fields.get(prop);
|
|
779
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
780
|
+
if (!test) {
|
|
781
|
+
throw new Error(`No field named ${String(prop)} on ${identifier.type}`);
|
|
782
|
+
}
|
|
783
|
+
})(schemaForField) : {};
|
|
784
|
+
if (isNonEnumerableProp(prop)) {
|
|
785
|
+
return {
|
|
786
|
+
writable: false,
|
|
787
|
+
enumerable: false,
|
|
788
|
+
configurable: true
|
|
789
|
+
};
|
|
790
|
+
}
|
|
791
|
+
switch (schemaForField.kind) {
|
|
792
|
+
case 'derived':
|
|
793
|
+
return {
|
|
794
|
+
writable: false,
|
|
795
|
+
enumerable: true,
|
|
796
|
+
configurable: true
|
|
797
|
+
};
|
|
798
|
+
case '@id':
|
|
799
|
+
return {
|
|
800
|
+
writable: identifier.id === null,
|
|
801
|
+
enumerable: true,
|
|
802
|
+
configurable: true
|
|
803
|
+
};
|
|
804
|
+
case '@local':
|
|
805
|
+
case 'field':
|
|
806
|
+
case 'attribute':
|
|
807
|
+
case 'resource':
|
|
808
|
+
case 'alias':
|
|
809
|
+
case 'belongsTo':
|
|
810
|
+
case 'hasMany':
|
|
811
|
+
case 'collection':
|
|
812
|
+
case 'schema-array':
|
|
813
|
+
case 'array':
|
|
814
|
+
case 'schema-object':
|
|
815
|
+
case 'object':
|
|
816
|
+
return {
|
|
817
|
+
writable: IS_EDITABLE,
|
|
818
|
+
enumerable: true,
|
|
819
|
+
configurable: true
|
|
820
|
+
};
|
|
821
|
+
default:
|
|
822
|
+
return {
|
|
823
|
+
writable: false,
|
|
824
|
+
enumerable: false,
|
|
825
|
+
configurable: false
|
|
826
|
+
};
|
|
827
|
+
}
|
|
828
|
+
},
|
|
829
|
+
get(target, prop, receiver) {
|
|
830
|
+
if (RecordSymbols.has(prop)) {
|
|
831
|
+
if (prop === Destroy) {
|
|
832
|
+
return () => _DESTROY(receiver);
|
|
833
|
+
}
|
|
834
|
+
if (prop === Checkout) {
|
|
835
|
+
return () => _CHECKOUT(receiver);
|
|
836
|
+
}
|
|
837
|
+
return target[prop];
|
|
838
|
+
}
|
|
839
|
+
if (prop === Signals) {
|
|
840
|
+
return signals;
|
|
841
|
+
}
|
|
842
|
+
|
|
843
|
+
// TODO make this a symbol
|
|
844
|
+
if (prop === '___notifications') {
|
|
845
|
+
return target.___notifications;
|
|
846
|
+
}
|
|
847
|
+
|
|
848
|
+
// ReactiveResource reserves use of keys that begin with these characters
|
|
849
|
+
// for its own usage.
|
|
850
|
+
// _, @, $, *
|
|
851
|
+
|
|
852
|
+
const maybeField = prop === identityField?.name ? identityField : fields.get(prop);
|
|
853
|
+
if (!maybeField) {
|
|
854
|
+
if (IgnoredGlobalFields.has(prop)) {
|
|
855
|
+
return undefined;
|
|
856
|
+
}
|
|
857
|
+
|
|
858
|
+
/////////////////////////////////////////////////////////////
|
|
859
|
+
//// Note these bound function behaviors are essentially ////
|
|
860
|
+
//// built-in but overrideable derivations. ////
|
|
861
|
+
//// ////
|
|
862
|
+
//// The bar for this has to be "basic expectations of ////
|
|
863
|
+
/// an object" – very, very high ////
|
|
864
|
+
/////////////////////////////////////////////////////////////
|
|
865
|
+
|
|
866
|
+
if (prop === Symbol.toStringTag || prop === 'toString') {
|
|
867
|
+
let fn = BoundFns.get('toString');
|
|
868
|
+
if (!fn) {
|
|
869
|
+
fn = function () {
|
|
870
|
+
entangleSignal(signals, receiver, '@identity', null);
|
|
871
|
+
return `Record<${identifier.type}:${identifier.id} (${identifier.lid})>`;
|
|
872
|
+
};
|
|
873
|
+
BoundFns.set(prop, fn);
|
|
874
|
+
}
|
|
875
|
+
return fn;
|
|
876
|
+
}
|
|
877
|
+
if (prop === 'toHTML') {
|
|
878
|
+
let fn = BoundFns.get('toHTML');
|
|
879
|
+
if (!fn) {
|
|
880
|
+
fn = function () {
|
|
881
|
+
entangleSignal(signals, receiver, '@identity', null);
|
|
882
|
+
return `<span>Record<${identifier.type}:${identifier.id} (${identifier.lid})></span>`;
|
|
883
|
+
};
|
|
884
|
+
BoundFns.set(prop, fn);
|
|
885
|
+
}
|
|
886
|
+
return fn;
|
|
887
|
+
}
|
|
888
|
+
if (prop === 'toJSON') {
|
|
889
|
+
let fn = BoundFns.get('toJSON');
|
|
890
|
+
if (!fn) {
|
|
891
|
+
fn = function () {
|
|
892
|
+
const json = {};
|
|
893
|
+
for (const key in receiver) {
|
|
894
|
+
json[key] = receiver[key];
|
|
895
|
+
}
|
|
896
|
+
return json;
|
|
897
|
+
};
|
|
898
|
+
BoundFns.set(prop, fn);
|
|
899
|
+
}
|
|
900
|
+
return fn;
|
|
901
|
+
}
|
|
902
|
+
if (prop === Symbol.toPrimitive) return () => null;
|
|
903
|
+
if (prop === Symbol.iterator) {
|
|
904
|
+
let fn = BoundFns.get(Symbol.iterator);
|
|
905
|
+
if (!fn) {
|
|
906
|
+
fn = function* () {
|
|
907
|
+
for (const key in receiver) {
|
|
908
|
+
yield [key, receiver[key]];
|
|
909
|
+
}
|
|
910
|
+
};
|
|
911
|
+
BoundFns.set(Symbol.iterator, fn);
|
|
912
|
+
}
|
|
913
|
+
return fn;
|
|
914
|
+
}
|
|
915
|
+
if (prop === 'constructor') {
|
|
916
|
+
return ReactiveResource;
|
|
917
|
+
}
|
|
918
|
+
if (isExtensionProp(extensions, prop)) {
|
|
919
|
+
return performObjectExtensionGet(receiver, extensions, signals, prop);
|
|
920
|
+
}
|
|
921
|
+
|
|
922
|
+
// too many things check for random symbols
|
|
923
|
+
if (typeof prop === 'symbol') return undefined;
|
|
924
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
925
|
+
{
|
|
926
|
+
throw new Error(`No field named ${String(prop)} on ${isEmbedded ? embeddedField.type : identifier.type}`);
|
|
927
|
+
}
|
|
928
|
+
})() : {};
|
|
929
|
+
return undefined;
|
|
930
|
+
}
|
|
931
|
+
const field = maybeField.kind === 'alias' ? maybeField.options : maybeField;
|
|
932
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
933
|
+
if (!test) {
|
|
934
|
+
throw new Error(`Alias fields cannot alias '@id' '@local' '@hash' or 'derived' fields`);
|
|
935
|
+
}
|
|
936
|
+
})(maybeField.kind !== 'alias' || !['@id', '@local', '@hash', 'derived'].includes(maybeField.options.kind)) : {};
|
|
937
|
+
const propArray = isEmbedded ? embeddedPath.slice() : [];
|
|
938
|
+
// we use the field.name instead of prop here because we want to use the cache-path not
|
|
939
|
+
// the record path.
|
|
940
|
+
propArray.push(field.name);
|
|
941
|
+
// propArray.push(prop as string);
|
|
942
|
+
|
|
943
|
+
switch (field.kind) {
|
|
944
|
+
case '@id':
|
|
945
|
+
entangleSignal(signals, receiver, '@identity', null);
|
|
946
|
+
return identifier.id;
|
|
947
|
+
case '@hash':
|
|
948
|
+
// TODO pass actual cache value not {}
|
|
949
|
+
return schema.hashFn(field)({}, field.options ?? null, field.name ?? null);
|
|
950
|
+
case '@local':
|
|
951
|
+
{
|
|
952
|
+
return computeLocal(receiver, field, prop);
|
|
953
|
+
}
|
|
954
|
+
case 'field':
|
|
955
|
+
entangleSignal(signals, receiver, field.name, null);
|
|
956
|
+
return computeField(schema, cache, target, identifier, field, propArray, IS_EDITABLE);
|
|
957
|
+
case 'attribute':
|
|
958
|
+
entangleSignal(signals, receiver, field.name, null);
|
|
959
|
+
return computeAttribute(cache, identifier, prop, IS_EDITABLE);
|
|
960
|
+
case 'resource':
|
|
961
|
+
entangleSignal(signals, receiver, field.name, null);
|
|
962
|
+
return computeResource(store, cache, target, identifier, field, prop, IS_EDITABLE);
|
|
963
|
+
case 'derived':
|
|
964
|
+
return computeDerivation(schema, receiver, identifier, field, prop);
|
|
965
|
+
case 'schema-array':
|
|
966
|
+
case 'array':
|
|
967
|
+
entangleSignal(signals, receiver, field.name, null);
|
|
968
|
+
return computeArray(store, schema, cache, target, identifier, field, propArray, Mode[Editable], Mode[Legacy]);
|
|
969
|
+
case 'object':
|
|
970
|
+
entangleSignal(signals, receiver, field.name, null);
|
|
971
|
+
return computeObject(schema, cache, target, identifier, field, propArray, Mode[Editable], Mode[Legacy]);
|
|
972
|
+
case 'schema-object':
|
|
973
|
+
entangleSignal(signals, receiver, field.name, null);
|
|
974
|
+
// run transform, then use that value as the object to manage
|
|
975
|
+
return computeSchemaObject(store, cache, target, identifier, field, propArray, Mode[Legacy], Mode[Editable]);
|
|
976
|
+
case 'belongsTo':
|
|
977
|
+
if (field.options.linksMode) {
|
|
978
|
+
entangleSignal(signals, receiver, field.name, null);
|
|
979
|
+
const rawValue = IS_EDITABLE ? cache.getRelationship(identifier, field.name) : cache.getRemoteRelationship(identifier, field.name);
|
|
980
|
+
|
|
981
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
982
|
+
return rawValue.data ? store.peekRecord(rawValue.data) : null;
|
|
983
|
+
}
|
|
984
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
985
|
+
if (!test) {
|
|
986
|
+
throw new Error(`Can only use belongsTo fields when the resource is in legacy mode`);
|
|
987
|
+
}
|
|
988
|
+
})(Mode[Legacy]) : {};
|
|
989
|
+
entangleSignal(signals, receiver, field.name, null);
|
|
990
|
+
return schema._kind('@legacy', 'belongsTo').get(store, receiver, identifier, field);
|
|
991
|
+
case 'hasMany':
|
|
992
|
+
if (field.options.linksMode) {
|
|
993
|
+
entangleSignal(signals, receiver, field.name, null);
|
|
994
|
+
return computeHasMany(store, schema, cache, target, identifier, field, propArray, Mode[Editable], Mode[Legacy]);
|
|
995
|
+
}
|
|
996
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
997
|
+
if (!test) {
|
|
998
|
+
throw new Error(`Can only use hasMany fields when the resource is in legacy mode`);
|
|
999
|
+
}
|
|
1000
|
+
})(Mode[Legacy]) : {};
|
|
1001
|
+
entangleSignal(signals, receiver, field.name, null);
|
|
1002
|
+
return schema._kind('@legacy', 'hasMany').get(store, receiver, identifier, field);
|
|
1003
|
+
default:
|
|
1004
|
+
throw new Error(`Field '${String(prop)}' on '${identifier.type}' has the unknown kind '${field.kind}'`);
|
|
1005
|
+
}
|
|
1006
|
+
},
|
|
1007
|
+
set(target, prop, value, receiver) {
|
|
1008
|
+
if (!IS_EDITABLE) {
|
|
1009
|
+
const type = isEmbedded ? embeddedField.type : identifier.type;
|
|
1010
|
+
throw new Error(`Cannot set ${String(prop)} on ${type} because the record is not editable`);
|
|
1011
|
+
}
|
|
1012
|
+
const maybeField = prop === identityField?.name ? identityField : fields.get(prop);
|
|
1013
|
+
if (!maybeField) {
|
|
1014
|
+
const type = isEmbedded ? embeddedField.type : identifier.type;
|
|
1015
|
+
if (isExtensionProp(extensions, prop)) {
|
|
1016
|
+
return performExtensionSet(receiver, extensions, signals, prop, value);
|
|
1017
|
+
}
|
|
1018
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
1019
|
+
{
|
|
1020
|
+
throw new Error(`There is no settable field named ${String(prop)} on ${type}`);
|
|
1021
|
+
}
|
|
1022
|
+
})() : {};
|
|
1023
|
+
return false;
|
|
1024
|
+
}
|
|
1025
|
+
const field = maybeField.kind === 'alias' ? maybeField.options : maybeField;
|
|
1026
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
1027
|
+
if (!test) {
|
|
1028
|
+
throw new Error(`Alias fields cannot alias '@id' '@local' '@hash' or 'derived' fields`);
|
|
1029
|
+
}
|
|
1030
|
+
})(maybeField.kind !== 'alias' || !['@id', '@local', '@hash', 'derived'].includes(maybeField.options.kind)) : {};
|
|
1031
|
+
const propArray = isEmbedded ? embeddedPath.slice() : [];
|
|
1032
|
+
// we use the field.name instead of prop here because we want to use the cache-path not
|
|
1033
|
+
// the record path.
|
|
1034
|
+
propArray.push(field.name);
|
|
1035
|
+
// propArray.push(prop as string);
|
|
1036
|
+
|
|
1037
|
+
switch (field.kind) {
|
|
1038
|
+
case '@id':
|
|
1039
|
+
{
|
|
1040
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
1041
|
+
if (!test) {
|
|
1042
|
+
throw new Error(`Expected to receive a string id`);
|
|
1043
|
+
}
|
|
1044
|
+
})(typeof value === 'string' && value.length) : {};
|
|
1045
|
+
const normalizedId = String(value);
|
|
1046
|
+
const didChange = normalizedId !== identifier.id;
|
|
1047
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
1048
|
+
if (!test) {
|
|
1049
|
+
throw new Error(`Cannot set ${identifier.type} record's id to ${normalizedId}, because id is already ${identifier.id}`);
|
|
1050
|
+
}
|
|
1051
|
+
})(!didChange || identifier.id === null) : {};
|
|
1052
|
+
if (normalizedId !== null && didChange) {
|
|
1053
|
+
store._instanceCache.setRecordId(identifier, normalizedId);
|
|
1054
|
+
store.notifications.notify(identifier, 'identity');
|
|
1055
|
+
}
|
|
1056
|
+
return true;
|
|
1057
|
+
}
|
|
1058
|
+
case '@local':
|
|
1059
|
+
{
|
|
1060
|
+
const signal = getOrCreateInternalSignal(signals, receiver, prop, field.options?.defaultValue ?? null);
|
|
1061
|
+
if (signal.value !== value) {
|
|
1062
|
+
signal.value = value;
|
|
1063
|
+
notifyInternalSignal(signal);
|
|
1064
|
+
}
|
|
1065
|
+
return true;
|
|
1066
|
+
}
|
|
1067
|
+
case 'field':
|
|
1068
|
+
{
|
|
1069
|
+
if (!field.type) {
|
|
1070
|
+
cache.setAttr(identifier, propArray, value);
|
|
1071
|
+
return true;
|
|
1072
|
+
}
|
|
1073
|
+
const transform = schema.transformation(field);
|
|
1074
|
+
const rawValue = transform.serialize(value, field.options ?? null, target);
|
|
1075
|
+
cache.setAttr(identifier, propArray, rawValue);
|
|
1076
|
+
return true;
|
|
1077
|
+
}
|
|
1078
|
+
case 'attribute':
|
|
1079
|
+
{
|
|
1080
|
+
cache.setAttr(identifier, propArray, value);
|
|
1081
|
+
return true;
|
|
1082
|
+
}
|
|
1083
|
+
case 'array':
|
|
1084
|
+
{
|
|
1085
|
+
if (!field.type) {
|
|
1086
|
+
cache.setAttr(identifier, propArray, value?.slice());
|
|
1087
|
+
const peeked = peekManagedArray(self, field);
|
|
1088
|
+
if (peeked) {
|
|
1089
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
1090
|
+
if (!test) {
|
|
1091
|
+
throw new Error(`Expected the peekManagedArray for ${field.kind} to return a ManagedArray`);
|
|
1092
|
+
}
|
|
1093
|
+
})(ARRAY_SIGNAL in peeked) : {};
|
|
1094
|
+
const arrSignal = peeked[ARRAY_SIGNAL];
|
|
1095
|
+
arrSignal.isStale = true;
|
|
1096
|
+
}
|
|
1097
|
+
if (!Array.isArray(value)) {
|
|
1098
|
+
ManagedArrayMap.delete(target);
|
|
1099
|
+
}
|
|
1100
|
+
return true;
|
|
1101
|
+
}
|
|
1102
|
+
const transform = schema.transformation(field);
|
|
1103
|
+
const rawValue = value.map(item => transform.serialize(item, field.options ?? null, target));
|
|
1104
|
+
cache.setAttr(identifier, propArray, rawValue);
|
|
1105
|
+
const peeked = peekManagedArray(self, field);
|
|
1106
|
+
if (peeked) {
|
|
1107
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
1108
|
+
if (!test) {
|
|
1109
|
+
throw new Error(`Expected the peekManagedArray for ${field.kind} to return a ManagedArray`);
|
|
1110
|
+
}
|
|
1111
|
+
})(ARRAY_SIGNAL in peeked) : {};
|
|
1112
|
+
const arrSignal = peeked[ARRAY_SIGNAL];
|
|
1113
|
+
arrSignal.isStale = true;
|
|
1114
|
+
}
|
|
1115
|
+
return true;
|
|
1116
|
+
}
|
|
1117
|
+
case 'schema-array':
|
|
1118
|
+
{
|
|
1119
|
+
const arrayValue = value?.slice();
|
|
1120
|
+
if (!Array.isArray(arrayValue)) {
|
|
1121
|
+
ManagedArrayMap.delete(target);
|
|
1122
|
+
}
|
|
1123
|
+
cache.setAttr(identifier, propArray, arrayValue);
|
|
1124
|
+
const peeked = peekManagedArray(self, field);
|
|
1125
|
+
if (peeked) {
|
|
1126
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
1127
|
+
if (!test) {
|
|
1128
|
+
throw new Error(`Expected the peekManagedArray for ${field.kind} to return a ManagedArray`);
|
|
1129
|
+
}
|
|
1130
|
+
})(ARRAY_SIGNAL in peeked) : {};
|
|
1131
|
+
const arrSignal = peeked[ARRAY_SIGNAL];
|
|
1132
|
+
arrSignal.isStale = true;
|
|
1133
|
+
}
|
|
1134
|
+
if (!Array.isArray(value)) {
|
|
1135
|
+
ManagedArrayMap.delete(target);
|
|
1136
|
+
}
|
|
1137
|
+
return true;
|
|
1138
|
+
}
|
|
1139
|
+
case 'object':
|
|
1140
|
+
{
|
|
1141
|
+
if (!field.type) {
|
|
1142
|
+
let newValue = value;
|
|
1143
|
+
if (value !== null) {
|
|
1144
|
+
newValue = {
|
|
1145
|
+
...value
|
|
1146
|
+
};
|
|
1147
|
+
} else {
|
|
1148
|
+
ManagedObjectMap.delete(target);
|
|
1149
|
+
}
|
|
1150
|
+
cache.setAttr(identifier, propArray, newValue);
|
|
1151
|
+
const peeked = peekManagedObject(self, field);
|
|
1152
|
+
if (peeked) {
|
|
1153
|
+
const objSignal = peeked[OBJECT_SIGNAL];
|
|
1154
|
+
objSignal.isStale = true;
|
|
1155
|
+
}
|
|
1156
|
+
return true;
|
|
1157
|
+
}
|
|
1158
|
+
const transform = schema.transformation(field);
|
|
1159
|
+
const rawValue = transform.serialize({
|
|
1160
|
+
...value
|
|
1161
|
+
}, field.options ?? null, target);
|
|
1162
|
+
cache.setAttr(identifier, propArray, rawValue);
|
|
1163
|
+
const peeked = peekManagedObject(self, field);
|
|
1164
|
+
if (peeked) {
|
|
1165
|
+
const objSignal = peeked[OBJECT_SIGNAL];
|
|
1166
|
+
objSignal.isStale = true;
|
|
1167
|
+
}
|
|
1168
|
+
return true;
|
|
1169
|
+
}
|
|
1170
|
+
case 'schema-object':
|
|
1171
|
+
{
|
|
1172
|
+
let newValue = value;
|
|
1173
|
+
if (value !== null) {
|
|
1174
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
1175
|
+
if (!test) {
|
|
1176
|
+
throw new Error(`Expected value to be an object`);
|
|
1177
|
+
}
|
|
1178
|
+
})(typeof value === 'object') : {};
|
|
1179
|
+
newValue = {
|
|
1180
|
+
...value
|
|
1181
|
+
};
|
|
1182
|
+
const schemaFields = schema.fields({
|
|
1183
|
+
type: field.type
|
|
1184
|
+
});
|
|
1185
|
+
for (const key of Object.keys(newValue)) {
|
|
1186
|
+
if (!schemaFields.has(key)) {
|
|
1187
|
+
throw new Error(`Field ${key} does not exist on schema object ${field.type}`);
|
|
1188
|
+
}
|
|
1189
|
+
}
|
|
1190
|
+
} else {
|
|
1191
|
+
ManagedObjectMap.delete(target);
|
|
1192
|
+
}
|
|
1193
|
+
cache.setAttr(identifier, propArray, newValue);
|
|
1194
|
+
// const peeked = peekManagedObject(self, field);
|
|
1195
|
+
// if (peeked) {
|
|
1196
|
+
// const objSignal = peeked[OBJECT_SIGNAL];
|
|
1197
|
+
// objSignal.isStale = true;
|
|
1198
|
+
// }
|
|
1199
|
+
return true;
|
|
1200
|
+
}
|
|
1201
|
+
case 'derived':
|
|
1202
|
+
{
|
|
1203
|
+
throw new Error(`Cannot set ${String(prop)} on ${identifier.type} because it is derived`);
|
|
1204
|
+
}
|
|
1205
|
+
case 'belongsTo':
|
|
1206
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
1207
|
+
if (!test) {
|
|
1208
|
+
throw new Error(`Can only use belongsTo fields when the resource is in legacy mode`);
|
|
1209
|
+
}
|
|
1210
|
+
})(Mode[Legacy]) : {};
|
|
1211
|
+
schema._kind('@legacy', 'belongsTo').set(store, receiver, identifier, field, value);
|
|
1212
|
+
return true;
|
|
1213
|
+
case 'hasMany':
|
|
1214
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
1215
|
+
if (!test) {
|
|
1216
|
+
throw new Error(`Can only use hasMany fields when the resource is in legacy mode`);
|
|
1217
|
+
}
|
|
1218
|
+
})(Mode[Legacy]) : {};
|
|
1219
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
1220
|
+
if (!test) {
|
|
1221
|
+
throw new Error(`You must pass an array of records to set a hasMany relationship`);
|
|
1222
|
+
}
|
|
1223
|
+
})(Array.isArray(value)) : {};
|
|
1224
|
+
schema._kind('@legacy', 'hasMany').set(store, receiver, identifier, field, value);
|
|
1225
|
+
return true;
|
|
1226
|
+
default:
|
|
1227
|
+
throw new Error(`Unknown field kind ${field.kind}`);
|
|
1228
|
+
}
|
|
1229
|
+
}
|
|
1230
|
+
});
|
|
1231
|
+
|
|
1232
|
+
// what signal do we need for embedded record?
|
|
1233
|
+
this.___notifications = store.notifications.subscribe(identifier, (_, type, key) => {
|
|
1234
|
+
switch (type) {
|
|
1235
|
+
case 'identity':
|
|
1236
|
+
{
|
|
1237
|
+
if (isEmbedded || !identityField) return; // base paths never apply to embedded records
|
|
1238
|
+
|
|
1239
|
+
if (identityField.name && identityField.kind === '@id') {
|
|
1240
|
+
const signal = signals.get('@identity');
|
|
1241
|
+
if (signal) {
|
|
1242
|
+
notifyInternalSignal(signal);
|
|
1243
|
+
}
|
|
1244
|
+
}
|
|
1245
|
+
break;
|
|
1246
|
+
}
|
|
1247
|
+
case 'attributes':
|
|
1248
|
+
if (key) {
|
|
1249
|
+
if (Array.isArray(key)) {
|
|
1250
|
+
if (!isEmbedded) return; // deep paths will be handled by embedded records
|
|
1251
|
+
// TODO we should have the notification manager
|
|
1252
|
+
// ensure it is safe for each callback to mutate this array
|
|
1253
|
+
if (isPathMatch(embeddedPath, key)) {
|
|
1254
|
+
// handle the notification
|
|
1255
|
+
// TODO we should likely handle this notification here
|
|
1256
|
+
// also we should add a LOGGING flag
|
|
1257
|
+
// eslint-disable-next-line no-console
|
|
1258
|
+
console.warn(`Notification unhandled for ${key.join(',')} on ${identifier.type}`, self);
|
|
1259
|
+
return;
|
|
1260
|
+
}
|
|
1261
|
+
|
|
1262
|
+
// TODO we should add a LOGGING flag
|
|
1263
|
+
// console.log(`Deep notification skipped for ${key.join('.')} on ${identifier.type}`, self);
|
|
1264
|
+
// deep notify the key path
|
|
1265
|
+
} else {
|
|
1266
|
+
if (isEmbedded) return; // base paths never apply to embedded records
|
|
1267
|
+
|
|
1268
|
+
// TODO determine what LOGGING flag to wrap this in if any
|
|
1269
|
+
// console.log(`Notification for ${key} on ${identifier.type}`, self);
|
|
1270
|
+
const signal = signals.get(key);
|
|
1271
|
+
if (signal) {
|
|
1272
|
+
notifyInternalSignal(signal);
|
|
1273
|
+
}
|
|
1274
|
+
const field = fields.get(key);
|
|
1275
|
+
if (field?.kind === 'array' || field?.kind === 'schema-array') {
|
|
1276
|
+
const peeked = peekManagedArray(self, field);
|
|
1277
|
+
if (peeked) {
|
|
1278
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
1279
|
+
if (!test) {
|
|
1280
|
+
throw new Error(`Expected the peekManagedArray for ${field.kind} to return a ManagedArray`);
|
|
1281
|
+
}
|
|
1282
|
+
})(ARRAY_SIGNAL in peeked) : {};
|
|
1283
|
+
const arrSignal = peeked[ARRAY_SIGNAL];
|
|
1284
|
+
notifyInternalSignal(arrSignal);
|
|
1285
|
+
}
|
|
1286
|
+
}
|
|
1287
|
+
if (field?.kind === 'object') {
|
|
1288
|
+
const peeked = peekManagedObject(self, field);
|
|
1289
|
+
if (peeked) {
|
|
1290
|
+
const objSignal = peeked[OBJECT_SIGNAL];
|
|
1291
|
+
notifyInternalSignal(objSignal);
|
|
1292
|
+
}
|
|
1293
|
+
}
|
|
1294
|
+
}
|
|
1295
|
+
}
|
|
1296
|
+
break;
|
|
1297
|
+
case 'relationships':
|
|
1298
|
+
if (key) {
|
|
1299
|
+
if (Array.isArray(key)) ;else {
|
|
1300
|
+
if (isEmbedded) return; // base paths never apply to embedded records
|
|
1301
|
+
|
|
1302
|
+
const field = fields.get(key);
|
|
1303
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
1304
|
+
if (!test) {
|
|
1305
|
+
throw new Error(`Expected relationshp ${key} to be the name of a field`);
|
|
1306
|
+
}
|
|
1307
|
+
})(field) : {};
|
|
1308
|
+
if (field.kind === 'belongsTo') {
|
|
1309
|
+
// TODO determine what LOGGING flag to wrap this in if any
|
|
1310
|
+
// console.log(`Notification for ${key} on ${identifier.type}`, self);
|
|
1311
|
+
const signal = signals.get(key);
|
|
1312
|
+
if (signal) {
|
|
1313
|
+
notifyInternalSignal(signal);
|
|
1314
|
+
}
|
|
1315
|
+
// FIXME
|
|
1316
|
+
} else if (field.kind === 'resource') ;else if (field.kind === 'hasMany') {
|
|
1317
|
+
if (field.options.linksMode) {
|
|
1318
|
+
const peeked = peekManagedArray(self, field);
|
|
1319
|
+
if (peeked) {
|
|
1320
|
+
notifyInternalSignal(peeked[ARRAY_SIGNAL]);
|
|
1321
|
+
}
|
|
1322
|
+
return;
|
|
1323
|
+
}
|
|
1324
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
1325
|
+
if (!test) {
|
|
1326
|
+
throw new Error(`Can only use hasMany fields when the resource is in legacy mode`);
|
|
1327
|
+
}
|
|
1328
|
+
})(Mode[Legacy]) : {};
|
|
1329
|
+
if (schema._kind('@legacy', 'hasMany').notify(store, proxy, identifier, field)) {
|
|
1330
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
1331
|
+
if (!test) {
|
|
1332
|
+
throw new Error(`Expected options to exist on relationship meta`);
|
|
1333
|
+
}
|
|
1334
|
+
})(field.options) : {};
|
|
1335
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
1336
|
+
if (!test) {
|
|
1337
|
+
throw new Error(`Expected async to exist on relationship meta options`);
|
|
1338
|
+
}
|
|
1339
|
+
})('async' in field.options) : {};
|
|
1340
|
+
if (field.options.async) {
|
|
1341
|
+
const signal = signals.get(key);
|
|
1342
|
+
if (signal) {
|
|
1343
|
+
notifyInternalSignal(signal);
|
|
1344
|
+
}
|
|
1345
|
+
}
|
|
1346
|
+
}
|
|
1347
|
+
} else if (field.kind === 'collection') ;
|
|
1348
|
+
}
|
|
1349
|
+
}
|
|
1350
|
+
break;
|
|
1351
|
+
}
|
|
1352
|
+
});
|
|
1353
|
+
if (macroCondition(getGlobalConfig().WarpDrive.env.DEBUG)) {
|
|
1354
|
+
Object.defineProperty(this, '__SHOW_ME_THE_DATA_(debug mode only)__', {
|
|
1355
|
+
enumerable: false,
|
|
1356
|
+
configurable: true,
|
|
1357
|
+
get() {
|
|
1358
|
+
const data = {};
|
|
1359
|
+
for (const key of fields.keys()) {
|
|
1360
|
+
data[key] = proxy[key];
|
|
1361
|
+
}
|
|
1362
|
+
return data;
|
|
1363
|
+
}
|
|
1364
|
+
});
|
|
1365
|
+
}
|
|
1366
|
+
return proxy;
|
|
1367
|
+
}
|
|
1368
|
+
}
|
|
1369
|
+
function _CHECKOUT(record) {
|
|
1370
|
+
// IF we are already the editable record, throw an error
|
|
1371
|
+
if (record[Editable]) {
|
|
1372
|
+
throw new Error(`Cannot checkout an already editable record`);
|
|
1373
|
+
}
|
|
1374
|
+
const editable = Editables.get(record);
|
|
1375
|
+
if (editable) {
|
|
1376
|
+
return Promise.resolve(editable);
|
|
1377
|
+
}
|
|
1378
|
+
const embeddedType = record[EmbeddedField];
|
|
1379
|
+
const embeddedPath = record[EmbeddedPath];
|
|
1380
|
+
const isEmbedded = embeddedType !== null && embeddedPath !== null;
|
|
1381
|
+
if (isEmbedded) {
|
|
1382
|
+
throw new Error(`Cannot checkout an embedded record (yet)`);
|
|
1383
|
+
}
|
|
1384
|
+
const editableRecord = new ReactiveResource(record[RecordStore], record[Identifier], {
|
|
1385
|
+
[Editable]: true,
|
|
1386
|
+
[Legacy]: record[Legacy]
|
|
1387
|
+
}, isEmbedded, embeddedType, embeddedPath);
|
|
1388
|
+
setRecordIdentifier(editableRecord, recordIdentifierFor(record));
|
|
1389
|
+
return Promise.resolve(editableRecord);
|
|
1390
|
+
}
|
|
1391
|
+
function _DESTROY(record) {
|
|
1392
|
+
if (record[Legacy]) {
|
|
1393
|
+
// @ts-expect-error
|
|
1394
|
+
record.isDestroying = true;
|
|
1395
|
+
// @ts-expect-error
|
|
1396
|
+
record.isDestroyed = true;
|
|
1397
|
+
}
|
|
1398
|
+
record[RecordStore].notifications.unsubscribe(record.___notifications);
|
|
1399
|
+
}
|
|
1400
|
+
function instantiateRecord(store, identifier, createArgs) {
|
|
1401
|
+
const schema = store.schema;
|
|
1402
|
+
const resourceSchema = schema.resource(identifier);
|
|
1403
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
1404
|
+
if (!test) {
|
|
1405
|
+
throw new Error(`Expected a resource schema`);
|
|
1406
|
+
}
|
|
1407
|
+
})(isResourceSchema(resourceSchema)) : {};
|
|
1408
|
+
const isLegacy = resourceSchema?.legacy ?? false;
|
|
1409
|
+
const isEditable = isLegacy || store.cache.isNew(identifier);
|
|
1410
|
+
const record = new ReactiveResource(store, identifier, {
|
|
1411
|
+
[Editable]: isEditable,
|
|
1412
|
+
[Legacy]: isLegacy
|
|
1413
|
+
});
|
|
1414
|
+
if (createArgs) {
|
|
1415
|
+
Object.assign(record, createArgs);
|
|
1416
|
+
}
|
|
1417
|
+
return record;
|
|
1418
|
+
}
|
|
1419
|
+
function assertReactiveResource(record) {
|
|
1420
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
1421
|
+
if (!test) {
|
|
1422
|
+
throw new Error('Expected a ReactiveResource');
|
|
1423
|
+
}
|
|
1424
|
+
})(record && typeof record === 'object' && Destroy in record) : {};
|
|
1425
|
+
}
|
|
1426
|
+
function teardownRecord(record) {
|
|
1427
|
+
assertReactiveResource(record);
|
|
1428
|
+
record[Destroy]();
|
|
1429
|
+
}
|
|
1430
|
+
const Support = getOrSetGlobal('Support', new WeakMap());
|
|
1431
|
+
const ConstructorField = {
|
|
1432
|
+
type: '@constructor',
|
|
1433
|
+
name: 'constructor',
|
|
1434
|
+
kind: 'derived'
|
|
1435
|
+
};
|
|
1436
|
+
const TypeField = {
|
|
1437
|
+
type: '@identity',
|
|
1438
|
+
name: '$type',
|
|
1439
|
+
kind: 'derived',
|
|
1440
|
+
options: {
|
|
1441
|
+
key: 'type'
|
|
1442
|
+
}
|
|
1443
|
+
};
|
|
1444
|
+
const DefaultIdentityField = {
|
|
1445
|
+
name: 'id',
|
|
1446
|
+
kind: '@id'
|
|
1447
|
+
};
|
|
1448
|
+
function _constructor(record) {
|
|
1449
|
+
let state = Support.get(record);
|
|
1450
|
+
if (!state) {
|
|
1451
|
+
state = {};
|
|
1452
|
+
Support.set(record, state);
|
|
1453
|
+
}
|
|
1454
|
+
return state._constructor = state._constructor || {
|
|
1455
|
+
name: `ReactiveResource<${recordIdentifierFor(record).type}>`,
|
|
1456
|
+
get modelName() {
|
|
1457
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
1458
|
+
{
|
|
1459
|
+
throw new Error(`record.constructor.modelName is not available outside of legacy mode`);
|
|
1460
|
+
}
|
|
1461
|
+
})() : {};
|
|
1462
|
+
return undefined;
|
|
1463
|
+
}
|
|
1464
|
+
};
|
|
1465
|
+
}
|
|
1466
|
+
_constructor[Type] = '@constructor';
|
|
1467
|
+
|
|
1468
|
+
/**
|
|
1469
|
+
* Extensions allow providing non-schema driven behaviors to
|
|
1470
|
+
* reactive resources and arrays.
|
|
1471
|
+
*/
|
|
1472
|
+
|
|
1473
|
+
const BannedKeys = ['constructor', '__proto__'];
|
|
1474
|
+
function processExtension(extension) {
|
|
1475
|
+
const {
|
|
1476
|
+
kind,
|
|
1477
|
+
name
|
|
1478
|
+
} = extension;
|
|
1479
|
+
const features = new Map();
|
|
1480
|
+
const baseFeatures = typeof extension.features === 'function' ? extension.features.prototype : extension.features;
|
|
1481
|
+
for (const key of Object.getOwnPropertyNames(baseFeatures)) {
|
|
1482
|
+
if (BannedKeys.includes(key)) continue;
|
|
1483
|
+
const decl = Object.getOwnPropertyDescriptor(baseFeatures, key);
|
|
1484
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
1485
|
+
if (!test) {
|
|
1486
|
+
throw new Error(`Expected to find a declaration for ${key} on extension ${name}`);
|
|
1487
|
+
}
|
|
1488
|
+
})(decl) : {};
|
|
1489
|
+
if (decl.value) {
|
|
1490
|
+
const {
|
|
1491
|
+
value
|
|
1492
|
+
} = decl;
|
|
1493
|
+
features.set(key, typeof value === 'function' ? {
|
|
1494
|
+
kind: 'method',
|
|
1495
|
+
fn: value
|
|
1496
|
+
} : decl.writable ? {
|
|
1497
|
+
kind: 'mutable-value',
|
|
1498
|
+
value
|
|
1499
|
+
} : {
|
|
1500
|
+
kind: 'readonly-value',
|
|
1501
|
+
value
|
|
1502
|
+
});
|
|
1503
|
+
continue;
|
|
1504
|
+
}
|
|
1505
|
+
if (decl.get || decl.set) {
|
|
1506
|
+
const {
|
|
1507
|
+
get,
|
|
1508
|
+
set
|
|
1509
|
+
} = decl;
|
|
1510
|
+
features.set(key,
|
|
1511
|
+
// prettier-ignore
|
|
1512
|
+
get && set ? {
|
|
1513
|
+
kind: 'mutable-field',
|
|
1514
|
+
get,
|
|
1515
|
+
set
|
|
1516
|
+
} : get ? {
|
|
1517
|
+
kind: 'readonly-field',
|
|
1518
|
+
get
|
|
1519
|
+
} : {
|
|
1520
|
+
kind: 'writeonly-field',
|
|
1521
|
+
set: set
|
|
1522
|
+
});
|
|
1523
|
+
continue;
|
|
1524
|
+
}
|
|
1525
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
1526
|
+
{
|
|
1527
|
+
throw new Error(`The feature ${key} on extension ${name} is of an unknown variety.`);
|
|
1528
|
+
}
|
|
1529
|
+
})() : {};
|
|
1530
|
+
}
|
|
1531
|
+
return {
|
|
1532
|
+
kind,
|
|
1533
|
+
name,
|
|
1534
|
+
features
|
|
1535
|
+
};
|
|
1536
|
+
}
|
|
1537
|
+
function getExt(extCache, type, extName) {
|
|
1538
|
+
const ext = extCache[type].get(extName);
|
|
1539
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
1540
|
+
if (!test) {
|
|
1541
|
+
throw new Error(`expected to have an extension named ${String(extName)} available for ${type}s`);
|
|
1542
|
+
}
|
|
1543
|
+
})(ext) : {};
|
|
1544
|
+
return ext?.features ?? null;
|
|
1545
|
+
}
|
|
1546
|
+
function hasObjectSchema(field) {
|
|
1547
|
+
return 'kind' in field && (field.kind === 'schema-array' || field.kind === 'schema-object');
|
|
1548
|
+
}
|
|
1549
|
+
function processExtensions(schema, field, scenario) {
|
|
1550
|
+
// if we're looking up extensions for a resource, there is no
|
|
1551
|
+
// merging required so if we have no objectExtensions
|
|
1552
|
+
// we are done.
|
|
1553
|
+
if (scenario === 'resource') {
|
|
1554
|
+
if (!('objectExtensions' in field || !field.objectExtensions?.length)) {
|
|
1555
|
+
return null;
|
|
1556
|
+
}
|
|
1557
|
+
}
|
|
1558
|
+
const type = scenario === 'resource' ? 'object' : scenario;
|
|
1559
|
+
const extCache = schema._extensions;
|
|
1560
|
+
const fieldCache = schema._cachedFieldExtensionsByField;
|
|
1561
|
+
if (fieldCache[type].has(field)) {
|
|
1562
|
+
return fieldCache[type].get(field);
|
|
1563
|
+
}
|
|
1564
|
+
|
|
1565
|
+
// prettier-ignore
|
|
1566
|
+
const extensions = (scenario === 'resource' ? field.objectExtensions : scenario === 'object' ? field.options?.objectExtensions : field.options?.arrayExtensions) || null;
|
|
1567
|
+
|
|
1568
|
+
// if we are a resource scenario, we know from the first check we do have extensions
|
|
1569
|
+
// if we are an object scenario, we can now return the resource scenario.
|
|
1570
|
+
// if we are an array scenario, there is nothing more to process.
|
|
1571
|
+
if (!extensions) {
|
|
1572
|
+
if (scenario === 'array') return null;
|
|
1573
|
+
if (!hasObjectSchema(field)) {
|
|
1574
|
+
return null;
|
|
1575
|
+
}
|
|
1576
|
+
return schema.CAUTION_MEGA_DANGER_ZONE_resourceExtensions(field);
|
|
1577
|
+
}
|
|
1578
|
+
|
|
1579
|
+
// if we have made it here, we have extensions, lets check if there's
|
|
1580
|
+
// a cached version we can use
|
|
1581
|
+
const baseExtensions = scenario === 'resource' && hasObjectSchema(field) ? schema.CAUTION_MEGA_DANGER_ZONE_resourceExtensions(field) : scenario === 'object' && hasObjectSchema(field) ? schema.CAUTION_MEGA_DANGER_ZONE_resourceExtensions(field) : null;
|
|
1582
|
+
if (!baseExtensions && extensions.length === 1) {
|
|
1583
|
+
const value = getExt(extCache, type, extensions[0]);
|
|
1584
|
+
fieldCache[type].set(field, value);
|
|
1585
|
+
return value;
|
|
1586
|
+
}
|
|
1587
|
+
const features = new Map(baseExtensions);
|
|
1588
|
+
for (const extName of extensions) {
|
|
1589
|
+
const value = getExt(extCache, type, extName);
|
|
1590
|
+
if (value) {
|
|
1591
|
+
for (const [feature, desc] of value) {
|
|
1592
|
+
features.set(feature, desc);
|
|
1593
|
+
}
|
|
1594
|
+
}
|
|
1595
|
+
}
|
|
1596
|
+
const value = features.size ? features : null;
|
|
1597
|
+
fieldCache[type].set(field, value);
|
|
1598
|
+
return value;
|
|
1599
|
+
}
|
|
1600
|
+
|
|
1601
|
+
/**
|
|
1602
|
+
* Utility for constructing a ResourceSchema with the recommended
|
|
1603
|
+
* fields for the PolarisMode experience.
|
|
1604
|
+
*
|
|
1605
|
+
* Using this requires registering the PolarisMode derivations
|
|
1606
|
+
*
|
|
1607
|
+
* ```ts
|
|
1608
|
+
* import { registerDerivations } from '@warp-drive/schema-record';
|
|
1609
|
+
*
|
|
1610
|
+
* registerDerivations(schema);
|
|
1611
|
+
* ```
|
|
1612
|
+
*
|
|
1613
|
+
* @public
|
|
1614
|
+
* @param schema
|
|
1615
|
+
* @return {PolarisResourceSchema}
|
|
1616
|
+
*/
|
|
1617
|
+
function withDefaults(schema) {
|
|
1618
|
+
schema.identity = schema.identity || DefaultIdentityField;
|
|
1619
|
+
|
|
1620
|
+
// because fields gets iterated in definition order,
|
|
1621
|
+
// we add TypeField to the beginning so that it will
|
|
1622
|
+
// appear right next to the identity field
|
|
1623
|
+
schema.fields.unshift(TypeField);
|
|
1624
|
+
schema.fields.push(ConstructorField);
|
|
1625
|
+
return schema;
|
|
1626
|
+
}
|
|
1627
|
+
/**
|
|
1628
|
+
* A derivation that computes its value from the
|
|
1629
|
+
* record's identity.
|
|
1630
|
+
*
|
|
1631
|
+
* It can be used via a derived field definition like:
|
|
1632
|
+
*
|
|
1633
|
+
* ```ts
|
|
1634
|
+
* {
|
|
1635
|
+
* kind: 'derived',
|
|
1636
|
+
* name: 'id',
|
|
1637
|
+
* type: '@identity',
|
|
1638
|
+
* options: { key: 'id' }
|
|
1639
|
+
* }
|
|
1640
|
+
* ```
|
|
1641
|
+
*
|
|
1642
|
+
* Valid keys are `'id'`, `'lid'`, `'type'`, and `'^'`.
|
|
1643
|
+
*
|
|
1644
|
+
* `^` returns the entire identifier object.
|
|
1645
|
+
*
|
|
1646
|
+
* @public
|
|
1647
|
+
*/
|
|
1648
|
+
const fromIdentity = (record, options, key) => {
|
|
1649
|
+
const identifier = record[Identifier];
|
|
1650
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
1651
|
+
if (!test) {
|
|
1652
|
+
throw new Error(`Cannot compute @identity for a record without an identifier`);
|
|
1653
|
+
}
|
|
1654
|
+
})(identifier) : {};
|
|
1655
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
1656
|
+
if (!test) {
|
|
1657
|
+
throw new Error(`Expected to receive a key to compute @identity, but got ${String(options)}`);
|
|
1658
|
+
}
|
|
1659
|
+
})(options?.key && ['lid', 'id', 'type', '^'].includes(options.key)) : {};
|
|
1660
|
+
return options.key === '^' ? identifier : identifier[options.key];
|
|
1661
|
+
};
|
|
1662
|
+
fromIdentity[Type] = '@identity';
|
|
1663
|
+
|
|
1664
|
+
/**
|
|
1665
|
+
* Registers the default derivations for records that want
|
|
1666
|
+
* to use the PolarisMode defaults provided by
|
|
1667
|
+
*
|
|
1668
|
+
* ```ts
|
|
1669
|
+
* import { withDefaults } from '@warp-drive/schema-record';
|
|
1670
|
+
* ```
|
|
1671
|
+
*
|
|
1672
|
+
* @public
|
|
1673
|
+
* @param {SchemaService} schema
|
|
1674
|
+
*/
|
|
1675
|
+
function registerDerivations(schema) {
|
|
1676
|
+
schema.registerDerivation(fromIdentity);
|
|
1677
|
+
schema.registerDerivation(_constructor);
|
|
1678
|
+
}
|
|
1679
|
+
/**
|
|
1680
|
+
* Wraps a derivation in a new function with Derivation signature but that looks
|
|
1681
|
+
* up the value in the cache before recomputing.
|
|
1682
|
+
*
|
|
1683
|
+
* @internal
|
|
1684
|
+
*/
|
|
1685
|
+
function makeCachedDerivation(derivation) {
|
|
1686
|
+
const memoizedDerivation = (record, options, prop) => {
|
|
1687
|
+
const signals = withSignalStore(record);
|
|
1688
|
+
let signal = signals.get(prop);
|
|
1689
|
+
if (!signal) {
|
|
1690
|
+
signal = createMemo(record, prop, () => {
|
|
1691
|
+
return derivation(record, options, prop);
|
|
1692
|
+
}); // a total lie, for convenience of reusing the storage
|
|
1693
|
+
signals.set(prop, signal);
|
|
1694
|
+
}
|
|
1695
|
+
return signal();
|
|
1696
|
+
};
|
|
1697
|
+
memoizedDerivation[Type] = derivation[Type];
|
|
1698
|
+
return memoizedDerivation;
|
|
1699
|
+
}
|
|
1700
|
+
/**
|
|
1701
|
+
* A SchemaService designed to work with dynamically registered schemas.
|
|
1702
|
+
*
|
|
1703
|
+
* @class SchemaService
|
|
1704
|
+
* @public
|
|
1705
|
+
*/
|
|
1706
|
+
class SchemaService {
|
|
1707
|
+
/** @internal */
|
|
1708
|
+
|
|
1709
|
+
/** @internal */
|
|
1710
|
+
|
|
1711
|
+
/** @internal */
|
|
1712
|
+
|
|
1713
|
+
/** @internal */
|
|
1714
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1715
|
+
|
|
1716
|
+
/** @internal */
|
|
1717
|
+
|
|
1718
|
+
/** @internal */
|
|
1719
|
+
|
|
1720
|
+
/** @internal */
|
|
1721
|
+
|
|
1722
|
+
constructor() {
|
|
1723
|
+
this._schemas = new Map();
|
|
1724
|
+
this._transforms = new Map();
|
|
1725
|
+
this._hashFns = new Map();
|
|
1726
|
+
this._derivations = new Map();
|
|
1727
|
+
this._traits = new Set();
|
|
1728
|
+
this._modes = new Map();
|
|
1729
|
+
this._extensions = {
|
|
1730
|
+
object: new Map(),
|
|
1731
|
+
array: new Map()
|
|
1732
|
+
};
|
|
1733
|
+
this._cachedFieldExtensionsByField = {
|
|
1734
|
+
object: new Map(),
|
|
1735
|
+
array: new Map()
|
|
1736
|
+
};
|
|
1737
|
+
}
|
|
1738
|
+
resourceTypes() {
|
|
1739
|
+
return Array.from(this._schemas.keys());
|
|
1740
|
+
}
|
|
1741
|
+
hasTrait(type) {
|
|
1742
|
+
return this._traits.has(type);
|
|
1743
|
+
}
|
|
1744
|
+
resourceHasTrait(resource, trait) {
|
|
1745
|
+
return this._schemas.get(resource.type).traits.has(trait);
|
|
1746
|
+
}
|
|
1747
|
+
transformation(field) {
|
|
1748
|
+
const kind = 'kind' in field ? field.kind : '<unknown kind>';
|
|
1749
|
+
const name = 'name' in field ? field.name : '<unknown name>';
|
|
1750
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
1751
|
+
if (!test) {
|
|
1752
|
+
throw new Error(`'${kind}' fields cannot be transformed. Only fields of kind 'field' 'object' or 'array' can specify a transformation. Attempted to find '${field.type ?? '<unknown type>'}' on field '${name}'.`);
|
|
1753
|
+
}
|
|
1754
|
+
})(!('kind' in field) || ['field', 'object', 'array'].includes(kind)) : {};
|
|
1755
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
1756
|
+
if (!test) {
|
|
1757
|
+
throw new Error(`Expected the '${kind}' field '${name}' to specify a transformation via 'field.type', but none was present`);
|
|
1758
|
+
}
|
|
1759
|
+
})(field.type) : {};
|
|
1760
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
1761
|
+
if (!test) {
|
|
1762
|
+
throw new Error(`No transformation registered with name '${field.type}' for '${kind}' field '${name}'`);
|
|
1763
|
+
}
|
|
1764
|
+
})(this._transforms.has(field.type)) : {};
|
|
1765
|
+
return this._transforms.get(field.type);
|
|
1766
|
+
}
|
|
1767
|
+
derivation(field) {
|
|
1768
|
+
const kind = 'kind' in field ? field.kind : '<unknown kind>';
|
|
1769
|
+
const name = 'name' in field ? field.name : '<unknown name>';
|
|
1770
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
1771
|
+
if (!test) {
|
|
1772
|
+
throw new Error(`The '${kind}' field '${name}' is not derived and so cannot be used to lookup a derivation`);
|
|
1773
|
+
}
|
|
1774
|
+
})(!('kind' in field) || kind === 'derived') : {};
|
|
1775
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
1776
|
+
if (!test) {
|
|
1777
|
+
throw new Error(`Expected the '${kind}' field '${name}' to specify a derivation via 'field.type', but no value was present`);
|
|
1778
|
+
}
|
|
1779
|
+
})(field.type) : {};
|
|
1780
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
1781
|
+
if (!test) {
|
|
1782
|
+
throw new Error(`No '${field.type}' derivation registered for use by the '${kind}' field '${name}'`);
|
|
1783
|
+
}
|
|
1784
|
+
})(this._derivations.has(field.type)) : {};
|
|
1785
|
+
return this._derivations.get(field.type);
|
|
1786
|
+
}
|
|
1787
|
+
hashFn(field) {
|
|
1788
|
+
const kind = 'kind' in field ? field.kind : '<unknown kind>';
|
|
1789
|
+
const name = 'name' in field ? field.name : '<unknown name>';
|
|
1790
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
1791
|
+
if (!test) {
|
|
1792
|
+
throw new Error(`The '${kind}' field '${name}' is not a HashField and so cannot be used to lookup a hash function`);
|
|
1793
|
+
}
|
|
1794
|
+
})(!('kind' in field) || kind === '@hash') : {};
|
|
1795
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
1796
|
+
if (!test) {
|
|
1797
|
+
throw new Error(`Expected the '${kind}' field '${name}' to specify a hash function via 'field.type', but no value was present`);
|
|
1798
|
+
}
|
|
1799
|
+
})(field.type) : {};
|
|
1800
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
1801
|
+
if (!test) {
|
|
1802
|
+
throw new Error(`No '${field.type}' hash function is registered for use by the '${kind}' field '${name}'`);
|
|
1803
|
+
}
|
|
1804
|
+
})(this._hashFns.has(field.type)) : {};
|
|
1805
|
+
return this._hashFns.get(field.type);
|
|
1806
|
+
}
|
|
1807
|
+
resource(resource) {
|
|
1808
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
1809
|
+
if (!test) {
|
|
1810
|
+
throw new Error(`No resource registered with name '${resource.type}'`);
|
|
1811
|
+
}
|
|
1812
|
+
})(this._schemas.has(resource.type)) : {};
|
|
1813
|
+
return this._schemas.get(resource.type).original;
|
|
1814
|
+
}
|
|
1815
|
+
registerResources(schemas) {
|
|
1816
|
+
schemas.forEach(schema => {
|
|
1817
|
+
this.registerResource(schema);
|
|
1818
|
+
});
|
|
1819
|
+
}
|
|
1820
|
+
registerResource(schema) {
|
|
1821
|
+
const fields = new Map();
|
|
1822
|
+
const relationships = {};
|
|
1823
|
+
const attributes = {};
|
|
1824
|
+
schema.fields.forEach(field => {
|
|
1825
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
1826
|
+
if (!test) {
|
|
1827
|
+
throw new Error(`${field.kind} is not valid inside a ResourceSchema's fields.`);
|
|
1828
|
+
}
|
|
1829
|
+
})(
|
|
1830
|
+
// @ts-expect-error we are checking for mistakes at runtime
|
|
1831
|
+
field.kind !== '@id' && field.kind !== '@hash') : {};
|
|
1832
|
+
fields.set(field.name, field);
|
|
1833
|
+
if (field.kind === 'attribute') {
|
|
1834
|
+
attributes[field.name] = field;
|
|
1835
|
+
} else if (field.kind === 'belongsTo' || field.kind === 'hasMany') {
|
|
1836
|
+
relationships[field.name] = field;
|
|
1837
|
+
}
|
|
1838
|
+
});
|
|
1839
|
+
const traits = new Set(isResourceSchema(schema) ? schema.traits : []);
|
|
1840
|
+
traits.forEach(trait => {
|
|
1841
|
+
this._traits.add(trait);
|
|
1842
|
+
});
|
|
1843
|
+
const internalSchema = {
|
|
1844
|
+
original: schema,
|
|
1845
|
+
fields,
|
|
1846
|
+
relationships,
|
|
1847
|
+
attributes,
|
|
1848
|
+
traits
|
|
1849
|
+
};
|
|
1850
|
+
this._schemas.set(schema.type, internalSchema);
|
|
1851
|
+
}
|
|
1852
|
+
registerTransformation(transformation) {
|
|
1853
|
+
this._transforms.set(transformation[Type], transformation);
|
|
1854
|
+
}
|
|
1855
|
+
registerDerivation(derivation) {
|
|
1856
|
+
this._derivations.set(derivation[Type], makeCachedDerivation(derivation));
|
|
1857
|
+
}
|
|
1858
|
+
CAUTION_MEGA_DANGER_ZONE_registerExtension(extension) {
|
|
1859
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
1860
|
+
if (!test) {
|
|
1861
|
+
throw new Error(`an extension named ${extension.name} for ${extension.kind} already exists!`);
|
|
1862
|
+
}
|
|
1863
|
+
})(!this._extensions[extension.kind].has(extension.name)) : {};
|
|
1864
|
+
this._extensions[extension.kind].set(extension.name, processExtension(extension));
|
|
1865
|
+
}
|
|
1866
|
+
CAUTION_MEGA_DANGER_ZONE_resourceExtensions(resource) {
|
|
1867
|
+
const schema = this.resource(resource);
|
|
1868
|
+
return processExtensions(this, schema, 'resource');
|
|
1869
|
+
}
|
|
1870
|
+
CAUTION_MEGA_DANGER_ZONE_objectExtensions(field) {
|
|
1871
|
+
return processExtensions(this, field, 'object');
|
|
1872
|
+
}
|
|
1873
|
+
CAUTION_MEGA_DANGER_ZONE_arrayExtensions(field) {
|
|
1874
|
+
return processExtensions(this, field, 'array');
|
|
1875
|
+
}
|
|
1876
|
+
|
|
1877
|
+
/**
|
|
1878
|
+
* This is an internal method used to register behaviors for legacy mode.
|
|
1879
|
+
* It is not intended for public use.
|
|
1880
|
+
*
|
|
1881
|
+
* We do think a generalized `kind` registration system would be useful,
|
|
1882
|
+
* but we have not yet designed it.
|
|
1883
|
+
*
|
|
1884
|
+
* See https://github.com/emberjs/data/issues/9534
|
|
1885
|
+
*
|
|
1886
|
+
* @internal
|
|
1887
|
+
*/
|
|
1888
|
+
_registerMode(mode, kinds) {
|
|
1889
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
1890
|
+
if (!test) {
|
|
1891
|
+
throw new Error(`Mode '${mode}' is already registered`);
|
|
1892
|
+
}
|
|
1893
|
+
})(!this._modes.has(mode)) : {};
|
|
1894
|
+
this._modes.set(mode, kinds);
|
|
1895
|
+
}
|
|
1896
|
+
|
|
1897
|
+
/**
|
|
1898
|
+
* This is an internal method used to enable legacy behaviors for legacy mode.
|
|
1899
|
+
* It is not intended for public use.
|
|
1900
|
+
*
|
|
1901
|
+
* We do think a generalized `kind` registration system would be useful,
|
|
1902
|
+
* but we have not yet designed it.
|
|
1903
|
+
*
|
|
1904
|
+
* See https://github.com/emberjs/data/issues/9534
|
|
1905
|
+
*
|
|
1906
|
+
* @internal
|
|
1907
|
+
*/
|
|
1908
|
+
_kind(mode, kind) {
|
|
1909
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
1910
|
+
if (!test) {
|
|
1911
|
+
throw new Error(`Mode '${mode}' is not registered`);
|
|
1912
|
+
}
|
|
1913
|
+
})(this._modes.has(mode)) : {};
|
|
1914
|
+
const kinds = this._modes.get(mode);
|
|
1915
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
1916
|
+
if (!test) {
|
|
1917
|
+
throw new Error(`Kind '${kind}' is not registered for mode '${mode}'`);
|
|
1918
|
+
}
|
|
1919
|
+
})(kinds[kind]) : {};
|
|
1920
|
+
return kinds[kind];
|
|
1921
|
+
}
|
|
1922
|
+
registerHashFn(hashFn) {
|
|
1923
|
+
this._hashFns.set(hashFn[Type], hashFn);
|
|
1924
|
+
}
|
|
1925
|
+
fields({
|
|
1926
|
+
type
|
|
1927
|
+
}) {
|
|
1928
|
+
const schema = this._schemas.get(type);
|
|
1929
|
+
if (!schema) {
|
|
1930
|
+
throw new Error(`No schema defined for ${type}`);
|
|
1931
|
+
}
|
|
1932
|
+
return schema.fields;
|
|
1933
|
+
}
|
|
1934
|
+
hasResource(resource) {
|
|
1935
|
+
return this._schemas.has(resource.type);
|
|
1936
|
+
}
|
|
1937
|
+
}
|
|
1938
|
+
if (macroCondition(getGlobalConfig().WarpDrive.deprecations.ENABLE_LEGACY_SCHEMA_SERVICE)) {
|
|
1939
|
+
SchemaService.prototype.attributesDefinitionFor = function ({
|
|
1940
|
+
type
|
|
1941
|
+
}) {
|
|
1942
|
+
deprecate(`Use \`schema.fields({ type })\` instead of \`schema.attributesDefinitionFor({ type })\``, false, {
|
|
1943
|
+
id: 'ember-data:schema-service-updates',
|
|
1944
|
+
until: '6.0',
|
|
1945
|
+
for: 'ember-data',
|
|
1946
|
+
since: {
|
|
1947
|
+
available: '4.13',
|
|
1948
|
+
enabled: '5.4'
|
|
1949
|
+
}
|
|
1950
|
+
});
|
|
1951
|
+
const schema = this._schemas.get(type);
|
|
1952
|
+
if (!schema) {
|
|
1953
|
+
throw new Error(`No schema defined for ${type}`);
|
|
1954
|
+
}
|
|
1955
|
+
return schema.attributes;
|
|
1956
|
+
};
|
|
1957
|
+
SchemaService.prototype.relationshipsDefinitionFor = function ({
|
|
1958
|
+
type
|
|
1959
|
+
}) {
|
|
1960
|
+
deprecate(`Use \`schema.fields({ type })\` instead of \`schema.relationshipsDefinitionFor({ type })\``, false, {
|
|
1961
|
+
id: 'ember-data:schema-service-updates',
|
|
1962
|
+
until: '6.0',
|
|
1963
|
+
for: 'ember-data',
|
|
1964
|
+
since: {
|
|
1965
|
+
available: '4.13',
|
|
1966
|
+
enabled: '5.4'
|
|
1967
|
+
}
|
|
1968
|
+
});
|
|
1969
|
+
const schema = this._schemas.get(type);
|
|
1970
|
+
if (!schema) {
|
|
1971
|
+
throw new Error(`No schema defined for ${type}`);
|
|
1972
|
+
}
|
|
1973
|
+
return schema.relationships;
|
|
1974
|
+
};
|
|
1975
|
+
SchemaService.prototype.doesTypeExist = function (type) {
|
|
1976
|
+
deprecate(`Use \`schema.hasResource({ type })\` instead of \`schema.doesTypeExist(type)\``, false, {
|
|
1977
|
+
id: 'ember-data:schema-service-updates',
|
|
1978
|
+
until: '6.0',
|
|
1979
|
+
for: 'ember-data',
|
|
1980
|
+
since: {
|
|
1981
|
+
available: '4.13',
|
|
1982
|
+
enabled: '5.4'
|
|
1983
|
+
}
|
|
1984
|
+
});
|
|
1985
|
+
return this._schemas.has(type);
|
|
1986
|
+
};
|
|
1987
|
+
}
|
|
1988
|
+
export { Checkout, SchemaService, fromIdentity, instantiateRecord, registerDerivations, teardownRecord, withDefaults };
|