Skip to content

Commit 6ccd0d4

Browse files
committed
finalized
1 parent 7695712 commit 6ccd0d4

File tree

4 files changed

+63
-81
lines changed

4 files changed

+63
-81
lines changed

packages/plexus-core/src/collection/collection.ts

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -310,25 +310,6 @@ export class CollectionInstance<
310310
startedFromInnerBatch?: boolean
311311
) => {
312312
// if the instance is batching and this collection has batching enabled, add this action to the batchedSetters
313-
// if (
314-
// this.instance().runtime.isBatching &&
315-
// this.config.useBatching &&
316-
// !startedFromInnerBatch
317-
// ) {
318-
// this.instance().runtime.log(
319-
// 'debug',
320-
// `Batching an collect call for collection ${this.instanceId}`
321-
// )
322-
// // store this in the batchedSetters for execution once batching is over
323-
// // this.instance().runtime.batchedCalls.push(() => {
324-
// // this.instance().runtime.log(
325-
// // 'debug',
326-
// // `Batched collect call fulfilled for collection ${this.instanceId}`
327-
// // )
328-
// // return collectFn(dataToCollect, groups, true)
329-
// // })
330-
// return this
331-
// }
332313

333314
const addedKeys: any[] = collectItems(
334315
Array.isArray(dataToCollect) ? dataToCollect : [dataToCollect]

packages/plexus-core/src/instance/engine.ts

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { deepMerge } from '@plexusjs/utils'
22
import { PlexusInternalWatcher } from '../types'
3+
import { PlexusInstance } from './instance'
34
export interface EngineEventReceiver {
45
from: string
56
listener: PlexusInternalWatcher
@@ -12,7 +13,7 @@ export class EventEngine {
1213
events: Map<string, Array<EngineEventReceiver>>
1314
pendingEventPayloads: Map<string, EventPayload>
1415

15-
constructor() {
16+
constructor(public instance: () => PlexusInstance) {
1617
this.events = new Map()
1718
this.pendingEventPayloads = new Map()
1819
}
@@ -22,8 +23,7 @@ export class EventEngine {
2223
*/
2324
halt() {
2425
this.halts++
25-
if (!this.halted) console.log('halting engine...')
26-
26+
if (!this.halted) this.instance().runtime.log('debug', 'Halting engine...')
2727
this.halted = true
2828
return () => this.release()
2929
}
@@ -36,12 +36,13 @@ export class EventEngine {
3636
this.halted = false
3737
if (this.pendingEventPayloads.size === 0) return
3838
// if (this.batching === false) return
39-
const pendingEntries = Array.from(this.pendingEventPayloads.entries())
40-
console.log(
41-
`releasing ${pendingEntries.length} (${this.pendingEventPayloads.size}) events`
39+
this.instance().runtime.log(
40+
'debug',
41+
`Releasing Engine; collected (${this.pendingEventPayloads.size}) payloads`
4242
)
43-
for (const [eventId, args] of pendingEntries) {
44-
console.log(`releasing ${eventId} stored payload`)
43+
for (const [eventId, args] of Array.from(
44+
this.pendingEventPayloads.entries()
45+
)) {
4546
this.emit(eventId, args)
4647
}
4748

@@ -114,13 +115,6 @@ export class EventEngine {
114115
const eventPayload = pendingPayload
115116
? deepMerge<EventPayload>(pendingPayload, args)
116117
: args
117-
// console.log(
118-
// 'Emit occured while batching enabled',
119-
// eventId,
120-
// pendingPayload,
121-
// this.events.get(eventId),
122-
// eventPayload
123-
// )
124118
this.pendingEventPayloads.set(eventId, eventPayload)
125119
return
126120
}

packages/plexus-core/src/instance/runtime.ts

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export class RuntimeInstance {
3737
protected config: Partial<RuntimeConfig> = {}
3838
) {
3939
this.instance = instance
40-
this._engine = new EventEngine()
40+
this._engine = new EventEngine(this.instance)
4141
this.schedule = new Scheduler(`${config.name}_runtime`)
4242
}
4343
/**
@@ -227,8 +227,10 @@ export class RuntimeInstance {
227227
}
228228
// if we are already batching, add the function to the array and return
229229
if (this.batching) {
230-
// return fn()
231-
console.log('Already batching something, ')
230+
this.log(
231+
'debug',
232+
'Already batching something, adding a function to the list of batched things...'
233+
)
232234
// ++this.batchesInProgress
233235
this.batchedCalls.push(() => fn())
234236
return null
@@ -273,30 +275,30 @@ export class RuntimeInstance {
273275
--this.batchesInProgress
274276
// if there are still batches in progress, just return
275277
if (this.batchesInProgress > 0) {
276-
console.log(
278+
this.log(
279+
'debug',
277280
`Aborting batch end because ${this.batchesInProgress} batches are still in progress`
278281
)
279282
return
280283
}
281-
282-
const unhalt = this.engine.halt()
283-
console.log(`batchedCalls ${this.batchesInProgress}`, this.batchedCalls)
284+
this.engine.halt()
285+
this.log(
286+
'debug',
287+
`Executing batch (${this.batchedCalls.length} calls)`,
288+
this.batchedCalls
289+
)
284290
// call all the pending functions and clear the array
285291
this.batching = false
286-
const v = this.batchesInProgress
287-
for (const pendingFn of this.batchedCalls) {
292+
while (this.batchedCalls.length > 0) {
293+
const pendingFn = this.batchedCalls.shift()
294+
if (!pendingFn) continue
288295
pendingFn()
289-
console.log(
290-
`Running pending function with ${this.batchesInProgress} others in progress`
291-
)
292296
}
293297

294-
this.batchedCalls.length = 0
295-
296298
// release the reactivity engine
297-
unhalt()
299+
this.engine.release()
298300
// if(this.batchesInProgress === 0) { unhalt() }
299-
this.instance().runtime.log('info', 'Batch function completed!')
301+
this.log('info', 'Batch function completed!')
300302
}
301303
/**
302304
* Begin batching any calls to the runtime
@@ -308,7 +310,7 @@ export class RuntimeInstance {
308310
++this.batchesInProgress
309311
// hold the reactivity engine and start storing changes
310312
// this.engine.halt()
311-
this.instance().runtime.log('info', 'Batch function started!')
313+
this.log('info', 'Batch function started!')
312314
return () => {
313315
this.endBatching()
314316
}

tests/efficiency.test.ts

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -56,42 +56,47 @@ afterEach(() => {
5656
appointments.clear()
5757
})
5858

59+
const LG_SIZE = 5000
60+
const MD_SIZE = 1000
61+
const SM_SIZE = 100
62+
5963
describe('Efficiency tests for ', () => {
6064
test('The speed of a plexus collection collecting more than a thousand randomly generated objects into multiple groups', () => {
6165
instance({ logLevel: 'debug' })
6266
console.log('Starting test...')
63-
console.log(`${users1k.length} items being pulled into collection`)
64-
usersLite.collect(users1k.slice(0, 30), ['firstNames'])
67+
const data = users10k.slice(0, MD_SIZE)
68+
console.log(`${data.length} items being pulled into collection`)
69+
usersLite.collect(data.slice(0, MD_SIZE), ['firstNames'])
6570
// expect(usersLite.size).toBe(1000)
6671
console.log('items in collection:', usersLite.size)
67-
expect(usersLite.size).toBe(1000)
68-
// expect(usersLite.value.length).toBe(1000)
69-
// expect(usersLite.groups.firstNames.value.length).toBe(1000)
72+
expect(usersLite.size).toBe(MD_SIZE)
73+
expect(usersLite.value.length).toBe(MD_SIZE)
74+
expect(usersLite.groups.firstNames.value.length).toBe(MD_SIZE)
7075
instance({ logLevel: undefined })
7176
})
72-
// test('Testing the same as above but with an absurd amount of data', () => {
73-
// // instance({ logLevel: 'debug' })
74-
// console.log('Starting test...')
75-
// console.log('items in collection:', users10k.length)
76-
// usersLite.collect(users10k, ['firstNames'])
77-
// console.log('items in collection:', usersLite.value.length)
78-
// // const group1 = collectionInstance.group('appointmentId')
79-
// // const group2 = collectionInstance.group('name')
80-
// // expect(group1.value.length).toBe(1000)
81-
// // expect(group2.value.length).toBe(1000)
82-
// // instance({ logLevel: undefined })
83-
// })
84-
// test('An absurd amount of related data', () => {
85-
// // instance({ logLevel: 'debug' })
86-
// console.log('Starting test...')
87-
// console.log('items in collection:', users10k.length)
88-
// users.collect(users1kRelated, ['main'])
89-
// appointments.collect(appointments1kRelated, ['main'])
90-
// console.log('items in collection:', users.value.length)
91-
// // const group1 = collectionInstance.group('appointmentId')
92-
// // const group2 = collectionInstance.group('name')
93-
// // expect(group1.value.length).toBe(1000)
94-
// // expect(group2.value.length).toBe(1000)
95-
// // instance({ logLevel: undefined })
96-
// })
77+
test('Testing the same as above but with an absurd amount of data', () => {
78+
// instance({ logLevel: 'debug' })
79+
console.log('Starting test...')
80+
const data = users10k.slice(0, LG_SIZE)
81+
console.log('items in collection:', data.length)
82+
usersLite.collect(data.slice(0, LG_SIZE), ['firstNames'])
83+
console.log('items in collection:', usersLite.value.length)
84+
// const group1 = collectionInstance.group('appointmentId')
85+
// const group2 = collectionInstance.group('name')
86+
// expect(group1.value.length).toBe(1000)
87+
// expect(group2.value.length).toBe(1000)
88+
// instance({ logLevel: undefined })
89+
})
90+
test('An absurd amount of related data', () => {
91+
// instance({ logLevel: 'debug' })
92+
console.log('Starting test...')
93+
users.collect(users1kRelated, ['main'])
94+
appointments.collect(appointments1kRelated, ['main'])
95+
console.log('items in collection:', users.value.length)
96+
// const group1 = collectionInstance.group('appointmentId')
97+
// const group2 = collectionInstance.group('name')
98+
// expect(group1.value.length).toBe(1000)
99+
// expect(group2.value.length).toBe(1000)
100+
// instance({ logLevel: undefined })
101+
})
97102
})

0 commit comments

Comments
 (0)