@shortfuse/materialdesignweb 0.7.2 → 0.7.4

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.
package/core/observe.js CHANGED
@@ -189,8 +189,9 @@ export function parseObserverOptions(name, typeOrOptions, object) {
189
189
  changedCallback,
190
190
  watchers: options.watchers ?? [],
191
191
  values: options.values ?? new WeakMap(),
192
- validValues: options.validValues ?? new WeakMap(),
192
+ computedValues: options.computedValues ?? new WeakMap(),
193
193
  attributeChangedCallback: options.attributeChangedCallback,
194
+ needsSelfInvalidation: options.needsSelfInvalidation ?? new WeakSet(),
194
195
  };
195
196
  }
196
197
 
@@ -293,10 +294,9 @@ export function defineObservableProperty(object, key, options) {
293
294
  /**
294
295
  * @param {T2} oldValue
295
296
  * @param {T2} value
296
- * @param {boolean} [commit=false]
297
297
  * @return {boolean} changed
298
298
  */
299
- function detectChange(oldValue, value, commit) {
299
+ function detectChange(oldValue, value) {
300
300
  if (oldValue === value) return false;
301
301
  if (config.get) {
302
302
  // TODO: Custom getter vs parser
@@ -318,10 +318,6 @@ export function defineObservableProperty(object, key, options) {
318
318
  if (config.is.call(this, oldValue, newValue)) return false;
319
319
  }
320
320
 
321
- // Before changing, store old values of properties that will invalidate;
322
-
323
- // Do no set if transient (getter)
324
-
325
321
  config.values.set(this, newValue);
326
322
  // console.log(key, 'value.set', newValue);
327
323
  config.propChangedCallback?.call(this, key, oldValue, newValue, changes);
@@ -344,27 +340,27 @@ export function defineObservableProperty(object, key, options) {
344
340
  function internalSet(value) {
345
341
  const oldValue = this[key];
346
342
  // console.log(key, 'internalSet', oldValue, '=>', value);
347
- detectChange.call(this, oldValue, value, true);
343
+ detectChange.call(this, oldValue, value);
348
344
  }
349
345
 
350
- /**
351
- *
352
- */
346
+ /** @return {void} */
353
347
  function onInvalidate() {
354
- // console.log(key, 'onInvalidate', '???');
355
- const oldValue = config.validValues.get(this);
348
+ // Current value is now invalidated. Recompute and check if changed
349
+ const oldValue = config.computedValues.get(this);
356
350
  const newValue = this[key];
357
- // console.log(key, 'onInvalidate', oldValue, '=>', newValue);
351
+ // console.debug('observe: onInvalidate called for', key, oldValue, '=>', newValue, this);
352
+ config.needsSelfInvalidation.delete(this);
358
353
  detectChange.call(this, oldValue, newValue);
359
354
  }
360
355
 
361
356
  if (config.get) {
357
+ // Custom `get` uses computed values.
358
+ // Invalidate computed value when dependent `prop` changes
362
359
  const { props } = observeFunction(config.get.bind(object), object, internalGet.bind(object));
363
- // Set of watchers needed
364
- // console.log(key, 'invalidates with', props);
365
360
  config.watchers.push(
366
361
  ...[...props].map((prop) => [prop, onInvalidate]),
367
362
  );
363
+ // TODO: May be able to cache value if props are present
368
364
  }
369
365
  /** @type {Partial<PropertyDescriptor>} */
370
366
  const descriptor = {
@@ -376,8 +372,8 @@ export function defineObservableProperty(object, key, options) {
376
372
  get() {
377
373
  if (config.get) {
378
374
  const newValue = config.get.call(this, this, internalGet.bind(this));
379
- // Store value internally. Used by onInvalidate to get previous value
380
- config.validValues.set(this, newValue);
375
+ // Store computed value internally. Used by onInvalidate to get previous value
376
+ config.computedValues.set(this, newValue);
381
377
  return newValue;
382
378
  }
383
379
  return internalGet.call(this);
@@ -393,10 +389,12 @@ export function defineObservableProperty(object, key, options) {
393
389
  return;
394
390
  }
395
391
  if (config.set) {
392
+ config.needsSelfInvalidation.add(this);
396
393
  const oldValue = this[key];
397
394
  config.set.call(this, value, internalSet.bind(this));
398
395
  const newValue = this[key];
399
- // Invalidate self
396
+ if (!config.needsSelfInvalidation.has(this)) return;
397
+ config.needsSelfInvalidation.delete(this);
400
398
  detectChange.call(this, oldValue, newValue);
401
399
  } else {
402
400
  internalSet.call(this, value);
package/core/typings.d.ts CHANGED
@@ -46,8 +46,9 @@ type ObserverOptions<
46
46
  set?: (this:C, value: T2, fn?:(value2: T2) => any) => any,
47
47
  attributeChangedCallback?: (this:C, name:string, oldValue: string, newValue: string) => any;
48
48
  propChangedCallback?: (this:C, name:string, oldValue: T2, newValue: T2, changes:any) => any;
49
- validValues?: WeakMap<C, T2>;
49
+ computedValues?: WeakMap<C, T2>;
50
50
  watchers?: [keyof C, (this:C, ...args:any[]) => any][];
51
+ needsSelfInvalidation?: WeakSet<C>,
51
52
  }
52
53
 
53
54
  type ObserverConfiguration<