forked from emberjs/data
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(private): reactive simple fields (emberjs#8948)
- Loading branch information
1 parent
5bec4a3
commit 277ba7f
Showing
11 changed files
with
561 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
export { transact, memoTransact, untracked } from './-private'; | ||
export { transact, memoTransact, untracked, entangleSignal } from './-private'; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { render, TestContext } from '@ember/test-helpers'; | ||
import Component from '@glimmer/component'; | ||
|
||
import type { FieldSchema } from '@warp-drive/schema-record/schema'; | ||
|
||
import { hbs } from 'ember-cli-htmlbars'; | ||
|
||
import type { ResourceRelationship } from '@ember-data/types/cache/relationship'; | ||
|
||
export async function reactiveContext<T extends object>(this: TestContext, record: T, fields: FieldSchema[]) { | ||
const _fields: string[] = ['idCount', 'id', '$typeCount', '$type']; | ||
fields.forEach((field) => { | ||
_fields.push(field.name + 'Count'); | ||
_fields.push(field.name); | ||
}); | ||
|
||
class ReactiveComponent extends Component { | ||
get __allFields() { | ||
return _fields; | ||
} | ||
} | ||
|
||
const counters: Record<string, number> = {}; | ||
counters['id'] = 0; | ||
counters['$type'] = 0; | ||
|
||
Object.defineProperty(ReactiveComponent.prototype, 'idCount', { | ||
get() { | ||
return counters['id']; | ||
}, | ||
}); | ||
Object.defineProperty(ReactiveComponent.prototype, '$typeCount', { | ||
get() { | ||
return counters['$type']; | ||
}, | ||
}); | ||
|
||
Object.defineProperty(ReactiveComponent.prototype, 'id', { | ||
get() { | ||
counters['id']++; | ||
return record['id'] as unknown; | ||
}, | ||
}); | ||
Object.defineProperty(ReactiveComponent.prototype, '$type', { | ||
get() { | ||
counters['$type']++; | ||
return record['$type'] as unknown; | ||
}, | ||
}); | ||
|
||
fields.forEach((field) => { | ||
counters[field.name] = 0; | ||
Object.defineProperty(ReactiveComponent.prototype, field.name + 'Count', { | ||
get() { | ||
return counters[field.name]; | ||
}, | ||
}); | ||
Object.defineProperty(ReactiveComponent.prototype, field.name, { | ||
get() { | ||
counters[field.name]++; | ||
|
||
if (field.kind === 'attribute' || field.kind === 'derived') { | ||
return record[field.name] as unknown; | ||
} else if (field.kind === 'resource') { | ||
return (record[field.name] as ResourceRelationship).data?.id; | ||
} | ||
}, | ||
}); | ||
}); | ||
|
||
this.owner.register('component:reactive-component', ReactiveComponent); | ||
this.owner.register( | ||
'template:components/reactive-component', | ||
hbs`<div class="reactive-context"><ul>{{#each this.__allFields as |prop|}}<li>{{prop}}: {{get this prop}}</li>{{/each}}</ul></div>` | ||
); | ||
|
||
await render(hbs`<ReactiveComponent />`); | ||
|
||
function reset() { | ||
fields.forEach((field) => { | ||
counters[field.name] = 0; | ||
}); | ||
} | ||
|
||
return { counters, reset, fieldOrder: _fields }; | ||
} |
Oops, something went wrong.