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 you want to stop the hook from listening on state changes.

your-controller.js
import { CounterStore } from 'counter-store';

function YourController($scope, 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 the $destroy event, the hook also gets destroyed.

your-controller.js
import { CounterStore } from 'counter-store';

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

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

Last updated

Was this helpful?