Destroy a hook

Whenever you add a hook in the store, it always returns a HookLink instance that represents the link between the hook and store. You can destroy this link anytime if you want to stop the hook from listening on state changes.

The returned HookLink instance is only available inside the hook callback after the initial phase.

your-controller.ts
import { CounterState, CounterStore } from 'counter-store';

function YourController($scope: ng.IScope, counterStore: CounterStore) {
  const hookLink = counterStore.hook('INCREMENT_COUNT', (state) => {
    $scope.count = state.count;
    
    if ($scope.count === 10) {
      hookLink.destroy();
    }
  });
}

angular
  .module('App', [])
  .controller('YourController', YourController);

Another method of HookLink is destroyOn. You can use this method if you want to destroy a hook automatically. The only requirement is a scope where hook should be bound. When a scope triggers a $destroy event, the hook also gets destroyed.

your-controller.ts
import { CounterState, CounterStore } from 'counter-store';

function YourController($scope: ng.IScope, counterStore: CounterStore) {
  counterStore.hook('INCREMENT_COUNT', (state) => {
    $scope.count = state.count;
  }).destroyOn($scope);
}

angular
  .module('App', [])
  .controller('YourController', YourController);

Last updated