DrSensor / nusa

incremental runtime that bring both simplicity and power into webdev (buildless, cross-language, data-driven)
MIT License
4 stars 0 forks source link

reactive/bindable **Iterable** data structure #65

Open DrSensor opened 1 year ago

DrSensor commented 1 year ago

This replace/shim JS Array, Map, and Set with nusa/std Vector, Map, Set.

import { Vector as Vec, Map, Set } from "//esm.run/nusa/std/collections"

export default {
  coins = new Vec<number>()
  makets = new Map<string, number>()
  symbols = new Set<string>()

  shuffleLastCoin() {
    console.debug("previous last coin", this.coins.at(-1))
    this.coins.setAt(-1, dice([100, 300], [10, 50]))
  }
}

Yes, indexing operator (i.e arr[i]) is not reactive and there is no way to override/overload it in JS. That's why the name is Vector, not Array.

DrSensor commented 1 year ago

Also, supporting plain Object as Dictionary might be a good idea since it get optimized as long as the keys < 32. Although Map is superior than plain Object, plain Object get optimized most of the time.

export class Dictionary<T extends Record<infer K, infer V>> implements Map<K, V> {
  #data: T
  constructor(data: T = {}) {
    this.#data = data
  }
}
DrSensor commented 1 year ago

The more I think about making reactive ES data type, the more it give me a headache when I want to make the memory contiguous 😅

Alright, I should start with Arrow data type first then map it 1:1 into ES data type.