Skip to content

Exec and Subscribe ​

exec ​

In order to start the act, you must call the exec method of the ActMaster instance.

The exec method always returns Promise. The first argument is the name field property of the act you want to call. Then all the arguments the act needs to execute are passed.

For the example, we have an act of the following form:

ts
// login.act.ts
import { ActMasterAction } from 'act-master';
import { api } from '.../path/to/api';

export class Login implements ActMasterAction {
  name = 'Login';

  async exec(loginData: any): Promise<any> {
    return await api.login(loginData);
  }
}

Let's call him:

ts
import { act } from 'act-master';

const loginData = { /* ... */ };

const result = await act().exec('Login', loginData);
ts
import { ActMaster } from 'act-master';

const loginData = { /* ... */ };

const $act = ActMaster.getInstance();

const result = await $act.exec('Login', loginData);
ts
<script>
export default {
  // ...
  methods: {
    async login() {
      const result = await this.$act.exec('Login', this.loginData);
    }
  }
}
</script>

The helpers provide convenient access to this execution API: act() returns the instance for act().exec('Login', loginData), while the $act proxy forwards $act.Login(loginData) to the same exec call.

subscribe/unsubscribe on/off ​

In order to get the result of the exec method, you can subscribe to the desired act.

Then every time in successful execution, you will get the result in the callback.

The subscribe and on methods are synonymous, as are unsubscribe and off.

ts
act().subscribe('Login', (loginData: LoginData) => {
  // ...
});
ts
act().on('Login', (loginData: LoginData) => {
  // ...
});

If you subscribe to an act, you will need to unsubscribe if you no longer need it. For example when the component that made it is removed. Otherwise you might have problems with memory leaks.

There are several ways to unsubscribe:

Unsubscribe with the function subscribe returns ​

ts
const unsubscribe = act().subscribe('Login', loginCallback);

unsubscribe();
ts
const off = act().on('Login', loginCallback);

off();

Unsubscribe with method ​

ts
act().subscribe('Login', loginCallback);

act().unsubscribe('Login', loginCallback);
ts
act().on('Login', loginCallback);

act().off('Login', loginCallback);

Vue: useAutoUnsubscribe ​

In Vue, useAutoUnsubscribe groups subscriptions and removes them in onBeforeUnmount. Install VueActMaster first, then call the helper synchronously inside setup() or <script setup>, before registering the subscriptions:

vue
<script setup lang="ts">
import { ref } from 'vue';
import { act } from 'act-master';
import { useAutoUnsubscribe } from 'act-master/vue';

const balance = ref<number | null>(null);

useAutoUnsubscribe();

act().on('GetBalance', (value) => {
  balance.value = value;
});
</script>

<template>
  <button @click="act().exec('GetBalance', '2026.01.01')">Load balance</button>
  <p>Balance: {{ balance ?? 'Not loaded' }}</p>
</template>

This example assumes that GetBalance(day: string): number is registered in the generated actions. All subscriptions added to this group are removed when the component unmounts; no manual off() call is needed.

Subscription ownership

The helper changes the current subscription group on the shared ActMaster instance. Register subscriptions immediately after calling it, before an await or another group is selected. For subscriptions created later, use an explicit key as shown in the Vue guide.

For a single subscription, you can pass Vue's cleanup hook directly:

ts
// Inside setup()
import { onBeforeUnmount } from 'vue';
import { act } from 'act-master';

act().on('GetBalance', (balance) => {
  console.log(balance);
}, onBeforeUnmount);

If you only need a reactive value, useRefSubscription creates a ref and manages its own subscription and cleanup.

once method ​

If you need only one execution result and immediately unsubscribe, you can use the once method

ts
act().once('Login', loginCallback);

ADVANCED: Unsubscribe with subsList ​

You can mark subscriptions with a key and then delete them all together.

ts
const SUBSCRIBE_KEY = '...';

act().subscribe('Login', loginCallback, SUBSCRIBE_KEY);
act().subscribe('Login2', loginCallback, SUBSCRIBE_KEY);

// unsubscribe
act().subsList.clear(SUBSCRIBE_KEY);
ts
const SUBSCRIBE_KEY = '...';

act().on('Login', loginCallback, SUBSCRIBE_KEY);
act().on('Login2', loginCallback, SUBSCRIBE_KEY);

// unsubscribe
act().subsList.clear(SUBSCRIBE_KEY);

Or indicate the key by which to unsubscribe further.

ts
const SUBSCRIBE_KEY = '...';
act().subsList.add(SUBSCRIBE_KEY);

act().subscribe('Login', loginCallback);
act().subscribe('Login2', loginCallback);

// unsubscribe
act().subsList.clear(SUBSCRIBE_KEY);
ts
const SUBSCRIBE_KEY = '...';
act().subsList.add(SUBSCRIBE_KEY);

act().on('Login', loginCallback);
act().on('Login2', loginCallback);

// unsubscribe
act().subsList.clear(SUBSCRIBE_KEY);