-
Notifications
You must be signed in to change notification settings - Fork 562
Support "delete" keyword for object nodes #25738
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,12 @@ | ||
| --- | ||
| "@fluidframework/tree": minor | ||
| "__section": feature | ||
| --- | ||
| `delete` keyword support for ObjectNodes | ||
|
|
||
| Added support for using the `delete` keyword to remove content under optional fields for ObjectNodes. | ||
|
|
||
| ```ts | ||
| // This is now equivalent to node.foo = undefined | ||
| delete node.foo | ||
| ``` |
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -118,6 +118,23 @@ export type ObjectFromSchemaRecord<T extends RestrictiveStringRecord<ImplicitFie | |
| * No other own `own` or `enumerable` properties are included on object nodes unless the user of the node manually adds custom session only state. | ||
| * This allows a majority of general purpose JavaScript object processing operations (like `for...in`, `Reflect.ownKeys()` and `Object.entries()`) to enumerate all the children. | ||
| * | ||
| * Field Assignment and Deletion | ||
| * Assigning to a field updates the tree value as long as it is still valid based on the schema. | ||
| * - For required fields, assigning `undefined` is invalid, and will throw. | ||
| * - For optional fields, assigning `undefined` removes the field's contents, and marks it as empty (non-enumerable and value set to undefined). | ||
| * | ||
| * Example: | ||
| * ```ts | ||
| * const Foo = schemaFactory.object("Foo", {bar: schemaFactory.optional(schemaFactory.number)}); | ||
| * const node = new Foo({bar: 1}) | ||
| * | ||
| * // This clears the field, is non-enumerable, and value is undefined. | ||
| * delete node.bar; | ||
| * | ||
| * // This is equivalent to the delete example. | ||
| * node.bar = undefined | ||
| * ``` | ||
| * | ||
| * The API for fields is defined by {@link ObjectFromSchemaRecord}. | ||
| * @public | ||
| */ | ||
|
|
@@ -275,27 +292,17 @@ function createProxyHandler( | |
| : false; | ||
| } | ||
|
|
||
| const innerNode = getInnerNode(proxy); | ||
|
|
||
| const innerSchema = innerNode.context.schema.nodeSchema.get(brand(schema.identifier)); | ||
| assert( | ||
| innerSchema instanceof ObjectNodeStoredSchema, | ||
| 0xc18 /* Expected ObjectNodeStoredSchema */, | ||
| ); | ||
|
|
||
| setField( | ||
| innerNode.getBoxed(fieldInfo.storedKey), | ||
| fieldInfo.schema, | ||
| value, | ||
| innerSchema.getFieldSchema(fieldInfo.storedKey), | ||
| ); | ||
| applyFieldChange(schema, { kind: "proxy", node: proxy }, fieldInfo, value); | ||
| return true; | ||
| }, | ||
| deleteProperty(target, propertyKey): boolean { | ||
| // TODO: supporting delete when it makes sense (custom local fields, and optional field) could be added as a feature in the future. | ||
| throw new UsageError( | ||
| `Object nodes do not support the delete operator. Optional fields can be assigned to undefined instead.`, | ||
| ); | ||
| const fieldInfo = schema.flexKeyMap.get(propertyKey); | ||
| if (fieldInfo === undefined) { | ||
| return allowAdditionalProperties ? Reflect.deleteProperty(target, propertyKey) : false; | ||
| } | ||
|
|
||
| applyFieldChange(schema, { kind: "target", node: target }, fieldInfo, undefined); | ||
| return true; | ||
| }, | ||
| has: (target, propertyKey) => { | ||
| return ( | ||
|
|
@@ -750,3 +757,29 @@ function getFieldProperty( | |
| } | ||
| return undefined; | ||
| } | ||
|
|
||
| function applyFieldChange( | ||
| schema: ObjectNodeSchemaPrivate, | ||
| from: { kind: "proxy"; node: TreeNode } | { kind: "target"; node: object }, | ||
| fieldInfo: { storedKey: FieldKey; schema: FieldSchema }, | ||
| value: InsertableContent | undefined, | ||
| ): void { | ||
| const proxy = | ||
| from.kind === "proxy" | ||
| ? from.node | ||
| : (targetToProxy.get(from.node) ?? fail("missing proxy")); | ||
| const inner = getInnerNode(proxy); | ||
| const storedSchema = inner.context.schema.nodeSchema.get(brand(schema.identifier)); | ||
| assert(storedSchema instanceof ObjectNodeStoredSchema, "Expected ObjectNodeStoredSchema"); | ||
|
||
|
|
||
| if (value === undefined && inner.tryGetField(fieldInfo.storedKey) === undefined) { | ||
| return; | ||
| } | ||
|
|
||
| setField( | ||
| inner.getBoxed(fieldInfo.storedKey), | ||
| fieldInfo.schema, | ||
| value, | ||
| storedSchema.getFieldSchema(fieldInfo.storedKey), | ||
| ); | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.