|
| 1 | +import Route from '@ember/routing/route'; |
| 2 | +import { inject as service } from '@ember/service'; |
| 3 | + |
| 4 | +export default Route.extend({ |
| 5 | + store: service(), |
| 6 | + |
| 7 | + async model() { |
| 8 | + performance.mark('start-data-generation'); |
| 9 | + |
| 10 | + const initialPayload = await fetch('./fixtures/add-children-initial.json').then((r) => r.json()); |
| 11 | + const initialPayload2 = structuredClone(initialPayload); |
| 12 | + |
| 13 | + const minusOnePayload = structuredClone(initialPayload); |
| 14 | + minusOnePayload.data.relationships.children.data.pop(); |
| 15 | + minusOnePayload.included.pop(); |
| 16 | + |
| 17 | + performance.mark('start-push-initial-payload'); |
| 18 | + this.store.push(initialPayload); |
| 19 | + |
| 20 | + performance.mark('start-peek-records'); |
| 21 | + const peekedChildren = this.store.peekAll('child'); |
| 22 | + const peekedParents = this.store.peekAll('parent'); |
| 23 | + |
| 24 | + performance.mark('start-record-materialization'); |
| 25 | + peekedChildren.slice(); |
| 26 | + peekedParents.slice(); |
| 27 | + |
| 28 | + performance.mark('start-relationship-materialization'); |
| 29 | + const seen = new Set(); |
| 30 | + peekedParents.forEach((parent) => iterateParent(parent, seen)); |
| 31 | + const parent = peekedParents[0]; |
| 32 | + const children = await parent.children; |
| 33 | + |
| 34 | + performance.mark('start-local-removal'); |
| 35 | + const removedChild = children.pop(); |
| 36 | + |
| 37 | + performance.mark('start-push-minus-one-payload'); |
| 38 | + this.store.push(minusOnePayload); |
| 39 | + |
| 40 | + performance.mark('start-local-addition'); |
| 41 | + children.push(removedChild); |
| 42 | + |
| 43 | + performance.mark('start-push-plus-one-payload'); |
| 44 | + this.store.push(initialPayload2); |
| 45 | + |
| 46 | + performance.mark('end-push-plus-one-payload'); |
| 47 | + }, |
| 48 | +}); |
| 49 | + |
| 50 | +function iterateChild(record, seen) { |
| 51 | + if (seen.has(record)) { |
| 52 | + return; |
| 53 | + } |
| 54 | + seen.add(record); |
| 55 | + |
| 56 | + record.bestFriend.get('name'); |
| 57 | + record.secondBestFriend.get('name'); |
| 58 | + record.friends.forEach((child) => iterateChild(child, seen)); |
| 59 | +} |
| 60 | +function iterateParent(record, seen) { |
| 61 | + seen.add(record); |
| 62 | + record.children.forEach((child) => iterateChild(child, seen)); |
| 63 | +} |
0 commit comments