Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dispatch an event when the status changes, to allow other plugins to react #3123

Open
4 tasks done
feathers-l opened this issue Oct 17, 2024 · 6 comments
Open
4 tasks done
Labels
scope: for plugin developers Tools for use by plugin developers, including the Tasks API type: enhancement New feature or request

Comments

@feathers-l
Copy link

⚠️ Please check that this feature request hasn't been suggested before.

  • I searched previous Ideas in Discussions didn't find any similar feature requests.
  • I searched previous Issues didn't find any similar feature requests.
  • I am only requesting a single feature. Multiple changes should be split into individual requests, with links between them.
  • I believe my requested feature will be generally applicable to other users, not just me: it is not uniquely tied to my personal workflow.

🔖 Feature description

Dispatch an event when the status changes. Make developer can easily implement some feature about the status change of the task.

✔️ Solution

I want to implement a feature that can automatically complete some operations when the task status changes. I found there are many troubles in developing it. I think it will be easy if this plugin can dispatch an event when the task's status changes.

❓ Alternatives

I try to develop myself, but I found it is complicated to monitor checkbox in many situations.

📝 Additional Context

No response

@feathers-l feathers-l added the type: enhancement New feature or request label Oct 17, 2024
@claremacrae
Copy link
Collaborator

Thanks for the suggestion.

I don't really understand the request. What is it you are trying to actually do?

@claremacrae claremacrae added question Further information is requested scope: for plugin developers Tools for use by plugin developers, including the Tasks API labels Oct 17, 2024
@feathers-l
Copy link
Author

Thanks for the suggestion.

I don't really understand the request. What is it you are trying to actually do?

In my use-case, I use a file to record the task detail and I want to sync the task status to the file metadata, it is easy if I can register a event to know which task has just been completed.

@claremacrae
Copy link
Collaborator

In my use-case, I use a file to record the task detail and I want to sync the task status to the file metadata,

Thank you.

it is easy if I can register a event to know which task has just been completed.

Excellent - could you show us, here, what that code would look like please?

I need a clear, concrete understanding of the mechanism you are imagining...

@claremacrae
Copy link
Collaborator

And where would you write your code?
Are you thinking of writing a new plugin?
Or would you be writing code to run in some other plugin? Which one?

@feathers-l
Copy link
Author

feathers-l commented Oct 17, 2024

Excellent - could you show us, here, what that code would look like please?

I need a clear, concrete understanding of the mechanism you are imagining...

I just learn how to write a plugin, my code maybe bad
this is a sample what I want to use the event task:status-change

this.registerEvent(
			// @ts-ignore
			this.app.vault.on("task:status-change",async (task: Task)=>{
				// use dataview api to get task list
				const tasks = await dv.page(task.taskLocation._filepath).file.tasks
				tasks.forEach((t)=>{
					// find the task just change
					if (t.line === task.taskLocation._lineNumber){
						// get the task outlinks
						t.outlinks.forEach( async (outlinks)=>{
							// get file through outlink
							const outlinks_file=this.app.metadataCache.getFirstLinkpathDest(outlinks.path,t.path);
							if (task.status == 'done'){
								// update outlink file 
								await this.updatePropertyInFile("status",true ,outlinks_file);
							}else if (task.status == 'empty')
								await this.updatePropertyInFile("status",false ,outlinks_file);
						})
					}
				})
			})
		);

I am trying to write a new plugin for this function, I have read the source code of other plugins like dataview, it provide some kind of event.
image

It use the app.metadataCache.trigger() to do that, I also find obsidian have the trigger() api in a lot of place like app.worksapce.trigger() `app.vault.trigger()'

@claremacrae
Copy link
Collaborator

Hello @feathers-l,

This is brilliant - just what was needed to clarify the request. Many thanks.

@claremacrae claremacrae removed the question Further information is requested label Oct 19, 2024
@claremacrae claremacrae changed the title Dispatch an event when the status changes Dispatch an event when the status changes, to allow other plugins to react Oct 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
scope: for plugin developers Tools for use by plugin developers, including the Tasks API type: enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants