@viewfly/core 1.0.0-alpha.14 → 1.0.0-alpha.15

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.
@@ -530,8 +530,6 @@ interface TextAtom {
530
530
  child: Atom | null;
531
531
  sibling: Atom | null;
532
532
  isSvg: boolean;
533
- update: null | ((insertOffset: number) => void);
534
- next: Atom | null;
535
533
  }
536
534
  interface ElementAtom {
537
535
  type: typeof ElementAtomType;
@@ -541,8 +539,6 @@ interface ElementAtom {
541
539
  child: Atom | null;
542
540
  sibling: Atom | null;
543
541
  isSvg: boolean;
544
- update: null | ((insertOffset: number) => void);
545
- next: Atom | null;
546
542
  }
547
543
  interface ComponentAtom {
548
544
  type: typeof ComponentAtomType;
@@ -552,8 +548,6 @@ interface ComponentAtom {
552
548
  child: Atom | null;
553
549
  sibling: Atom | null;
554
550
  isSvg: boolean;
555
- update: null | ((insertOffset: number) => void);
556
- next: Atom | null;
557
551
  }
558
552
  type Atom = TextAtom | ElementAtom | ComponentAtom;
559
553
  interface ComponentView {
@@ -1298,9 +1298,7 @@ function createRenderer(component, nativeRenderer) {
1298
1298
  sibling: null,
1299
1299
  child: null,
1300
1300
  nativeNode: null,
1301
- isSvg: false,
1302
- update: null,
1303
- next: null
1301
+ isSvg: false
1304
1302
  };
1305
1303
  componentRender(nativeRenderer, component, atom, {
1306
1304
  isParent: true,
@@ -1370,61 +1368,71 @@ function applyChanges(nativeRenderer, component) {
1370
1368
  }
1371
1369
  }
1372
1370
  function diff(nativeRenderer, parentComponent, newAtom, oldAtom, context) {
1373
- let updateAtom = newAtom;
1374
- let insertOffset = 0;
1375
- let deleteOffset = 0;
1371
+ const commits = [];
1372
+ function changeOffset() {
1373
+ offset++;
1374
+ }
1375
+ while (newAtom) {
1376
+ oldAtom = createChanges(newAtom, oldAtom, nativeRenderer, commits, context, parentComponent, changeOffset);
1377
+ newAtom = newAtom.sibling;
1378
+ }
1379
+ let dirtyDiffAtom = oldAtom;
1380
+ while (dirtyDiffAtom) {
1381
+ cleanView(nativeRenderer, dirtyDiffAtom, true);
1382
+ dirtyDiffAtom = dirtyDiffAtom.sibling;
1383
+ }
1384
+ let offset = 0;
1385
+ for (let i = 0; i < commits.length; i++) {
1386
+ const commit = commits[i];
1387
+ while (oldAtom) {
1388
+ if (oldAtom.index <= i) {
1389
+ offset--;
1390
+ oldAtom = oldAtom.sibling;
1391
+ continue;
1392
+ }
1393
+ break;
1394
+ }
1395
+ commit(offset);
1396
+ }
1397
+ }
1398
+ function createChanges(newAtom, oldAtom, nativeRenderer, commits, context, parentComponent, effect) {
1399
+ const startDiffAtom = oldAtom;
1400
+ const { jsxNode: newJsxNode, type } = newAtom;
1401
+ const key = newJsxNode.key;
1402
+ let prev = null;
1376
1403
  while (oldAtom) {
1377
- const startDiffAtom = newAtom;
1378
- let prev = null;
1379
- let isUsed = false;
1380
- while (newAtom) {
1381
- const newAtomType = newAtom.type;
1382
- if (oldAtom.type === newAtomType) {
1383
- if (newAtomType === TextAtomType) {
1384
- newAtom.update = updateText(newAtom, oldAtom, nativeRenderer, context);
1385
- isUsed = true;
1404
+ if (type === oldAtom.type) {
1405
+ let commit;
1406
+ if (type === TextAtomType) {
1407
+ commit = updateText(newAtom, oldAtom, nativeRenderer, context);
1408
+ }
1409
+ else {
1410
+ const { key: diffKey, type: diffType } = oldAtom.jsxNode;
1411
+ if (diffKey !== key || newJsxNode.type !== diffType) {
1412
+ prev = oldAtom;
1413
+ oldAtom = oldAtom.sibling;
1414
+ continue;
1415
+ }
1416
+ if (type === ComponentAtomType) {
1417
+ commit = updateComponent(newAtom, oldAtom, nativeRenderer, context);
1386
1418
  }
1387
1419
  else {
1388
- const { key: newKey, type: newType } = newAtom.jsxNode;
1389
- const { key: oldKey, type: oldType } = oldAtom.jsxNode;
1390
- if (newType === oldType && newKey === oldKey) {
1391
- if (newAtomType === ComponentAtomType) {
1392
- newAtom.update = updateComponent(newAtom, oldAtom, deleteOffset, nativeRenderer, context);
1393
- }
1394
- else {
1395
- newAtom.update = updateElement(newAtom, oldAtom, deleteOffset, nativeRenderer, context, parentComponent);
1396
- }
1397
- isUsed = true;
1398
- }
1420
+ commit = updateElement(newAtom, oldAtom, nativeRenderer, context, parentComponent);
1399
1421
  }
1400
1422
  }
1401
- if (isUsed) {
1402
- const sibling = newAtom.next || newAtom.sibling;
1403
- if (prev) {
1404
- prev.next = sibling;
1405
- }
1406
- newAtom = newAtom === startDiffAtom ? sibling : startDiffAtom;
1407
- break;
1423
+ commits.push(commit);
1424
+ const next = oldAtom.sibling;
1425
+ if (!prev) {
1426
+ return next;
1408
1427
  }
1409
- prev = newAtom;
1410
- newAtom = newAtom.next || newAtom.sibling;
1411
- }
1412
- if (!isUsed) {
1413
- newAtom = startDiffAtom;
1414
- deleteOffset++;
1415
- cleanView(nativeRenderer, oldAtom, true);
1428
+ prev.sibling = next;
1429
+ return startDiffAtom;
1416
1430
  }
1431
+ prev = oldAtom;
1417
1432
  oldAtom = oldAtom.sibling;
1418
1433
  }
1419
- function changeOffset() {
1420
- insertOffset++;
1421
- }
1422
- while (updateAtom) {
1423
- const update = updateAtom.update || createNewView(updateAtom, nativeRenderer, context, parentComponent, changeOffset);
1424
- update(insertOffset);
1425
- updateAtom.next = updateAtom.update = null;
1426
- updateAtom = updateAtom.sibling;
1427
- }
1434
+ commits.push(createNewView(newAtom, nativeRenderer, context, parentComponent, effect));
1435
+ return startDiffAtom;
1428
1436
  }
1429
1437
  function createNewView(start, nativeRenderer, context, parentComponent, effect) {
1430
1438
  return function () {
@@ -1443,10 +1451,10 @@ function updateText(newAtom, oldAtom, nativeRenderer, context) {
1443
1451
  context.isParent = false;
1444
1452
  };
1445
1453
  }
1446
- function updateElement(newAtom, oldAtom, deleteOffset, nativeRenderer, context, parentComponent) {
1447
- return function (insertOffset) {
1454
+ function updateElement(newAtom, oldAtom, nativeRenderer, context, parentComponent) {
1455
+ return function (offset) {
1448
1456
  newAtom.nativeNode = oldAtom.nativeNode;
1449
- if (newAtom.index - insertOffset !== oldAtom.index - deleteOffset) {
1457
+ if (newAtom.index - offset !== oldAtom.index) {
1450
1458
  insertNode(nativeRenderer, newAtom, context);
1451
1459
  }
1452
1460
  context.host = newAtom.nativeNode;
@@ -1454,8 +1462,8 @@ function updateElement(newAtom, oldAtom, deleteOffset, nativeRenderer, context,
1454
1462
  updateNativeNodeProperties(nativeRenderer, newAtom, oldAtom, parentComponent, context);
1455
1463
  };
1456
1464
  }
1457
- function updateComponent(newAtom, reusedAtom, deleteOffset, nativeRenderer, context) {
1458
- return function (insertOffset) {
1465
+ function updateComponent(newAtom, reusedAtom, nativeRenderer, context) {
1466
+ return function (offset) {
1459
1467
  const component = reusedAtom.jsxNode;
1460
1468
  const newProps = newAtom.jsxNode.props;
1461
1469
  const oldTemplate = component.template;
@@ -1465,7 +1473,7 @@ function updateComponent(newAtom, reusedAtom, deleteOffset, nativeRenderer, cont
1465
1473
  componentViewCache.set(component, Object.assign({ atom: newAtom }, context));
1466
1474
  newAtom.jsxNode = component;
1467
1475
  if (newTemplate === oldTemplate) {
1468
- reuseComponentView(nativeRenderer, newAtom, reusedAtom, context, newAtom.index - insertOffset !== reusedAtom.index - deleteOffset);
1476
+ reuseComponentView(nativeRenderer, newAtom, reusedAtom, context, newAtom.index - offset !== reusedAtom.index);
1469
1477
  updateView(nativeRenderer, component);
1470
1478
  return;
1471
1479
  }
@@ -1560,9 +1568,7 @@ function createChainByJSXNode(type, jsxNode, prevAtom, isSvg) {
1560
1568
  sibling: null,
1561
1569
  child: null,
1562
1570
  nativeNode: null,
1563
- isSvg,
1564
- update: null,
1565
- next: null
1571
+ isSvg
1566
1572
  };
1567
1573
  prevAtom.sibling = atom;
1568
1574
  return atom;
package/bundles/index.js CHANGED
@@ -1300,9 +1300,7 @@ function createRenderer(component, nativeRenderer) {
1300
1300
  sibling: null,
1301
1301
  child: null,
1302
1302
  nativeNode: null,
1303
- isSvg: false,
1304
- update: null,
1305
- next: null
1303
+ isSvg: false
1306
1304
  };
1307
1305
  componentRender(nativeRenderer, component, atom, {
1308
1306
  isParent: true,
@@ -1372,61 +1370,71 @@ function applyChanges(nativeRenderer, component) {
1372
1370
  }
1373
1371
  }
1374
1372
  function diff(nativeRenderer, parentComponent, newAtom, oldAtom, context) {
1375
- let updateAtom = newAtom;
1376
- let insertOffset = 0;
1377
- let deleteOffset = 0;
1373
+ const commits = [];
1374
+ function changeOffset() {
1375
+ offset++;
1376
+ }
1377
+ while (newAtom) {
1378
+ oldAtom = createChanges(newAtom, oldAtom, nativeRenderer, commits, context, parentComponent, changeOffset);
1379
+ newAtom = newAtom.sibling;
1380
+ }
1381
+ let dirtyDiffAtom = oldAtom;
1382
+ while (dirtyDiffAtom) {
1383
+ cleanView(nativeRenderer, dirtyDiffAtom, true);
1384
+ dirtyDiffAtom = dirtyDiffAtom.sibling;
1385
+ }
1386
+ let offset = 0;
1387
+ for (let i = 0; i < commits.length; i++) {
1388
+ const commit = commits[i];
1389
+ while (oldAtom) {
1390
+ if (oldAtom.index <= i) {
1391
+ offset--;
1392
+ oldAtom = oldAtom.sibling;
1393
+ continue;
1394
+ }
1395
+ break;
1396
+ }
1397
+ commit(offset);
1398
+ }
1399
+ }
1400
+ function createChanges(newAtom, oldAtom, nativeRenderer, commits, context, parentComponent, effect) {
1401
+ const startDiffAtom = oldAtom;
1402
+ const { jsxNode: newJsxNode, type } = newAtom;
1403
+ const key = newJsxNode.key;
1404
+ let prev = null;
1378
1405
  while (oldAtom) {
1379
- const startDiffAtom = newAtom;
1380
- let prev = null;
1381
- let isUsed = false;
1382
- while (newAtom) {
1383
- const newAtomType = newAtom.type;
1384
- if (oldAtom.type === newAtomType) {
1385
- if (newAtomType === TextAtomType) {
1386
- newAtom.update = updateText(newAtom, oldAtom, nativeRenderer, context);
1387
- isUsed = true;
1406
+ if (type === oldAtom.type) {
1407
+ let commit;
1408
+ if (type === TextAtomType) {
1409
+ commit = updateText(newAtom, oldAtom, nativeRenderer, context);
1410
+ }
1411
+ else {
1412
+ const { key: diffKey, type: diffType } = oldAtom.jsxNode;
1413
+ if (diffKey !== key || newJsxNode.type !== diffType) {
1414
+ prev = oldAtom;
1415
+ oldAtom = oldAtom.sibling;
1416
+ continue;
1417
+ }
1418
+ if (type === ComponentAtomType) {
1419
+ commit = updateComponent(newAtom, oldAtom, nativeRenderer, context);
1388
1420
  }
1389
1421
  else {
1390
- const { key: newKey, type: newType } = newAtom.jsxNode;
1391
- const { key: oldKey, type: oldType } = oldAtom.jsxNode;
1392
- if (newType === oldType && newKey === oldKey) {
1393
- if (newAtomType === ComponentAtomType) {
1394
- newAtom.update = updateComponent(newAtom, oldAtom, deleteOffset, nativeRenderer, context);
1395
- }
1396
- else {
1397
- newAtom.update = updateElement(newAtom, oldAtom, deleteOffset, nativeRenderer, context, parentComponent);
1398
- }
1399
- isUsed = true;
1400
- }
1422
+ commit = updateElement(newAtom, oldAtom, nativeRenderer, context, parentComponent);
1401
1423
  }
1402
1424
  }
1403
- if (isUsed) {
1404
- const sibling = newAtom.next || newAtom.sibling;
1405
- if (prev) {
1406
- prev.next = sibling;
1407
- }
1408
- newAtom = newAtom === startDiffAtom ? sibling : startDiffAtom;
1409
- break;
1425
+ commits.push(commit);
1426
+ const next = oldAtom.sibling;
1427
+ if (!prev) {
1428
+ return next;
1410
1429
  }
1411
- prev = newAtom;
1412
- newAtom = newAtom.next || newAtom.sibling;
1413
- }
1414
- if (!isUsed) {
1415
- newAtom = startDiffAtom;
1416
- deleteOffset++;
1417
- cleanView(nativeRenderer, oldAtom, true);
1430
+ prev.sibling = next;
1431
+ return startDiffAtom;
1418
1432
  }
1433
+ prev = oldAtom;
1419
1434
  oldAtom = oldAtom.sibling;
1420
1435
  }
1421
- function changeOffset() {
1422
- insertOffset++;
1423
- }
1424
- while (updateAtom) {
1425
- const update = updateAtom.update || createNewView(updateAtom, nativeRenderer, context, parentComponent, changeOffset);
1426
- update(insertOffset);
1427
- updateAtom.next = updateAtom.update = null;
1428
- updateAtom = updateAtom.sibling;
1429
- }
1436
+ commits.push(createNewView(newAtom, nativeRenderer, context, parentComponent, effect));
1437
+ return startDiffAtom;
1430
1438
  }
1431
1439
  function createNewView(start, nativeRenderer, context, parentComponent, effect) {
1432
1440
  return function () {
@@ -1445,10 +1453,10 @@ function updateText(newAtom, oldAtom, nativeRenderer, context) {
1445
1453
  context.isParent = false;
1446
1454
  };
1447
1455
  }
1448
- function updateElement(newAtom, oldAtom, deleteOffset, nativeRenderer, context, parentComponent) {
1449
- return function (insertOffset) {
1456
+ function updateElement(newAtom, oldAtom, nativeRenderer, context, parentComponent) {
1457
+ return function (offset) {
1450
1458
  newAtom.nativeNode = oldAtom.nativeNode;
1451
- if (newAtom.index - insertOffset !== oldAtom.index - deleteOffset) {
1459
+ if (newAtom.index - offset !== oldAtom.index) {
1452
1460
  insertNode(nativeRenderer, newAtom, context);
1453
1461
  }
1454
1462
  context.host = newAtom.nativeNode;
@@ -1456,8 +1464,8 @@ function updateElement(newAtom, oldAtom, deleteOffset, nativeRenderer, context,
1456
1464
  updateNativeNodeProperties(nativeRenderer, newAtom, oldAtom, parentComponent, context);
1457
1465
  };
1458
1466
  }
1459
- function updateComponent(newAtom, reusedAtom, deleteOffset, nativeRenderer, context) {
1460
- return function (insertOffset) {
1467
+ function updateComponent(newAtom, reusedAtom, nativeRenderer, context) {
1468
+ return function (offset) {
1461
1469
  const component = reusedAtom.jsxNode;
1462
1470
  const newProps = newAtom.jsxNode.props;
1463
1471
  const oldTemplate = component.template;
@@ -1467,7 +1475,7 @@ function updateComponent(newAtom, reusedAtom, deleteOffset, nativeRenderer, cont
1467
1475
  componentViewCache.set(component, Object.assign({ atom: newAtom }, context));
1468
1476
  newAtom.jsxNode = component;
1469
1477
  if (newTemplate === oldTemplate) {
1470
- reuseComponentView(nativeRenderer, newAtom, reusedAtom, context, newAtom.index - insertOffset !== reusedAtom.index - deleteOffset);
1478
+ reuseComponentView(nativeRenderer, newAtom, reusedAtom, context, newAtom.index - offset !== reusedAtom.index);
1471
1479
  updateView(nativeRenderer, component);
1472
1480
  return;
1473
1481
  }
@@ -1562,9 +1570,7 @@ function createChainByJSXNode(type, jsxNode, prevAtom, isSvg) {
1562
1570
  sibling: null,
1563
1571
  child: null,
1564
1572
  nativeNode: null,
1565
- isSvg,
1566
- update: null,
1567
- next: null
1573
+ isSvg
1568
1574
  };
1569
1575
  prevAtom.sibling = atom;
1570
1576
  return atom;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@viewfly/core",
3
- "version": "1.0.0-alpha.14",
3
+ "version": "1.0.0-alpha.15",
4
4
  "description": "Viewfly is a simple and easy-to-use JavaScript framework with an intuitive development experience.",
5
5
  "main": "./bundles/index.js",
6
6
  "module": "./bundles/index.esm.js",
@@ -50,7 +50,7 @@
50
50
  "bugs": {
51
51
  "url": "https://github.com/viewfly/viewfly.git/issues"
52
52
  },
53
- "gitHead": "48779d88f1390751bbd06e4c4581bf3dbe613a26",
53
+ "gitHead": "20910725ab3302fe98b04db6e6f6b44a3f5b64b5",
54
54
  "dependencies": {
55
55
  "reflect-metadata": "^0.2.2"
56
56
  }