Skip to content

Commit

Permalink
feat(setter-observer): separate start/stop from subscribe,
Browse files Browse the repository at this point in the history
- remove lifecycle dependency
  • Loading branch information
bigopon committed Oct 4, 2020
1 parent edf854b commit c895f93
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/runtime-html/src/observation/observer-locator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export class TargetObserverLocator implements ITargetObserverLocator {
case 'css':
return new StyleAttributeAccessor(scheduler, flags, obj as HTMLElement);
case 'model':
return new SetterObserver(lifecycle, flags, obj as Node & IIndexable, propertyName);
return new SetterObserver(flags, obj as Node & IIndexable, propertyName);
case 'role':
return new DataAttributeAccessor(scheduler, flags, obj as HTMLElement, propertyName);
default:
Expand Down
8 changes: 6 additions & 2 deletions packages/runtime/src/observation/binding-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ const enum RuntimeError {
const marker = Object.freeze({});

/** @internal */
export class InternalObserversLookup {
export class InternalObserversLookup implements ObserversLookup {
[x: string]: PropertyObserver;
[y: number]: PropertyObserver;

// @ts-expect-error
public getOrCreate(
this: { [key: string]: PropertyObserver },
lifecycle: ILifecycle,
Expand All @@ -30,7 +34,7 @@ export class InternalObserversLookup {
key: string,
): PropertyObserver {
if (this[key] === void 0) {
this[key] = new SetterObserver(lifecycle, flags, obj, key);
this[key] = new SetterObserver(flags, obj, key);
}
return this[key];
}
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime/src/observation/observer-locator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ export class ObserverLocator implements IObserverLocator {

return createComputedObserver(flags, this, this.dirtyChecker, this.lifecycle, obj, propertyName, descriptor);
}
return new SetterObserver(this.lifecycle, flags, obj, propertyName);
return new SetterObserver(flags, obj, propertyName);
}
}

Expand Down
24 changes: 22 additions & 2 deletions packages/runtime/src/observation/setter-observer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export class SetterObserver {
public type: AccessorType = AccessorType.Obj;

public constructor(
public readonly lifecycle: ILifecycle,
flags: LifecycleFlags,
public readonly obj: IIndexable,
public readonly propertyKey: string,
Expand Down Expand Up @@ -59,6 +58,14 @@ export class SetterObserver {
}

public subscribe(subscriber: ISubscriber): void {
if (this.observing === false) {
this.start();
}

this.addSubscriber(subscriber);
}

public start(): this {
if (this.observing === false) {
this.observing = true;
this.currentValue = this.obj[this.propertyKey];
Expand All @@ -81,7 +88,20 @@ export class SetterObserver {
Reporter.write(1, this.propertyKey, this.obj);
}
}
return this;
}

this.addSubscriber(subscriber);
public stop(): this {
if (this.observing) {
Reflect.defineProperty(this.obj, this.propertyKey, {
enumerable: true,
configurable: true,
writable: true,
value: this.currentValue,
});
this.observing = false;
// todo(bigopon/fred): add .removeAllSubscribers()
}
return this;
}
}

0 comments on commit c895f93

Please sign in to comment.