Skip to content

Commit cc62196

Browse files
committed
[delta] less mixing: setAttr and $setAttrOp
1 parent 41c5f66 commit cc62196

File tree

3 files changed

+126
-112
lines changed

3 files changed

+126
-112
lines changed

delta/delta-pitch.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ export const testDeltaBasicSchema = _tc => {
6565
const $d = delta.$delta({ attrs: { key: s.$string }, children: s.$number, text: false })
6666
const d = delta.create($d)
6767
// @ts-expect-error
68-
d.set('key', false) // invalid change: will throw a type error
68+
d.setAttr('key', false) // invalid change: will throw a type error
6969
t.fails(() => {
7070
// @ts-expect-error
71-
d.apply(delta.create().set('key', false)) // invalid delta: will throw a type error
71+
d.apply(delta.create().setAttr('key', false)) // invalid delta: will throw a type error
7272
})
7373
}
7474

@@ -83,7 +83,7 @@ export const testDeltaBasicSchema = _tc => {
8383
* @param {t.TestCase} _tc
8484
*/
8585
export const testDeltaValues = _tc => {
86-
const change = delta.create().set('a', 42).unset('b').retain(5).delete(6).insert('!').insert([{ my: 'custom object' }])
86+
const change = delta.create().setAttr('a', 42).deleteAttr('b').retain(5).delete(6).insert('!').insert([{ my: 'custom object' }])
8787
// iterate through attribute changes
8888
for (const attrChange of change.attrs) {
8989
if (delta.$insertOp.check(attrChange)) {

delta/delta.js

Lines changed: 74 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export const $attribution = s.$object({
7676
*/
7777

7878
/**
79-
* @typedef {AttrInsertOp<any>|AttrDeleteOp<any>|AttrModifyOp} AttrOpAny
79+
* @typedef {SetAttrOp<any>|DeleteAttrOp<any>|ModifyAttrOp} AttrOpAny
8080
*/
8181

8282
/**
@@ -572,10 +572,10 @@ export class ModifyOp extends list.ListNode {
572572
}
573573

574574
/**
575-
* @template {any} V
575+
* @template {any} [V=any]
576576
* @template {string|number} [K=any]
577577
*/
578-
export class AttrInsertOp {
578+
export class SetAttrOp {
579579
/**
580580
* @param {K} key
581581
* @param {V} value
@@ -655,25 +655,25 @@ export class AttrInsertOp {
655655
}
656656

657657
/**
658-
* @param {AttrInsertOp<V>} other
658+
* @param {SetAttrOp<V>} other
659659
*/
660660
[equalityTrait.EqualityTraitSymbol] (other) {
661661
return this.key === other.key && fun.equalityDeep(this.value, other.value) && fun.equalityDeep(this.attribution, other.attribution)
662662
}
663663

664664
/**
665-
* @return {AttrInsertOp<V,K>}
665+
* @return {SetAttrOp<V,K>}
666666
*/
667667
clone () {
668-
return new AttrInsertOp(this.key, _markMaybeDeltaAsDone(this.value), _markMaybeDeltaAsDone(this.prevValue), _cloneAttrs(this.attribution))
668+
return new SetAttrOp(this.key, _markMaybeDeltaAsDone(this.value), _markMaybeDeltaAsDone(this.prevValue), _cloneAttrs(this.attribution))
669669
}
670670
}
671671

672672
/**
673-
* @template V
673+
* @template [V=any]
674674
* @template {string|number} [K=string]
675675
*/
676-
export class AttrDeleteOp {
676+
export class DeleteAttrOp {
677677
/**
678678
* @param {K} key
679679
* @param {V|undefined} prevValue
@@ -720,22 +720,22 @@ export class AttrDeleteOp {
720720
}
721721

722722
/**
723-
* @param {AttrDeleteOp<V>} other
723+
* @param {DeleteAttrOp<V>} other
724724
*/
725725
[equalityTrait.EqualityTraitSymbol] (other) {
726726
return this.key === other.key && fun.equalityDeep(this.attribution, other.attribution)
727727
}
728728

729729
clone () {
730-
return new AttrDeleteOp(this.key, _markMaybeDeltaAsDone(this.prevValue), _cloneAttrs(this.attribution))
730+
return new DeleteAttrOp(this.key, _markMaybeDeltaAsDone(this.prevValue), _cloneAttrs(this.attribution))
731731
}
732732
}
733733

734734
/**
735735
* @template {DeltaAny} [Modifier=DeltaAny]
736736
* @template {string|number} [K=string]
737737
*/
738-
export class AttrModifyOp {
738+
export class ModifyAttrOp {
739739
/**
740740
* @param {K} key
741741
* @param {Modifier} delta
@@ -794,41 +794,49 @@ export class AttrModifyOp {
794794
}
795795

796796
/**
797-
* @param {AttrModifyOp<Modifier>} other
797+
* @param {ModifyAttrOp<Modifier>} other
798798
*/
799799
[equalityTrait.EqualityTraitSymbol] (other) {
800800
return this.key === other.key && this.value[equalityTrait.EqualityTraitSymbol](other.value)
801801
}
802802

803803
/**
804-
* @return {AttrModifyOp<Modifier,K>}
804+
* @return {ModifyAttrOp<Modifier,K>}
805805
*/
806806
clone () {
807-
return new AttrModifyOp(this.key, /** @type {Modifier} */ (this.value.done()))
807+
return new ModifyAttrOp(this.key, /** @type {Modifier} */ (this.value.done()))
808808
}
809809
}
810810

811811
/**
812-
* @type {s.Schema<AttrDeleteOp<{}> | DeleteOp>}
812+
* @type {s.Schema<DeleteOp>}
813+
*/
814+
export const $deleteOp = s.$constructedBy(DeleteOp)
815+
export const $deleteAttrOp = /** @type {s.Schema<DeleteAttrOp<{},string|number>>} */ (s.$constructedBy(DeleteAttrOp))
816+
817+
/**
818+
* @type {s.Schema<InsertOp<any>>}
813819
*/
814-
export const $deleteOp = s.$custom(o => o != null && (o.constructor === DeleteOp || o.constructor === AttrDeleteOp))
820+
export const $insertOp = s.$constructedBy(InsertOp)
815821

816822
/**
817-
* @type {s.Schema<AttrInsertOp<any> | InsertOp<any>>}
823+
* @type {s.Schema<SetAttrOp<any>>}
818824
*/
819-
export const $insertOp = s.$custom(o => o != null && (o.constructor === AttrInsertOp || o.constructor === InsertOp))
825+
export const $setAttrOp = s.$constructedBy(SetAttrOp)
820826

821827
/**
822828
* @template {fingerprintTrait.Fingerprintable} Content
823829
* @param {s.Schema<Content>} $content
824-
* @return {s.Schema<AttrInsertOp<Content> | InsertOp<Content>>}
830+
* @return {s.Schema<SetAttrOp<Content>>}
825831
*/
826-
export const $insertOpWith = $content => s.$custom(o =>
827-
o != null && (
828-
(o.constructor === AttrInsertOp && $content.check(/** @type {AttrInsertOp<Content>} */ (o).value)) ||
829-
(o.constructor === InsertOp && /** @type {InsertOp<Content>} */ (o).insert.every(ins => $content.check(ins)))
830-
)
831-
)
832+
export const $setAttrOpWith = $content => /** @type {any} */ (s.$constructedBy(SetAttrOp, o => $content.check(o.value)))
833+
834+
/**
835+
* @template {fingerprintTrait.Fingerprintable} Content
836+
* @param {s.Schema<Content>} $content
837+
* @return {s.Schema<InsertOp<Content>>}
838+
*/
839+
export const $insertOpWith = $content => /** @type {any} */ (s.$constructedBy(InsertOp, o => $content.check(o.insert.every(ins => $content.check(ins)))))
832840

833841
/**
834842
* @type {s.Schema<TextOp>}
@@ -841,23 +849,29 @@ export const $textOp = s.$constructedBy(TextOp)
841849
export const $retainOp = s.$constructedBy(RetainOp)
842850

843851
/**
844-
* @type {s.Schema<AttrModifyOp | ModifyOp>}
852+
* @type {s.Schema<ModifyAttrOp<any,string|number>>}
845853
*/
846-
export const $modifyOp = s.$custom(o => o != null && (o.constructor === AttrModifyOp || o.constructor === ModifyOp))
854+
export const $modifyAttrOp = s.$constructedBy(ModifyAttrOp)
855+
856+
/**
857+
* @type {s.Schema<ModifyOp>}
858+
*/
859+
export const $modifyOp = s.$constructedBy(ModifyOp)
847860

848861
/**
849862
* @template {DeltaAny} Modify
850863
* @param {s.Schema<Modify>} $content
851-
* @return {s.Schema<AttrModifyOp<Modify> | ModifyOp<Modify>>}
864+
* @return {s.Schema<ModifyAttrOp<Modify> | ModifyOp<Modify>>}
852865
*/
853866
export const $modifyOpWith = $content => s.$custom(o =>
854867
o != null && (
855-
(o.constructor === AttrModifyOp && $content.check(/** @type {AttrModifyOp<Modify>} */ (o).value)) ||
868+
(o.constructor === ModifyAttrOp && $content.check(/** @type {ModifyAttrOp<Modify>} */ (o).value)) ||
856869
(o.constructor === ModifyOp && $content.check(/** @type {ModifyOp<Modify>} */ (o).value))
857870
)
858871
)
859872

860873
export const $anyOp = s.$union($insertOp, $deleteOp, $textOp, $modifyOp)
874+
export const $anyAttrOp = s.$union($setAttrOp, $deleteAttrOp, $modifyAttrOp)
861875

862876
/**
863877
* @template {Array<any>|string} C1
@@ -988,8 +1002,8 @@ class DeltaData {
9881002
this.name = /** @type {Name} */ (name)
9891003
this.$schema = $schema
9901004
/**
991-
* @type {{ [K in keyof Attrs]?: K extends string|number ? (AttrInsertOp<Attrs[K],K>|AttrDeleteOp<Attrs[K],K>|(Attrs[K] extends never ? never : (Attrs[K] extends Delta ? AttrModifyOp<Extract<Attrs[K],Delta>,K> : never))) : never }
992-
* & { [Symbol.iterator]: () => Iterator<{ [K in keyof Attrs]: K extends string|number ? (AttrInsertOp<Attrs[K],K>|AttrDeleteOp<Attrs[K],K>|(Attrs[K] extends never ? never : (Delta extends Attrs[K] ? AttrModifyOp<Extract<Attrs[K],Delta>,K> : never))) : never }[keyof Attrs]> }
1005+
* @type {{ [K in keyof Attrs]?: K extends string|number ? (SetAttrOp<Attrs[K],K>|DeleteAttrOp<Attrs[K],K>|(Attrs[K] extends never ? never : (Attrs[K] extends Delta ? ModifyAttrOp<Extract<Attrs[K],Delta>,K> : never))) : never }
1006+
* & { [Symbol.iterator]: () => Iterator<{ [K in keyof Attrs]: K extends string|number ? (SetAttrOp<Attrs[K],K>|DeleteAttrOp<Attrs[K],K>|(Attrs[K] extends never ? never : (Delta extends Attrs[K] ? ModifyAttrOp<Extract<Attrs[K],Delta>,K> : never))) : never }[keyof Attrs]> }
9931007
* }
9941008
*/
9951009
this.attrs = /** @type {any} */ ({
@@ -1420,11 +1434,11 @@ export class DeltaBuilder extends Delta {
14201434
* @param {Val|undefined} [prevValue]
14211435
* @return {DeltaBuilder<DeltaConfOverwrite<DConf,{attrs:AddToAttrs<DeltaConfGetAttrs<DConf>,Key,Val>}>>}
14221436
*/
1423-
set (key, val, attribution = null, prevValue) {
1437+
setAttr (key, val, attribution = null, prevValue) {
14241438
modDeltaCheck(this)
14251439
// @ts-ignore
14261440
this.attrs[key] /** @type {any} */ =
1427-
(new AttrInsertOp(/** @type {any} */ (key), val, prevValue, mergeAttrs(this.usedAttribution, attribution)))
1441+
(new SetAttrOp(/** @type {any} */ (key), val, prevValue, mergeAttrs(this.usedAttribution, attribution)))
14281442
return /** @type {any} */ (this)
14291443
}
14301444

@@ -1438,10 +1452,10 @@ export class DeltaBuilder extends Delta {
14381452
* >>
14391453
* }
14401454
*/
1441-
setMany (attrs, attribution = null) {
1455+
setAttrs (attrs, attribution = null) {
14421456
modDeltaCheck(this)
14431457
for (const k in attrs) {
1444-
this.set(/** @type {any} */ (k), /** @type {any} */ (attrs)[/** @type {any} */ (k)], attribution)
1458+
this.setAttr(/** @type {any} */ (k), /** @type {any} */ (attrs)[/** @type {any} */ (k)], attribution)
14451459
}
14461460
return /** @type {any} */ (this)
14471461
}
@@ -1455,11 +1469,11 @@ export class DeltaBuilder extends Delta {
14551469
* attrs: AddToAttrs<DeltaConfGetAttrs<DConf>,Key,never>
14561470
* }>>}
14571471
*/
1458-
unset (key, attribution = null, prevValue) {
1472+
deleteAttr (key, attribution = null, prevValue) {
14591473
modDeltaCheck(this)
14601474
// @ts-ignore
14611475
this.attrs[key] /** @type {any} */ =
1462-
(new AttrDeleteOp(/** @type {any} */ (key), prevValue, mergeAttrs(this.usedAttribution, attribution)))
1476+
(new DeleteAttrOp(/** @type {any} */ (key), prevValue, mergeAttrs(this.usedAttribution, attribution)))
14631477
return /** @type {any} */ (this)
14641478
}
14651479

@@ -1470,9 +1484,9 @@ export class DeltaBuilder extends Delta {
14701484
* @param {D} modify
14711485
* @return {DeltaBuilder<DeltaConfOverwrite<DConf,{attrs:AddToAttrs<DeltaConfGetAttrs<DConf>,Key,D>}>>}
14721486
*/
1473-
update (key, modify) {
1487+
modifyAttr (key, modify) {
14741488
modDeltaCheck(this)
1475-
this.attrs[key] = /** @type {any} */ (new AttrModifyOp(key, modify))
1489+
this.attrs[key] = /** @type {any} */ (new ModifyAttrOp(key, modify))
14761490
return /** @type {any} */ (this)
14771491
}
14781492

@@ -1485,21 +1499,21 @@ export class DeltaBuilder extends Delta {
14851499
// apply attrs
14861500
for (const op of other.attrs) {
14871501
// @ts-ignore
1488-
const c = /** @type {AttrInsertOp<any,any>|AttrDeleteOp<any>|AttrModifyOp<any,any>} */ (this.attrs[op.key])
1489-
if ($modifyOp.check(op)) {
1502+
const c = /** @type {SetAttrOp<any,any>|DeleteAttrOp<any>|ModifyAttrOp<any,any>} */ (this.attrs[op.key])
1503+
if ($modifyAttrOp.check(op)) {
14901504
if ($deltaAny.check(c?.value)) {
14911505
c._modValue.apply(op.value)
14921506
} else {
14931507
// then this is a simple modify
14941508
// @ts-ignore
14951509
this.attrs[op.key] = op.clone()
14961510
}
1497-
} else if ($insertOp.check(op)) {
1511+
} else if ($setAttrOp.check(op)) {
14981512
// @ts-ignore
14991513
op.prevValue = c?.value
15001514
// @ts-ignore
15011515
this.attrs[op.key] = op.clone()
1502-
} else if ($deleteOp.check(op)) {
1516+
} else if ($deleteAttrOp.check(op)) {
15031517
op.prevValue = c?.value
15041518
// @ts-ignore
15051519
delete this.attrs[op.key]
@@ -1625,13 +1639,13 @@ export class DeltaBuilder extends Delta {
16251639
remainingLen -= delLen
16261640
}
16271641
}
1628-
} else if ($modifyOp.check(op)) {
1642+
} else if ($modifyAttrOp.check(op)) {
16291643
if (opsI == null) {
16301644
list.pushEnd(this.children, op.clone())
16311645
this.childCnt += 1
16321646
return
16331647
}
1634-
if ($modifyOp.check(opsI)) {
1648+
if ($modifyAttrOp.check(opsI)) {
16351649
opsI._modValue.apply(/** @type {any} */ (op.value))
16361650
} else if ($insertOp.check(opsI)) {
16371651
opsI._modValue(offset).apply(op.value)
@@ -1685,30 +1699,30 @@ export class DeltaBuilder extends Delta {
16851699
* - modify vs modify ⇒ rebase using priority
16861700
*/
16871701
for (const op of this.attrs) {
1688-
if ($insertOp.check(op)) {
1689-
// @ts-ignore
1690-
if ($insertOp.check(other.attrs[op.key]) && !priority) {
1702+
if ($setAttrOp.check(op)) {
1703+
if ($setAttrOp.check(other.attrs[op.key]) && !priority) {
16911704
// @ts-ignore
16921705
delete this.attrs[op.key]
16931706
}
1694-
} else if ($deleteOp.check(op)) {
1707+
} else if ($deleteAttrOp.check(op)) {
16951708
// @ts-ignore
16961709
const otherOp = other.attrs[/** @type {any} */ (op.key)]
1697-
if ($insertOp.check(otherOp)) {
1710+
if ($setAttrOp.check(otherOp)) {
16981711
// @ts-ignore
16991712
delete this.attrs[otherOp.key]
17001713
}
1701-
} else if ($modifyOp.check(op)) {
1702-
// @ts-ignore
1714+
} else if ($modifyAttrOp.check(op)) {
17031715
const otherOp = other.attrs[/** @type {any} */ (op.key)]
17041716
if (otherOp == null) {
17051717
// nop
1706-
} else if ($modifyOp.check(otherOp)) {
1718+
} else if ($modifyAttrOp.check(otherOp)) {
17071719
op._modValue.rebase(otherOp.value, priority)
17081720
} else {
17091721
// @ts-ignore
17101722
delete this.attrs[otherOp.key]
17111723
}
1724+
} else {
1725+
error.unexpectedCase()
17121726
}
17131727
}
17141728
/**
@@ -1926,7 +1940,7 @@ export class $Delta extends s.Schema {
19261940
err?.extend('Delta.name', $name.toString(), o.name, 'Unexpected node name')
19271941
} else if (list.toArray(o.children).some(c => (!hasText && $textOp.check(c)) || (hasText && $textOp.check(c) && c.format != null && !$formats.check(c.format)) || ($insertOp.check(c) && !c.insert.every(ins => $children.check(ins))))) {
19281942
err?.extend('Delta.children', '', '', 'Children don\'t match the schema')
1929-
} else if (object.some(o.attrs, (op, k) => $insertOp.check(op) && !$attrs.check({ [k]: op.value }, err))) {
1943+
} else if (object.some(o.attrs, (op, k) => $setAttrOp.check(op) && !$attrs.check({ [k]: op.value }, err))) {
19301944
err?.extend('Delta.attrs', '', '', 'Attrs don\'t match the schema')
19311945
} else {
19321946
return true
@@ -2000,7 +2014,7 @@ export const _$delta = ({ name, attrs, children, text, recursive }) => {
20002014
if (
20012015
!$name.check(d.name) ||
20022016
object.some(d.attrs,
2003-
(op, k) => $insertOp.check(op) && !$attrsPartial.check({ [k]: op.value })
2017+
(op, k) => $setAttrOp.check(op) && !$attrsPartial.check({ [k]: op.value })
20042018
)
20052019
) return false
20062020
for (const op of d.children) {
@@ -2062,7 +2076,7 @@ export const random = (gen, $d) => {
20622076
const { $name, $attrs, $children, hasText, $formats: $formats_ } = /** @type {$Delta<any>} */ (/** @type {any} */ ($d)).shape
20632077
const d = s.$$any.check($name) ? create($deltaAny) : create(s.random(gen, $name), $deltaAny)
20642078
const $formats = s.$$any.check($formats_) ? s.$null : $formats_
2065-
prng.bool(gen) && d.setMany(s.random(gen, $attrs))
2079+
prng.bool(gen) && d.setAttrs(s.random(gen, $attrs))
20662080
for (let i = prng.uint32(gen, 0, 5); i > 0; i--) {
20672081
if (hasText && prng.bool(gen)) {
20682082
d.insert(prng.word(gen), s.random(gen, $formats))
@@ -2131,7 +2145,7 @@ export const create = (nodeNameOrSchema, attrsOrSchema, children) => {
21312145
const schema = /** @type {any} */ (s.$$schema.check(nodeNameOrSchema) ? nodeNameOrSchema : (s.$$schema.check(attrsOrSchema) ? attrsOrSchema : null))
21322146
const d = /** @type {DeltaBuilder<any>} */ (new DeltaBuilder(nodeName, schema))
21332147
if (s.$objectAny.check(attrsOrSchema)) {
2134-
d.setMany(attrsOrSchema)
2148+
d.setAttrs(attrsOrSchema)
21352149
}
21362150
children && d.insert(children)
21372151
return d
@@ -2335,8 +2349,8 @@ export const diff = (d1, d2) => {
23352349
const attr1 = d1.attrs[attr2.key]
23362350
if (attr1 == null || (attr1.fingerprint !== attr2.fingerprint)) {
23372351
/* c8 ignore else */
2338-
if ($insertOp.check(attr2)) {
2339-
d.set(attr2.key, attr2.value)
2352+
if ($setAttrOp.check(attr2)) {
2353+
d.setAttr(attr2.key, attr2.value)
23402354
} else {
23412355
/* c8 ignore next 2 */
23422356
error.unexpectedCase()
@@ -2346,7 +2360,7 @@ export const diff = (d1, d2) => {
23462360
for (const attr1 of d1.attrs) {
23472361
// @ts-ignore
23482362
if (d2.attrs[attr1.key] == null) {
2349-
d.unset(attr1.key)
2363+
d.deleteAttr(attr1.key)
23502364
}
23512365
}
23522366
}

0 commit comments

Comments
 (0)