Skip to content

feat: universal reactivity hooks #9965

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 27 commits into from
Apr 27, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
most the impl
  • Loading branch information
runspired committed Apr 26, 2025
commit 428d952f5d88d269c34cb7bd9bbeec2f5d0a789c
7 changes: 3 additions & 4 deletions packages/core-types/src/-private.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,15 @@ type GlobalKey =
| 'NotFoundError'
| 'ConflictError'
| 'ServerError'
// @ember-data/tracking
// @ember-data/store/reactivity
| '#{}'
| '#[]'
| 'Signals'
// @ember-data/store LegacySupport
| 'AvailableShims'
// @ember-data/store RecordArrayManager
| 'FAKE_ARR'
// @ember-data/store IdentifierArray
| '#signal'
| '#source'
| '#update'
| '#notify'
Expand Down Expand Up @@ -93,8 +94,6 @@ type GlobalKey =
| 'Support'
| 'SOURCE'
| 'MUTATE'
| 'ARRAY_SIGNAL'
| 'OBJECT_SIGNAL'
| 'Destroy'
| 'Identifier'
| 'Editable'
Expand Down
11 changes: 5 additions & 6 deletions packages/model/src/-private/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ import type { Snapshot } from '@ember-data/legacy-compat/-private';
import type Store from '@ember-data/store';
import type { NotificationType } from '@ember-data/store';
import { recordIdentifierFor, storeFor } from '@ember-data/store';
import { coerceId } from '@ember-data/store/-private';
import { compat, notifySignal } from '@ember-data/tracking';
import { defineSignal, subscribed as tagged } from '@ember-data/tracking/-private';
import { coerceId, compat, defineSignal, entangleSignal, gate, withSignalStore } from '@ember-data/store/-private';
import { DEBUG } from '@warp-drive/build-config/env';
import { assert } from '@warp-drive/build-config/macros';
import type { StableRecordIdentifier } from '@warp-drive/core-types';
Expand Down Expand Up @@ -498,7 +496,7 @@ class Model extends EmberObject implements MinimalLegacyRecord {
@public
@type {String}
*/
@tagged
@gate
get id(): string | null {
// this guard exists, because some dev-only deprecation code
// (addListener via validatePropertyInjections) invokes toString before the
Expand Down Expand Up @@ -538,7 +536,7 @@ class Model extends EmberObject implements MinimalLegacyRecord {
*/
// TODO we can probably make this a computeOnce
// we likely do not need to notify the currentState root anymore
@tagged
@gate
get currentState() {
// descriptors are called with the wrong `this` context during mergeMixins
// when using legacy/classic ember classes. Basically: lazy in prod and eager in dev.
Expand Down Expand Up @@ -662,7 +660,8 @@ class Model extends EmberObject implements MinimalLegacyRecord {
*/
// @ts-expect-error no return is necessary, but Ember's types are forcing it
notifyPropertyChange(prop: string): this {
notifySignal(this, prop as keyof this & string);
const signals = withSignalStore(this);
entangleSignal(signals, this, prop, undefined);
super.notifyPropertyChange(prop);
}

Expand Down
4 changes: 2 additions & 2 deletions packages/model/src/-private/promise-belongs-to.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type PromiseProxyMixin from '@ember/object/promise-proxy-mixin';
import type ObjectProxy from '@ember/object/proxy';

import type Store from '@ember-data/store';
import { cached } from '@ember-data/tracking';
import { memoized } from '@ember-data/store/-private';
import { assert } from '@warp-drive/build-config/macros';
import type { OpaqueRecordInstance, TypeFromInstanceOrString } from '@warp-drive/core-types/record';

Expand Down Expand Up @@ -50,7 +50,7 @@ const Extended: PromiseObjectType<OpaqueRecordInstance> =
class PromiseBelongsTo<T = unknown> extends Extended<T> {
declare _belongsToState: BelongsToProxyMeta<T>;

@cached
@memoized
get id(): string | null {
const { key, legacySupport } = this._belongsToState;
const ref = legacySupport.referenceFor('belongsTo', key);
Expand Down
3 changes: 1 addition & 2 deletions packages/model/src/-private/promise-many-array.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { RelatedCollection as ManyArray } from '@ember-data/store/-private';
import { compat, defineSignal } from '@ember-data/store/-private';
import type { BaseFinderOptions } from '@ember-data/store/types';
import { compat } from '@ember-data/tracking';
import { defineSignal } from '@ember-data/tracking/-private';
import { DEPRECATE_COMPUTED_CHAINS } from '@warp-drive/build-config/deprecations';
import { assert } from '@warp-drive/build-config/macros';

Expand Down
31 changes: 15 additions & 16 deletions packages/model/src/-private/record-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import type Store from '@ember-data/store';
import type { NotificationType } from '@ember-data/store';
import { storeFor } from '@ember-data/store';
import type { RequestCacheRequestState, RequestStateService } from '@ember-data/store/-private';
import { recordIdentifierFor } from '@ember-data/store/-private';
import { cached, notifySignal } from '@ember-data/tracking';
import { defineSignal, subscribed as tagged } from '@ember-data/tracking/-private';
import { defineSignal, gate, memoized, recordIdentifierFor } from '@ember-data/store/-private';
import { notifySignal } from '@ember-data/tracking';
import { assert } from '@warp-drive/build-config/macros';
import type { StableRecordIdentifier } from '@warp-drive/core-types';
import type { Cache } from '@warp-drive/core-types/cache';
Expand Down Expand Up @@ -228,20 +227,20 @@ export default class RecordState {

declare isSaving: boolean;

@tagged
@gate
get isLoading() {
return !this.isLoaded && this.pendingCount > 0 && this.fulfilledCount === 0;
}

@tagged
@gate
get isLoaded() {
if (this.isNew) {
return true;
}
return this.fulfilledCount > 0 || !this.isEmpty;
}

@tagged
@gate
get isSaved() {
const rd = this.cache;
if (this.isDeleted) {
Expand All @@ -254,7 +253,7 @@ export default class RecordState {
return true;
}

@tagged
@gate
get isEmpty() {
const rd = this.cache;
// TODO this is not actually an RFC'd concept. Determine the
Expand All @@ -263,26 +262,26 @@ export default class RecordState {
return !this.isNew && rd.isEmpty(this.identifier);
}

@tagged
@gate
get isNew() {
const rd = this.cache;
assert(`Expected Cache to implement isNew()`, typeof rd.isNew === 'function');
return rd.isNew(this.identifier);
}

@tagged
@gate
get isDeleted() {
const rd = this.cache;
assert(`Expected Cache to implement isDeleted()`, typeof rd.isDeleted === 'function');
return rd.isDeleted(this.identifier);
}

@tagged
@gate
get isValid() {
return this.record.errors.length === 0;
}

@tagged
@gate
get isDirty() {
const rd = this.cache;
if (this.isEmpty || rd.isDeletionCommitted(this.identifier) || (this.isDeleted && this.isNew)) {
Expand All @@ -291,7 +290,7 @@ export default class RecordState {
return this.isDeleted || this.isNew || rd.hasChangedAttrs(this.identifier);
}

@tagged
@gate
get isError() {
const errorReq = this._errorRequests[this._errorRequests.length - 1];
if (!errorReq) {
Expand All @@ -301,7 +300,7 @@ export default class RecordState {
}
}

@tagged
@gate
get adapterError() {
const request = this._lastError;
if (!request) {
Expand All @@ -310,12 +309,12 @@ export default class RecordState {
return request.state === 'rejected' && request.response!.data;
}

@cached
@memoized
get isPreloaded() {
return !this.isEmpty && this.isLoading;
}

@cached
@memoized
get stateName() {
// we might be empty while loading so check this first
if (this.isLoading) {
Expand Down Expand Up @@ -361,7 +360,7 @@ export default class RecordState {
}
}

@cached
@memoized
get dirtyType() {
// we might be empty while loading so check this first
if (this.isLoading || this.isEmpty) {
Expand Down
8 changes: 3 additions & 5 deletions packages/model/src/-private/references/belongs-to.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import type { Graph, ResourceEdge } from '@ember-data/graph/-private';
import type Store from '@ember-data/store';
import type { NotificationType } from '@ember-data/store';
import { cached, compat } from '@ember-data/tracking';
import { defineSignal } from '@ember-data/tracking/-private';
import { defineNonEnumerableSignal, memoized } from '@ember-data/store/-private';
import { DEBUG } from '@warp-drive/build-config/env';
import type { StableRecordIdentifier } from '@warp-drive/core-types';
import type { StableExistingRecordIdentifier } from '@warp-drive/core-types/identifier';
Expand Down Expand Up @@ -143,8 +142,7 @@ export default class BelongsToReference<
* @property {StableRecordIdentifier | null} identifier
* @public
*/
@cached
@compat
@memoized
get identifier(): StableRecordIdentifier<TypeFromInstanceOrString<Related>> | null {
if (this.___relatedToken) {
this.store.notifications.unsubscribe(this.___relatedToken);
Expand Down Expand Up @@ -704,4 +702,4 @@ export default class BelongsToReference<
return support.reloadBelongsTo(this.key, options).then(() => this.value());
}
}
defineSignal(BelongsToReference.prototype, '_ref', 0);
defineNonEnumerableSignal(BelongsToReference.prototype, '_ref', 0);
8 changes: 3 additions & 5 deletions packages/model/src/-private/references/has-many.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import type { CollectionEdge, Graph } from '@ember-data/graph/-private';
import type Store from '@ember-data/store';
import type { NotificationType } from '@ember-data/store';
import type { RelatedCollection as ManyArray } from '@ember-data/store/-private';
import { defineNonEnumerableSignal, memoized } from '@ember-data/store/-private';
import type { BaseFinderOptions } from '@ember-data/store/types';
import { cached, compat } from '@ember-data/tracking';
import { defineSignal } from '@ember-data/tracking/-private';
import { DEBUG } from '@warp-drive/build-config/env';
import { assert } from '@warp-drive/build-config/macros';
import type { StableRecordIdentifier } from '@warp-drive/core-types';
Expand Down Expand Up @@ -146,8 +145,7 @@ export default class HasManyReference<
* @property {StableRecordIdentifier[]} identifiers
* @public
*/
@cached
@compat
@memoized
get identifiers(): StableRecordIdentifier<TypeFromInstanceOrString<Related>>[] {
ensureRefCanSubscribe(this);
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
Expand Down Expand Up @@ -748,7 +746,7 @@ export default class HasManyReference<
return support.reloadHasMany(this.key, options) as Promise<ManyArray<Related>>;
}
}
defineSignal(HasManyReference.prototype, '_ref', 0);
defineNonEnumerableSignal(HasManyReference.prototype, '_ref', 0);

export function isMaybeResource(object: ExistingResourceObject | ResourceIdentifier): object is ExistingResourceObject {
const keys = Object.keys(object).filter((k) => k !== 'id' && k !== 'type' && k !== 'lid');
Expand Down
10 changes: 5 additions & 5 deletions packages/schema-record/src/-private/fields/compute.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { Future } from '@ember-data/request';
import type Store from '@ember-data/store';
import type { StoreRequestInput } from '@ember-data/store';
import { RelatedCollection as ManyArray } from '@ember-data/store/-private';
import { defineSignal, getSignal, peekSignal } from '@ember-data/tracking/-private';
import { defineSignal, RelatedCollection as ManyArray } from '@ember-data/store/-private';
import { getSignal, peekSignal } from '@ember-data/tracking/-private';
import { DEBUG } from '@warp-drive/build-config/env';
import type { StableRecordIdentifier } from '@warp-drive/core-types';
import { getOrSetGlobal } from '@warp-drive/core-types/-private';
Expand Down Expand Up @@ -303,9 +303,9 @@ class ResourceRelationship<T extends SchemaRecord = SchemaRecord> {
}
}

defineSignal(ResourceRelationship.prototype, 'data');
defineSignal(ResourceRelationship.prototype, 'links');
defineSignal(ResourceRelationship.prototype, 'meta');
defineSignal(ResourceRelationship.prototype, 'data', null);
defineSignal(ResourceRelationship.prototype, 'links', null);
defineSignal(ResourceRelationship.prototype, 'meta', null);

function getHref(link?: Link | null): string | null {
if (!link) {
Expand Down
Loading