angular-scope-value-store.ts 742 B

12345678910111213141516171819202122232425262728
  1. import { ScopeValueStore } from '../../../../../types/ide/scope-value-store'
  2. import { Scope } from '../../../../../types/angular/scope'
  3. import _ from 'lodash'
  4. export class AngularScopeValueStore implements ScopeValueStore {
  5. // eslint-disable-next-line no-useless-constructor
  6. constructor(readonly $scope: Scope) {}
  7. get(path: string) {
  8. return _.get(this.$scope, path)
  9. }
  10. set(path: string, value: unknown): void {
  11. this.$scope.$applyAsync(() => _.set(this.$scope, path, value))
  12. }
  13. watch<T>(
  14. path: string,
  15. callback: (newValue: T) => void,
  16. deep: boolean
  17. ): () => void {
  18. return this.$scope.$watch(
  19. path,
  20. (newValue: T) => callback(deep ? _.cloneDeep(newValue) : newValue),
  21. deep
  22. )
  23. }
  24. }